Skip to content

Commit

Permalink
linting and logic simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
archie9211 committed Oct 8, 2023
1 parent 90b2079 commit 426e696
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 97 deletions.
2 changes: 1 addition & 1 deletion .changeset/shy-billo-bagga.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
'@udecode/plate-select': minor
---

With this patch, deleteForward will check if current Line is empty then current line is deleted and cursor is moved to next line
Added `createDeletePlugin`. If enabled, performing a delete forward inside an empty block will remove that block without affecting the subsequent block.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import { PlatePlugin } from '@udecode/plate-common';
import { ELEMENT_PARAGRAPH } from '@udecode/plate-paragraph';
import { DeletePlugin } from '@udecode/plate-select';

export const removeOnDeleteForwardPlugin: Partial<
PlatePlugin<DeletePlugin>
> = {
export const deletePlugin: Partial<PlatePlugin<DeletePlugin>> = {
options: {
query: {
allow: [ELEMENT_PARAGRAPH],
Expand Down
10 changes: 6 additions & 4 deletions apps/www/src/registry/default/example/playground-demo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ import { createNodeIdPlugin } from '@udecode/plate-node-id';
import { createNormalizeTypesPlugin } from '@udecode/plate-normalizers';
import { createParagraphPlugin } from '@udecode/plate-paragraph';
import { createResetNodePlugin } from '@udecode/plate-reset-node';
import { createSelectOnBackspacePlugin } from '@udecode/plate-select';
import {
createDeletePlugin,
createSelectOnBackspacePlugin,
} from '@udecode/plate-select';
import { createBlockSelectionPlugin } from '@udecode/plate-selection';
import { createDeserializeDocxPlugin } from '@udecode/plate-serializer-docx';
import { createDeserializeMdPlugin } from '@udecode/plate-serializer-md';
Expand All @@ -89,6 +92,7 @@ import { HTML5Backend } from 'react-dnd-html5-backend';

import { ValueId } from '@/config/customizer-plugins';
import { captionPlugin } from '@/lib/plate/demo/plugins/captionPlugin';
import { deletePlugin } from '@/lib/plate/demo/plugins/deletePlugin';
import { cn } from '@/lib/utils';
import { settingsStore } from '@/components/context/settings-store';
import { PlaygroundFixedToolbarButtons } from '@/components/plate-ui/playground-fixed-toolbar-buttons';
Expand All @@ -99,8 +103,6 @@ import { Editor } from '@/registry/default/plate-ui/editor';
import { FixedToolbar } from '@/registry/default/plate-ui/fixed-toolbar';
import { FloatingToolbar } from '@/registry/default/plate-ui/floating-toolbar';
import { MentionCombobox } from '@/registry/default/plate-ui/mention-combobox';
import { removeOnDeleteForwardPlugin } from '@/lib/plate/demo/plugins/removeOnDeleteForwardPlugin';
import { createDeletePlugin } from '@udecode/plate-select';

export const usePlaygroundPlugins = ({
id,
Expand Down Expand Up @@ -219,7 +221,7 @@ export const usePlaygroundPlugins = ({
enabled: !!enabled.selectOnBackspace,
}),
createDeletePlugin({
...removeOnDeleteForwardPlugin,
...deletePlugin,
enabled: !!enabled.selectOnBackspace,
}),
createSingleLinePlugin({
Expand Down
30 changes: 14 additions & 16 deletions packages/select/src/createDeletePlugin.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import { createPlateEditor, PlateEditor } from '@udecode/plate-common';
import { jsx } from '@udecode/plate-test-utils';
import { createDeletePlugin } from './createDeletePlugin'

import { createDeletePlugin } from './createDeletePlugin';

jsx;

Expand All @@ -14,18 +15,18 @@ describe('p (empty) + list when selection not in list', () => {
<cursor />
</hp>
<hcodeblock>
<hcodeline>test</hcodeline>
<hcodeline>test2</hcodeline>
</hcodeblock>
<hcodeline>test</hcodeline>
<hcodeline>test2</hcodeline>
</hcodeblock>
</editor>
) as any as PlateEditor;

const expected = (
<editor>
<hcodeblock>
<hcodeline>test</hcodeline>
<hcodeline>test2</hcodeline>
</hcodeblock>
<hcodeline>test</hcodeline>
<hcodeline>test2</hcodeline>
</hcodeblock>
</editor>
) as any as PlateEditor;

Expand All @@ -40,7 +41,6 @@ describe('p (empty) + list when selection not in list', () => {
});
});


describe('p (empty) + list when selection not in list', () => {
it('should remove the p', () => {
const input = (
Expand All @@ -50,20 +50,18 @@ describe('p (empty) + list when selection not in list', () => {
<cursor />
</hp>
<hcodeblock>
<hcodeline>test</hcodeline>
<hcodeline>test2</hcodeline>
</hcodeblock>
<hcodeline>test</hcodeline>
<hcodeline>test2</hcodeline>
</hcodeblock>
</editor>
) as any as PlateEditor;

const expected = (
<editor>
<hp>
paratest
</hp>
<hp>paratest</hp>
<hcodeblock>
<hcodeline>test2</hcodeline>
</hcodeblock>
<hcodeline>test2</hcodeline>
</hcodeblock>
</editor>
) as any as PlateEditor;

Expand Down
11 changes: 5 additions & 6 deletions packages/select/src/createDeletePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ export type DeletePlugin = {
query?: QueryNodeOptions;
};

export const KEY_FORWARD_DELETE = 'forwardDeleteBeforeCodeBlock';
export const KEY_DELETE = 'delete';

/**
* @see {@link withDelete}
*/
export const createDeletePlugin =
createPluginFactory<DeletePlugin>({
key: KEY_FORWARD_DELETE,
withOverrides: withDelete,
});
export const createDeletePlugin = createPluginFactory<DeletePlugin>({
key: KEY_DELETE,
withOverrides: withDelete,
});
104 changes: 38 additions & 66 deletions packages/select/src/withDelete.ts
Original file line number Diff line number Diff line change
@@ -1,77 +1,49 @@
import {
deleteForward,
getNodeEntries,
getPointBefore,
isBlockAboveEmpty,
isSelectionExpanded,
PlateEditor,
queryNode,
removeNodes,
Value,
WithPlatePlugin,
} from '@udecode/plate-common';

import { DeletePlugin } from './createDeletePlugin';
import Slate from 'slate';

/**
* Set a list of element types to select on backspace
*/
export const withDelete = <
V extends Value = Value,
E extends PlateEditor<V> = PlateEditor<V>,
>(
editor: E,
{
options: { query },
}: WithPlatePlugin<DeletePlugin, V, E>
) => {
const { deleteForward } = editor;
editor.deleteForward = (unit: 'character' | 'word' | 'line' | 'block') => {
const { selection } = editor;
console.log("outer" ,query);
if(!editor.selection) return;

getAboveNode,
isBlockAboveEmpty,
isSelectionExpanded,
PlateEditor,
queryNode,
removeNodes,
Value,
WithPlatePlugin,
} from '@udecode/plate-common';

import { DeletePlugin } from './createDeletePlugin';

/**
* Set a list of element types to select on backspace
*/
export const withDelete = <
V extends Value = Value,
E extends PlateEditor<V> = PlateEditor<V>,
>(
editor: E,
{ options: { query } }: WithPlatePlugin<DeletePlugin, V, E>
) => {
const { deleteForward } = editor;
editor.deleteForward = (unit: 'character' | 'word' | 'line' | 'block') => {
if (!editor.selection) return;
if (!isSelectionExpanded(editor) && isBlockAboveEmpty(editor)) {
console.log("asdas");
// check when line is empty
const isValidNode = queryNode(getAboveNode(editor), query);
if (query) {
//if query value is passed
const pointBefore = getPointBefore(editor, selection as Slate.Location, {
unit,
});

if (pointBefore) {
const [prevCell] = getNodeEntries(editor, {
match: (node) => queryNode([node, pointBefore.path], query),
at: pointBefore,
});
if (!!prevCell && pointBefore) {
console.log("valid cell");
removeNodes(editor as any);

}
else {
console.log("invalid cell");

deleteForward(unit);
}
}
else{
deleteForward(unit);
}
}
else{
console.log("outside query");
//is query is passed
if (isValidNode) {
// cursor is in query blocks
removeNodes(editor as any);
} else {
//fallback to default behaiour
deleteForward(unit);
}
} else {
// query is not passed, then plugin is active everywhere
removeNodes(editor as any);
}
} else {
// when line is not empty, fall back to default behavior
deleteForward(unit);
}
};

return editor;
};



};
1 change: 0 additions & 1 deletion packages/select/src/withSelectOnBackspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export const withSelectOnBackspace = <

editor.deleteBackward = (unit: 'character' | 'word' | 'line' | 'block') => {
const { selection } = editor;
console.log("query", query);
if (unit === 'character' && isCollapsed(selection)) {
const pointBefore = getPointBefore(editor, selection as Slate.Location, {
unit,
Expand Down

0 comments on commit 426e696

Please sign in to comment.