From 516a5e4c7fe6309867b63b2a77e3b05f4e6eda24 Mon Sep 17 00:00:00 2001 From: Shawn Erquhart Date: Tue, 26 Sep 2017 15:52:50 -0400 Subject: [PATCH] improve markdown editor serialization debounce --- .../Widgets/Markdown/MarkdownControl/RawEditor/index.js | 8 +++----- .../Markdown/MarkdownControl/VisualEditor/index.js | 8 +++----- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/components/Widgets/Markdown/MarkdownControl/RawEditor/index.js b/src/components/Widgets/Markdown/MarkdownControl/RawEditor/index.js index d3ccda2d..80b4295a 100644 --- a/src/components/Widgets/Markdown/MarkdownControl/RawEditor/index.js +++ b/src/components/Widgets/Markdown/MarkdownControl/RawEditor/index.js @@ -22,16 +22,14 @@ export default class RawEditor extends React.Component { this.setState({ editorState }); } - onChange = debounce(this.props.onChange, 250); - /** * When the document value changes, serialize from Slate's AST back to plain * text (which is Markdown) and pass that up as the new value. */ - handleDocumentChange = (doc, editorState) => { + handleDocumentChange = debounce((doc, editorState) => { const value = Plain.serialize(editorState); - this.onChange(value); - }; + this.props.onChange(value); + }, 150); /** * If a paste contains plain text, deserialize it to Slate's AST and insert diff --git a/src/components/Widgets/Markdown/MarkdownControl/VisualEditor/index.js b/src/components/Widgets/Markdown/MarkdownControl/VisualEditor/index.js index ee18459d..34d2ceab 100644 --- a/src/components/Widgets/Markdown/MarkdownControl/VisualEditor/index.js +++ b/src/components/Widgets/Markdown/MarkdownControl/VisualEditor/index.js @@ -44,14 +44,12 @@ export default class Editor extends Component { return state.transform().insertFragment(doc).apply(); } - onChange = debounce(this.props.onChange, 250); - - handleDocumentChange = (doc, editorState) => { + handleDocumentChange = debounce((doc, editorState) => { const raw = Raw.serialize(editorState, { terse: true }); const plugins = this.state.shortcodePlugins; const markdown = slateToMarkdown(raw, plugins); - this.onChange(markdown); - }; + this.props.onChange(markdown); + }, 150); hasMark = type => this.state.editorState.marks.some(mark => mark.type === type); hasBlock = type => this.state.editorState.blocks.some(node => node.type === type);