Skip to content

Commit

Permalink
Get rid of TS error suppressions
Browse files Browse the repository at this point in the history
  • Loading branch information
kudlajz committed Nov 21, 2024
1 parent b2d7993 commit 272cce5
Show file tree
Hide file tree
Showing 46 changed files with 140 additions and 171 deletions.
8 changes: 4 additions & 4 deletions packages/slate-commons/src/commands/alignment.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { AlignableNode, Alignment } from '@prezly/slate-types';
import { isAlignableElement, isTableCellNode } from '@prezly/slate-types';
import type { SlateEditor } from '@udecode/plate-common';
import type { Node, NodeEntry, Path } from 'slate';
import { getAboveNode, type SlateEditor, type TNodeEntry } from '@udecode/plate-common';
import type { Node, Path } from 'slate';

export function getAlignment(editor: SlateEditor, defaultAlignment: Alignment): Alignment[] {
const nodes = editor.nodes<AlignableNode>({
Expand Down Expand Up @@ -31,7 +31,7 @@ export function toggleAlignment(editor: SlateEditor, align: Alignment | undefine
);
}

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

import { isSelectionValid } from './isSelectionValid';

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

return editor.node(editor.selection, { depth: 1 });
return getNodeEntry(editor, editor.selection, { depth: 1}) ?? null;
}
1 change: 0 additions & 1 deletion packages/slate-commons/src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export {
} from './isCursorOnEdgeOfContainer';
export { isEmpty } from './isEmpty';
export { isEmptyParagraphElement } from './isEmptyParagraphElement';
export { isInline } from './isInline';
export { isMarkActive } from './isMarkActive';
export { isNodeEmpty } from './isNodeEmpty';
export { isSelectionAtBlockEnd } from './isSelectionAtBlockEnd';
Expand Down
8 changes: 3 additions & 5 deletions packages/slate-commons/src/commands/insertNodes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +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';

Expand All @@ -9,7 +10,6 @@ import { insertEmptyParagraph } from './insertEmptyParagraph';
import { isAtEmptyBlock } from './isAtEmptyBlock';
import { isBlock } from './isBlock';
import { isCursorInEmptyParagraph } from './isCursorInEmptyParagraph';
import { isInline } from './isInline';
import { isVoid } from './isVoid';
import { roughlyNormalizeNodes } from './roughly-normalize';

Expand Down Expand Up @@ -40,8 +40,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);
// @ts-expect-error TODO: Fix this
const isAppendingToCurrentNode = Text.isText(nodes[0]) || editor.isInline(nodes[0]);
const isAppendingToCurrentNode = Text.isText(nodes[0]) || isInline(editor, nodes[0]);
const isInsertingBlockNodes = nodes.some((node) => isBlock(editor, node));

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

if (isInline(editor, node)) {
if (isElement(node) && isInline(editor, node)) {
// Slate does not allow inline nodes next to inline nodes.
// Adding text nodes around it helps to prevent unwanted side-effects.
//
Expand All @@ -64,7 +63,6 @@ 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.
// @ts-expect-error TODO: Fix this
editor.insertFragment([{ text: '' }, node, { text: '' }]);
} else {
editor.insertNodes([node], { mode });
Expand Down
3 changes: 2 additions & 1 deletion packages/slate-commons/src/commands/isBlock.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { TNode} from '@udecode/plate-common';
import { isElement, type SlateEditor } from '@udecode/plate-common';
import type { Node } from 'slate';

export function isBlock(editor: SlateEditor, node: Node): boolean {
export function isBlock(editor: SlateEditor, node: Node | TNode): boolean {
return isElement(node) && editor.isBlock(node);
}
6 changes: 0 additions & 6 deletions packages/slate-commons/src/commands/isInline.ts

This file was deleted.

3 changes: 2 additions & 1 deletion packages/slate-commons/src/commands/isVoid.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { TNode} from '@udecode/plate-common';
import { isElement, type SlateEditor } from '@udecode/plate-common';
import type { Node } from 'slate';

export function isVoid(editor: SlateEditor, node: Node): boolean {
export function isVoid(editor: SlateEditor, node: Node | TNode): boolean {
return isElement(node) && editor.isVoid(node);
}
10 changes: 4 additions & 6 deletions packages/slate-commons/src/commands/removeChildren.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import type { SlateEditor } from '@udecode/plate-common';
import type { NodeEntry } from 'slate';
import { Element, Text } from 'slate';
import { isElement, isText, type SlateEditor, type TNodeEntry } from '@udecode/plate-common';

/**
* Ensures given element has a single, empty `Text` child.
* Returns `true` when removal occurred.
* Returns `false` when nothing changed.
*/
export function removeChildren(editor: SlateEditor, [node, path]: NodeEntry): boolean {
if (!Element.isElement(node)) {
export function removeChildren(editor: SlateEditor, [node, path]: TNodeEntry): boolean {
if (!isElement(node)) {
return false;
}

const [child] = node.children;

if (node.children.length === 1 && Text.isText(child) && child.text.length === 0) {
if (node.children.length === 1 && isText(child) && child.text.length === 0) {
return false;
}

Expand Down
6 changes: 2 additions & 4 deletions packages/slate-commons/src/commands/toDomNode.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type { SlateEditor } from '@udecode/plate-common';
import type { SlateEditor, TNode } from '@udecode/plate-common';
import { toDOMNode } from '@udecode/plate-common/react';
import type { Node } from 'slate';

export function toDomNode(editor: SlateEditor, node: Node): HTMLElement | null {
// @ts-expect-error TODO: Fix this
export function toDomNode(editor: SlateEditor, node: TNode): HTMLElement | null {
return toDOMNode(editor, node) ?? null;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { ElementNode } from '@prezly/slate-types';
import { isElementNode } from '@prezly/slate-types';
import type { TNode } from '@udecode/plate-common';
import type { Node } from 'slate';

type Uuid = string;
Expand Down Expand Up @@ -29,7 +30,7 @@ export namespace ButtonBlockNode {
OUTLINE = 'outline',
}

export function isButtonBlockNode(node: Node): node is ButtonBlockNode {
export function isButtonBlockNode(node: Node | TNode): node is ButtonBlockNode {
return isElementNode(node, Type);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { EditorCommands } from '@prezly/slate-commons';
import type { Alignment } from '@prezly/slate-types';
import { isAlignableElement, isImageNode } from '@prezly/slate-types';
import type { SlateEditor } from '@udecode/plate-common';
import { Node, Path, Range } from 'slate';
import { type SlateEditor } from '@udecode/plate-common';
import type { Node} from 'slate';
import { Path, Range } from 'slate';

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

Expand Down Expand Up @@ -36,8 +37,8 @@ function prevBlock(editor: SlateEditor): Node | undefined {
if (editor.selection && Range.isCollapsed(editor.selection)) {
const topLevelPath = editor.selection.focus.path.slice(0, 1);
if (Path.hasPrevious(topLevelPath)) {
// @ts-expect-error TODO: Fix this
return Node.get(editor, Path.previous(topLevelPath));
const [node] = editor.node(Path.previous(topLevelPath));
return node;
}
}
return undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import {
isHeadingNode,
isParagraphNode,
} from '@prezly/slate-types';
import { getNodeString } from '@udecode/plate-common';
import { useEditorState } from '@udecode/plate-common/react';
import classNames from 'classnames';
import { isHotkey } from 'is-hotkey';
import type { KeyboardEvent, RefObject } from 'react';
import React, { useEffect, useState } from 'react';
import type { Modifier } from 'react-popper';
import { Node } from 'slate';

import { TooltipV2 } from '#components';
import { useKeyboardNavigation, useSize } from '#lib';
Expand Down Expand Up @@ -138,7 +138,7 @@ export function FloatingAddMenu<Action>({
const isParagraph = isParagraphNode(currentNode);
const isHeading1 = isHeadingNode(currentNode, HEADING_1_NODE_TYPE);
const isHeading2 = isHeadingNode(currentNode, HEADING_2_NODE_TYPE);
const text = currentNode ? Node.string(currentNode) : '';
const text = currentNode ? getNodeString(currentNode) : '';

return (
<FloatingContainer.Container
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Node } from 'slate';
import { isNode, isNodeList, type TDescendant } from '@udecode/plate-common';

export type Fragment = Node[];
export type Fragment = TDescendant[];

/**
* Checks recursively whether all members of the fragment are Nodes.
* It does not validate schema/hierarchy.
*/
export function isFragment(value: unknown): value is Fragment {
if (!Node.isNodeList(value)) {
if (!isNodeList(value)) {
return false;
}

return value.length > 0 && value.every(Node.isNode);
return value.length > 0 && value.every(isNode);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export function withSlatePasting(isPreservedBlock: IsPreservedBlock) {
}

if (editor.selection) {
// @ts-expect-error TODO: Fix this
editor.insertFragment(fragment);
} else {
editor.insertNodes(fragment);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { ElementNode } from '@prezly/slate-types';
import { isElementNode } from '@prezly/slate-types';
import type { TNode } from '@udecode/plate-common';
import type { Node } from 'slate';

type Uuid = string;
Expand Down Expand Up @@ -55,24 +56,24 @@ export namespace PlaceholderNode {
WEB_BOOKMARK = 'placeholder:bookmark',
}

export function isPlaceholderNode(node: Node): node is PlaceholderNode;
export function isPlaceholderNode(node: Node | TNode): node is PlaceholderNode;

export function isPlaceholderNode<T extends Type>(
node: Node,
node: Node | TNode,
type: T,
): node is PlaceholderNode<T>;

export function isPlaceholderNode<T extends Type>(
node: Node,
node: Node | TNode,
types: T[],
): node is PlaceholderNode<T>;

export function isPlaceholderNode<T extends Type>(
node: Node,
node: Node | TNode,
type: `${T}`,
): node is PlaceholderNode<T>;

export function isPlaceholderNode(node: Node, type?: string | string[]): boolean {
export function isPlaceholderNode(node: Node | TNode, type?: string | string[]): boolean {
if (typeof type === 'string') {
return isElementNode(node, type);
}
Expand All @@ -82,14 +83,14 @@ export namespace PlaceholderNode {
return isElementNode(node, Object.values(Type));
}

export function isSameAs<T extends PlaceholderNode>(placeholder: T, node: Node): node is T;
export function isSameAs<T extends PlaceholderNode>(placeholder: T): (node: Node) => node is T;
export function isSameAs<T extends PlaceholderNode>(placeholder: T, node: Node | TNode): node is T;
export function isSameAs<T extends PlaceholderNode>(placeholder: T): (node: Node | TNode) => node is T;
export function isSameAs<T extends PlaceholderNode>(
placeholder: T,
node?: Node,
): boolean | ((node: Node) => boolean) {
node?: Node | TNode,
): boolean | ((node: Node | TNode) => boolean) {
if (!node) {
return (node: Node): node is T => {
return (node: Node | TNode): node is T => {
return (
PlaceholderNode.isPlaceholderNode(node, placeholder.type) &&
node.uuid === placeholder.uuid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ export function useFloatingSnippetInput(editor: SlateEditor): [State, Actions] {

EditorCommands.insertNodes(editor, node.children, { mode: 'highest' });

// TODO: Fix this!
// editor.flash(node.children.at(0), node.children.at(-1));
editor.flash(node.children.at(0), node.children.at(-1));
savedSelection.restore(editor, { focus: true });
} catch (error) {
console.error(error);
Expand Down
10 changes: 2 additions & 8 deletions packages/slate-editor/src/lib/withResetFormattingOnBreak.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import { EditorCommands } from '@prezly/slate-commons';
import type { SlateEditor } from '@udecode/plate-common';
import type { Node } from 'slate';
import type { SlateEditor, TNode } from '@udecode/plate-common';

export function withResetFormattingOnBreak(match: (node: Node) => boolean) {
export function withResetFormattingOnBreak(match: (node: TNode) => boolean) {
return function <T extends SlateEditor>(editor: T): T {
const { insertBreak } = editor;

editor.insertBreak = () => {
/**
* The `currentNode` is the top-level block, which means when the
* cursor is at a list item, the type is bulleted or numbered list.
* This is why we have to perform `isList` check and not `isListItem`.
*/
const [currentNode] = EditorCommands.getCurrentNodeEntry(editor) || [];

if (currentNode && match(currentNode) && EditorCommands.isSelectionAtBlockEnd(editor)) {
Expand Down
6 changes: 2 additions & 4 deletions packages/slate-editor/src/modules/editor/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -841,11 +841,9 @@ export const Editor = forwardRef<EditorRef, EditorProps>((props, forwardedRef) =
* in two" work as expected.
*/
onChange(value);
// variables.onChange(editor);
// userMentions.onChange(editor);
variables.onChange(editor);
userMentions.onChange(editor);
}}
// @ts-expect-error TODO: Fix this
initialValue={getInitialValue()}
>
<DecorationsProvider decorate={decorate}>
{(combinedDecorate) => (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import type { SlateEditor } from '@udecode/plate-common';
import type { NodeEntry } from 'slate';
import type { SlateEditor, TNodeEntry } from '@udecode/plate-common';

import { createParagraph } from '#extensions/paragraphs';

import { isInlineNode } from '../queries';

export function convertToParagraph(editor: SlateEditor, [node, path]: NodeEntry) {
export function convertToParagraph(editor: SlateEditor, [node, path]: TNodeEntry) {
if (isInlineNode(node)) {
editor.wrapNodes(createParagraph(), { at: path });
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type { SlateEditor } from '@udecode/plate-common';
import type { NodeEntry } from 'slate';
import type { SlateEditor, TNodeEntry } from '@udecode/plate-common';

import { createParagraph } from '#extensions/paragraphs';

export function insertParagraph(editor: SlateEditor, [node, path]: NodeEntry) {
export function insertParagraph(editor: SlateEditor, [node, path]: TNodeEntry) {
editor.insertNodes([createParagraph()], { at: path, match: (n) => n === node });
return true;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import type { SlateEditor } from '@udecode/plate-common';
import { Element, Path } from 'slate';
import type { NodeEntry } from 'slate';
import { isElement, type SlateEditor, type TNodeEntry } from '@udecode/plate-common';
import { Path } from 'slate';

/**
* This fixer just moves node up, without parent node splitting
*/
export function liftNodeNoSplit(editor: SlateEditor, [, path]: NodeEntry) {
export function liftNodeNoSplit(editor: SlateEditor, [, path]: TNodeEntry) {
const ancestor = editor.above({ at: path });

if (!ancestor) {
Expand All @@ -14,7 +13,7 @@ export function liftNodeNoSplit(editor: SlateEditor, [, path]: NodeEntry) {

const [ancestorNode] = ancestor;

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

Expand Down
Loading

0 comments on commit 272cce5

Please sign in to comment.