Basic plugin API example

This commit is contained in:
Rainer Simon 2020-09-11 09:14:19 +02:00
parent fd4fb6cdb1
commit 64aa253a5e
2 changed files with 58 additions and 3 deletions

41
src/editor/DOMWidget.jsx Normal file
View File

@ -0,0 +1,41 @@
import React from 'preact/compat';
import { useRef, useEffect } from 'preact/hooks';
const DOMWidget = props => {
const element = useRef();
const {
annotation,
readOnly,
onAppendBody,
onUpdateBody,
onRemoveBody,
onSaveAndClose
} = props;
useEffect(() => {
if (element.current) {
// Instantiate the widget...
const widgetEl = props.widget({
annotation,
readOnly,
onAppendBody,
onUpdateBody,
onRemoveBody,
onSaveAndClose
});
// ...and append to JSX wrapper
element.current.appendChild(widgetEl);
}
}, []);
return (
<div ref={element} class="widget"></div>
)
}
export default DOMWidget;

View File

@ -1,6 +1,7 @@
import React from 'preact/compat';
import { useState, useRef, useEffect } from 'preact/hooks';
import Environment from '../Environment';
import DOMWidget from './DOMWidget';
import setPosition from './setPosition';
import i18n from '../i18n';
@ -19,6 +20,8 @@ const bounds = elem => {
*/
const Editor = props => {
const widgets = props.widgets.map(fn => <DOMWidget widget={fn} />);
// The current state of the edited annotation vs. original
const [ currentAnnotation, setCurrentAnnotation ] = useState();
@ -147,13 +150,24 @@ const Editor = props => {
...child.props,
annotation : currentAnnotation,
readOnly : props.readOnly,
onAppendBody : onAppendBody,
onUpdateBody : onUpdateBody,
onRemoveBody : onRemoveBody,
onAppendBody,
onUpdateBody,
onRemoveBody,
onSaveAndClose : onOk
}))
}
{widgets.map(widget =>
React.cloneElement(widget, {
annotation : currentAnnotation,
readOnly : props.readOnly,
onAppendBody,
onUpdateBody,
onRemoveBody,
onSaveAndClose : onOk
})
)}
{ props.readOnly ? (
<div className="r6o-footer">
<button