Skip to content

Commit 42ff650

Browse files
committed
Update Plate types and APIs in slate-commons
1 parent bd6c7f3 commit 42ff650

Some content is hidden

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

60 files changed

+226
-274
lines changed

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

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import type { AlignableNode, Alignment } from '@prezly/slate-types';
22
import { isAlignableElement, isTableCellNode } from '@prezly/slate-types';
3-
import { getAboveNode, type SlateEditor, type TNodeEntry } from '@udecode/plate-common';
4-
import type { Node, Path } from 'slate';
3+
import { type Node, type Path, type SlateEditor, type NodeEntry } from '@udecode/plate';
54

65
export function getAlignment(editor: SlateEditor, defaultAlignment: Alignment): Alignment[] {
7-
const nodes = editor.nodes<AlignableNode>({
6+
const nodes = editor.api.nodes<AlignableNode>({
87
match: (node, path) => isTopLevelAlignableElement(editor, node, path),
98
});
109

@@ -19,19 +18,19 @@ export function getAlignment(editor: SlateEditor, defaultAlignment: Alignment):
1918

2019
export function toggleAlignment(editor: SlateEditor, align: Alignment | undefined): void {
2120
if (align === undefined) {
22-
editor.unsetNodes('align', {
21+
editor.tf.unsetNodes('align', {
2322
match: (node, path) => isTopLevelAlignableElement(editor, node, path),
2423
});
2524
return;
2625
}
2726

28-
editor.setNodes<AlignableNode>(
27+
editor.tf.setNodes<AlignableNode>(
2928
{ align },
3029
{ match: (node, path) => isTopLevelAlignableElement(editor, node, path) },
3130
);
3231
}
3332

34-
function isAlignmentRoot([node, path]: TNodeEntry): boolean {
33+
function isAlignmentRoot([node, path]: NodeEntry): boolean {
3534
// We allow aligning elements either at top-level or inside table cells.
3635
return path.length === 0 || isTableCellNode(node);
3736
}
@@ -41,6 +40,6 @@ function isTopLevelAlignableElement(
4140
node: Node,
4241
path: Path,
4342
): node is AlignableNode {
44-
const parent = getAboveNode(editor, { at: path });
43+
const parent = editor.api.above({ at: path });
4544
return parent !== undefined && isAlignmentRoot(parent) && isAlignableElement(node);
4645
}
+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import type { SlateEditor } from '@udecode/plate-common';
2-
import { deselectEditor } from '@udecode/plate-common/react';
1+
import type { SlateEditor } from '@udecode/plate';
32

43
export function blur(editor: SlateEditor): void {
5-
deselectEditor(editor);
4+
editor.tf.deselectDOM();
65
}

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
import type { SlateEditor } from '@udecode/plate-common';
2-
import type { Location } from 'slate';
3-
import { Path, Point } from 'slate';
1+
import { PathApi, PointApi, type Location, type SlateEditor } from '@udecode/plate';
42

53
import { findLeafPath } from './findLeafPath';
64
import { findLeafPoint } from './findLeafPoint';
75
import { findLeafRange } from './findLeafRange';
86

97
export function findLeafLocation(editor: SlateEditor, location: Location): Location | undefined {
10-
if (Path.isPath(location)) {
8+
if (PathApi.isPath(location)) {
119
return findLeafPath(editor, location);
1210
}
1311

14-
if (Point.isPoint(location)) {
12+
if (PointApi.isPoint(location)) {
1513
return findLeafPoint(editor, location);
1614
}
1715

Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import type { SlateEditor } from '@udecode/plate-common';
2-
import type { Path } from 'slate';
3-
import { Node, Text } from 'slate';
1+
import { NodeApi, type Path, type SlateEditor, TextApi } from '@udecode/plate';
42

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

@@ -9,17 +7,21 @@ export function findLeafPath(
97
path: Path,
108
edge: Edge = 'highest',
119
): Path | undefined {
12-
if (!Node.has(editor, path)) {
10+
if (!NodeApi.has(editor, path)) {
1311
return undefined;
1412
}
1513

16-
const node = Node.get(editor, path);
14+
const node = NodeApi.get(editor, path);
1715

18-
if (Text.isText(node)) {
16+
if (TextApi.isText(node)) {
1917
return path;
2018
}
2119

22-
const [, nestedPath] = edge === 'highest' ? editor.first(path) : editor.last(path);
20+
const result = edge === 'highest' ? editor.api.first(path) : editor.api.last(path);
21+
if (!result) {
22+
return undefined;
23+
}
2324

25+
const [, nestedPath] = result;
2426
return findLeafPath(editor, nestedPath);
2527
}

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

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import type { SlateEditor } from '@udecode/plate-common';
2-
import { Path, Point } from 'slate';
1+
import { PathApi, type Point, PointApi, type SlateEditor } from '@udecode/plate';
32

43
import type { Edge } from './findLeafPath';
54
import { findLeafPath } from './findLeafPath';
@@ -15,10 +14,14 @@ export function findLeafPoint(
1514
return undefined;
1615
}
1716

18-
const [, end] = editor.edges(path);
17+
const edges = editor.api.edges(path);
18+
if (!edges) {
19+
return undefined;
20+
}
1921

20-
if (Path.equals(point.path, path)) {
21-
if (Point.isAfter(point, end)) {
22+
const [, end] = edges;
23+
if (PathApi.equals(point.path, path)) {
24+
if (PointApi.isAfter(point, end)) {
2225
return end;
2326
}
2427

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import type { SlateEditor } from '@udecode/plate-common';
2-
import type { Range } from 'slate';
1+
import type { Range, SlateEditor } from '@udecode/plate';
32

43
import { findLeafPoint } from './findLeafPoint';
54

Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import type { SlateEditor } from '@udecode/plate-common';
2-
import { focusEditor } from '@udecode/plate-common/react';
1+
import type { SlateEditor } from '@udecode/plate';
32

43
import { moveCursorToEndOfDocument } from './moveCursorToEndOfDocument';
54

65
export function focus(editor: SlateEditor): void {
7-
focusEditor(editor);
6+
editor.tf.focus();
87
moveCursorToEndOfDocument(editor);
98
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import type { SlateEditor } from '@udecode/plate-common';
2-
import { toDOMNode } from '@udecode/plate-common/react';
1+
import type { SlateEditor } from '@udecode/plate';
32

43
import { getCurrentNodeEntry } from './getCurrentNodeEntry';
54

@@ -10,5 +9,5 @@ export function getCurrentDomNode(editor: SlateEditor): HTMLElement | null {
109
return null;
1110
}
1211

13-
return toDOMNode(editor, currentNode) ?? null;
12+
return editor.api.toDOMNode(currentNode) ?? null;
1413
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import type { TNodeEntry } from '@udecode/plate-common';
2-
import { getNodeEntry, type SlateEditor } from '@udecode/plate-common';
1+
import { type NodeEntry, type SlateEditor } from '@udecode/plate';
32

43
import { isSelectionValid } from './isSelectionValid';
54

6-
export function getCurrentNodeEntry(editor: SlateEditor): TNodeEntry | null {
5+
export function getCurrentNodeEntry(editor: SlateEditor): NodeEntry | null {
76
if (!editor.selection || !isSelectionValid(editor)) {
87
return null;
98
}
109

11-
return getNodeEntry(editor, editor.selection, { depth: 1 }) ?? null;
10+
return editor.api.node(editor.selection, { depth: 1 }) ?? null;
1211
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import type { SlateEditor } from '@udecode/plate-common';
2-
import type { Range } from 'slate';
1+
import type { Range, SlateEditor } from '@udecode/plate';
32

43
export function getEditorRange(editor: SlateEditor): Range | undefined {
54
// editor.children can sometimes be undefined, even though TypeScript says otherwise
@@ -8,7 +7,7 @@ export function getEditorRange(editor: SlateEditor): Range | undefined {
87
}
98

109
return {
11-
anchor: editor.start([0]),
12-
focus: editor.end([editor.children.length - 1]),
10+
anchor: editor.api.start([0]),
11+
focus: editor.api.end([editor.children.length - 1]),
1312
};
1413
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import type { SlateEditor } from '@udecode/plate-common';
2-
import type { Path } from 'slate';
1+
import type { Path, SlateEditor } from '@udecode/plate';
32

43
export function getNodePath(
54
editor: SlateEditor,
6-
options: NonNullable<Parameters<typeof editor.nodes>[0]>,
5+
options: NonNullable<Parameters<typeof editor.api.nodes>[0]>,
76
): Path | null {
8-
const [entry] = editor.nodes(options);
7+
const [entry] = editor.api.nodes(options);
98
return entry ? entry[1] : null;
109
}

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

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import type { SlateEditor } from '@udecode/plate-common';
2-
import { Path, Text } from 'slate';
1+
import { PathApi, TextApi, type SlateEditor } from '@udecode/plate';
32

43
import { isVoid } from './isVoid';
54

@@ -11,30 +10,30 @@ export function getPrevChars(editor: SlateEditor, length: number): string {
1110
}
1211

1312
const { focus } = selection;
14-
let text = editor.string({ focus, anchor: { path: focus.path, offset: 0 } });
13+
let text = editor.api.string({ focus, anchor: { path: focus.path, offset: 0 } });
1514

1615
if (text.length > length) {
1716
return text.slice(-length);
1817
}
1918

20-
const start = { path: [...Path.parent(focus.path), 0], offset: 0 };
19+
const start = { path: [...PathApi.parent(focus.path), 0], offset: 0 };
2120

22-
const nodes = editor.nodes({
21+
const nodes = editor.api.nodes({
2322
mode: 'lowest',
2423
at: { anchor: start, focus },
2524
reverse: true,
2625
});
2726

2827
for (const [node, path] of nodes) {
29-
if (Path.equals(path, focus.path)) {
28+
if (PathApi.equals(path, focus.path)) {
3029
continue;
3130
}
3231

3332
if (isVoid(editor, node)) {
3433
break;
3534
}
3635

37-
if (Text.isText(node)) {
36+
if (TextApi.isText(node)) {
3837
text = `${node.text}${text}`;
3938
}
4039

packages/slate-commons/src/commands/hasVoidElements.tsx

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import type { SlateEditor } from '@udecode/plate-common';
2-
import type { Node } from 'slate';
3-
import { Text } from 'slate';
1+
import { TextApi, type Node, type SlateEditor } from '@udecode/plate';
42

53
import { isVoid } from './isVoid';
64

75
export function hasVoidElements(editor: SlateEditor, node: Node): boolean {
8-
if (Text.isText(node)) {
6+
if (TextApi.isText(node)) {
97
return false;
108
}
119
if (isVoid(editor, node)) {

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { ParagraphNode } from '@prezly/slate-types';
22
import { PARAGRAPH_NODE_TYPE } from '@prezly/slate-types';
3-
import type { SlateEditor } from '@udecode/plate-common';
4-
import type { Location } from 'slate';
3+
import type { Location, SlateEditor } from '@udecode/plate';
54

65
function createEmptyParagraph(): ParagraphNode {
76
return {
@@ -16,5 +15,5 @@ export function insertEmptyParagraph(
1615
): void {
1716
// Using `mode: 'highest' under assumption that "paragraph" can only be
1817
// at the root of the document.
19-
editor.insertNodes([createEmptyParagraph()], { ...options, mode: 'highest' });
18+
editor.tf.insertNodes([createEmptyParagraph()], { ...options, mode: 'highest' });
2019
}

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

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
/* eslint-disable no-param-reassign */
22

3-
import type { SlateEditor } from '@udecode/plate-common';
4-
import { isElement, isInline } from '@udecode/plate-common';
5-
import type { Node } from 'slate';
6-
import { Text } from 'slate';
3+
import { ElementApi, TextApi } from '@udecode/plate';
4+
import type { Node, SlateEditor } from '@udecode/plate';
75

86
import { getCurrentNodeEntry } from './getCurrentNodeEntry';
97
import { insertEmptyParagraph } from './insertEmptyParagraph';
@@ -40,7 +38,7 @@ function insertNormalizedNodes(editor: SlateEditor, nodes: Node[], options: Opti
4038
// In case we're inserting things into an empty paragraph, we will want to replace that paragraph.
4139
const initialSelection = editor.selection;
4240
const isInitialSelectionAtEmptyBlock = isAtEmptyBlock(editor);
43-
const isAppendingToCurrentNode = Text.isText(nodes[0]) || isInline(editor, nodes[0]);
41+
const isAppendingToCurrentNode = TextApi.isText(nodes[0]) || editor.api.isInline(nodes[0]);
4442
const isInsertingBlockNodes = nodes.some((node) => isBlock(editor, node));
4543

4644
for (const node of nodes) {
@@ -53,7 +51,7 @@ function insertNormalizedNodes(editor: SlateEditor, nodes: Node[], options: Opti
5351
insertEmptyParagraph(editor);
5452
}
5553

56-
if (isElement(node) && isInline(editor, node)) {
54+
if (ElementApi.isElement(node) && editor.api.isInline(node)) {
5755
// Slate does not allow inline nodes next to inline nodes.
5856
// Adding text nodes around it helps to prevent unwanted side-effects.
5957
//
@@ -63,9 +61,9 @@ function insertNormalizedNodes(editor: SlateEditor, nodes: Node[], options: Opti
6361
// > nor can it be next to another inline node in the children array.
6462
// > If this is the case, an empty text node will be added to correct
6563
// > this to be in compliance with the constraint.
66-
editor.insertFragment([{ text: '' }, node, { text: '' }]);
64+
editor.tf.insertFragment([{ text: '' }, node, { text: '' }]);
6765
} else {
68-
editor.insertNodes([node], { mode });
66+
editor.tf.insertNodes([node], { mode });
6967
}
7068
}
7169
}
@@ -85,11 +83,11 @@ function insertNormalizedNodes(editor: SlateEditor, nodes: Node[], options: Opti
8583
// For example, if originally selected element was preceeded by a "list",
8684
// the selection would move to the last "list-item-text" in that "list", and
8785
// `element` would get inserted as a child of that "list-item-text".
88-
editor.removeNodes({ at: initialSelection });
86+
editor.tf.removeNodes({ at: initialSelection });
8987
}
9088

9189
// Some normalizing operations may not trigger follow-up normalizations, so we want
9290
// to force one more loop of normalizations. This happens e.g. when fixing hierarchy
9391
// when pasting lists.
94-
editor.normalize({ force: true });
92+
editor.tf.normalize({ force: true });
9593
}

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

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import type { SlateEditor } from '@udecode/plate-common';
2-
import { Element } from 'slate';
3-
import { Range } from 'slate';
1+
import { ElementApi, type Range, RangeApi, type SlateEditor } from '@udecode/plate';
42

53
import { isNodeEmpty } from './isNodeEmpty';
64

@@ -20,17 +18,17 @@ export function isAtEmptyBlock(
2018
return false;
2119
}
2220

23-
if (Range.isExpanded(at)) {
21+
if (RangeApi.isExpanded(at)) {
2422
return false;
2523
}
2624

27-
const entry = editor.node(at, { depth: 1 });
25+
const entry = editor.api.node(at, { depth: 1 });
2826
if (!entry) {
2927
return false;
3028
}
3129

3230
const [node] = entry;
3331
return (
34-
Element.isElement(node) && editor.isBlock(node) && isNodeEmpty(editor, node, options?.trim)
32+
ElementApi.isElement(node) && editor.api.isBlock(node) && isNodeEmpty(editor, node, options?.trim)
3533
);
3634
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import type { TNode } from '@udecode/plate-common';
2-
import { isElement, type SlateEditor } from '@udecode/plate-common';
3-
import type { Node } from 'slate';
1+
import type { Node, SlateEditor } from '@udecode/plate';
2+
import { ElementApi } from '@udecode/plate';
43

5-
export function isBlock(editor: SlateEditor, node: Node | TNode): boolean {
6-
return isElement(node) && editor.isBlock(node);
4+
export function isBlock(editor: SlateEditor, node: Node): boolean {
5+
return ElementApi.isElement(node) && editor.api.isBlock(node);
76
}

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import type { SlateEditor } from '@udecode/plate-common';
2-
import { Range } from 'slate';
1+
import { RangeApi, type SlateEditor } from '@udecode/plate';
32

43
import { getCurrentNodeEntry } from './getCurrentNodeEntry';
54
import { isEmptyParagraphElement } from './isEmptyParagraphElement';
@@ -13,7 +12,7 @@ export function isCursorInEmptyParagraph(editor: SlateEditor, options?: Options)
1312
return false;
1413
}
1514

16-
if (Range.isExpanded(editor.selection)) {
15+
if (RangeApi.isExpanded(editor.selection)) {
1716
return false;
1817
}
1918

0 commit comments

Comments
 (0)