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

Delete forward on empty line before code block unwraps code line #2686 #2687

Merged
merged 18 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from 15 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
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
3 changes: 2 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,7 @@ 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 foward',
zbeyens marked this conversation as resolved.
Show resolved Hide resolved
[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.createDeletePlugin,
zbeyens marked this conversation as resolved.
Show resolved Hide resolved
}),
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);
});
});
22 changes: 22 additions & 0 deletions packages/select/src/createDeletePlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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,
12joan marked this conversation as resolved.
Show resolved Hide resolved
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 = queryNode(getAboveNode(editor), query);
zbeyens marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -154,6 +154,7 @@ import { TableRowElement } from '@/components/plate-ui/table-row-element';
import { TodoListElement } from '@/components/plate-ui/todo-list-element';
import { withDraggables } from '@/components/plate-ui/with-draggables';
import { TabbableElement } from '@/components/tabbable-element';
import { createDeletePlugin } from '@udecode/plate-select';
zbeyens marked this conversation as resolved.
Show resolved Hide resolved

const resetBlockTypesCommonRule = {
types: [ELEMENT_BLOCKQUOTE, ELEMENT_TODO_LI],
Expand Down Expand Up @@ -321,6 +322,7 @@ export const plugins = createPlugins(
},
},
}),

createSoftBreakPlugin({
options: {
rules: [
Expand Down
Loading