applyTemplate API method

This commit is contained in:
Rainer Simon 2020-04-22 18:47:42 +02:00
parent f245fd2ed9
commit 59b7517414
2 changed files with 20 additions and 10 deletions

View File

@ -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}

View File

@ -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 ]);