From 0e50210dcf88df8ae8acaadd36a17ac9b0f2c508 Mon Sep 17 00:00:00 2001 From: Shawn Erquhart Date: Wed, 12 Jul 2017 18:31:05 -0400 Subject: [PATCH] close blocks on backspace --- .../MarkdownControl/VisualEditor/index.js | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/components/Widgets/Markdown/MarkdownControl/VisualEditor/index.js b/src/components/Widgets/Markdown/MarkdownControl/VisualEditor/index.js index b69137a3..f29654f3 100644 --- a/src/components/Widgets/Markdown/MarkdownControl/VisualEditor/index.js +++ b/src/components/Widgets/Markdown/MarkdownControl/VisualEditor/index.js @@ -276,10 +276,38 @@ const SoftBreak = (options = {}) => ({ } }); +const BackspaceCloseBlock = (options = {}) => ({ + onKeyDown(e, data, state) { + if (data.key != 'backspace' || state.startBlock.type === 'paragraph') return; + + const { defaultBlock = 'paragraph', wrapped = {} } = options; + const { startBlock } = state; + const { type } = startBlock; + + const characters = startBlock.getFirstText().characters; + const isEmpty = !characters || characters.isEmpty(); + + if (isEmpty) { + const transform = state.transform(); + + if (wrapped[type] && state.document.getPreviousSibling(startBlock.key)) { + return; + } + + if (wrapped[type]) { + wrapped[type].forEach(wrapper => transform.unwrapBlock(wrapper)); + } + + return transform.insertBlock(defaultBlock).focus().apply(); + } + } +}); + const slatePlugins = [ SoftBreak({ ignoreIn: ['paragraph', 'list-item'], closeAfter: 2 }), SoftBreak({ onlyIn: ['list-item'], shift: true}), SoftBreak({ onlyIn: ['paragraph'], closeAfter: 1 }), + BackspaceCloseBlock({ wrapped: { 'list-item': ['bulleted-list', 'numbered-list'] } }), ]; export default class Editor extends Component {