-
-
Notifications
You must be signed in to change notification settings - Fork 755
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2687 from archie9211/main
Delete forward on empty line before code block unwraps code line #2686
- Loading branch information
Showing
12 changed files
with
177 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
}, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -321,6 +321,7 @@ export const plugins = createPlugins( | |
}, | ||
}, | ||
}), | ||
|
||
createSoftBreakPlugin({ | ||
options: { | ||
rules: [ | ||
|