From 68414e06a42c2fd833a788e554a949ccb91d3c40 Mon Sep 17 00:00:00 2001 From: Giannis kopanas <jkopanas@gmail.com> Date: Thu, 15 Apr 2021 12:30:11 +0300 Subject: [PATCH] fix(core): fix portal views editor --- wax-prosemirror-core/src/Wax.js | 65 ++++++++++--------- .../src/{useWaxOptions.js => WaxOptions.js} | 9 +-- wax-prosemirror-core/src/WaxView.js | 27 ++++++-- 3 files changed, 58 insertions(+), 43 deletions(-) rename wax-prosemirror-core/src/{useWaxOptions.js => WaxOptions.js} (87%) diff --git a/wax-prosemirror-core/src/Wax.js b/wax-prosemirror-core/src/Wax.js index 222961eb8..92c12e65f 100644 --- a/wax-prosemirror-core/src/Wax.js +++ b/wax-prosemirror-core/src/Wax.js @@ -22,7 +22,7 @@ const serializer = schema => { let schema; const createApplication = props => { const application = Application.create(props); - schema = application.getSchema(); + // schema = application.getSchema(); // application.bootServices(); return application; }; @@ -54,39 +54,40 @@ const Wax = props => { if (!application) return null; const WaxOnchange = onChange || (v => true); - const finalOnChange = debounce( - // eslint-disable-next-line no-shadow - value => { - /* HACK alter toDOM of footnote, because of how PM treats inline nodes - with content */ - if (schema.nodes.footnote) { - const old = schema.nodes.footnote.spec.toDOM; - schema.nodes.footnote.spec.toDOM = node => { - // eslint-disable-next-line prefer-rest-params - old.apply(this); - return ['footnote', node.attrs, 0]; - }; - } + const finalOnChange = () => {}; + // const finalOnChange = debounce( + // // eslint-disable-next-line no-shadow + // value => { + // /* HACK alter toDOM of footnote, because of how PM treats inline nodes + // with content */ + // if (schema.nodes.footnote) { + // const old = schema.nodes.footnote.spec.toDOM; + // schema.nodes.footnote.spec.toDOM = node => { + // // eslint-disable-next-line prefer-rest-params + // old.apply(this); + // return ['footnote', node.attrs, 0]; + // }; + // } - if (targetFormat === 'JSON') { - WaxOnchange(value); - } else { - const serialize = serializer(schema); - WaxOnchange(serialize(value)); - } + // if (targetFormat === 'JSON') { + // WaxOnchange(value); + // } else { + // const serialize = serializer(schema); + // WaxOnchange(serialize(value)); + // } - if (schema.nodes.footnote) { - const old = schema.nodes.footnote.spec.toDOM; - schema.nodes.footnote.spec.toDOM = node => { - // eslint-disable-next-line prefer-rest-params - old.apply(this); - return ['footnote', node.attrs]; - }; - } - }, - 1000, - { maxWait: 5000 }, - ); + // if (schema.nodes.footnote) { + // const old = schema.nodes.footnote.spec.toDOM; + // schema.nodes.footnote.spec.toDOM = node => { + // // eslint-disable-next-line prefer-rest-params + // old.apply(this); + // return ['footnote', node.attrs]; + // }; + // } + // }, + // 1000, + // { maxWait: 5000 }, + // ); const TrackChange = application.config.get('config.EnableTrackChangeService'); const Layout = application.container.get('Layout'); diff --git a/wax-prosemirror-core/src/useWaxOptions.js b/wax-prosemirror-core/src/WaxOptions.js similarity index 87% rename from wax-prosemirror-core/src/useWaxOptions.js rename to wax-prosemirror-core/src/WaxOptions.js index 41d0fbb69..5f8bad7df 100644 --- a/wax-prosemirror-core/src/useWaxOptions.js +++ b/wax-prosemirror-core/src/WaxOptions.js @@ -15,9 +15,7 @@ const parser = schema => { }; }; -export default ({ placeholder, targetFormat, value }) => { - const context = useContext(WaxContext); - +export default ({ placeholder, targetFormat, value, schema, plugins }) => { let finalPlugins = []; // eslint-disable-next-line no-shadow @@ -25,13 +23,12 @@ export default ({ placeholder, targetFormat, value }) => { return Placeholder({ content: placeholder }); }; + console.log(plugins); finalPlugins = defaultPlugins.concat([ createPlaceholder(placeholder), - ...context.app.getPlugins(), + ...plugins, ]); - const schema = context.app.getSchema(); - const WaxOptions = { schema, plugins: finalPlugins, diff --git a/wax-prosemirror-core/src/WaxView.js b/wax-prosemirror-core/src/WaxView.js index 309421105..7f6e5b04f 100644 --- a/wax-prosemirror-core/src/WaxView.js +++ b/wax-prosemirror-core/src/WaxView.js @@ -1,4 +1,10 @@ -import React, { useRef, useContext, useCallback, useMemo } from 'react'; +import React, { + useRef, + useContext, + useCallback, + useMemo, + useEffect, +} from 'react'; import applyDevTools from 'prosemirror-dev-tools'; import { EditorState } from 'prosemirror-state'; @@ -10,7 +16,7 @@ import ComponentPlugin from './ComponentPlugin'; import { WaxContext } from './WaxContext'; import { PortalContext } from './PortalContext'; import transformPasted from './helpers/TransformPasted'; -import useWaxOptions from './useWaxOptions'; +import WaxOptions from './WaxOptions'; let previousDoc; @@ -29,14 +35,17 @@ export default props => { export default props => { const { readonly, onBlur, debug, autoFocus, user, targetFormat } = props; const editorRef = useRef(); - let view; + const context = useContext(WaxContext); const { createPortal } = useContext(PortalContext); context.app.setContext({ ...context, createPortal }); - context.app.bootServices(); - const options = useWaxOptions(props); + const schema = context.app.getSchema(); + + if (!view) { + context.app.bootServices(); + } const setEditorRef = useCallback( // eslint-disable-next-line consistent-return @@ -47,6 +56,14 @@ export default props => { // clean up the unmount if you need to. } if (node) { + const options = WaxOptions({ + ...props, + schema, + plugins: context.app.getPlugins(), + }); + + console.log(options); + // debugger; view = new EditorView( { mount: node }, { -- GitLab