Skip to content

Commit

Permalink
Merge pull request recogito#184 from oleksandr-danylchenko/fix-react-…
Browse files Browse the repository at this point in the history
…props-internal-generic

Broaden the internal type generic usage
  • Loading branch information
rsimon authored Dec 16, 2024
2 parents 1f79e72 + 2a950e7 commit 52e1733
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 23 deletions.
10 changes: 6 additions & 4 deletions packages/text-annotator-react/src/TextAnnotator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@ import { createTextAnnotator } from '@recogito/text-annotator';

import '@recogito/text-annotator/dist/text-annotator.css';

export interface TextAnnotatorProps<E extends unknown> extends Omit<TextAnnotatorOptions<TextAnnotation, E>, 'adapter'> {
export interface TextAnnotatorProps<I extends TextAnnotation = TextAnnotation, E extends unknown = TextAnnotation> extends Omit<TextAnnotatorOptions<I, E>, 'adapter'> {

children?: ReactNode | JSX.Element;

adapter?: FormatAdapter<TextAnnotation, E> | ((container: HTMLElement) => FormatAdapter<TextAnnotation, E>) | null;
adapter?: FormatAdapter<I, E> | ((container: HTMLElement) => FormatAdapter<I, E>) | null;

filter?: Filter;
filter?: Filter<I>;

style?: HighlightStyleExpression;

className?: string;

}

export const TextAnnotator = <E extends unknown>(props: TextAnnotatorProps<E>) => {
export const TextAnnotator = <I extends TextAnnotation = TextAnnotation, E extends unknown = TextAnnotation>(
props: TextAnnotatorProps<I, E>
) => {

const el = useRef<HTMLDivElement>(null);

Expand Down
18 changes: 9 additions & 9 deletions packages/text-annotator/src/TextAnnotator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,31 @@ export interface TextAnnotator<I extends TextAnnotation = TextAnnotation, E exte

}

export const createTextAnnotator = <E extends unknown = TextAnnotation>(
export const createTextAnnotator = <I extends TextAnnotation = TextAnnotation, E extends unknown = TextAnnotation>(
container: HTMLElement,
options: TextAnnotatorOptions<TextAnnotation, E> = {}
): TextAnnotator<TextAnnotation, E> => {
options: TextAnnotatorOptions<I, E> = {}
): TextAnnotator<I, E> => {
// Prevent mobile browsers from triggering word selection on single click.
cancelSingleClickEvents(container);

// Make sure that the container is focusable and can receive both pointer and keyboard events
programmaticallyFocusable(container);

const opts = fillDefaults<TextAnnotation, E>(options, {
const opts = fillDefaults<I, E>(options, {
annotatingEnabled: true,
user: createAnonymousGuest()
});

const state: TextAnnotatorState<TextAnnotation, E> =
createTextAnnotatorState<TextAnnotation, E>(container, opts.userSelectAction);
const state: TextAnnotatorState<I, E> =
createTextAnnotatorState<I, E>(container, opts.userSelectAction);

const { selection, viewport } = state;

const store: TextAnnotationStore = state.store;

const undoStack = createUndoStack(store);

const lifecycle = createLifecycleObserver<TextAnnotation, E>(state, undoStack, opts.adapter);
const lifecycle = createLifecycleObserver<I, E>(state, undoStack, opts.adapter);

let currentUser: User = opts.user;

Expand Down Expand Up @@ -84,11 +84,11 @@ export const createTextAnnotator = <E extends unknown = TextAnnotation>(
/******++++++*************/

// Most of the external API functions are covered in the base annotator
const base = createBaseAnnotator<TextAnnotation, E>(state, undoStack, opts.adapter);
const base = createBaseAnnotator<I, E>(state, undoStack, opts.adapter);

const getUser = () => currentUser;

const setFilter = (filter?: Filter) => {
const setFilter = (filter?: Filter<I>) => {
highlightRenderer.setFilter(filter);
selectionHandler.setFilter(filter);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/text-annotator/src/highlight/HighlightStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface HighlightStyle extends Pick<DrawingStyle, 'fill' | 'fillOpacity
}

export type HighlightStyleExpression = HighlightStyle
| ((annotation: TextAnnotation, state: AnnotationState, zIndex?: number) => HighlightStyle | undefined);
| (<I extends TextAnnotation = TextAnnotation>(annotation: I, state: AnnotationState, zIndex?: number) => HighlightStyle | undefined);

export const DEFAULT_STYLE: HighlightStyle = {
fill: 'rgb(0, 128, 255)',
Expand Down
20 changes: 11 additions & 9 deletions packages/text-annotator/src/model/w3c/W3CTextFormatAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ export type W3CTextFormatAdapter<I extends TextAnnotation = TextAnnotation, E ex
* @param container - the HTML container of the annotated content,
* Required to locate the content's `range` within the DOM
*/
export const W3CTextFormat = <E extends W3CTextAnnotation = W3CTextAnnotation>(
export const W3CTextFormat =<I extends TextAnnotation = TextAnnotation, E extends W3CTextAnnotation = W3CTextAnnotation>(
source: string,
container: HTMLElement
): W3CTextFormatAdapter<TextAnnotation, E> => ({
): W3CTextFormatAdapter<I, E> => ({
parse: (serialized) => parseW3CTextAnnotation(serialized),
serialize: (annotation) => serializeW3CTextAnnotation(annotation, source, container)
});
Expand Down Expand Up @@ -90,9 +90,9 @@ const parseW3CTextTargets = (annotation: W3CTextAnnotation) => {
return { parsed };
};

export const parseW3CTextAnnotation = (
annotation: W3CTextAnnotation
): ParseResult<TextAnnotation> => {
export const parseW3CTextAnnotation = <I extends TextAnnotation = TextAnnotation, E extends W3CTextAnnotation = W3CTextAnnotation>(
annotation: E
): ParseResult<I> => {
const annotationId = annotation.id || uuidv4();

const {
Expand All @@ -106,7 +106,7 @@ export const parseW3CTextAnnotation = (
const bodies = parseW3CBodies(body, annotationId);
const target = parseW3CTextTargets(annotation);

return 'error' in target
const parseResult = 'error' in target
? { error: target.error }
: {
parsed: {
Expand All @@ -117,10 +117,12 @@ export const parseW3CTextAnnotation = (
}
};

return parseResult as ParseResult<I>;

};

export const serializeW3CTextAnnotation = <E extends W3CTextAnnotation = W3CTextAnnotation>(
annotation: TextAnnotation,
export const serializeW3CTextAnnotation = <I extends TextAnnotation = TextAnnotation, E extends W3CTextAnnotation = W3CTextAnnotation>(
annotation: I,
source: string,
container: HTMLElement
): E => {
Expand Down Expand Up @@ -171,6 +173,6 @@ export const serializeW3CTextAnnotation = <E extends W3CTextAnnotation = W3CText
created: created?.toISOString(),
modified: updated?.toISOString(),
target: w3cTargets
} as E;
} as unknown as E;

};

0 comments on commit 52e1733

Please sign in to comment.