From 6c06286002816a704db5a8acf26c66d82895a288 Mon Sep 17 00:00:00 2001 From: Zihua Li Date: Sun, 13 Aug 2023 11:54:31 +0800 Subject: [PATCH] Improve types for embed and cursor --- blots/cursor.ts | 8 ++++---- blots/embed.ts | 11 +++++------ 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/blots/cursor.ts b/blots/cursor.ts index e969b91c12..8b277093f5 100644 --- a/blots/cursor.ts +++ b/blots/cursor.ts @@ -1,7 +1,7 @@ import { EmbedBlot, Parent, Scope, ScrollBlot } from 'parchment'; import Selection from '../core/selection'; import TextBlot from './text'; -import { EmbedContext } from './embed'; +import { EmbedContextRange } from './embed'; class Cursor extends EmbedBlot { static blotName = 'cursor'; @@ -52,7 +52,7 @@ class Cursor extends EmbedBlot { } } - index(node: Text, offset: number) { + index(node: Node, offset: number) { if (node === this.textNode) return 0; return super.index(node, offset); } @@ -71,7 +71,7 @@ class Cursor extends EmbedBlot { this.parent = null; } - restore(): EmbedContext | null { + restore(): EmbedContextRange | null { if (this.selection.composing || this.parent == null) return null; const range = this.selection.getNativeRange(); // Browser may push down styles/nodes inside the cursor blot. @@ -149,7 +149,7 @@ class Cursor extends EmbedBlot { return null; } - update(mutations: MutationRecord[], context: EmbedContext) { + update(mutations: MutationRecord[], context: Record) { if ( mutations.some(mutation => { return ( diff --git a/blots/embed.ts b/blots/embed.ts index e4b3bfb890..4eb748edd5 100644 --- a/blots/embed.ts +++ b/blots/embed.ts @@ -3,12 +3,11 @@ import TextBlot from './text'; const GUARD_TEXT = '\uFEFF'; -export interface EmbedContext { +export interface EmbedContextRange { startNode: Node | Text; startOffset: number; endNode?: Node | Text; endOffset?: number; - range?: EmbedContext; } class Embed extends EmbedBlot { @@ -36,9 +35,9 @@ class Embed extends EmbedBlot { return super.index(node, offset); } - restore(node: Text): EmbedContext | null { - let range = null; - let textNode; + restore(node: Text): EmbedContextRange | null { + let range: EmbedContextRange | null = null; + let textNode: Text; const text = node.data.split(GUARD_TEXT).join(''); if (node === this.leftGuard) { if (this.prev instanceof TextBlot) { @@ -77,7 +76,7 @@ class Embed extends EmbedBlot { return range; } - update(mutations: MutationRecord[], context: EmbedContext) { + update(mutations: MutationRecord[], context: Record) { mutations.forEach(mutation => { if ( mutation.type === 'characterData' &&