Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move check-list logic from paragrph to check-list #29

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions src/extension/plugins/check-list/plugin.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -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);
Expand Down
2 changes: 0 additions & 2 deletions src/extension/plugins/paragraph/index.js
Original file line number Diff line number Diff line change
@@ -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]
};

Expand Down
40 changes: 0 additions & 40 deletions src/extension/plugins/paragraph/plugin.js

This file was deleted.

Loading