Skip to content

Commit

Permalink
Improve types for embed and cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
luin committed Aug 13, 2023
1 parent 80ad672 commit 6c06286
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
8 changes: 4 additions & 4 deletions blots/cursor.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);
}
Expand All @@ -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.
Expand Down Expand Up @@ -149,7 +149,7 @@ class Cursor extends EmbedBlot {
return null;
}

update(mutations: MutationRecord[], context: EmbedContext) {
update(mutations: MutationRecord[], context: Record<string, unknown>) {
if (
mutations.some(mutation => {
return (
Expand Down
11 changes: 5 additions & 6 deletions blots/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -77,7 +76,7 @@ class Embed extends EmbedBlot {
return range;
}

update(mutations: MutationRecord[], context: EmbedContext) {
update(mutations: MutationRecord[], context: Record<string, unknown>) {
mutations.forEach(mutation => {
if (
mutation.type === 'characterData' &&
Expand Down

0 comments on commit 6c06286

Please sign in to comment.