Skip to content

Commit

Permalink
Merge pull request #2687 from archie9211/main
Browse files Browse the repository at this point in the history
Delete forward on empty line before code block unwraps code line #2686
  • Loading branch information
zbeyens authored Oct 10, 2023
2 parents 81f860f + 33f4129 commit 6caf98b
Show file tree
Hide file tree
Showing 12 changed files with 177 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/shy-billo-bagga.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@udecode/plate-select': minor
---

Added `createDeletePlugin`. If enabled, performing a delete forward inside an empty block will remove that block without affecting the subsequent block.
9 changes: 8 additions & 1 deletion apps/www/src/config/customizer-items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import { KEY_NODE_ID } from '@udecode/plate-node-id';
import { KEY_NORMALIZE_TYPES } from '@udecode/plate-normalizers';
import { ELEMENT_PARAGRAPH } from '@udecode/plate-paragraph';
import { KEY_RESET_NODE } from '@udecode/plate-reset-node';
import { KEY_SELECT_ON_BACKSPACE } from '@udecode/plate-select';
import { KEY_DELETE, KEY_SELECT_ON_BACKSPACE } from '@udecode/plate-select';
import { KEY_BLOCK_SELECTION } from '@udecode/plate-selection';
import { KEY_DESERIALIZE_CSV } from '@udecode/plate-serializer-csv';
import { KEY_DESERIALIZE_DOCX } from '@udecode/plate-serializer-docx';
Expand Down Expand Up @@ -957,6 +957,13 @@ export const customizerItems: Record<string, SettingPlugin> = {
badges: [customizerBadges.handler],
route: customizerPlugins.media.route,
},
[KEY_DELETE]: {
id: KEY_DELETE,
npmPackage: '@udecode/plate-select',
pluginFactory: 'createDeletePlugin',
label: 'Delete',
badges: [customizerBadges.handler],
},
[KEY_SINGLE_LINE]: {
id: KEY_SINGLE_LINE,
npmPackage: '@udecode/plate-break',
Expand Down
4 changes: 3 additions & 1 deletion apps/www/src/config/customizer-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import { KEY_NODE_ID } from '@udecode/plate-node-id';
import { KEY_NORMALIZE_TYPES } from '@udecode/plate-normalizers';
import { ELEMENT_PARAGRAPH } from '@udecode/plate-paragraph';
import { KEY_RESET_NODE } from '@udecode/plate-reset-node';
import { KEY_SELECT_ON_BACKSPACE } from '@udecode/plate-select';
import { KEY_DELETE, KEY_SELECT_ON_BACKSPACE } from '@udecode/plate-select';
import { KEY_BLOCK_SELECTION } from '@udecode/plate-selection';
import { KEY_DESERIALIZE_CSV } from '@udecode/plate-serializer-csv';
import { KEY_DESERIALIZE_DOCX } from '@udecode/plate-serializer-docx';
Expand Down Expand Up @@ -117,6 +117,7 @@ export const customizerList = [
customizerItems[KEY_NORMALIZE_TYPES],
customizerItems[KEY_RESET_NODE],
customizerItems[KEY_SELECT_ON_BACKSPACE],
customizerItems[KEY_DELETE],
customizerItems[KEY_SINGLE_LINE],
customizerItems[KEY_SOFT_BREAK],
customizerItems[KEY_TABBABLE],
Expand Down Expand Up @@ -182,6 +183,7 @@ export const orderedPluginKeys = [
KEY_NORMALIZE_TYPES,
KEY_RESET_NODE,
KEY_SELECT_ON_BACKSPACE,
KEY_DELETE,
KEY_SINGLE_LINE,
KEY_SOFT_BREAK,
KEY_TABBABLE,
Expand Down
4 changes: 3 additions & 1 deletion apps/www/src/config/descriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import { KEY_NODE_ID } from '@udecode/plate-node-id';
import { KEY_NORMALIZE_TYPES } from '@udecode/plate-normalizers';
import { ELEMENT_PARAGRAPH } from '@udecode/plate-paragraph';
import { KEY_RESET_NODE } from '@udecode/plate-reset-node';
import { KEY_SELECT_ON_BACKSPACE } from '@udecode/plate-select';
import { KEY_DELETE, KEY_SELECT_ON_BACKSPACE } from '@udecode/plate-select';
import { KEY_BLOCK_SELECTION } from '@udecode/plate-selection';
import { KEY_DESERIALIZE_CSV } from '@udecode/plate-serializer-csv';
import { KEY_DESERIALIZE_DOCX } from '@udecode/plate-serializer-docx';
Expand Down Expand Up @@ -94,6 +94,8 @@ export const descriptions: Record<string, string> = {
[KEY_RESET_NODE]: 'Reset the block type using rules.',
[KEY_SELECT_ON_BACKSPACE]:
'Select the preceding block instead of deleting when pressing backspace.',
[KEY_DELETE]:
'Remove the current block if empty when pressing delete forward',
[KEY_SINGLE_LINE]: 'Restrict the editor to a single block.',
[KEY_SOFT_BREAK]:
'Insert line breaks within a block of text without starting a new block.',
Expand Down
8 changes: 7 additions & 1 deletion 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 Down Expand Up @@ -216,6 +219,9 @@ export const usePlaygroundPlugins = ({
...selectOnBackspacePlugin,
enabled: !!enabled.selectOnBackspace,
}),
createDeletePlugin({
enabled: !!enabled.delete,
}),
createSingleLinePlugin({
enabled: id === 'singleline' || !!enabled.singleLine,
}),
Expand Down
77 changes: 77 additions & 0 deletions packages/select/src/createDeletePlugin.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/** @jsx jsx */

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

import { createDeletePlugin } from './createDeletePlugin';

jsx;

describe('p (empty) + codeblock when selection not in code block', () => {
it('should remove the p', () => {
const input = (
<editor>
<hp>
<cursor />
</hp>
<hcodeblock>
<hcodeline>test</hcodeline>
<hcodeline>test2</hcodeline>
</hcodeblock>
</editor>
) as any as PlateEditor;

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

const editor = createPlateEditor({
editor: input,
plugins: [createDeletePlugin()],
});

editor.deleteForward('character');

expect(editor.children).toEqual(expected.children);
});
});

describe('p (not empty) + code block when selection not in code block', () => {
it('should remove the p', () => {
const input = (
<editor>
<hp>
para
<cursor />
</hp>
<hcodeblock>
<hcodeline>test</hcodeline>
<hcodeline>test2</hcodeline>
</hcodeblock>
</editor>
) as any as PlateEditor;

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

const editor = createPlateEditor({
editor: input,
plugins: [createDeletePlugin()],
});

editor.deleteForward('character');

expect(editor.children).toEqual(expected.children);
});
});
26 changes: 26 additions & 0 deletions packages/select/src/createDeletePlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {
createPluginFactory,
ELEMENT_DEFAULT,
QueryNodeOptions,
} from '@udecode/plate-common';

import { withDelete } from './withDelete';

export type DeletePlugin = {
query?: QueryNodeOptions;
};

export const KEY_DELETE = 'delete';

/**
* @see {@link withDelete}
*/
export const createDeletePlugin = createPluginFactory<DeletePlugin>({
key: KEY_DELETE,
withOverrides: withDelete,
options: {
query: {
allow: [ELEMENT_DEFAULT],
},
},
});
2 changes: 2 additions & 0 deletions packages/select/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@

export * from './createSelectOnBackspacePlugin';
export * from './withSelectOnBackspace';
export * from './createDeletePlugin';
export * from './withDelete';
41 changes: 41 additions & 0 deletions packages/select/src/withDelete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {
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) => {
if (!editor.selection) return;
const isValidNode = !query || queryNode(getAboveNode(editor), query);
if (
!isSelectionExpanded(editor) &&
isBlockAboveEmpty(editor) &&
isValidNode
) {
// Cursor is in query blocks and line is empty
removeNodes(editor as any);
} else {
// When the line is not empty or other conditions are not met, 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;

if (unit === 'character' && isCollapsed(selection)) {
const pointBefore = getPointBefore(editor, selection as Slate.Location, {
unit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ export const cleanDocxImageElements = (

const alt = element.getAttribute('alt');

if (typeof alt === 'string' && validator.isURL(alt, { require_protocol: true })) {
if (
typeof alt === 'string' &&
validator.isURL(alt, { require_protocol: true })
) {
element.setAttribute('src', alt);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ export const plugins = createPlugins(
},
},
}),

createSoftBreakPlugin({
options: {
rules: [
Expand Down

0 comments on commit 6caf98b

Please sign in to comment.