Skip to content

Commit 6d68c9e

Browse files
committed
Update the remaining code to use Plate API
1 parent 611b8cc commit 6d68c9e

File tree

263 files changed

+1876
-1813
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

263 files changed

+1876
-1813
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import type { AlignableNode, Alignment } from '@prezly/slate-types';
22
import { isAlignableElement, isTableCellNode } from '@prezly/slate-types';
3+
import type { SlateEditor } from '@udecode/plate-common';
34
import type { Node, NodeEntry, Path } from 'slate';
4-
import { Editor, Transforms } from 'slate';
55

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

@@ -17,16 +17,15 @@ export function getAlignment(editor: Editor, defaultAlignment: Alignment): Align
1717
return [...alignments];
1818
}
1919

20-
export function toggleAlignment(editor: Editor, align: Alignment | undefined): void {
20+
export function toggleAlignment(editor: SlateEditor, align: Alignment | undefined): void {
2121
if (align === undefined) {
22-
Transforms.unsetNodes(editor, 'align', {
22+
editor.unsetNodes('align', {
2323
match: (node, path) => isTopLevelAlignableElement(editor, node, path),
2424
});
2525
return;
2626
}
2727

28-
Transforms.setNodes<AlignableNode>(
29-
editor,
28+
editor.setNodes<AlignableNode>(
3029
{ align },
3130
{ match: (node, path) => isTopLevelAlignableElement(editor, node, path) },
3231
);
@@ -37,7 +36,7 @@ function isAlignmentRoot([node, path]: NodeEntry): boolean {
3736
return path.length === 0 || isTableCellNode(node);
3837
}
3938

40-
function isTopLevelAlignableElement(editor: Editor, node: Node, path: Path): node is AlignableNode {
41-
const parent = Editor.above(editor, { at: path });
39+
function isTopLevelAlignableElement(editor: SlateEditor, node: Node, path: Path): node is AlignableNode {
40+
const parent = editor.above({ at: path });
4241
return parent !== undefined && isAlignmentRoot(parent) && isAlignableElement(node);
4342
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
import type { SlateEditor } from '@udecode/plate-common';
12
import type { Path } from 'slate';
2-
import { Editor } from 'slate';
33

44
export function getNodePath(
5-
editor: Editor,
6-
options: NonNullable<Parameters<typeof Editor.nodes>[1]>,
5+
editor: SlateEditor,
6+
options: NonNullable<Parameters<typeof editor.nodes>[0]>,
77
): Path | null {
8-
const [entry] = Editor.nodes(editor, options);
8+
const [entry] = editor.nodes(options);
99
return entry ? entry[1] : null;
1010
}

packages/slate-commons/src/commands/isCursorOnEdgeOfContainer.ts

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
import { Editor } from 'slate';
1+
import type { SlateEditor } from '@udecode/plate-common';
2+
import { toDOMRange } from '@udecode/slate-react';
23
import type { Range, Point } from 'slate';
3-
import { ReactEditor } from 'slate-react';
44

55
export type ContainerEdge = 'top' | 'bottom';
66

7-
export function isCursorOnFirstLine(editor: ReactEditor, container: Point, cursor: Point): boolean {
7+
export function isCursorOnFirstLine(editor: SlateEditor, container: Point, cursor: Point): boolean {
88
return isCursorOnEdgeOfContainer(editor, container, cursor, 'top');
99
}
1010

11-
export function isCursorOnLastLine(editor: ReactEditor, container: Point, cursor: Point): boolean {
11+
export function isCursorOnLastLine(editor: SlateEditor, container: Point, cursor: Point): boolean {
1212
return isCursorOnEdgeOfContainer(editor, container, cursor, 'bottom');
1313
}
1414

1515
export function isCursorOnEdgeOfContainer(
16-
editor: ReactEditor,
16+
editor: SlateEditor,
1717
container: Point,
1818
cursor: Point,
1919
edge: ContainerEdge,
@@ -33,8 +33,8 @@ export function isCursorOnEdgeOfContainer(
3333
}
3434
}
3535

36-
function getPointRect(editor: ReactEditor, point: Point) {
37-
const range = Editor.range(editor, { ...point, offset: Math.max(point.offset, 0) });
36+
function getPointRect(editor: SlateEditor, point: Point) {
37+
const range = editor.range({ ...point, offset: Math.max(point.offset, 0) });
3838
try {
3939
return getRangeRect(editor, range);
4040
} catch {
@@ -45,8 +45,9 @@ function getPointRect(editor: ReactEditor, point: Point) {
4545
/**
4646
* @throws error when `ReactEditor.toDOMRange()` cannot match range to a DOM node
4747
*/
48-
function getRangeRect(editor: ReactEditor, range: Range) {
49-
const domRange = ReactEditor.toDOMRange(editor, range);
48+
function getRangeRect(editor: SlateEditor, range: Range) {
49+
const domRange = toDOMRange(editor, range);
50+
// @ts-expect-error TODO: Fix this
5051
const rects = domRange.getClientRects();
5152

5253
// if the cursor will be in the beginning of next line there will be two rects:
@@ -55,5 +56,6 @@ function getRangeRect(editor: ReactEditor, range: Range) {
5556
return Array.from(rects);
5657
}
5758

59+
// @ts-expect-error TODO: Fix this
5860
return [domRange.getBoundingClientRect()];
5961
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import type { SlateEditor } from '@udecode/plate-common';
12
import type { Text } from 'slate';
2-
import { Editor } from 'slate';
33

4-
export function isMarkActive<T extends Text>(editor: Editor, mark: keyof Omit<T, 'text'>): boolean {
5-
const marks = Editor.marks(editor) as Record<keyof T, boolean>;
4+
export function isMarkActive<T extends Text>(editor: SlateEditor, mark: keyof Omit<T, 'text'>): boolean {
5+
const marks = editor.getMarks() as Record<keyof T, boolean>;
66
return marks ? marks[mark] === true : false;
77
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { Editor, Path } from 'slate';
2-
import { Transforms } from 'slate';
1+
import type { SlateEditor } from '@udecode/plate-common';
2+
import type { Path } from 'slate';
33
import { v4 as uuidV4 } from 'uuid';
44

55
const DIRTY_PROPERTY_NAME = uuidV4();
@@ -9,7 +9,7 @@ const DIRTY_PROPERTY_NAME = uuidV4();
99
* It is useful when a normalization is applicable on a node multiple times.
1010
* Without it, Slate will not run subsequent normalization.
1111
*/
12-
export function makeDirty(editor: Editor, at: Path): void {
13-
Transforms.setNodes(editor, { [DIRTY_PROPERTY_NAME]: uuidV4() }, { at });
14-
Transforms.setNodes(editor, { [DIRTY_PROPERTY_NAME]: undefined }, { at });
12+
export function makeDirty(editor: SlateEditor, at: Path): void {
13+
editor.setNodes({ [DIRTY_PROPERTY_NAME]: uuidV4() }, { at });
14+
editor.setNodes({ [DIRTY_PROPERTY_NAME]: undefined }, { at });
1515
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1+
import type { SlateEditor } from '@udecode/plate-common';
12
import type { Location } from 'slate';
2-
import { Editor, Transforms } from 'slate';
33

44
export function moveCursorToNextBlock(
5-
editor: Editor,
5+
editor: SlateEditor,
66
location: Location | null = editor.selection,
77
): void {
88
if (location) {
9-
const next = Editor.after(editor, location, { unit: 'block' });
10-
const nextBlockPoint = next ?? Editor.end(editor, []);
11-
Transforms.select(editor, nextBlockPoint);
9+
const next = editor.after(location, { unit: 'block' });
10+
const nextBlockPoint = next ?? editor.end([]);
11+
editor.select(nextBlockPoint);
1212
}
1313
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1+
import type { SlateEditor } from '@udecode/plate-common';
12
import type { Location } from 'slate';
2-
import { Editor, Transforms } from 'slate';
33

44
export function moveCursorToPreviousBlock(
5-
editor: Editor,
5+
editor: SlateEditor,
66
location: Location | null = editor.selection,
77
): void {
88
if (location) {
9-
const before = Editor.before(editor, location, { unit: 'block' });
10-
const prevBlockPoint = before ?? Editor.start(editor, []);
11-
Transforms.select(editor, prevBlockPoint);
9+
const before = editor.before(location, { unit: 'block' });
10+
const prevBlockPoint = before ?? editor.start([]);
11+
editor.select(prevBlockPoint);
1212
}
1313
}

packages/slate-commons/src/commands/normalizeNestedElement.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import type { ElementNode } from '@prezly/slate-types';
22
import { isElementNode } from '@prezly/slate-types';
3+
import { isInline, isVoid, type SlateEditor } from '@udecode/plate-common';
34
import type { ElementEntry } from 'slate';
4-
import { Editor, Element, Transforms } from 'slate';
5+
import { Element } from 'slate';
56

67
import { makeDirty } from './makeDirty';
78

89
export function normalizeNestedElement(
9-
editor: Editor,
10+
editor: SlateEditor,
1011
[element, path]: ElementEntry,
1112
isParentAllowed: (element: Element) => boolean,
1213
): boolean {
13-
const ancestor = Editor.above(editor, { at: path });
14+
const ancestor = editor.above({ at: path });
1415
if (!ancestor) {
1516
return false;
1617
}
@@ -28,17 +29,17 @@ export function normalizeNestedElement(
2829
makeDirty(editor, path);
2930

3031
if (
31-
Editor.isInline(editor, element) ||
32-
Editor.isVoid(editor, element) ||
32+
isInline(editor, element) ||
33+
isVoid(editor, element) ||
3334
isElementNode(ancestorNode, (element as ElementNode).type)
3435
) {
3536
if (ancestorNode.children.length === 1) {
36-
Transforms.unwrapNodes(editor, { at: ancestorPath, voids: true });
37+
editor.unwrapNodes({ at: ancestorPath, voids: true });
3738
} else {
38-
Transforms.liftNodes(editor, { at: path, voids: true });
39+
editor.liftNodes({ at: path, voids: true });
3940
}
4041
} else {
41-
Transforms.unwrapNodes(editor, { at: path });
42+
editor.unwrapNodes({ at: path });
4243
}
4344

4445
return true;

packages/slate-commons/src/commands/normalizeRedundantAttributes.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import type { Editor, ElementEntry } from 'slate';
2-
import { Transforms } from 'slate';
1+
import type { SlateEditor } from '@udecode/plate-common';
2+
import type { ElementEntry } from 'slate';
33

44
import { NODE_ID_MANAGER_ID_PROPERTY_NAME } from '../constants';
55

66
const GLOBALLY_ALLOWED_ATTRIBUTES = [NODE_ID_MANAGER_ID_PROPERTY_NAME];
77

88
export function normalizeRedundantAttributes(
9-
editor: Editor,
9+
editor: SlateEditor,
1010
[element, path]: ElementEntry,
1111
allowedAttributes: string[],
1212
): boolean {
@@ -27,7 +27,7 @@ export function normalizeRedundantAttributes(
2727
{},
2828
);
2929

30-
Transforms.setNodes(editor, attributesToUnset, { at: path });
30+
editor.setNodes(attributesToUnset, { at: path });
3131

3232
return true;
3333
}

packages/slate-commons/src/commands/removeChildren.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
import type { SlateEditor } from '@udecode/plate-common';
12
import type { NodeEntry } from 'slate';
2-
import { Editor, Element, Text, Transforms } from 'slate';
3+
import { Element, Text } from 'slate';
34

45
/**
56
* Ensures given element has a single, empty `Text` child.
67
* Returns `true` when removal occurred.
78
* Returns `false` when nothing changed.
89
*/
9-
export function removeChildren(editor: Editor, [node, path]: NodeEntry): boolean {
10+
export function removeChildren(editor: SlateEditor, [node, path]: NodeEntry): boolean {
1011
if (!Element.isElement(node)) {
1112
return false;
1213
}
@@ -17,11 +18,11 @@ export function removeChildren(editor: Editor, [node, path]: NodeEntry): boolean
1718
return false;
1819
}
1920

20-
Editor.withoutNormalizing(editor, () => {
21-
Transforms.insertNodes(editor, [{ text: '' }], { at: [...path, 0] });
21+
editor.withoutNormalizing(() => {
22+
editor.insertNodes([{ text: '' }], { at: [...path, 0] });
2223

2324
node.children.forEach(() => {
24-
Transforms.removeNodes(editor, {
25+
editor.removeNodes({
2526
at: [...path, 1],
2627
voids: true,
2728
});

packages/slate-commons/src/commands/removeNode.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1+
import type { SlateEditor } from '@udecode/plate-common';
12
import type { Node } from 'slate';
2-
import { Editor, Transforms } from 'slate';
33

44
export function removeNode<N extends Node>(
5-
editor: Editor,
6-
options: NonNullable<Parameters<typeof Editor.nodes>[1]>,
5+
editor: SlateEditor,
6+
options: NonNullable<Parameters<typeof editor.nodes>[0]>,
77
): N | null {
8-
const [nodeEntry] = Editor.nodes<N>(editor, options);
8+
const [nodeEntry] = editor.nodes<N>(options);
99
if (nodeEntry) {
1010
const [node, nodePath] = nodeEntry;
11-
Transforms.removeNodes(editor, { at: nodePath });
11+
editor.removeNodes({ at: nodePath });
1212
return node;
1313
}
1414
return null;

packages/slate-commons/src/commands/toDomRange.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1+
import type { SlateEditor } from '@udecode/plate-common';
2+
import { toDOMRange } from '@udecode/slate-react';
13
import type { Range } from 'slate';
2-
import { ReactEditor } from 'slate-react';
34

45
/**
56
* Type-safe wrapper for `ReactEditor.toDOMRange`
67
*/
7-
export function toDomRange(editor: ReactEditor, range: Range): globalThis.Range | null {
8+
export function toDomRange(editor: SlateEditor, range: Range): globalThis.Range | null {
89
try {
910
// "Slate throws exceptions too liberally in relation to selection failures"
1011
// see: https://app.clubhouse.io/prezly/story/20456/error-cannot-resolve-a-dom-node-from-slate-node-text
1112
// see: https://github.com/ianstormtaylor/slate/issues/3641
12-
return ReactEditor.toDOMRange(editor, range);
13+
return toDOMRange(editor, range) ?? null;
1314
} catch {
1415
return null;
1516
}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1+
import type { SlateEditor } from '@udecode/plate-common';
12
import type { Text } from 'slate';
2-
import { Editor } from 'slate';
33

44
import { isMarkActive } from './isMarkActive';
55

66
export function toggleMark<T extends Text>(
7-
editor: Editor,
7+
editor: SlateEditor,
88
mark: keyof Omit<T, 'text'>,
99
force?: boolean,
1010
): void {
1111
const shouldSet = force ?? !isMarkActive(editor, mark);
1212

1313
if (shouldSet) {
14-
Editor.addMark(editor, mark as string, true);
14+
editor.addMark(mark as string, true);
1515
} else {
16-
Editor.removeMark(editor, mark as string);
16+
editor.removeMark(mark as string);
1717
}
1818
}

packages/slate-commons/src/commands/unsetMark.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
import { Transforms } from 'slate';
1+
import type { SlateEditor } from '@udecode/plate-common';
22
import type { Text, NodeEntry } from 'slate';
3-
import type { Editor } from 'slate';
43

54
export function unsetMark<T extends Text>(
6-
editor: Editor,
5+
editor: SlateEditor,
76
entry: NodeEntry<T>,
87
mark: keyof Omit<T, 'text'>,
98
) {
109
const [node, path] = entry;
1110

1211
if (node[mark]) {
13-
Transforms.unsetNodes<T>(editor, mark.toString(), {
12+
editor.unsetNodes(mark.toString(), {
1413
at: path,
1514
match: (n) => n === node,
1615
});

packages/slate-commons/src/plugins/withNormalization.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
/* eslint-disable no-param-reassign */
2-
import type { Editor } from 'slate';
1+
import type { SlateEditor } from '@udecode/plate-common';
32

43
import type { Extension } from '../types';
54

65
export function withNormalization(getExtensions: () => Extension[]) {
7-
return function <T extends Editor>(editor: T) {
6+
return function <T extends SlateEditor>(editor: T) {
87
const { normalizeNode } = editor;
98

109
editor.normalizeNode = (entry) => {
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
import type { Editor, NodeEntry } from 'slate';
1+
import type { SlateEditor } from '@udecode/plate-common';
2+
import type { NodeEntry } from 'slate';
23

3-
export type Normalize = (editor: Editor, entry: NodeEntry) => boolean;
4+
export type Normalize = (editor: SlateEditor, entry: NodeEntry) => boolean;
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import type { Editor } from 'slate';
1+
import type { SlateEditor } from '@udecode/plate-common';
22

3-
export type WithOverrides = <T extends Editor>(editor: T) => T;
3+
export type WithOverrides = <T extends SlateEditor>(editor: T) => T;

packages/slate-editor/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969
"@technically/is-not-undefined": "^1.0.0",
7070
"@technically/lodash": "^4.17.0",
7171
"@types/combine-reducers": "^1.0.1",
72-
"@udecode/plate-core": "^9.0.0",
7372
"classnames": "^2.3.1",
7473
"combine-reducers": "^1.0.0",
7574
"emoji-picker-react": "^4.10.0",

0 commit comments

Comments
 (0)