From 9c61d342c8f37fd8231bdb95a4be6bd7d59a8a33 Mon Sep 17 00:00:00 2001 From: liuhongbo <916196375@qq.com> Date: Fri, 20 Oct 2023 11:31:52 +0800 Subject: [PATCH] refactor: move check-list logic from paragrph to check-list --- src/extension/plugins/check-list/plugin.js | 28 +++++++++++++-- src/extension/plugins/paragraph/index.js | 2 -- src/extension/plugins/paragraph/plugin.js | 40 ---------------------- 3 files changed, 25 insertions(+), 45 deletions(-) delete mode 100644 src/extension/plugins/paragraph/plugin.js diff --git a/src/extension/plugins/check-list/plugin.js b/src/extension/plugins/check-list/plugin.js index 25edcba6..06ec7dc0 100644 --- a/src/extension/plugins/check-list/plugin.js +++ b/src/extension/plugins/check-list/plugin.js @@ -1,6 +1,6 @@ -import { Node, Range, Transforms } from 'slate'; -import { getSelectedNodeByType } from '../../core'; -import { CHECK_LIST_ITEM } from '../../constants/element-types'; +import { Editor, Node, Range, Transforms } from 'slate'; +import { getPrevNode, getSelectedNodeByType, getSelectedNodeEntryByType, isStartPoint } from '../../core'; +import { CHECK_LIST_ITEM, PARAGRAPH } from '../../constants/element-types'; import { transformToParagraph } from '../paragraph/helper'; const withCheckList = (editor) => { @@ -27,7 +27,29 @@ const withCheckList = (editor) => { newEditor.deleteBackward = (unit) => { const { selection } = newEditor; + if (selection && Range.isCollapsed(selection)) { + const selectedParagraphNodeEntry = getSelectedNodeEntryByType(newEditor, PARAGRAPH); + // If cursor is at start of line and previous node is check list,remove the paragraph + // instead of delete backward, which fix the bug that the check list will be removed when user + // press backspace at start of line. + if (selectedParagraphNodeEntry) { + const isCursorAtStartOfLine = isStartPoint(editor, selection.anchor, selection); + const previouseNodeEntry = getPrevNode(newEditor); + const isCheckListAtPrevious = previouseNodeEntry && previouseNodeEntry[0].type === CHECK_LIST_ITEM; + if (isCursorAtStartOfLine && isCheckListAtPrevious) { + const focusPoint = Editor.end(newEditor, previouseNodeEntry[1]); + const selectedParagraphText = Node.string(selectedParagraphNodeEntry[0]); + const checkListText = Node.string(previouseNodeEntry[0]); + const newCheckListText = checkListText + selectedParagraphText; + Transforms.insertText(newEditor, newCheckListText, { at: previouseNodeEntry[1] }); + Transforms.removeNodes(newEditor, { at: selectedParagraphNodeEntry[1] }); + Transforms.select(newEditor, focusPoint); + return; + } + } + + // If selected check list node is empty, transform it to paragraph. const selectedCheckListNode = getSelectedNodeByType(newEditor, CHECK_LIST_ITEM); if (selectedCheckListNode) { const checkListNodeText = Node.string(selectedCheckListNode); diff --git a/src/extension/plugins/paragraph/index.js b/src/extension/plugins/paragraph/index.js index 04734ae4..5feb8680 100644 --- a/src/extension/plugins/paragraph/index.js +++ b/src/extension/plugins/paragraph/index.js @@ -1,11 +1,9 @@ import { PARAGRAPH } from '../../constants/element-types'; -import withParagraph from './plugin'; import renderParagraph from './render-elem'; const ParagraphPlugin = { type: PARAGRAPH, nodeType: 'element', - editorPlugin: withParagraph, renderElements: [renderParagraph] }; diff --git a/src/extension/plugins/paragraph/plugin.js b/src/extension/plugins/paragraph/plugin.js deleted file mode 100644 index 9ad5b956..00000000 --- a/src/extension/plugins/paragraph/plugin.js +++ /dev/null @@ -1,40 +0,0 @@ -import { Editor, Node, Range, Transforms } from 'slate'; -import { getPrevNode, getSelectedNodeEntryByType, isStartPoint } from '../../core'; -import { CHECK_LIST_ITEM, PARAGRAPH } from '../../constants/element-types'; - -const withParagraph = (editor) => { - const { deleteBackward } = editor; - const newEditor = editor; - - newEditor.deleteBackward = (unit) => { - const { selection } = newEditor; - if (!selection) { - deleteBackward(unit); - return; - } - const selectedParagraphNodeEntry = getSelectedNodeEntryByType(newEditor, PARAGRAPH); - if (selectedParagraphNodeEntry) { - const isCollapsed = Range.isCollapsed(selection); - const isCursorAtStartOfLine = isStartPoint(editor, selection.anchor, selection); - const previouseNodeEntry = getPrevNode(newEditor); - const isCheckListAtPrevious = previouseNodeEntry && previouseNodeEntry[0].type === CHECK_LIST_ITEM; - // If cursor is at start of line and previous node is check list,remove the paragraph - // instead of delete backward, which fix the bug that the check list will be removed when user - // press backspace at start of line. - if (isCollapsed && isCursorAtStartOfLine && isCheckListAtPrevious) { - const focusPoint = Editor.end(newEditor, previouseNodeEntry[1]); - const selectedParagraphText = Node.string(selectedParagraphNodeEntry[0]); - const checkListText = Node.string(previouseNodeEntry[0]); - const newCheckListText = checkListText + selectedParagraphText; - Transforms.insertText(newEditor, newCheckListText, { at: previouseNodeEntry[1] }); - Transforms.removeNodes(newEditor, { at: selectedParagraphNodeEntry[1] }); - Transforms.select(newEditor, focusPoint); - return; - } - } - return deleteBackward(unit); - }; - return newEditor; -}; - -export default withParagraph;