From dbf14a8f7b2e475b27ab6f0e051be0133901ae31 Mon Sep 17 00:00:00 2001 From: Shawn Erquhart Date: Tue, 25 Jul 2017 21:45:33 -0400 Subject: [PATCH] re-enable shortcode insertion via toolbar --- .../MarkdownControl/VisualEditor/index.js | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/components/Widgets/Markdown/MarkdownControl/VisualEditor/index.js b/src/components/Widgets/Markdown/MarkdownControl/VisualEditor/index.js index ec2070fe..78a42cd9 100644 --- a/src/components/Widgets/Markdown/MarkdownControl/VisualEditor/index.js +++ b/src/components/Widgets/Markdown/MarkdownControl/VisualEditor/index.js @@ -397,6 +397,23 @@ export default class Editor extends Component { schema: { nodes: NODE_COMPONENTS, marks: MARK_COMPONENTS, + rules: [ + { + match: object => object.kind === 'document', + validate: doc => { + const hasBlocks = !doc.getBlocks().isEmpty(); + return hasBlocks ? null : {}; + }, + normalize: transform => { + const block = SlateBlock.create({ + type: 'paragraph', + nodes: [SlateText.createFromString('')], + }); + const { key } = transform.state.document; + return transform.insertNodeByKey(key, 0, block).focus(); + }, + }, + ], }, plugins, }; @@ -504,11 +521,15 @@ export default class Editor extends Component { command(this.view.state, this.handleAction); }; - handlePluginSubmit = (plugin, data) => { + handlePluginSubmit = (plugin, shortcodeData) => { const { editorState } = this.state; - const markdown = plugin.toBlock(data.toJS()); - const html = markdownToHtml(markdown); - const block = serializer.deserialize(html).document.getBlocks().first(); + const data = { + shortcode: plugin.id, + shortcodeValue: plugin.toBlock(shortcodeData.toJS()), + shortcodeData, + }; + const nodes = [SlateText.createFromString('')]; + const block = { kind: 'block', type: 'shortcode', data, isVoid: true, nodes }; const resolvedState = editorState.transform().insertBlock(block).apply(); this.ref.onChange(resolvedState); this.setState({ editorState: resolvedState });