Skip to content

Commit

Permalink
[DEV-19139] Fix inaccurate types
Browse files Browse the repository at this point in the history
  • Loading branch information
e1himself committed Jan 15, 2025
1 parent 131a3dc commit 21adaf1
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions packages/slate-editor/src/modules/editor/lib/isEditorValueEqual.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { isEqual } from '@technically/lodash';
import { isVoid, type SlateEditor } from '@udecode/plate-common';
import { type SlateEditor } from '@udecode/plate-common';
import type { Element, Text, Descendant } from 'slate';

import type { ElementsEqualityCheckEditor } from '../plugins';

export function isEditorValueEqual<T extends Descendant>(
editor: SlateEditor,
editor: SlateEditor & ElementsEqualityCheckEditor,
a: T[],
b: T[],
): boolean {
Expand All @@ -18,7 +20,7 @@ export function isEditorValueEqual<T extends Descendant>(
if (!isNodeText && !isAnotherText) {
const equal = editor.isElementEqual(node as Element, another as Element);
if (typeof equal !== 'undefined') {
if (isVoid(editor, node) || isVoid(editor, another)) {
if (editor.isVoid(node) || editor.isVoid(another)) {
// Do not compare void elements children
return equal;
}
Expand Down Expand Up @@ -49,14 +51,17 @@ function isText(node: Descendant): node is Text {
// CACHE

const CACHE: WeakMap<
SlateEditor,
ElementsEqualityCheckEditor,
WeakMap<Descendant, WeakMap<Descendant, boolean>>
> = new WeakMap();

type WeakMatrix = WeakMap<SlateEditor, WeakMap<Descendant, WeakMap<Descendant, boolean>>>;
type WeakMatrix = WeakMap<
ElementsEqualityCheckEditor,
WeakMap<Descendant, WeakMap<Descendant, boolean>>
>;
type NodesComparator = (node: Descendant, another: Descendant) => boolean;

function cached(editor: SlateEditor, fn: NodesComparator): NodesComparator {
function cached(editor: ElementsEqualityCheckEditor, fn: NodesComparator): NodesComparator {
return (node, another) => {
const cached = get(CACHE, editor, node, another) ?? get(CACHE, editor, another, node);

Expand All @@ -75,7 +80,7 @@ function cached(editor: SlateEditor, fn: NodesComparator): NodesComparator {

function get(
matrix: WeakMatrix,
editor: SlateEditor,
editor: ElementsEqualityCheckEditor,
node: Descendant,
another: Descendant,
): boolean | undefined {
Expand All @@ -84,7 +89,7 @@ function get(

function set(
matrix: WeakMatrix,
editor: SlateEditor,
editor: ElementsEqualityCheckEditor,
node: Descendant,
another: Descendant,
value: boolean,
Expand Down

0 comments on commit 21adaf1

Please sign in to comment.