From 27d33d2a8448238301bc86fef0e8c6bfa77b5133 Mon Sep 17 00:00:00 2001 From: Mathias Biilmann Christensen Date: Tue, 1 Nov 2016 17:51:49 -0700 Subject: [PATCH] Make headers buttons toggle headers --- .../VisualEditor/index.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/components/Widgets/MarkdownControlElements/VisualEditor/index.js b/src/components/Widgets/MarkdownControlElements/VisualEditor/index.js index 64dead30..1f19ea35 100644 --- a/src/components/Widgets/MarkdownControlElements/VisualEditor/index.js +++ b/src/components/Widgets/MarkdownControlElements/VisualEditor/index.js @@ -82,8 +82,21 @@ export default class Editor extends Component { handleHeader = level => ( () => { - const command = setBlockType(schema.nodes.heading, { level }); - command(this.view.state, this.handleAction); + const state = this.view.state; + const { $from, to, node } = state.selection; + let nodeType = schema.nodes.heading; + let attrs = { level }; + let inHeader = node && node.hasMarkup(nodeType, attrs); + if (!inHeader) { + inHeader = to <= $from.end() && $from.parent.hasMarkup(nodeType, attrs); + } + if (inHeader) { + nodeType = schema.nodes.paragraph; + attrs = {}; + } + + const command = setBlockType(nodeType, { level }); + command(state, this.handleAction); } );