diff --git a/src/editor/Editor.jsx b/src/editor/Editor.jsx index 69174a6..64a7a80 100644 --- a/src/editor/Editor.jsx +++ b/src/editor/Editor.jsx @@ -26,8 +26,9 @@ const Editor = props => { const element = useRef(); // Set derived annotation state - useEffect(() => - setCurrentAnnotation(props.annotation), [ props.annotation ]); + useEffect(() => { + setCurrentAnnotation(props.annotation); + }, [ props.annotation ]); // Change editor position if element has moved useEffect(() => { @@ -89,6 +90,22 @@ const Editor = props => { }) ); + const onSetProperty = (property, value) => { + // A list of properties the user is NOT allowed to set + const isForbidden = [ '@context', 'id', 'type', 'body', 'target' ].includes(property); + + if (isForbidden) + throw new Exception(`Cannot set ${property} - not allowed`); + + if (value) { + setCurrentAnnotation(currentAnnotation.clone({ [property]: value })); + } else { + const updated = currentAnnotation.clone(); + delete updated[property]; + setCurrentAnnotation(updated); + } + }; + const onCancel = () => props.onCancel(currentAnnotation); @@ -130,6 +147,7 @@ const Editor = props => { onAppendBody, onUpdateBody, onRemoveBody, + onSetProperty, onSaveAndClose: onOk }) )} diff --git a/src/selection/Selection.js b/src/selection/Selection.js index aba36e6..596edb4 100644 --- a/src/selection/Selection.js +++ b/src/selection/Selection.js @@ -19,9 +19,9 @@ export default class Selection { /** Creates a copy of this selection **/ clone = opt_props => { - // Deep-clone target - const clonedTarget = JSON.parse(JSON.stringify(this.underlying.target)); - const cloned = new Selection(clonedTarget); + // Deep-clone + const cloned = new Selection(); + cloned.underlying = JSON.parse(JSON.stringify(this.underlying)); if (opt_props) cloned.underlying = { ...cloned.underlying, ...opt_props };