Skip to content

Commit

Permalink
Update Plate types and APIs in slate-commons
Browse files Browse the repository at this point in the history
  • Loading branch information
kudlajz committed Jan 15, 2025
1 parent bd6c7f3 commit 42ff650
Show file tree
Hide file tree
Showing 60 changed files with 226 additions and 274 deletions.
13 changes: 6 additions & 7 deletions packages/slate-commons/src/commands/alignment.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import type { AlignableNode, Alignment } from '@prezly/slate-types';
import { isAlignableElement, isTableCellNode } from '@prezly/slate-types';
import { getAboveNode, type SlateEditor, type TNodeEntry } from '@udecode/plate-common';
import type { Node, Path } from 'slate';
import { type Node, type Path, type SlateEditor, type NodeEntry } from '@udecode/plate';

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

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

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

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

function isAlignmentRoot([node, path]: TNodeEntry): boolean {
function isAlignmentRoot([node, path]: NodeEntry): boolean {
// We allow aligning elements either at top-level or inside table cells.
return path.length === 0 || isTableCellNode(node);
}
Expand All @@ -41,6 +40,6 @@ function isTopLevelAlignableElement(
node: Node,
path: Path,
): node is AlignableNode {
const parent = getAboveNode(editor, { at: path });
const parent = editor.api.above({ at: path });
return parent !== undefined && isAlignmentRoot(parent) && isAlignableElement(node);
}
5 changes: 2 additions & 3 deletions packages/slate-commons/src/commands/blur.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { SlateEditor } from '@udecode/plate-common';
import { deselectEditor } from '@udecode/plate-common/react';
import type { SlateEditor } from '@udecode/plate';

export function blur(editor: SlateEditor): void {
deselectEditor(editor);
editor.tf.deselectDOM();
}
8 changes: 3 additions & 5 deletions packages/slate-commons/src/commands/findLeafLocation.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import type { SlateEditor } from '@udecode/plate-common';
import type { Location } from 'slate';
import { Path, Point } from 'slate';
import { PathApi, PointApi, type Location, type SlateEditor } from '@udecode/plate';

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

export function findLeafLocation(editor: SlateEditor, location: Location): Location | undefined {
if (Path.isPath(location)) {
if (PathApi.isPath(location)) {
return findLeafPath(editor, location);
}

if (Point.isPoint(location)) {
if (PointApi.isPoint(location)) {
return findLeafPoint(editor, location);
}

Expand Down
16 changes: 9 additions & 7 deletions packages/slate-commons/src/commands/findLeafPath.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import type { SlateEditor } from '@udecode/plate-common';
import type { Path } from 'slate';
import { Node, Text } from 'slate';
import { NodeApi, type Path, type SlateEditor, TextApi } from '@udecode/plate';

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

Expand All @@ -9,17 +7,21 @@ export function findLeafPath(
path: Path,
edge: Edge = 'highest',
): Path | undefined {
if (!Node.has(editor, path)) {
if (!NodeApi.has(editor, path)) {
return undefined;
}

const node = Node.get(editor, path);
const node = NodeApi.get(editor, path);

if (Text.isText(node)) {
if (TextApi.isText(node)) {
return path;
}

const [, nestedPath] = edge === 'highest' ? editor.first(path) : editor.last(path);
const result = edge === 'highest' ? editor.api.first(path) : editor.api.last(path);
if (!result) {
return undefined;
}

const [, nestedPath] = result;
return findLeafPath(editor, nestedPath);
}
13 changes: 8 additions & 5 deletions packages/slate-commons/src/commands/findLeafPoint.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { SlateEditor } from '@udecode/plate-common';
import { Path, Point } from 'slate';
import { PathApi, type Point, PointApi, type SlateEditor } from '@udecode/plate';

import type { Edge } from './findLeafPath';
import { findLeafPath } from './findLeafPath';
Expand All @@ -15,10 +14,14 @@ export function findLeafPoint(
return undefined;
}

const [, end] = editor.edges(path);
const edges = editor.api.edges(path);
if (!edges) {
return undefined;
}

if (Path.equals(point.path, path)) {
if (Point.isAfter(point, end)) {
const [, end] = edges;
if (PathApi.equals(point.path, path)) {
if (PointApi.isAfter(point, end)) {
return end;
}

Expand Down
3 changes: 1 addition & 2 deletions packages/slate-commons/src/commands/findLeafRange.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { SlateEditor } from '@udecode/plate-common';
import type { Range } from 'slate';
import type { Range, SlateEditor } from '@udecode/plate';

import { findLeafPoint } from './findLeafPoint';

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

import { moveCursorToEndOfDocument } from './moveCursorToEndOfDocument';

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

import { getCurrentNodeEntry } from './getCurrentNodeEntry';

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

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

import { isSelectionValid } from './isSelectionValid';

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

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

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

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

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

import { isVoid } from './isVoid';

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

const { focus } = selection;
let text = editor.string({ focus, anchor: { path: focus.path, offset: 0 } });
let text = editor.api.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 start = { path: [...PathApi.parent(focus.path), 0], offset: 0 };

const nodes = editor.nodes({
const nodes = editor.api.nodes({
mode: 'lowest',
at: { anchor: start, focus },
reverse: true,
});

for (const [node, path] of nodes) {
if (Path.equals(path, focus.path)) {
if (PathApi.equals(path, focus.path)) {
continue;
}

if (isVoid(editor, node)) {
break;
}

if (Text.isText(node)) {
if (TextApi.isText(node)) {
text = `${node.text}${text}`;
}

Expand Down
6 changes: 2 additions & 4 deletions packages/slate-commons/src/commands/hasVoidElements.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import type { SlateEditor } from '@udecode/plate-common';
import type { Node } from 'slate';
import { Text } from 'slate';
import { TextApi, type Node, type SlateEditor } from '@udecode/plate';

import { isVoid } from './isVoid';

export function hasVoidElements(editor: SlateEditor, node: Node): boolean {
if (Text.isText(node)) {
if (TextApi.isText(node)) {
return false;
}
if (isVoid(editor, node)) {
Expand Down
5 changes: 2 additions & 3 deletions packages/slate-commons/src/commands/insertEmptyParagraph.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { ParagraphNode } from '@prezly/slate-types';
import { PARAGRAPH_NODE_TYPE } from '@prezly/slate-types';
import type { SlateEditor } from '@udecode/plate-common';
import type { Location } from 'slate';
import type { Location, SlateEditor } from '@udecode/plate';

function createEmptyParagraph(): ParagraphNode {
return {
Expand All @@ -16,5 +15,5 @@ export function insertEmptyParagraph(
): void {
// Using `mode: 'highest' under assumption that "paragraph" can only be
// at the root of the document.
editor.insertNodes([createEmptyParagraph()], { ...options, mode: 'highest' });
editor.tf.insertNodes([createEmptyParagraph()], { ...options, mode: 'highest' });
}
18 changes: 8 additions & 10 deletions packages/slate-commons/src/commands/insertNodes.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/* eslint-disable no-param-reassign */

import type { SlateEditor } from '@udecode/plate-common';
import { isElement, isInline } from '@udecode/plate-common';
import type { Node } from 'slate';
import { Text } from 'slate';
import { ElementApi, TextApi } from '@udecode/plate';
import type { Node, SlateEditor } from '@udecode/plate';

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

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

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

// Some normalizing operations may not trigger follow-up normalizations, so we want
// to force one more loop of normalizations. This happens e.g. when fixing hierarchy
// when pasting lists.
editor.normalize({ force: true });
editor.tf.normalize({ force: true });
}
10 changes: 4 additions & 6 deletions packages/slate-commons/src/commands/isAtEmptyBlock.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import type { SlateEditor } from '@udecode/plate-common';
import { Element } from 'slate';
import { Range } from 'slate';
import { ElementApi, type Range, RangeApi, type SlateEditor } from '@udecode/plate';

import { isNodeEmpty } from './isNodeEmpty';

Expand All @@ -20,17 +18,17 @@ export function isAtEmptyBlock(
return false;
}

if (Range.isExpanded(at)) {
if (RangeApi.isExpanded(at)) {
return false;
}

const entry = editor.node(at, { depth: 1 });
const entry = editor.api.node(at, { depth: 1 });
if (!entry) {
return false;
}

const [node] = entry;
return (
Element.isElement(node) && editor.isBlock(node) && isNodeEmpty(editor, node, options?.trim)
ElementApi.isElement(node) && editor.api.isBlock(node) && isNodeEmpty(editor, node, options?.trim)
);
}
9 changes: 4 additions & 5 deletions packages/slate-commons/src/commands/isBlock.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { TNode } from '@udecode/plate-common';
import { isElement, type SlateEditor } from '@udecode/plate-common';
import type { Node } from 'slate';
import type { Node, SlateEditor } from '@udecode/plate';
import { ElementApi } from '@udecode/plate';

export function isBlock(editor: SlateEditor, node: Node | TNode): boolean {
return isElement(node) && editor.isBlock(node);
export function isBlock(editor: SlateEditor, node: Node): boolean {
return ElementApi.isElement(node) && editor.api.isBlock(node);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { SlateEditor } from '@udecode/plate-common';
import { Range } from 'slate';
import { RangeApi, type SlateEditor } from '@udecode/plate';

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

if (Range.isExpanded(editor.selection)) {
if (RangeApi.isExpanded(editor.selection)) {
return false;
}

Expand Down
Loading

0 comments on commit 42ff650

Please sign in to comment.