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