Basic plugin API example
This commit is contained in:
parent
fd4fb6cdb1
commit
64aa253a5e
|
@ -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;
|
|
@ -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';
|
||||||
|
|
||||||
|
@ -19,6 +20,8 @@ const bounds = elem => {
|
||||||
*/
|
*/
|
||||||
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,13 +150,24 @@ 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">
|
||||||
<button
|
<button
|
||||||
|
|
Loading…
Reference in New Issue