From 59b75174140dfd343f21e18dbcb2677e078c03d0 Mon Sep 17 00:00:00 2001 From: Rainer Simon Date: Wed, 22 Apr 2020 18:47:42 +0200 Subject: [PATCH] applyTemplate API method --- src/TextAnnotator.jsx | 11 +++++++++-- src/editor/Editor.jsx | 19 +++++++++++-------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/TextAnnotator.jsx b/src/TextAnnotator.jsx index 4f7aecd..b5c2b77 100644 --- a/src/TextAnnotator.jsx +++ b/src/TextAnnotator.jsx @@ -16,7 +16,10 @@ export default class TextAnnotator extends Component { selectedAnnotation: null, showRelationEditor: false, - selectedRelation: null + selectedRelation: null, + + applyTemplate: null, + headless: false } /** Shorthand **/ @@ -178,6 +181,9 @@ export default class TextAnnotator extends Component { } } + applyTemplate = (bodies, headless) => + this.setState({ applyTemplate: bodies, headless }) + render() { return ( <> @@ -187,7 +193,8 @@ export default class TextAnnotator extends Component { bounds={this.state.selectionBounds} annotation={this.state.selectedAnnotation} readOnly={this.props.readOnly} - autoApply={this.props.autoApply} + headless={this.state.headless} + applyTemplate={this.state.applyTemplate} onAnnotationCreated={this.onCreateOrUpdateAnnotation('onAnnotationCreated')} onAnnotationUpdated={this.onCreateOrUpdateAnnotation('onAnnotationUpdated')} onAnnotationDeleted={this.onDeleteAnnotation} diff --git a/src/editor/Editor.jsx b/src/editor/Editor.jsx index f15f78a..22b70f5 100644 --- a/src/editor/Editor.jsx +++ b/src/editor/Editor.jsx @@ -23,18 +23,21 @@ const Editor = props => { // Re-render: set derived annotation state & position the editor useEffect(() => { - if (!currentAnnotation?.isEqual(props.annotation)) - setCurrentAnnotation(props.annotation); + // Shorthand: user wants a template applied and this is a selection + const shouldApplyTemplate = props.applyTemplate && props.annotation?.isSelection + // Apply template if needed + const annotation = shouldApplyTemplate ? + props.annotation.clone({ body: [ ...props.applyTemplate ]}) : + props.annotation; + if (!currentAnnotation?.isEqual(annotation)) + setCurrentAnnotation(annotation); - /** Experimental **/ - if (props.autoApply && props.annotation?.isSelection) - props.onAnnotationCreated(autoApply(props)); - /** Experimental **/ + // In headless mode, create immediately + if (shouldApplyTemplate && props.headless) + props.onAnnotationCreated(annotation.toAnnotation()); - - if (element.current) setPosition(props.wrapperEl, element.current, props.bounds); }, [ props.bounds ]);