Skip to content

Commit f4d160e

Browse files
committed
Update Plate types and APIs in slate-tables and slate-types
1 parent b480364 commit f4d160e

File tree

16 files changed

+76
-75
lines changed

16 files changed

+76
-75
lines changed

packages/slate-tables/src/TablesEditor.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
import type { SlateEditor, TElement, TText } from '@udecode/plate-common';
2-
import type { Location, Node } from 'slate';
1+
import {
2+
NodeApi,
3+
type Element,
4+
type Location,
5+
type Node,
6+
type SlateEditor,
7+
type Text,
8+
} from '@udecode/plate';
39

410
import * as TableCommands from './commands';
511
import { TableCellNode, TableRowNode, TableNode } from './nodes';
612
import * as TableQueries from './queries';
713

814
export interface TablesSchema {
9-
createContentNode: () => TElement | TText;
15+
createContentNode: () => Element | Text;
1016
createTableNode: (props: Partial<TableNode>) => TableNode;
1117
createTableRowNode: (props: Partial<TableRowNode>) => TableRowNode;
1218
createTableCellNode: (props: Partial<TableCellNode>) => TableCellNode;
@@ -51,6 +57,6 @@ export namespace TablesEditor {
5157
}
5258

5359
export function isTablesEditor(editor: SlateEditor): editor is TablesEditor {
54-
return typeof editor === 'object' && editor !== null && 'isTableCellNode' in editor;
60+
return NodeApi.isEditor(editor) && 'isTableCellNode' in editor;
5561
}
5662
}

packages/slate-tables/src/commands/insertColumn.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { focusEditor } from '@udecode/plate-common/react';
2-
import { type Location, Path } from 'slate';
1+
import { type Location, type Path, PathApi } from '@udecode/plate';
32

43
import { Traverse } from '../core';
54
import { TableCellNode } from '../nodes';
@@ -25,24 +24,23 @@ export function insertColumn(
2524
let firstCellInNewColumnPath: Path | undefined = undefined;
2625

2726
// As we insert cells one by one Slate calls normalization which insert empty cells
28-
editor.withoutNormalizing(() => {
27+
editor.tf.withoutNormalizing(() => {
2928
activeColumn.cells.forEach((columnCell, index) => {
30-
const at = side === 'left' ? columnCell.path : Path.next(columnCell.path);
29+
const at = side === 'left' ? columnCell.path : PathApi.next(columnCell.path);
3130

3231
if (index === 0) {
3332
firstCellInNewColumnPath = at;
3433
}
3534

36-
editor.insertNodes(TableCellNode.createTableCell(editor), { at });
35+
editor.tf.insertNodes(TableCellNode.createTableCell(editor), { at });
3736
});
3837
});
3938

40-
editor.normalize();
41-
42-
focusEditor(editor);
39+
editor.tf.normalize();
40+
editor.tf.focus();
4341

4442
if (firstCellInNewColumnPath) {
45-
editor.select(firstCellInNewColumnPath);
43+
editor.tf.select(firstCellInNewColumnPath);
4644
}
4745

4846
return true;

packages/slate-tables/src/commands/insertRow.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { focusEditor } from '@udecode/plate-common/react';
2-
import { type Location, Path, Node } from 'slate';
1+
import { type Location, NodeApi, PathApi } from '@udecode/plate';
32

43
import { Traverse } from '../core';
54
import { TableRowNode, TableCellNode } from '../nodes';
@@ -33,13 +32,17 @@ export function insertRow(
3332

3433
const newRow = TableRowNode.createTableRow(editor, { children: cellsToAdd });
3534

36-
const at = side === 'bellow' ? Path.next(activeRow.path) : activeRow.path;
37-
editor.insertNodes(newRow, { at });
35+
const at = side === 'bellow' ? PathApi.next(activeRow.path) : activeRow.path;
36+
editor.tf.insertNodes(newRow, { at });
37+
editor.tf.focus();
3838

39-
focusEditor(editor);
39+
const firstNode = NodeApi.first(editor, at);
40+
if (!firstNode) {
41+
return false;
42+
}
4043

41-
const [, firstCellInNewRowPath] = Node.first(editor, at);
42-
editor.select(firstCellInNewRowPath);
44+
const [, firstCellInNewRowPath] = firstNode;
45+
editor.tf.select(firstCellInNewRowPath);
4346

4447
return true;
4548
}

packages/slate-tables/src/commands/insertTable.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { focusEditor } from '@udecode/plate-common/react';
2-
import { type Location } from 'slate';
1+
import { type Location } from '@udecode/plate';
32

43
import { TableNode } from '../nodes';
54
import type { TablesEditor } from '../TablesEditor';
@@ -13,11 +12,11 @@ export function insertTable(
1312
return false;
1413
}
1514

16-
editor.insertNodes(TableNode.createTable(editor, props), {
15+
editor.tf.insertNodes(TableNode.createTable(editor, props), {
1716
at: location,
1817
});
1918

20-
focusEditor(editor);
19+
editor.tf.focus();
2120

2221
return true;
2322
}

packages/slate-tables/src/commands/removeColumn.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { focusEditor } from '@udecode/plate-common/react';
2-
import { type Location } from 'slate';
1+
import { type Location } from '@udecode/plate';
32

43
import { Traverse } from '../core';
54
import { TableCellNode } from '../nodes';
@@ -27,7 +26,7 @@ export function removeColumn(
2726
}
2827

2928
// As we remove cells one by one Slate calls normalization which insert empty cells
30-
editor.withoutNormalizing(() => {
29+
editor.tf.withoutNormalizing(() => {
3130
activeColumn.cells.forEach((cell) => {
3231
if (TableCellNode.getCellColspan(cell.node) > 1) {
3332
TableCellNode.update(
@@ -36,12 +35,12 @@ export function removeColumn(
3635
cell.path,
3736
);
3837
} else {
39-
editor.removeNodes({ at: cell.path });
38+
editor.tf.removeNodes({ at: cell.path });
4039
}
4140
});
4241
});
4342

44-
editor.normalize();
43+
editor.tf.normalize();
4544

4645
let anchorFocusColumn = activeColumn;
4746

@@ -52,11 +51,11 @@ export function removeColumn(
5251
const firstCell = anchorFocusColumn.cells.at(0);
5352

5453
if (firstCell) {
55-
editor.select(firstCell.path);
56-
editor.collapse({ edge: 'start' });
54+
editor.tf.select(firstCell.path);
55+
editor.tf.collapse({ edge: 'start' });
5756
}
5857

59-
focusEditor(editor);
58+
editor.tf.focus();
6059

6160
return true;
6261
}

packages/slate-tables/src/commands/removeRow.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { focusEditor } from '@udecode/plate-common/react';
2-
import { type Location } from 'slate';
1+
import { type Location } from '@udecode/plate';
32

43
import { Traverse } from '../core';
54
import { TableCellNode } from '../nodes';
@@ -35,18 +34,18 @@ export function removeRow(
3534
}
3635
});
3736

38-
editor.removeNodes({ at: activeRow.path });
37+
editor.tf.removeNodes({ at: activeRow.path });
3938

4039
const anchorFocusRow = activeRow.rowBelow ?? activeRow;
4140

4241
const firstCell = anchorFocusRow.rowAbove.cells.at(0);
4342

4443
if (firstCell) {
45-
editor.select(firstCell.path);
46-
editor.collapse({ edge: 'start' });
44+
editor.tf.select(firstCell.path);
45+
editor.tf.collapse({ edge: 'start' });
4746
}
4847

49-
focusEditor(editor);
48+
editor.tf.focus();
5049

5150
return true;
5251
}

packages/slate-tables/src/commands/removeTable.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { focusEditor } from '@udecode/plate-common/react';
2-
import { type Location } from 'slate';
1+
import { type Location } from '@udecode/plate';
32

43
import { Traverse } from '../core';
54
import type { TablesEditor } from '../TablesEditor';
@@ -20,9 +19,9 @@ export function removeTable(
2019

2120
const { matrix } = traverse;
2221

23-
focusEditor(editor);
22+
editor.tf.focus();
2423

25-
editor.removeNodes({ at: matrix.path });
24+
editor.tf.removeNodes({ at: matrix.path });
2625

2726
return true;
2827
}

packages/slate-tables/src/core/withTablesCopyPasteBehavior.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { getNodeFragment } from '@udecode/plate-common';
2-
import { type Range, Path } from 'slate';
1+
import { NodeApi, type Path, PathApi, type Range } from '@udecode/plate';
32

43
import { findParentCell } from '../queries';
54
import type { TablesEditor } from '../TablesEditor';
@@ -15,14 +14,14 @@ export function withTablesCopyPasteBehavior<T extends TablesEditor>(editor: T):
1514
const [cell, cellPath] = cellEntry;
1615
const { focus, anchor } = editor.selection;
1716

18-
return getNodeFragment(cell, {
17+
return NodeApi.fragment(cell, {
1918
anchor: {
2019
offset: anchor.offset,
21-
path: Path.relative(anchor.path, cellPath),
20+
path: PathApi.relative(anchor.path, cellPath),
2221
},
2322
focus: {
2423
offset: focus.offset,
25-
path: Path.relative(focus.path, cellPath),
24+
path: PathApi.relative(focus.path, cellPath),
2625
},
2726
});
2827
}
@@ -35,5 +34,8 @@ export function withTablesCopyPasteBehavior<T extends TablesEditor>(editor: T):
3534
}
3635

3736
function isRangeInside(selection: Range, path: Path) {
38-
return Path.isCommon(path, selection.anchor.path) && Path.isCommon(path, selection.focus.path);
37+
return (
38+
PathApi.isCommon(path, selection.anchor.path) &&
39+
PathApi.isCommon(path, selection.focus.path)
40+
);
3941
}

packages/slate-tables/src/nodes/TableCellNode.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import type { TElement } from '@udecode/plate-common';
2-
import type { Location } from 'slate';
1+
import { type Element, type Location } from '@udecode/plate';
32

43
import type { TablesEditor } from '../TablesEditor';
54

6-
export interface TableCellNode extends TElement {
5+
export interface TableCellNode extends Element {
76
rowspan?: number;
87
colspan?: number;
98
}
@@ -66,7 +65,7 @@ export namespace TableCellNode {
6665
props: Partial<Omit<TableCellNode, 'children'>>,
6766
location: Location,
6867
) {
69-
editor.setNodes<TableCellNode>(props, {
68+
editor.tf.setNodes<TableCellNode>(props, {
7069
at: location,
7170
match: (node) => editor.isTableCellNode(node),
7271
});

packages/slate-tables/src/nodes/TableNode.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Element, Location } from 'slate';
1+
import { type Element, type Location } from '@udecode/plate';
22

33
import { Traverse } from '../core';
44
import type { TablesEditor } from '../TablesEditor';
@@ -36,7 +36,7 @@ export namespace TableNode {
3636
props: Partial<Omit<TableNode, 'children'>>,
3737
location: Location | undefined = editor.selection ?? undefined,
3838
) {
39-
editor.setNodes<TableNode>(props, {
39+
editor.tf.setNodes<TableNode>(props, {
4040
at: location,
4141
match: (node) => editor.isTableNode(node),
4242
});
@@ -62,7 +62,7 @@ export namespace TableNode {
6262
? traverse.matrix.node.header?.filter((h) => h !== headerType)
6363
: [...(traverse.matrix.node.header ?? []), headerType];
6464

65-
editor.setNodes<TableNode>(
65+
editor.tf.setNodes<TableNode>(
6666
{ header: newHeader },
6767
{
6868
at: location,
@@ -72,7 +72,7 @@ export namespace TableNode {
7272

7373
// When we mark text in cell as bold and then mark the first row as header the normalization is not called
7474
// and bold mark still present in cell content
75-
editor.normalize({ force: true });
75+
editor.tf.normalize({ force: true });
7676

7777
return newHeader;
7878
}

0 commit comments

Comments
 (0)