-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improves adding rich text voids to RTE
- Loading branch information
Showing
5 changed files
with
45 additions
and
7 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
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
25 changes: 25 additions & 0 deletions
25
src/admin/components/forms/field-types/RichText/injectVoid.ts
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,25 @@ | ||
import { Editor, Element, Transforms } from 'slate'; | ||
import { isLastSelectedElementEmpty } from './isLastSelectedElementEmpty'; | ||
|
||
export const injectVoidElement = (editor: Editor, element: Element): void => { | ||
const lastSelectedElementIsEmpty = isLastSelectedElementEmpty(editor); | ||
const previous = Editor.previous(editor); | ||
|
||
if (lastSelectedElementIsEmpty) { | ||
// If previous node is void | ||
if (Editor.isVoid(editor, previous?.[0])) { | ||
// Insert a blank element between void nodes | ||
// so user can place cursor between void nodes | ||
Transforms.insertNodes(editor, { children: [{ text: '' }] }); | ||
Transforms.setNodes(editor, element); | ||
// Otherwise just set the empty node equal to new upload | ||
} else { | ||
Transforms.setNodes(editor, element); | ||
} | ||
|
||
// Add an empty node after the new upload | ||
Transforms.insertNodes(editor, { children: [{ text: '' }] }); | ||
} else { | ||
Transforms.insertNodes(editor, [element, { children: [{ text: '' }] }]); | ||
} | ||
}; |
14 changes: 14 additions & 0 deletions
14
src/admin/components/forms/field-types/RichText/isLastSelectedElementEmpty.ts
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,14 @@ | ||
import { Editor, Element } from 'slate'; | ||
|
||
export const isLastSelectedElementEmpty = (editor: Editor): boolean => { | ||
const currentlySelectedNodes = Array.from(Editor.nodes(editor, { | ||
at: Editor.unhangRange(editor, editor.selection), | ||
match: (n) => !Editor.isEditor(n) && Element.isElement(n) && (!n.type || n.type === 'p'), | ||
})); | ||
|
||
const lastSelectedNode = currentlySelectedNodes?.[currentlySelectedNodes?.length - 1]; | ||
|
||
return lastSelectedNode && Element.isElement(lastSelectedNode[0]) | ||
&& (!lastSelectedNode[0].type || lastSelectedNode[0].type === 'p') | ||
&& lastSelectedNode[0].children?.[0].text === ''; | ||
}; |
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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
export default [{ | ||
type: 'p', | ||
children: [{ text: '' }], | ||
}]; |