Skip to content

Commit

Permalink
Remove 'slate' imports from the main codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
kudlajz committed Jan 16, 2025
1 parent f4d160e commit 124bc5c
Show file tree
Hide file tree
Showing 52 changed files with 147 additions and 162 deletions.
5 changes: 2 additions & 3 deletions packages/slate-commons/src/commands/findDescendants.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import type { Descendant, NodeEntry, Path } from 'slate';
import { Node } from 'slate';
import { type Descendant, type Node, NodeApi, type NodeEntry, type Path } from '@udecode/plate';

export function findDescendants<N extends Node>(
node: N,
match: (descendant: Descendant, path: Path) => boolean,
): NodeEntry[] {
const descendants = Array.from(Node.descendants(node));
const descendants = Array.from(NodeApi.descendants(node));
return descendants.filter(([element, path]) => match(element, path));
}
11 changes: 7 additions & 4 deletions packages/slate-commons/src/commands/isBlockActive.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import type { ElementNode } from '@prezly/slate-types';
import { isElementNode } from '@prezly/slate-types';
import type { Location } from 'slate';
import { Editor } from 'slate';
import { type Location, type SlateEditor } from '@udecode/plate';

export function isBlockActive(editor: Editor, type: ElementNode['type'], at?: Location): boolean {
export function isBlockActive(
editor: SlateEditor,
type: ElementNode['type'],
at?: Location,
): boolean {
const [match] = Array.from(
Editor.nodes(editor, {
editor.api.nodes({
match: (node) => isElementNode(node, type),
at,
}),
Expand Down
2 changes: 1 addition & 1 deletion packages/slate-commons/src/commands/isTopLevelNode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Node, Path } from 'slate';
import { type Node, type Path } from '@udecode/plate';

export function isTopLevelNode(_node: Node, path: Path): boolean {
return path.length === 1;
Expand Down
5 changes: 2 additions & 3 deletions packages/slate-commons/src/commands/isTopLevelNodeSelected.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Node } from 'slate';
import { Editor } from 'slate';
import { type Editor, type Node } from '@udecode/plate';

import { isTopLevelNode } from './isTopLevelNode';

Expand All @@ -11,7 +10,7 @@ export function isTopLevelNodeSelected(
const selection = editor.selection;
if (!selection) return false;

const selectedNodes = Array.from(Editor.nodes(editor, { match: isTopLevelNode }));
const selectedNodes = Array.from(editor.api.nodes({ match: isTopLevelNode }));

for (const [selectedNode] of selectedNodes) {
if (selectedNode === node) {
Expand Down
4 changes: 2 additions & 2 deletions packages/slate-commons/src/commands/unsetMark.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { NodeEntry, SlateEditor, Text } from '@udecode/plate';
import { type Editor, type NodeEntry, type Text } from '@udecode/plate';

export function unsetMark<T extends Text>(
editor: SlateEditor,
editor: Editor,
entry: NodeEntry<T>,
mark: keyof Omit<T, 'text'>,
) {
Expand Down
2 changes: 1 addition & 1 deletion packages/slate-commons/src/lib/createDeserializeElement.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Node } from 'slate';
import { type Node } from '@udecode/plate';

type Parse<E extends Node> = (serialized: string) => E | undefined;

Expand Down
5 changes: 2 additions & 3 deletions packages/slate-commons/src/lib/withoutNodes.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import type { Node } from 'slate';
import { Text } from 'slate';
import { type Node, TextApi } from '@udecode/plate';

export function withoutNodes<T extends Node>(nodes: T[], match: (node: Node) => boolean): T[] {
return nodes
.map((node: T): T | null => {
if (match(node)) {
return null;
}
if (Text.isText(node)) {
if (TextApi.isText(node)) {
return node;
}
return { ...node, children: withoutNodes(node.children, match) };
Expand Down
14 changes: 10 additions & 4 deletions packages/slate-commons/src/selection/highest.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import type { Location, Range } from 'slate';
import { Path, Point } from 'slate';
import {
type Location,
type Path,
PathApi,
type Point,
PointApi,
type Range,
} from '@udecode/plate';

export function highest(selection: Path): Path;
export function highest(selection: Point): Path;
export function highest(selection: Range): Path;
export function highest(selection: Location): Path {
if (Path.isPath(selection)) {
if (PathApi.isPath(selection)) {
return selection.slice(0, 1) as Path;
}
if (Point.isPoint(selection)) {
if (PointApi.isPoint(selection)) {
return highest(selection.path);
}
return highest(selection.focus);
Expand Down
2 changes: 1 addition & 1 deletion packages/slate-commons/src/types/Extension.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Node } from 'slate';
import { type Node } from '@udecode/plate';

import type { DecorateFactory } from './DecorateFactory';
import type { DeserializeHtml } from './DeserializeHtml';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Node, Path } from 'slate';
import { type Node, type Path } from '@udecode/plate';

export interface AllowedBlocksExtensionConfiguration {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
isSubtitleHeadingNode,
isTitleHeadingNode,
} from '@prezly/slate-types';
import { type Node } from '@udecode/plate';
import React from 'react';
import type { Node } from 'slate';

import { onBackspaceResetFormattingAtDocumentStart, withResetFormattingOnBreak } from '#lib';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Node } from 'slate';
import { type Node } from '@udecode/plate';
import validator from 'validator';

import { matchUrls } from '#lib';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Extension } from '@prezly/slate-commons';
import { createDeserializeElement } from '@prezly/slate-commons';
import type { Node } from 'slate';
import { Element } from 'slate';
import { ElementApi, type Node } from '@udecode/plate';

import { composeElementDeserializer } from '#modules/html-deserialization';

Expand All @@ -23,7 +22,7 @@ export function MentionsExtension<T extends string>({
type,
}: Options<T>): Extension {
function isMention(node: Node) {
return Element.isElementType(node, type);
return ElementApi.isElementType(node, type);
}

return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import type { Editor } from 'slate';
import { Transforms } from 'slate';
import { type Editor } from '@udecode/plate';

import { PlaceholderNode } from '../PlaceholderNode';

export function removePlaceholder(editor: Editor, placeholder: PlaceholderNode): void {
Transforms.removeNodes(editor, {
editor.tf.removeNodes({
match: (node) =>
PlaceholderNode.isPlaceholderNode(node) &&
node.type === placeholder.type &&
Expand Down
2 changes: 1 addition & 1 deletion packages/slate-editor/src/extensions/placeholders/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { NewsroomRef, OEmbedInfo } from '@prezly/sdk';
import type { Path } from 'slate';
import { type Path } from '@udecode/plate';

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ import {
isTableCellNode,
} from '@prezly/slate-types';
import { flow } from '@technically/lodash';
import { type Element, type RenderElementProps } from '@udecode/plate';
import React from 'react';
import type { Element } from 'slate';
import type { RenderElementProps } from 'slate-react';

import { composeElementDeserializer } from '#modules/html-deserialization';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { TablesEditor } from '@prezly/slate-tables';
import type { TableCellNode } from '@prezly/slate-types';
import { type RenderElementProps } from '@udecode/plate';
import classNames from 'classnames';
import React from 'react';
import type { RenderElementProps } from 'slate-react';

import styles from './elements.module.scss';
import { TableContext } from './TableContext';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { TableNode } from '@prezly/slate-types';
import { type RenderElementProps } from '@udecode/plate';
import classNames from 'classnames';
import React from 'react';
import type { RenderElementProps } from 'slate-react';

import { EditorBlock } from '#components';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { TableRowNode } from '@prezly/slate-types';
import { type RenderElementProps } from '@udecode/plate';
import React from 'react';
import type { RenderElementProps } from 'slate-react';

import styles from './elements.module.scss';

Expand Down
18 changes: 9 additions & 9 deletions packages/slate-editor/src/extensions/tables/onKeyDown.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TablesEditor } from '@prezly/slate-tables';
import { type Element, RangeApi } from '@udecode/plate';
import { isHotkey } from 'is-hotkey';
import type { KeyboardEvent } from 'react';
import { type Element, Range } from 'slate';

const isClipboardCopy = isHotkey(['mod+c', 'ctrl+insert']);
const isClipboardCut = isHotkey(['mod+x', 'shift+delete']);
Expand All @@ -13,7 +13,7 @@ export function onClipboardHotkey(
) {
const selection = editor.selection;

if (!selection || Range.isExpanded(selection)) return;
if (!selection || RangeApi.isExpanded(selection)) return;

if (isClipboardCopy(event)) {
const entry = TablesEditor.findParentTable(editor, selection);
Expand All @@ -22,7 +22,7 @@ export function onClipboardHotkey(
}
const [, path] = entry;

editor.select(path); // Select the table
editor.tf.select(path); // Select the table

document.execCommand('copy');

Expand All @@ -36,16 +36,16 @@ export function onClipboardHotkey(
}
const [, path] = entry;

editor.withoutNormalizing(() => {
editor.select(path); // Select the table
const ref = editor.pathRef(path);
editor.tf.withoutNormalizing(() => {
editor.tf.select(path); // Select the table
const ref = editor.api.pathRef(path);

if (document.execCommand('cut')) {
editor.insertNodes(createDefaultElement(), { at: path });
editor.tf.insertNodes(createDefaultElement(), { at: path });
if (ref.current) {
editor.removeNodes({ at: ref.current });
editor.tf.removeNodes({ at: ref.current });
}
editor.select(path);
editor.tf.select(path);
}

ref.unref();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type Range } from '@udecode/plate';
import React from 'react';
import type { Range } from 'slate';

import { Avatar } from '#components';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type Range } from '@udecode/plate';
import React from 'react';
import type { Range } from 'slate';

import type { Option } from '#extensions/mentions';
import { MentionsDropdown } from '#extensions/mentions';
Expand Down
2 changes: 1 addition & 1 deletion packages/slate-editor/src/lib/encodeSlateFragment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Node } from 'slate';
import { type Node } from '@udecode/plate';

export function encodeSlateFragment(fragment: Node[]): string {
return window.btoa(encodeURIComponent(JSON.stringify(fragment)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { EditorCommands, type Extension } from '@prezly/slate-commons';
import { TablesEditor } from '@prezly/slate-tables';
import type { Alignment } from '@prezly/slate-types';
import { CalloutNode, isImageNode, isQuoteNode } from '@prezly/slate-types';
import { Node } from 'slate';
import { NodeApi } from '@udecode/plate';

import { AllowedBlocksExtension } from '#extensions/allowed-blocks';
import { BlockquoteExtension } from '#extensions/blockquote';
Expand Down Expand Up @@ -312,7 +312,7 @@ export function* getEnabledExtensions(parameters: Parameters): Generator<Extensi
},
onCropped(editor, image) {
EventsEditor.dispatchEvent(editor, 'image-edited', {
description: Node.string(image),
description: NodeApi.string(image),
mimeType: image.file.mime_type,
size: image.file.size,
uuid: image.file.uuid,
Expand All @@ -328,7 +328,7 @@ export function* getEnabledExtensions(parameters: Parameters): Generator<Extensi
},
onReplaced(editor, image) {
EventsEditor.dispatchEvent(editor, 'image-edited', {
description: Node.string(image),
description: NodeApi.string(image),
mimeType: image.file.mime_type,
size: image.file.size,
uuid: image.file.uuid,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Extension } from '@prezly/slate-commons';
import type { Descendant } from 'slate';
import { type Descendant } from '@udecode/plate';

import { isGoogleDocsWrapper } from '#lib';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { DeserializeElement } from '@prezly/slate-commons';
import type { Descendant, Element } from 'slate';
import { type Descendant, type Element } from '@udecode/plate';

import type { HTMLElement } from './dom';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { DeserializeMarks } from '@prezly/slate-commons';
import type { Descendant } from 'slate';
import { Element, Text } from 'slate';
import { type Descendant, ElementApi, TextApi } from '@udecode/plate';

export type MarksDeserializer = (node: HTMLElement, children: Descendant[]) => Descendant[];

Expand All @@ -20,12 +19,12 @@ export function createMarksDeserializer(deserialize: DeserializeMarks): MarksDes
* Recursively apply the given style marks to all descendant Text nodes.
*/
function applyStylingProps<T extends Descendant>(node: T, styles: Record<string, any>): T {
if (Text.isText(node)) {
if (TextApi.isText(node)) {
const { text, ...props } = node;
return { ...props, ...styles, text } as T;
}

if (Element.isElement(node)) {
if (ElementApi.isElement(node)) {
const { children, ...props } = node;

return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Text } from 'slate';
import { type Text } from '@udecode/plate';

import type { HTMLText } from './dom';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Extension } from '@prezly/slate-commons';
import type { Descendant } from 'slate';
import { type Descendant } from '@udecode/plate';

import { createDeserializer } from '../createDeserialize';
import {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Transforms } from 'slate';
import type { NodeEntry } from 'slate';
import type { Editor } from 'slate';
import { type Editor, type NodeEntry } from '@udecode/plate';

export function insertTextNode(editor: Editor, [node, path]: NodeEntry) {
Transforms.insertNodes(editor, [{ text: '' }], { at: path, match: (n) => n === node });
editor.tf.insertNodes([{ text: '' }], { at: path, match: (n) => n === node });
return true;
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import { Editor, Element, Path, Transforms } from 'slate';
import type { NodeEntry } from 'slate';
import { type Editor, ElementApi, type NodeEntry, PathApi } from '@udecode/plate';

export function removeNode(editor: Editor, [, path]: NodeEntry) {
const targetPath = Path.parent(path);
const ancestor = Editor.above(editor, { at: targetPath });
const targetPath = PathApi.parent(path);
const ancestor = editor.api.above({ at: targetPath });

if (!ancestor) {
return false;
}

const [ancestorNode] = ancestor;

if (!Element.isElement(ancestorNode)) {
if (!ElementApi.isElement(ancestorNode)) {
return false;
}

Transforms.removeNodes(editor, { at: targetPath, voids: true });
editor.tf.removeNodes({ at: targetPath, voids: true });

return true;
}
Loading

0 comments on commit 124bc5c

Please sign in to comment.