Skip to content

Commit

Permalink
Update Plate types and APIs in slate-tables and slate-types
Browse files Browse the repository at this point in the history
  • Loading branch information
kudlajz committed Jan 15, 2025
1 parent b480364 commit f4d160e
Show file tree
Hide file tree
Showing 16 changed files with 76 additions and 75 deletions.
14 changes: 10 additions & 4 deletions packages/slate-tables/src/TablesEditor.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import type { SlateEditor, TElement, TText } from '@udecode/plate-common';
import type { Location, Node } from 'slate';
import {
NodeApi,
type Element,
type Location,
type Node,
type SlateEditor,
type Text,
} from '@udecode/plate';

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

export interface TablesSchema {
createContentNode: () => TElement | TText;
createContentNode: () => Element | Text;
createTableNode: (props: Partial<TableNode>) => TableNode;
createTableRowNode: (props: Partial<TableRowNode>) => TableRowNode;
createTableCellNode: (props: Partial<TableCellNode>) => TableCellNode;
Expand Down Expand Up @@ -51,6 +57,6 @@ export namespace TablesEditor {
}

export function isTablesEditor(editor: SlateEditor): editor is TablesEditor {
return typeof editor === 'object' && editor !== null && 'isTableCellNode' in editor;
return NodeApi.isEditor(editor) && 'isTableCellNode' in editor;
}
}
16 changes: 7 additions & 9 deletions packages/slate-tables/src/commands/insertColumn.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { focusEditor } from '@udecode/plate-common/react';
import { type Location, Path } from 'slate';
import { type Location, type Path, PathApi } from '@udecode/plate';

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

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

if (index === 0) {
firstCellInNewColumnPath = at;
}

editor.insertNodes(TableCellNode.createTableCell(editor), { at });
editor.tf.insertNodes(TableCellNode.createTableCell(editor), { at });
});
});

editor.normalize();

focusEditor(editor);
editor.tf.normalize();
editor.tf.focus();

if (firstCellInNewColumnPath) {
editor.select(firstCellInNewColumnPath);
editor.tf.select(firstCellInNewColumnPath);
}

return true;
Expand Down
17 changes: 10 additions & 7 deletions packages/slate-tables/src/commands/insertRow.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { focusEditor } from '@udecode/plate-common/react';
import { type Location, Path, Node } from 'slate';
import { type Location, NodeApi, PathApi } from '@udecode/plate';

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

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

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

focusEditor(editor);
const firstNode = NodeApi.first(editor, at);
if (!firstNode) {
return false;
}

const [, firstCellInNewRowPath] = Node.first(editor, at);
editor.select(firstCellInNewRowPath);
const [, firstCellInNewRowPath] = firstNode;
editor.tf.select(firstCellInNewRowPath);

return true;
}
7 changes: 3 additions & 4 deletions packages/slate-tables/src/commands/insertTable.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { focusEditor } from '@udecode/plate-common/react';
import { type Location } from 'slate';
import { type Location } from '@udecode/plate';

import { TableNode } from '../nodes';
import type { TablesEditor } from '../TablesEditor';
Expand All @@ -13,11 +12,11 @@ export function insertTable(
return false;
}

editor.insertNodes(TableNode.createTable(editor, props), {
editor.tf.insertNodes(TableNode.createTable(editor, props), {
at: location,
});

focusEditor(editor);
editor.tf.focus();

return true;
}
15 changes: 7 additions & 8 deletions packages/slate-tables/src/commands/removeColumn.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { focusEditor } from '@udecode/plate-common/react';
import { type Location } from 'slate';
import { type Location } from '@udecode/plate';

import { Traverse } from '../core';
import { TableCellNode } from '../nodes';
Expand Down Expand Up @@ -27,7 +26,7 @@ export function removeColumn(
}

// As we remove cells one by one Slate calls normalization which insert empty cells
editor.withoutNormalizing(() => {
editor.tf.withoutNormalizing(() => {
activeColumn.cells.forEach((cell) => {
if (TableCellNode.getCellColspan(cell.node) > 1) {
TableCellNode.update(
Expand All @@ -36,12 +35,12 @@ export function removeColumn(
cell.path,
);
} else {
editor.removeNodes({ at: cell.path });
editor.tf.removeNodes({ at: cell.path });
}
});
});

editor.normalize();
editor.tf.normalize();

let anchorFocusColumn = activeColumn;

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

if (firstCell) {
editor.select(firstCell.path);
editor.collapse({ edge: 'start' });
editor.tf.select(firstCell.path);
editor.tf.collapse({ edge: 'start' });
}

focusEditor(editor);
editor.tf.focus();

return true;
}
11 changes: 5 additions & 6 deletions packages/slate-tables/src/commands/removeRow.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { focusEditor } from '@udecode/plate-common/react';
import { type Location } from 'slate';
import { type Location } from '@udecode/plate';

import { Traverse } from '../core';
import { TableCellNode } from '../nodes';
Expand Down Expand Up @@ -35,18 +34,18 @@ export function removeRow(
}
});

editor.removeNodes({ at: activeRow.path });
editor.tf.removeNodes({ at: activeRow.path });

const anchorFocusRow = activeRow.rowBelow ?? activeRow;

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

if (firstCell) {
editor.select(firstCell.path);
editor.collapse({ edge: 'start' });
editor.tf.select(firstCell.path);
editor.tf.collapse({ edge: 'start' });
}

focusEditor(editor);
editor.tf.focus();

return true;
}
7 changes: 3 additions & 4 deletions packages/slate-tables/src/commands/removeTable.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { focusEditor } from '@udecode/plate-common/react';
import { type Location } from 'slate';
import { type Location } from '@udecode/plate';

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

const { matrix } = traverse;

focusEditor(editor);
editor.tf.focus();

editor.removeNodes({ at: matrix.path });
editor.tf.removeNodes({ at: matrix.path });

return true;
}
14 changes: 8 additions & 6 deletions packages/slate-tables/src/core/withTablesCopyPasteBehavior.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { getNodeFragment } from '@udecode/plate-common';
import { type Range, Path } from 'slate';
import { NodeApi, type Path, PathApi, type Range } from '@udecode/plate';

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

return getNodeFragment(cell, {
return NodeApi.fragment(cell, {
anchor: {
offset: anchor.offset,
path: Path.relative(anchor.path, cellPath),
path: PathApi.relative(anchor.path, cellPath),
},
focus: {
offset: focus.offset,
path: Path.relative(focus.path, cellPath),
path: PathApi.relative(focus.path, cellPath),
},
});
}
Expand All @@ -35,5 +34,8 @@ export function withTablesCopyPasteBehavior<T extends TablesEditor>(editor: T):
}

function isRangeInside(selection: Range, path: Path) {
return Path.isCommon(path, selection.anchor.path) && Path.isCommon(path, selection.focus.path);
return (
PathApi.isCommon(path, selection.anchor.path) &&
PathApi.isCommon(path, selection.focus.path)
);
}
7 changes: 3 additions & 4 deletions packages/slate-tables/src/nodes/TableCellNode.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type { TElement } from '@udecode/plate-common';
import type { Location } from 'slate';
import { type Element, type Location } from '@udecode/plate';

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

export interface TableCellNode extends TElement {
export interface TableCellNode extends Element {
rowspan?: number;
colspan?: number;
}
Expand Down Expand Up @@ -66,7 +65,7 @@ export namespace TableCellNode {
props: Partial<Omit<TableCellNode, 'children'>>,
location: Location,
) {
editor.setNodes<TableCellNode>(props, {
editor.tf.setNodes<TableCellNode>(props, {
at: location,
match: (node) => editor.isTableCellNode(node),
});
Expand Down
8 changes: 4 additions & 4 deletions packages/slate-tables/src/nodes/TableNode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Element, Location } from 'slate';
import { type Element, type Location } from '@udecode/plate';

import { Traverse } from '../core';
import type { TablesEditor } from '../TablesEditor';
Expand Down Expand Up @@ -36,7 +36,7 @@ export namespace TableNode {
props: Partial<Omit<TableNode, 'children'>>,
location: Location | undefined = editor.selection ?? undefined,
) {
editor.setNodes<TableNode>(props, {
editor.tf.setNodes<TableNode>(props, {
at: location,
match: (node) => editor.isTableNode(node),
});
Expand All @@ -62,7 +62,7 @@ export namespace TableNode {
? traverse.matrix.node.header?.filter((h) => h !== headerType)
: [...(traverse.matrix.node.header ?? []), headerType];

editor.setNodes<TableNode>(
editor.tf.setNodes<TableNode>(
{ header: newHeader },
{
at: location,
Expand All @@ -72,7 +72,7 @@ export namespace TableNode {

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

return newHeader;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/slate-tables/src/nodes/TableRowNode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Element, Location } from 'slate';
import { type Element, type Location } from '@udecode/plate';

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

Expand Down Expand Up @@ -28,7 +28,7 @@ export namespace TableRowNode {
props: Partial<Omit<TableRowNode, 'children'>>,
location: Location,
) {
editor.setNodes<TableRowNode>(props, {
editor.tf.setNodes<TableRowNode>(props, {
at: location,
match: (node) => editor.isTableRowNode(node),
});
Expand Down
11 changes: 5 additions & 6 deletions packages/slate-tables/src/queries/isHeaderCell.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { getLevels, isEditor as isEditorObject } from '@udecode/plate-common';
import { Location } from 'slate';
import { type Location, LocationApi, NodeApi } from '@udecode/plate';

import type { TableCellNode, TableNode } from '../nodes';
import { TablesEditor } from '../TablesEditor';
Expand Down Expand Up @@ -29,16 +28,16 @@ function findTableAndCellNodes(
let tableNode: TableNode | undefined;
let cellNode: TableCellNode | undefined;

const isEditor = isEditorObject(editorOrTable) && TablesEditor.isTablesEditor(editorOrTable);
const isLocation = Location.isLocation(locationOrCell);
const isEditor = NodeApi.isEditor(editorOrTable) && TablesEditor.isTablesEditor(editorOrTable);
const isLocation = LocationApi.isLocation(locationOrCell);

if (isEditor || isLocation || locationOrCell === null) {
if (isEditor) {
const editor = editorOrTable;
const location = locationOrCell ?? editor.selection;

if (Location.isLocation(location)) {
for (const [currentNodeToCheck] of getLevels(editor, { at: location })) {
if (LocationApi.isLocation(location)) {
for (const [currentNodeToCheck] of editor.api.levels({ at: location })) {
if (editor.isTableNode(currentNodeToCheck)) {
tableNode = currentNodeToCheck;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/slate-tables/src/withTables.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { SlateEditor } from '@udecode/plate-common';
import type { SlateEditor } from '@udecode/plate';

import type { TablesEditor, TablesSchema } from './TablesEditor';
import { withNormalization } from './withNormalization';
Expand Down
4 changes: 2 additions & 2 deletions packages/slate-types/src/nodes/ElementNode.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { TElement } from '@udecode/plate-common';
import type { Element } from '@udecode/plate';

import { isObject } from './validation';

export interface ElementNode extends TElement {
export interface ElementNode extends Element {
type: string;
}

Expand Down
9 changes: 4 additions & 5 deletions packages/slate-types/src/nodes/TableNode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { TNode } from '@udecode/plate-common';
import type { Node } from 'slate';
import { type Node } from '@udecode/plate';

import { type ElementNode, isElementNode } from './ElementNode';

Expand Down Expand Up @@ -27,14 +26,14 @@ export interface TableCellNode extends ElementNode {
colspan?: number;
}

export function isTableNode(value: Node | TNode): value is TableNode {
export function isTableNode(value: Node): value is TableNode {
return isElementNode<TableNode>(value, TABLE_NODE_TYPE);
}

export function isTableCellNode(value: Node | TNode): value is TableCellNode {
export function isTableCellNode(value: Node): value is TableCellNode {
return isElementNode<TableCellNode>(value, TABLE_CELL_NODE_TYPE);
}

export function isTableRowNode(value: Node | TNode): value is TableRowNode {
export function isTableRowNode(value: Node): value is TableRowNode {
return isElementNode<TableRowNode>(value, TABLE_ROW_NODE_TYPE);
}
Loading

0 comments on commit f4d160e

Please sign in to comment.