Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Refactor - Migrate to Plate #575

Merged
merged 54 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
42d73de
Install Plate packages and update tsconfig
kudlajz Nov 13, 2024
2c4327f
Update bunch of code to the new API
kudlajz Nov 13, 2024
8391d0e
Change build target
kudlajz Nov 18, 2024
611b8cc
Add plate-common as peer dependency
kudlajz Nov 18, 2024
6d68c9e
Update the remaining code to use Plate API
kudlajz Nov 18, 2024
90d037c
Update generic type
kudlajz Nov 19, 2024
4585afc
Re-enable decorators
kudlajz Nov 19, 2024
0835d0d
Update more API calls, re-enable extensions
kudlajz Nov 19, 2024
893b735
Provide initial value to the Plate editor
kudlajz Nov 19, 2024
d8a4896
Run prettier
kudlajz Nov 19, 2024
089cb02
Update and sync Slate packages
kudlajz Nov 19, 2024
8b1b096
Use SlateEditor instead of PlateEditor type
kudlajz Nov 19, 2024
231bbc3
Make ElementNode extend TElement and TextNode TText
kudlajz Nov 19, 2024
93d3701
Suppress code using node.children for now
kudlajz Nov 19, 2024
86bbd0b
Add skipLibCheck parameter to test:types scripts
kudlajz Nov 19, 2024
e0c5696
v0.117.0-alpha.0
kudlajz Nov 19, 2024
8ea8c42
Use SlateEditor type instead of BaseEditor where applicable
kudlajz Nov 20, 2024
8fa7b82
Use @udecode/plate-common as peer dependency, replace imports to use …
kudlajz Nov 20, 2024
b2d7993
Rewrite withEvents as Plate plugin, allow providing plugins from the …
kudlajz Nov 21, 2024
272cce5
Get rid of TS error suppressions
kudlajz Nov 21, 2024
ecad174
Bring back autoformat plugin and update some more types
kudlajz Nov 21, 2024
1f360ca
Use correct functions instead of `HistoryEditor` object
kudlajz Nov 21, 2024
b8b2586
v0.117.1-alpha.0
kudlajz Nov 21, 2024
39467bc
Export some more stuff so we can easily import it in the app
kudlajz Nov 21, 2024
3511d16
v0.117.2-alpha.0
kudlajz Nov 21, 2024
5effad9
Replace custom autoformat extension with Plate plugin
kudlajz Dec 5, 2024
e66a57f
Merge branch 'main' into feature/dev-14744-explore-migrating-to-plate…
kudlajz Jan 7, 2025
92295e8
Fixed select method to use Plate's API
kudlajz Jan 7, 2025
4648f69
Get rid of redundant toDomNode function
kudlajz Jan 7, 2025
3cbf3ba
Get rid of redundant toDomRange function
kudlajz Jan 7, 2025
cd051a8
Uncomment code in FlashNodesExtension
kudlajz Jan 7, 2025
126820e
Uncomment code in withTablesCopyPasteBehavior
kudlajz Jan 7, 2025
0763524
Return early instead of using non-null assertion
kudlajz Jan 7, 2025
9d1be1a
Use SlateEditor type instead of unknown
kudlajz Jan 7, 2025
3082634
Combine imports
kudlajz Jan 8, 2025
6015ba2
Remove skipLibCheck and use esModuleInterop instead
kudlajz Jan 8, 2025
bf617b6
Add plugins to dependency list
kudlajz Jan 8, 2025
1a82a85
Drop isEditor check to not drop the editor node
kudlajz Jan 8, 2025
b489ae6
Use getNodeString instead of Node.string or the string property
kudlajz Jan 8, 2025
2afe8ea
Use getLevels instead of Node.levels
kudlajz Jan 8, 2025
1a6f033
Use removeNodes instead of editor.removeNodes
kudlajz Jan 8, 2025
849f04b
Uncomment props in Editor stories
kudlajz Jan 8, 2025
22fdb99
Fixed linter issues
kudlajz Jan 8, 2025
b671c6f
Re-enable FlashNodes component
kudlajz Jan 8, 2025
3d755b7
Use correct hook to allow re-rendering
kudlajz Jan 8, 2025
d469c2a
Use correct hook to allow re-rendering
kudlajz Jan 8, 2025
4dfa84f
use onValueChange for more optimized updates
kudlajz Jan 8, 2025
61b1057
v0.117.3-alpha.0
kudlajz Jan 8, 2025
1c8b235
Use stable constant for empty plugins array
kudlajz Jan 9, 2025
62ebd6a
Drop withReact and withHistory as Plate already uses them automatically
kudlajz Jan 9, 2025
62997ef
Use withReact and withHistory for tests (for now)
kudlajz Jan 9, 2025
0a1e9b7
v0.117.4-alpha.0
kudlajz Jan 9, 2025
a8f7259
Use correct type
kudlajz Jan 9, 2025
941d07c
Enable custom rendering after the placeholder is mounted
kudlajz Jan 10, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
"version": "0.117.0"
"version": "0.117.4-alpha.0"
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
"@types/react-dom": "^18.3.0",
"react": "^18.3.0",
"react-dom": "^18.3.0",
"slate": "^0.101.5",
"slate-history": "^0.100.0",
"slate-react": "^0.101.5"
"slate": "^0.110.2",
"slate-history": "^0.110.3",
"slate-react": "^0.111.0"
},
"devDependencies": {
"@babel/cli": "^7.23.9",
Expand Down
9 changes: 5 additions & 4 deletions packages/slate-commons/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@prezly/slate-commons",
"version": "0.117.0",
"version": "0.117.3-alpha.0",
"description": "Low-level commands, utilities, plugins, types, etc. used throughout Prezly-Slate packages",
"license": "MIT",
"type": "module",
Expand Down Expand Up @@ -42,11 +42,12 @@
"clean:build": "rimraf build/ *.tsbuildinfo"
},
"peerDependencies": {
"@udecode/plate-common": "^40.0.0",
"react": "^18.3.0",
"react-dom": "^18.3.0",
"slate": "^0.101.5",
"slate-history": "^0.100.0",
"slate-react": "^0.101.5"
"slate": "^0.110.2",
"slate-history": "^0.110.3",
"slate-react": "^0.111.0"
},
"dependencies": {
"@prezly/slate-types": "workspace:*",
Expand Down
25 changes: 14 additions & 11 deletions packages/slate-commons/src/commands/alignment.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { AlignableNode, Alignment } from '@prezly/slate-types';
import { isAlignableElement, isTableCellNode } from '@prezly/slate-types';
import type { Node, NodeEntry, Path } from 'slate';
import { Editor, Transforms } from 'slate';
import { getAboveNode, type SlateEditor, type TNodeEntry } from '@udecode/plate-common';
import type { Node, Path } from 'slate';

export function getAlignment(editor: Editor, defaultAlignment: Alignment): Alignment[] {
const nodes = Editor.nodes<AlignableNode>(editor, {
export function getAlignment(editor: SlateEditor, defaultAlignment: Alignment): Alignment[] {
const nodes = editor.nodes<AlignableNode>({
match: (node, path) => isTopLevelAlignableElement(editor, node, path),
});

Expand All @@ -17,27 +17,30 @@ export function getAlignment(editor: Editor, defaultAlignment: Alignment): Align
return [...alignments];
}

export function toggleAlignment(editor: Editor, align: Alignment | undefined): void {
export function toggleAlignment(editor: SlateEditor, align: Alignment | undefined): void {
if (align === undefined) {
Transforms.unsetNodes(editor, 'align', {
editor.unsetNodes('align', {
match: (node, path) => isTopLevelAlignableElement(editor, node, path),
});
return;
}

Transforms.setNodes<AlignableNode>(
editor,
editor.setNodes<AlignableNode>(
{ align },
{ match: (node, path) => isTopLevelAlignableElement(editor, node, path) },
);
}

function isAlignmentRoot([node, path]: NodeEntry): boolean {
function isAlignmentRoot([node, path]: TNodeEntry): boolean {
// We allow aligning elements either at top-level or inside table cells.
return path.length === 0 || isTableCellNode(node);
}

function isTopLevelAlignableElement(editor: Editor, node: Node, path: Path): node is AlignableNode {
const parent = Editor.above(editor, { at: path });
function isTopLevelAlignableElement(
editor: SlateEditor,
node: Node,
path: Path,
): node is AlignableNode {
const parent = getAboveNode(editor, { at: path });
return parent !== undefined && isAlignmentRoot(parent) && isAlignableElement(node);
}
8 changes: 4 additions & 4 deletions packages/slate-commons/src/commands/blur.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Editor } from 'slate';
import { ReactEditor } from 'slate-react';
import type { SlateEditor } from '@udecode/plate-common';
import { deselectEditor } from '@udecode/plate-common/react';

export function blur(editor: Editor & ReactEditor): void {
ReactEditor.deselect(editor);
export function blur(editor: SlateEditor): void {
deselectEditor(editor);
}
5 changes: 3 additions & 2 deletions packages/slate-commons/src/commands/findLeafLocation.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { Editor, Location } from 'slate';
import type { SlateEditor } from '@udecode/plate-common';
import type { Location } from 'slate';
import { Path, Point } from 'slate';

import { findLeafPath } from './findLeafPath';
import { findLeafPoint } from './findLeafPoint';
import { findLeafRange } from './findLeafRange';

export function findLeafLocation(editor: Editor, location: Location): Location | undefined {
export function findLeafLocation(editor: SlateEditor, location: Location): Location | undefined {
if (Path.isPath(location)) {
return findLeafPath(editor, location);
}
Expand Down
12 changes: 8 additions & 4 deletions packages/slate-commons/src/commands/findLeafPath.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import type { SlateEditor } from '@udecode/plate-common';
import type { Path } from 'slate';
import { Editor, Node, Text } from 'slate';
import { Node, Text } from 'slate';

export type Edge = 'highest' | 'lowest';

export function findLeafPath(editor: Editor, path: Path, edge: Edge = 'highest'): Path | undefined {
export function findLeafPath(
editor: SlateEditor,
path: Path,
edge: Edge = 'highest',
): Path | undefined {
if (!Node.has(editor, path)) {
return undefined;
}
Expand All @@ -14,8 +19,7 @@ export function findLeafPath(editor: Editor, path: Path, edge: Edge = 'highest')
return path;
}

const [, nestedPath] =
edge === 'highest' ? Editor.first(editor, path) : Editor.last(editor, path);
const [, nestedPath] = edge === 'highest' ? editor.first(path) : editor.last(path);

return findLeafPath(editor, nestedPath);
}
7 changes: 4 additions & 3 deletions packages/slate-commons/src/commands/findLeafPoint.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Editor, Path, Point } from 'slate';
import type { SlateEditor } from '@udecode/plate-common';
import { Path, Point } from 'slate';

import type { Edge } from './findLeafPath';
import { findLeafPath } from './findLeafPath';

export function findLeafPoint(
editor: Editor,
editor: SlateEditor,
point: Point,
edge: Edge = 'highest',
): Point | undefined {
Expand All @@ -14,7 +15,7 @@ export function findLeafPoint(
return undefined;
}

const [, end] = Editor.edges(editor, path);
const [, end] = editor.edges(path);

if (Path.equals(point.path, path)) {
if (Point.isAfter(point, end)) {
Expand Down
5 changes: 3 additions & 2 deletions packages/slate-commons/src/commands/findLeafRange.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { Editor, Range } from 'slate';
import type { SlateEditor } from '@udecode/plate-common';
import type { Range } from 'slate';

import { findLeafPoint } from './findLeafPoint';

export function findLeafRange(editor: Editor, range: Range): Range | undefined {
export function findLeafRange(editor: SlateEditor, range: Range): Range | undefined {
const anchor = findLeafPoint(editor, range.anchor);
const focus = findLeafPoint(editor, range.focus);

Expand Down
8 changes: 4 additions & 4 deletions packages/slate-commons/src/commands/focus.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { Editor } from 'slate';
import { ReactEditor } from 'slate-react';
import type { SlateEditor } from '@udecode/plate-common';
import { focusEditor } from '@udecode/plate-common/react';

import { moveCursorToEndOfDocument } from './moveCursorToEndOfDocument';

export function focus(editor: Editor & ReactEditor): void {
ReactEditor.focus(editor);
export function focus(editor: SlateEditor): void {
focusEditor(editor);
moveCursorToEndOfDocument(editor);
}
9 changes: 4 additions & 5 deletions packages/slate-commons/src/commands/getCurrentDomNode.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import type { Editor } from 'slate';
import type { ReactEditor } from 'slate-react';
import type { SlateEditor } from '@udecode/plate-common';
import { toDOMNode } from '@udecode/plate-common/react';

import { getCurrentNodeEntry } from './getCurrentNodeEntry';
import { toDomNode } from './toDomNode';

export function getCurrentDomNode(editor: Editor & ReactEditor): HTMLElement | null {
export function getCurrentDomNode(editor: SlateEditor): HTMLElement | null {
const [currentNode] = getCurrentNodeEntry(editor) || [];

if (!currentNode) {
return null;
}

return toDomNode(editor, currentNode);
return toDOMNode(editor, currentNode) ?? null;
}
8 changes: 4 additions & 4 deletions packages/slate-commons/src/commands/getCurrentNodeEntry.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { NodeEntry } from 'slate';
import { Editor } from 'slate';
import type { TNodeEntry } from '@udecode/plate-common';
import { getNodeEntry, type SlateEditor } from '@udecode/plate-common';

import { isSelectionValid } from './isSelectionValid';

export function getCurrentNodeEntry(editor: Editor): NodeEntry | null {
export function getCurrentNodeEntry(editor: SlateEditor): TNodeEntry | null {
if (!editor.selection || !isSelectionValid(editor)) {
return null;
}

return Editor.node(editor, editor.selection, { depth: 1 });
return getNodeEntry(editor, editor.selection, { depth: 1 }) ?? null;
}
8 changes: 4 additions & 4 deletions packages/slate-commons/src/commands/getEditorRange.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { SlateEditor } from '@udecode/plate-common';
import type { Range } from 'slate';
import { Editor } from 'slate';

export function getEditorRange(editor: Editor): Range | undefined {
export function getEditorRange(editor: SlateEditor): Range | undefined {
// editor.children can sometimes be undefined, even though TypeScript says otherwise
if (!editor.children || editor.children.length === 0) {
return undefined;
}

return {
anchor: Editor.start(editor, [0]),
focus: Editor.end(editor, [editor.children.length - 1]),
anchor: editor.start([0]),
focus: editor.end([editor.children.length - 1]),
};
}
8 changes: 4 additions & 4 deletions packages/slate-commons/src/commands/getNodePath.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { SlateEditor } from '@udecode/plate-common';
import type { Path } from 'slate';
import { Editor } from 'slate';

export function getNodePath(
editor: Editor,
options: NonNullable<Parameters<typeof Editor.nodes>[1]>,
editor: SlateEditor,
options: NonNullable<Parameters<typeof editor.nodes>[0]>,
): Path | null {
const [entry] = Editor.nodes(editor, options);
const [entry] = editor.nodes(options);
return entry ? entry[1] : null;
}
9 changes: 5 additions & 4 deletions packages/slate-commons/src/commands/getPrevChars.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import { Editor, Path, Text } from 'slate';
import type { SlateEditor } from '@udecode/plate-common';
import { Path, Text } from 'slate';

import { isVoid } from './isVoid';

export function getPrevChars(editor: Editor, length: number): string {
export function getPrevChars(editor: SlateEditor, length: number): string {
const selection = editor.selection;

if (!selection) {
return '';
}

const { focus } = selection;
let text = Editor.string(editor, { focus, anchor: { path: focus.path, offset: 0 } });
let text = editor.string({ focus, anchor: { path: focus.path, offset: 0 } });

if (text.length > length) {
return text.slice(-length);
}

const start = { path: [...Path.parent(focus.path), 0], offset: 0 };

const nodes = Editor.nodes(editor, {
const nodes = editor.nodes({
mode: 'lowest',
at: { anchor: start, focus },
reverse: true,
Expand Down
5 changes: 3 additions & 2 deletions packages/slate-commons/src/commands/hasVoidElements.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { Node, Editor } from 'slate';
import type { SlateEditor } from '@udecode/plate-common';
import type { Node } from 'slate';
import { Text } from 'slate';

import { isVoid } from './isVoid';

export function hasVoidElements(editor: Editor, node: Node): boolean {
export function hasVoidElements(editor: SlateEditor, node: Node): boolean {
if (Text.isText(node)) {
return false;
}
Expand Down
3 changes: 0 additions & 3 deletions packages/slate-commons/src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export {
} from './isCursorOnEdgeOfContainer';
export { isEmpty } from './isEmpty';
export { isEmptyParagraphElement } from './isEmptyParagraphElement';
export { isInline } from './isInline';
export { isMarkActive } from './isMarkActive';
export { isNodeEmpty } from './isNodeEmpty';
export { isSelectionAtBlockEnd } from './isSelectionAtBlockEnd';
Expand Down Expand Up @@ -53,7 +52,5 @@ export {
} from './roughly-normalize';
export { saveSelection } from './saveSelection';
export { selectNode } from './selectNode';
export { toDomNode } from './toDomNode';
export { toDomRange } from './toDomRange';
export { toggleMark } from './toggleMark';
export { unsetMark } from './unsetMark';
8 changes: 4 additions & 4 deletions packages/slate-commons/src/commands/insertEmptyParagraph.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ParagraphNode } from '@prezly/slate-types';
import { PARAGRAPH_NODE_TYPE } from '@prezly/slate-types';
import type { Editor, Location } from 'slate';
import { Transforms } from 'slate';
import type { SlateEditor } from '@udecode/plate-common';
import type { Location } from 'slate';

function createEmptyParagraph(): ParagraphNode {
return {
Expand All @@ -11,10 +11,10 @@ function createEmptyParagraph(): ParagraphNode {
}

export function insertEmptyParagraph(
editor: Editor,
editor: SlateEditor,
options: { at?: Location; select?: boolean } = {},
): void {
// Using `mode: 'highest' under assumption that "paragraph" can only be
// at the root of the document.
Transforms.insertNodes(editor, [createEmptyParagraph()], { ...options, mode: 'highest' });
editor.insertNodes([createEmptyParagraph()], { ...options, mode: 'highest' });
}
Loading
Loading