Skip to content

Commit

Permalink
Merge branch 'main' into recogito#127-annotating-enabled-reactive
Browse files Browse the repository at this point in the history
# Conflicts:
#	packages/text-annotator/src/SelectionHandler.ts
#	packages/text-annotator/src/TextAnnotator.ts
  • Loading branch information
oleksandr-danylchenko committed Nov 11, 2024
2 parents 7a44635 + 2b00afd commit 843c284
Show file tree
Hide file tree
Showing 21 changed files with 371 additions and 304 deletions.
399 changes: 195 additions & 204 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@recogito/text-annotator-monorepo",
"version": "3.0.0-rc.50",
"version": "3.0.0-rc.52",
"description": "Recogito Text Annotator monorepo",
"author": "Rainer Simon",
"repository": {
Expand Down
8 changes: 4 additions & 4 deletions packages/extension-tei/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@recogito/text-annotator-tei",
"version": "3.0.0-rc.50",
"version": "3.0.0-rc.52",
"description": "Recogito Text Annotator TEI extension",
"author": "Rainer Simon",
"license": "BSD-3-Clause",
Expand Down Expand Up @@ -28,11 +28,11 @@
"devDependencies": {
"CETEIcean": "^1.9.3",
"typescript": "5.6.3",
"vite": "^5.4.10",
"vite": "^5.4.11",
"vite-plugin-dts": "^4.3.0"
},
"peerDependencies": {
"@annotorious/core": "^3.0.11",
"@recogito/text-annotator": "3.0.0-rc.50"
"@annotorious/core": "^3.0.12",
"@recogito/text-annotator": "3.0.0-rc.52"
}
}
14 changes: 7 additions & 7 deletions packages/text-annotator-react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@recogito/react-text-annotator",
"version": "3.0.0-rc.50",
"version": "3.0.0-rc.52",
"description": "Recogito Text Annotator React bindings",
"author": "Rainer Simon",
"license": "BSD-3-Clause",
Expand Down Expand Up @@ -29,9 +29,9 @@
"react": "^18.3.1",
"react-dom": "^18.3.1",
"typescript": "5.6.3",
"vite": "^5.4.10",
"vite": "^5.4.11",
"vite-plugin-dts": "^4.3.0",
"vite-tsconfig-paths": "^5.0.1"
"vite-tsconfig-paths": "^5.1.2"
},
"peerDependencies": {
"openseadragon": "^3.0.0 || ^4.0.0 || ^5.0.0",
Expand All @@ -44,11 +44,11 @@
}
},
"dependencies": {
"@annotorious/core": "^3.0.11",
"@annotorious/react": "^3.0.11",
"@annotorious/core": "^3.0.12",
"@annotorious/react": "^3.0.12",
"@floating-ui/react": "^0.26.27",
"@recogito/text-annotator": "3.0.0-rc.50",
"@recogito/text-annotator-tei": "3.0.0-rc.50",
"@recogito/text-annotator": "3.0.0-rc.52",
"@recogito/text-annotator-tei": "3.0.0-rc.52",
"CETEIcean": "^1.9.3"
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { PointerEvent, ReactNode, useCallback, useEffect, useMemo, useState } from 'react';
import { ReactNode, useEffect, useMemo, useRef, useState } from 'react';
import { useAnnotator, useSelection } from '@annotorious/react';
import { isRevived, TextAnnotation, TextAnnotator } from '@recogito/text-annotator';
import { isRevived, NOT_ANNOTATABLE_CLASS, TextAnnotation, TextAnnotator } from '@recogito/text-annotator';
import { isMobile } from './isMobile';
import {
arrow,
autoUpdate,
flip,
FloatingArrow,
FloatingArrowProps,
FloatingFocusManager,
FloatingPortal,
inline,
Expand All @@ -16,12 +19,16 @@ import {
useRole
} from '@floating-ui/react';

import './TextAnnotatorPopup.css';
import './TextAnnotationPopup.css';

interface TextAnnotationPopupProps {

ariaCloseWarning?: string;

arrow?: boolean;

arrowProps?: Omit<FloatingArrowProps, 'context' | 'ref'>;

popup(props: TextAnnotationPopupContentProps): ReactNode;

}
Expand All @@ -32,11 +39,11 @@ export interface TextAnnotationPopupContentProps {

editable?: boolean;

event?: PointerEvent;
event?: PointerEvent | KeyboardEvent;

}

export const TextAnnotatorPopup = (props: TextAnnotationPopupProps) => {
export const TextAnnotationPopup = (props: TextAnnotationPopupProps) => {

const r = useAnnotator<TextAnnotator>();

Expand All @@ -46,6 +53,22 @@ export const TextAnnotatorPopup = (props: TextAnnotationPopupProps) => {

const [isOpen, setOpen] = useState(selected?.length > 0);

const arrowRef = useRef(null);

// Conditional floating-ui middleware
const middleware = useMemo(() => {
const m = [
inline(),
offset(10),
flip({ crossAxis: true }),
shift({ crossAxis: true, padding: 10 })
];

return props.arrow
? [...m, arrow({ element: arrowRef }) ]
: m;
}, [props.arrow]);

const { refs, floatingStyles, update, context } = useFloating({
placement: isMobile() ? 'bottom' : 'top',
open: isOpen,
Expand All @@ -55,12 +78,7 @@ export const TextAnnotatorPopup = (props: TextAnnotationPopupProps) => {
r?.cancelSelected();
}
},
middleware: [
offset(10),
inline(),
flip(),
shift({ mainAxis: false, crossAxis: true, padding: 10 })
],
middleware,
whileElementsMounted: autoUpdate
});

Expand Down Expand Up @@ -106,12 +124,6 @@ export const TextAnnotatorPopup = (props: TextAnnotationPopupProps) => {
};
}, [update]);

// Prevent text-annotator from handling the irrelevant events triggered from the popup
const getStopEventsPropagationProps = useCallback(
() => ({ onPointerUp: (event: PointerEvent<HTMLDivElement>) => event.stopPropagation() }),
[]
);

// Don't shift focus to the floating element if selected via keyboard or on mobile.
const initialFocus = useMemo(() => {
return (event?.type === 'keyup' || event?.type === 'contextmenu' || isMobile()) ? -1 : 0;
Expand All @@ -128,17 +140,23 @@ export const TextAnnotatorPopup = (props: TextAnnotationPopupProps) => {
returnFocus={false}
initialFocus={initialFocus}>
<div
className="a9s-popup r6o-popup annotation-popup r6o-text-popup not-annotatable"
className={`a9s-popup r6o-popup annotation-popup r6o-text-popup ${NOT_ANNOTATABLE_CLASS}`}
ref={refs.setFloating}
style={floatingStyles}
{...getFloatingProps()}
{...getStopEventsPropagationProps()}>
{...getFloatingProps(getStopEventsPropagationProps())}>
{props.popup({
annotation: selected[0].annotation,
editable: selected[0].editable,
event
})}

{props.arrow && (
<FloatingArrow
ref={arrowRef}
context={context}
{...(props.arrowProps || {})} />
)}

<button className="r6o-popup-sr-only" aria-live="assertive" onClick={onClose}>
{props.ariaCloseWarning || 'Click or leave this dialog to close it.'}
</button>
Expand All @@ -148,3 +166,25 @@ export const TextAnnotatorPopup = (props: TextAnnotationPopupProps) => {
) : null;

}

/**
* Prevent text-annotator from handling the irrelevant events
* triggered from the popup/toolbar/dialog
*/
const getStopEventsPropagationProps = <T extends HTMLElement = HTMLElement>() => ({
onPointerUp: (event: React.PointerEvent<T>) => event.stopPropagation(),
onPointerDown: (event: React.PointerEvent<T>) => event.stopPropagation(),
onMouseDown: (event: React.MouseEvent<T>) => event.stopPropagation(),
onMouseUp: (event: React.MouseEvent<T>) => event.stopPropagation()
});

/** For backwards compatibility **/
/** @deprecated Use TextAnnotationPopup instead */
export const TextAnnotatorPopup = (props: TextAnnotationPopupProps) => {

useEffect(() => {
console.warn('TextAnnotatorPopup is deprecated and will be removed in a future version. Please use TextAnnotationPopup instead.');
}, []);

return <TextAnnotationPopup {...props} />;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './TextAnnotationPopup';

This file was deleted.

2 changes: 1 addition & 1 deletion packages/text-annotator-react/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export * from './tei';
export * from './TextAnnotator';
export * from './TextAnnotatorPopup';
export * from './TextAnnotationPopup';
export * from './TextAnnotatorPlugin';

// Essential re-exports from @annotorious/core
Expand Down
11 changes: 8 additions & 3 deletions packages/text-annotator-react/test/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { FC, useCallback, useEffect } from 'react';
import { AnnotationBody, Annotorious, useAnnotationStore, useAnnotator } from '@annotorious/react';
import { TextAnnotationPopupContentProps, TextAnnotator, TextAnnotatorPopup } from '../src';
import { TextAnnotationPopup, TextAnnotationPopupContentProps, TextAnnotator } from '../src';
import { W3CTextFormat, type TextAnnotation, type TextAnnotator as RecogitoTextAnnotator } from '@recogito/text-annotator';

const TestPopup: FC<TextAnnotationPopupContentProps> = (props) => {
Expand Down Expand Up @@ -67,7 +67,8 @@ export const App: FC = () => {

return (
<Annotorious>
<TextAnnotator adapter={w3cAdapter}>
<TextAnnotator
adapter={w3cAdapter}>
<p>
Tell me, O muse, of that ingenious hero who travelled far and wide
after he had sacked the famous town of Troy. Many cities did he
Expand Down Expand Up @@ -189,7 +190,11 @@ export const App: FC = () => {
</p>
</TextAnnotator>

<TextAnnotatorPopup
<TextAnnotationPopup
arrow
arrowProps={{
fill: '#000000'
}}
popup={
props => (<TestPopup {...props} />)
}
Expand Down
1 change: 1 addition & 0 deletions packages/text-annotator-react/test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
p {
font-size: 17px;
line-height: 160%;
min-width: 600px;
}

.r6o-popup {
Expand Down
10 changes: 5 additions & 5 deletions packages/text-annotator/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@recogito/text-annotator",
"version": "3.0.0-rc.50",
"version": "3.0.0-rc.52",
"description": "A JavaScript text annotation library",
"author": "Rainer Simon",
"license": "BSD-3-Clause",
Expand Down Expand Up @@ -30,17 +30,17 @@
"@types/rbush": "^4.0.0",
"jsdom": "^25.0.1",
"typescript": "5.6.3",
"vite": "^5.4.10",
"vite": "^5.4.11",
"vite-plugin-dts": "^4.3.0",
"vitest": "^2.1.4"
},
"dependencies": {
"@annotorious/core": "^3.0.11",
"@annotorious/core": "^3.0.12",
"colord": "^2.9.3",
"debounce": "^2.1.1",
"dequal": "^2.0.3",
"hotkeys-js": "^3.13.7",
"rbush": "^4.0.1",
"uuid": "^11.0.2"
"uuid": "^11.0.3"
}
}
}
Loading

0 comments on commit 843c284

Please sign in to comment.