Skip to content

Commit

Permalink
Toggle > Handle deleteForward before a non-selectable and deleteBackw…
Browse files Browse the repository at this point in the history
…ard after a non-selectable
  • Loading branch information
Liboul committed Feb 1, 2024
1 parent aa0b607 commit 516a2ac
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 3 deletions.
2 changes: 2 additions & 0 deletions packages/toggle/src/transforms/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
* @file Automatically generated by barrelsby.
*/

export * from './moveCurrentBlockAfterPreviousSelectable';
export * from './moveNextSelectableAfterCurrentBlock';
export * from './openNextToggles';
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {
getBlockAbove,
getPointBefore,
getPreviousNode,
isElement,
isSelectionAtBlockStart,
moveNodes,
PlateEditor,
} from '@udecode/plate-common';

import { isInClosedToggle } from '../queries';

// Return false only if the all previous blocks are not selectable
export const moveCurrentBlockAfterPreviousSelectable = (
editor: PlateEditor
): boolean | undefined => {
const { selection } = editor;
if (!selection) return;
const aboveBlock = getBlockAbove(editor);
if (!aboveBlock) return;
if (!isSelectionAtBlockStart(editor)) return;
const beforePoint = getPointBefore(editor, selection);
if (!beforePoint) return;
const blockBefore = getBlockAbove(editor, { at: beforePoint });
if (!blockBefore) return;
if (!isInClosedToggle(editor, blockBefore[0].id)) return; // We're already after a selectable then
const previousSelectableBlock = getPreviousNode(editor, {
match: (node) =>
isElement(node) && !isInClosedToggle(editor, node.id as string),
});
if (!previousSelectableBlock) return false;
const afterSelectableBlock = [previousSelectableBlock[1][0] + 1];
moveNodes(editor, {
at: aboveBlock[1],
to: afterSelectableBlock,
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {
getBlockAbove,
getNextNode,
getPointAfter,
isElement,
isSelectionAtBlockEnd,
moveNodes,
PlateEditor,
} from '@udecode/plate-common';

import { isInClosedToggle } from '../queries';

// Return false only if all next blocks are not selectable
export const moveNextSelectableAfterCurrentBlock = (editor: PlateEditor) => {
const { selection } = editor;
if (!selection) return;
const aboveBlock = getBlockAbove(editor);
if (!aboveBlock) return;
if (!isSelectionAtBlockEnd(editor)) return;
const afterPoint = getPointAfter(editor, selection);
if (!afterPoint) return;
const blockAfter = getBlockAbove(editor, { at: afterPoint });
if (!blockAfter) return;
if (!isInClosedToggle(editor, blockAfter[0].id)) return; // We're already before a selectable then
const nextSelectableBlock = getNextNode(editor, {
match: (node) =>
isElement(node) && !isInClosedToggle(editor, node.id as string),
});
if (!nextSelectableBlock) return false;
const afterCurrentBlock = [aboveBlock[1][0] + 1];
moveNodes(editor, {
at: nextSelectableBlock[1],
to: afterCurrentBlock,
});
};
30 changes: 27 additions & 3 deletions packages/toggle/src/withToggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ import {
isNode,
moveNodes,
PlateEditor,
select,
toggleNodeType,
Value,
} from '@udecode/plate-common';
import { indent, TIndentElement } from '@udecode/plate-indent';

import { getLastEntryEnclosedInToggle, isInClosedToggle } from './queries';
import { isToggleOpen } from './toggle-controller-store';
import {
moveCurrentBlockAfterPreviousSelectable,
moveNextSelectableAfterCurrentBlock,
} from './transforms';
import { ELEMENT_TOGGLE } from './types';

export const withToggle = <
Expand All @@ -18,14 +23,28 @@ export const withToggle = <
>(
editor: E
) => {
const { insertBreak, isSelectable } = editor;
const { insertBreak, isSelectable, deleteBackward, deleteForward } = editor;

editor.isSelectable = (element) => {
if (isNode(element) && isInClosedToggle<V, E>(editor, element.id as string))
return false;
return isSelectable(element);
};

editor.deleteBackward = (unit) => {
if (
moveCurrentBlockAfterPreviousSelectable(editor as PlateEditor) === false
)
return;
deleteBackward(unit);
};

editor.deleteForward = (unit) => {
if (moveNextSelectableAfterCurrentBlock(editor as PlateEditor) === false)
return;
deleteForward(unit);
};

editor.insertBreak = () => {
// If we are inserting a break in a toggle:
// If the toggle is open
Expand Down Expand Up @@ -57,10 +76,15 @@ export const withToggle = <
insertBreak();

if (lastEntryEnclosedInToggle) {
const newlyInsertedTogglePath = [currentBlockEntry[1][0] + 1];
const afterLastEntryEncloseInToggle = [
lastEntryEnclosedInToggle[1][0] + 1,
];
moveNodes(editor, {
at: [currentBlockEntry[1][0] + 1], // Newly inserted toggle
to: [lastEntryEnclosedInToggle[1][0] + 1], // Right after the last enclosed element
at: newlyInsertedTogglePath,
to: afterLastEntryEncloseInToggle,
});
select(editor, afterLastEntryEncloseInToggle);
}
}
});
Expand Down

0 comments on commit 516a2ac

Please sign in to comment.