From 7874622ad4d1b3f5de24c35d5d3985a9f8a86093 Mon Sep 17 00:00:00 2001 From: Rainer Simon Date: Sat, 12 Sep 2020 10:06:29 +0200 Subject: [PATCH] More work towards flexible widget API --- src/editor/Editor.jsx | 6 +++-- src/editor/index.js | 15 ----------- .../{DOMWidget.jsx => WrappedWidget.jsx} | 4 ++- src/editor/widgets/index.js | 27 +++++++++++++++++++ src/index.js | 2 +- 5 files changed, 35 insertions(+), 19 deletions(-) delete mode 100644 src/editor/index.js rename src/editor/widgets/{DOMWidget.jsx => WrappedWidget.jsx} (93%) diff --git a/src/editor/Editor.jsx b/src/editor/Editor.jsx index b2eb942..fc33e94 100644 --- a/src/editor/Editor.jsx +++ b/src/editor/Editor.jsx @@ -1,7 +1,7 @@ import React from 'preact/compat'; import { useState, useRef, useEffect } from 'preact/hooks'; import Environment from '../Environment'; -import DOMWidget from './widgets/DOMWidget'; +import { getWidget, DEFAULT_WIDGETS } from './widgets'; import setPosition from './setPosition'; import i18n from '../i18n'; @@ -139,7 +139,9 @@ const Editor = props => { } }; - const widgets = props.widgets ? props.widgets.map(fn => ) : []; + // Use default comment + tag widget unless host app overrides + const widgets = props.config.widgets ? + props.widgets.map(getWidget) : DEFAULT_WIDGETS; return (
diff --git a/src/editor/index.js b/src/editor/index.js deleted file mode 100644 index 9d69b35..0000000 --- a/src/editor/index.js +++ /dev/null @@ -1,15 +0,0 @@ -import Editor from './Editor'; - -import CommentWidget from './widgets/comment/CommentWidget'; -import TagWidget from './widgets/tag/TagWidget'; - -/** Standard widgets included by default **/ -const DEFAULT_WIDGETS = { - COMMENT: CommentWidget, - TAG: TagWidget -}; - -Editor.CommentWidget = CommentWidget; -Editor.TagWidget = TagWidget; - -export { Editor }; diff --git a/src/editor/widgets/DOMWidget.jsx b/src/editor/widgets/WrappedWidget.jsx similarity index 93% rename from src/editor/widgets/DOMWidget.jsx rename to src/editor/widgets/WrappedWidget.jsx index ddd2459..6bce4c6 100644 --- a/src/editor/widgets/DOMWidget.jsx +++ b/src/editor/widgets/WrappedWidget.jsx @@ -1,15 +1,17 @@ import React, { Component } from 'preact/compat'; -export default class DOMWidget extends Component { +export default class WrappedWidget extends Component { constructor(props) { super(props); + this.element = React.createRef(); } componentWillReceiveProps(next) { if (this.element.current) { if (this.props.annotation !== next.annotation) { + const widgetEl = this.props.widget({ annotation: next.annotation, readOnly: next.readOnly, diff --git a/src/editor/widgets/index.js b/src/editor/widgets/index.js index e69de29..d053a97 100644 --- a/src/editor/widgets/index.js +++ b/src/editor/widgets/index.js @@ -0,0 +1,27 @@ +import React from 'react'; +import CommentWidget from './comment/CommentWidget' +import TagWidget from './tag/TagWidget'; +import WrappedWidget from './WrappedWidget'; + +/** Standard widgets included by default **/ +const BUILTIN_WIDGETS = { + COMMENT: , + TAG: +}; + +/** Defaults to use if there's no overrides from the host app **/ +export const DEFAULT_WIDGETS = [ + , +] + +export const getWidget = arg => { + if (typeof arg === 'string' || arg instanceof String) { + return BUILTIN_WIDGETS[arg]; + } else if (typeof arg === 'function' || arg instanceof Function) { + return + } else if (React.isValidElement(arg)) { + return arg; + } else { + throw `${arg} is not a valid plugin` + } +} \ No newline at end of file diff --git a/src/index.js b/src/index.js index 950d9c6..38abb4f 100644 --- a/src/index.js +++ b/src/index.js @@ -1,9 +1,9 @@ +export { default as Editor } from './editor/Editor'; export { default as Environment } from './Environment'; export { default as I18n } from './i18n'; export { default as TextAnnotator } from './TextAnnotator'; export { default as WebAnnotation } from './WebAnnotation'; -export * from './editor'; export * from './highlighter'; export * from './relations'; export * from './selection';