From 181d6449441bfa978a676db7472a47e00e454924 Mon Sep 17 00:00:00 2001 From: Anze Demsar Date: Thu, 4 Apr 2024 15:25:13 +0200 Subject: [PATCH] feat(richtext): use own querynode --- .../src/RichtextControl/VisualEditor.js | 3 +-- ...tBreak.js => createBlockquoteExtPlugin.js} | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 6 deletions(-) rename packages/decap-cms-widget-richtext/src/RichtextControl/plugins/{createBlockquoteExitBreak.js => createBlockquoteExtPlugin.js} (71%) diff --git a/packages/decap-cms-widget-richtext/src/RichtextControl/VisualEditor.js b/packages/decap-cms-widget-richtext/src/RichtextControl/VisualEditor.js index a74cadbee122..89e8cc0dfb85 100644 --- a/packages/decap-cms-widget-richtext/src/RichtextControl/VisualEditor.js +++ b/packages/decap-cms-widget-richtext/src/RichtextControl/VisualEditor.js @@ -38,7 +38,7 @@ import ListElement from './components/Element/ListElement'; import { markdownToSlate, slateToMarkdown } from '../serializers'; import LinkElement from './components/Element/LinkElement'; import BlockquoteElement from './components/Element/BlockquoteElement'; -import createBlockquoteExtPlugin from './plugins/createBlockquoteExitBreak'; +import createBlockquoteExtPlugin from './plugins/createBlockquoteExtPlugin'; function visualEditorStyles({ minimal }) { return ` @@ -64,7 +64,6 @@ const emptyValue = [ ]; export default function VisualEditor({ t, field, className, isDisabled, onChange, ...props }) { - console.log('plff', createBlockquotePlugin()) const plugins = createPlugins( [ createParagraphPlugin(), diff --git a/packages/decap-cms-widget-richtext/src/RichtextControl/plugins/createBlockquoteExitBreak.js b/packages/decap-cms-widget-richtext/src/RichtextControl/plugins/createBlockquoteExtPlugin.js similarity index 71% rename from packages/decap-cms-widget-richtext/src/RichtextControl/plugins/createBlockquoteExitBreak.js rename to packages/decap-cms-widget-richtext/src/RichtextControl/plugins/createBlockquoteExtPlugin.js index d649e95c9765..403d958e3128 100644 --- a/packages/decap-cms-widget-richtext/src/RichtextControl/plugins/createBlockquoteExitBreak.js +++ b/packages/decap-cms-widget-richtext/src/RichtextControl/plugins/createBlockquoteExtPlugin.js @@ -5,6 +5,7 @@ import { isAncestorEmpty, unwrapNodes, isFirstChild, + isSelectionAtBlockStart, } from '@udecode/plate-common'; import { ELEMENT_BLOCKQUOTE } from '@udecode/plate-block-quote'; @@ -15,6 +16,14 @@ function isWithinBlockquote(editor, entry) { return blockAbove?.[0]?.type === ELEMENT_BLOCKQUOTE; } +function queryNode(editor, entry, { empty, first, start }) { + return ( + (!empty || isAncestorEmpty(editor, entry[0])) && + (!first || isFirstChild(entry[1])) && + (!start || isSelectionAtBlockStart(editor)) + ); +} + function unwrap(editor) { unwrapNodes(editor, { split: true, match: n => n.type === ELEMENT_BLOCKQUOTE }); return true; @@ -27,12 +36,11 @@ function onKeyDownBlockquoteExitBreak(editor, { options: { rules } }) { const entry = getBlockAbove(editor); if (!entry) return; - rules.forEach(({ hotkey, isFirstParagraph }) => { + rules.forEach(({ hotkey, query }) => { if ( isHotkey(hotkey, event) && - isAncestorEmpty(editor, entry[0]) && isWithinBlockquote(editor, entry) && - (!isFirstParagraph || isFirstChild(entry[1])) && + queryNode(editor, entry, query) && unwrap(editor) ) { event.preventDefault(); @@ -48,7 +56,10 @@ const createBlockquoteExtPlugin = createPluginFactory({ onKeyDown: onKeyDownBlockquoteExitBreak, }, options: { - rules: [{ hotkey: 'enter' }, { hotkey: 'backspace', isFirstParagraph: true }], + rules: [ + { hotkey: 'enter', query: { empty: true } }, + { hotkey: 'backspace', query: { first: true, start: true } }, + ], }, });