From c2ae4a7f42c4233df886498422b663302c93a46c Mon Sep 17 00:00:00 2001 From: Zihua Li Date: Fri, 16 Jun 2023 09:16:34 +0800 Subject: [PATCH] Upgrade Parchment to fix typings --- blots/block.ts | 23 ++++++++++------------- blots/cursor.ts | 3 +-- blots/scroll.ts | 7 ++++--- core/editor.ts | 6 ------ core/quill.ts | 10 +++------- formats/bold.ts | 1 - formats/header.ts | 1 - formats/script.ts | 1 - formats/table.ts | 5 ++--- modules/syntax.ts | 3 +-- modules/table.ts | 2 -- package-lock.json | 14 +++++++------- package.json | 2 +- themes/snow.ts | 1 - 14 files changed, 29 insertions(+), 50 deletions(-) diff --git a/blots/block.ts b/blots/block.ts index 31af239cea..38eb365e85 100644 --- a/blots/block.ts +++ b/blots/block.ts @@ -1,11 +1,11 @@ import { AttributorStore, BlockBlot, + Blot, EmbedBlot, LeafBlot, Scope, } from 'parchment'; -import { Blot } from 'parchment/dist/typings/blot/abstract/blot'; import Delta from 'quill-delta'; import Break from './break'; import Inline from './inline'; @@ -184,18 +184,15 @@ BlockEmbed.scope = Scope.BLOCK_BLOT; // It is important for cursor behavior BlockEmbeds use tags that are block level elements function blockDelta(blot: BlockBlot, filter = true) { - return ( - blot - // @ts-expect-error - .descendants(LeafBlot) - .reduce((delta, leaf: LeafBlot) => { - if (leaf.length() === 0) { - return delta; - } - return delta.insert(leaf.value(), bubbleFormats(leaf, {}, filter)); - }, new Delta()) - .insert('\n', bubbleFormats(blot)) - ); + return blot + .descendants(LeafBlot) + .reduce((delta, leaf) => { + if (leaf.length() === 0) { + return delta; + } + return delta.insert(leaf.value(), bubbleFormats(leaf, {}, filter)); + }, new Delta()) + .insert('\n', bubbleFormats(blot)); } function bubbleFormats(blot, formats = {}, filter = true) { diff --git a/blots/cursor.ts b/blots/cursor.ts index a744b1158f..ea0352f458 100644 --- a/blots/cursor.ts +++ b/blots/cursor.ts @@ -1,5 +1,4 @@ -import { EmbedBlot, Scope, ScrollBlot } from 'parchment'; -import { Parent } from 'parchment/dist/typings/blot/abstract/blot'; +import { EmbedBlot, Parent, Scope, ScrollBlot } from 'parchment'; import Selection from '../core/selection'; import TextBlot from './text'; diff --git a/blots/scroll.ts b/blots/scroll.ts index 87ba5a33a7..eb55634568 100644 --- a/blots/scroll.ts +++ b/blots/scroll.ts @@ -1,12 +1,13 @@ import { - Scope, - ScrollBlot, + Blot, ContainerBlot, LeafBlot, + Parent, ParentBlot, Registry, + Scope, + ScrollBlot, } from 'parchment'; -import { Blot, Parent } from 'parchment/dist/typings/blot/abstract/blot'; import Emitter, { EmitterSource } from '../core/emitter'; import Block, { BlockEmbed } from './block'; import Break from './break'; diff --git a/core/editor.ts b/core/editor.ts index cc2a38abfb..3657a286c1 100644 --- a/core/editor.ts +++ b/core/editor.ts @@ -45,13 +45,11 @@ class Editor { isImplicitNewlineAppended = !text.endsWith('\n') && (scrollLength <= index || - // @ts-expect-error !!this.scroll.descendant(BlockEmbed, index)[0]); this.scroll.insertAt(index, text); const [line, offset] = this.scroll.line(index); let formats = merge({}, bubbleFormats(line)); if (line instanceof Block) { - // @ts-expect-error const [leaf] = line.descendant(LeafBlot, offset); formats = merge(formats, bubbleFormats(leaf)); } @@ -63,13 +61,11 @@ class Editor { if (isInlineEmbed) { if ( scrollLength <= index || - // @ts-expect-error !!this.scroll.descendant(BlockEmbed, index)[0] ) { isImplicitNewlineAppended = true; } } else if (index > 0) { - // @ts-expect-error const [leaf, offset] = this.scroll.descendant(LeafBlot, index - 1); if (leaf instanceof TextBlot) { const text = leaf.value(); @@ -86,7 +82,6 @@ class Editor { this.scroll.insertAt(index, key, op.insert[key]); if (isInlineEmbed) { - // @ts-expect-error const [leaf] = this.scroll.descendant(LeafBlot, index); const formats = merge({}, bubbleFormats(leaf)); attributes = AttributeMap.diff(formats, attributes) || {}; @@ -181,7 +176,6 @@ class Editor { }); } else { lines = this.scroll.lines(index, length); - // @ts-expect-error leaves = this.scroll.descendants(LeafBlot, index, length); } const [lineFormats, leafFormats] = [lines, leaves].map(blots => { diff --git a/core/quill.ts b/core/quill.ts index 7f87145a97..16e83eb213 100644 --- a/core/quill.ts +++ b/core/quill.ts @@ -1,10 +1,6 @@ import cloneDeep from 'lodash.clonedeep'; import merge from 'lodash.merge'; import * as Parchment from 'parchment'; -import { - Blot, - BlotConstructor, -} from 'parchment/dist/typings/blot/abstract/blot'; import Delta, { Op } from 'quill-delta'; import Block, { BlockEmbed } from '../blots/block'; import Scroll, { ScrollConstructor } from '../blots/scroll'; @@ -94,10 +90,10 @@ class Quill { static register( path: | string - | BlotConstructor + | Parchment.BlotConstructor | Parchment.Attributor | Record, - target?: BlotConstructor | Parchment.Attributor | boolean, + target?: Parchment.BlotConstructor | Parchment.Attributor | boolean, overwrite = false, ) { if (typeof path !== 'string') { @@ -443,7 +439,7 @@ class Quill { return this.editor.getFormat(index.index, index.length); } - getIndex(blot: Blot) { + getIndex(blot: Parchment.Blot) { return blot.offset(this.scroll); } diff --git a/formats/bold.ts b/formats/bold.ts index feba51a7b9..4516b748dd 100644 --- a/formats/bold.ts +++ b/formats/bold.ts @@ -1,6 +1,5 @@ import Inline from '../blots/inline'; -// @ts-expect-error TODO: Inline.tagName should be string[] | string class Bold extends Inline { static blotName = 'bold'; static tagName = ['STRONG', 'B']; diff --git a/formats/header.ts b/formats/header.ts index af9276db5a..107aa66924 100644 --- a/formats/header.ts +++ b/formats/header.ts @@ -1,6 +1,5 @@ import Block from '../blots/block'; -// @ts-expect-error TODO: BlockBlot.tagName should be string[] | string class Header extends Block { static blotName = 'header'; static tagName = ['H1', 'H2', 'H3', 'H4', 'H5', 'H6']; diff --git a/formats/script.ts b/formats/script.ts index f95f61bb3f..48a37bf0ca 100644 --- a/formats/script.ts +++ b/formats/script.ts @@ -1,6 +1,5 @@ import Inline from '../blots/inline'; -// @ts-expect-error TODO: Inline.tagName should be string[] | string class Script extends Inline { static blotName = 'script'; static tagName = ['SUB', 'SUP']; diff --git a/formats/table.ts b/formats/table.ts index 44679fa3cb..07f75439ea 100644 --- a/formats/table.ts +++ b/formats/table.ts @@ -1,4 +1,4 @@ -import LinkedList from 'parchment/dist/typings/collection/linked-list'; +import { LinkedList } from 'parchment'; import Block from '../blots/block'; import Container from '../blots/container'; @@ -132,8 +132,7 @@ class TableContainer extends Container { children: LinkedList; balanceCells() { - // @ts-expect-error TODO: fix signature of ParentBlot.descendants - const rows = this.descendants(TableRow) as TableRow[]; + const rows = this.descendants(TableRow); const maxColumns = rows.reduce((max, row) => { return Math.max(row.children.length, max); }, 0); diff --git a/modules/syntax.ts b/modules/syntax.ts index 0e684cd4e5..e611c9d38b 100644 --- a/modules/syntax.ts +++ b/modules/syntax.ts @@ -266,8 +266,7 @@ class Syntax extends Module { const range = this.quill.getSelection(); const blots = blot == null - ? // @ts-expect-error - this.quill.scroll.descendants(SyntaxCodeBlockContainer) + ? this.quill.scroll.descendants(SyntaxCodeBlockContainer) : [blot]; blots.forEach(container => { container.highlight(this.highlightBlot, force); diff --git a/modules/table.ts b/modules/table.ts index 0d6d5a6f55..6f615f09f2 100644 --- a/modules/table.ts +++ b/modules/table.ts @@ -24,9 +24,7 @@ class Table extends Module { } balanceTables() { - // @ts-expect-error this.quill.scroll.descendants(TableContainer).forEach(table => { - // @ts-expect-error table.balanceCells(); }); } diff --git a/package-lock.json b/package-lock.json index b214351d68..f696fd4e0e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,7 +16,7 @@ "lodash.clonedeep": "^4.5.0", "lodash.isequal": "^4.5.0", "lodash.merge": "^4.5.0", - "parchment": "^2.0.1", + "parchment": "^3.0.0-alpha.1", "quill-delta": "^5.1.0" }, "devDependencies": { @@ -18428,9 +18428,9 @@ } }, "node_modules/parchment": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/parchment/-/parchment-2.0.1.tgz", - "integrity": "sha512-VBKrlEoZCBD+iwoeag0QTtY1Cti+Ma4nLpVYcc/uus/wHhMsPOi5InH3RL1s4aekahPZpabcS2ToKyGf7RMH/g==" + "version": "3.0.0-alpha.1", + "resolved": "https://registry.npmjs.org/parchment/-/parchment-3.0.0-alpha.1.tgz", + "integrity": "sha512-CJN5G75j0W3gPHMHDmY82sdtPf5wVO2YRhdXPBypvwj5YA5C9zsTnUdpq92Zxca8Eha331wMOlf6rTN+vi2dlA==" }, "node_modules/parent-module": { "version": "1.0.1", @@ -38471,9 +38471,9 @@ } }, "parchment": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/parchment/-/parchment-2.0.1.tgz", - "integrity": "sha512-VBKrlEoZCBD+iwoeag0QTtY1Cti+Ma4nLpVYcc/uus/wHhMsPOi5InH3RL1s4aekahPZpabcS2ToKyGf7RMH/g==" + "version": "3.0.0-alpha.1", + "resolved": "https://registry.npmjs.org/parchment/-/parchment-3.0.0-alpha.1.tgz", + "integrity": "sha512-CJN5G75j0W3gPHMHDmY82sdtPf5wVO2YRhdXPBypvwj5YA5C9zsTnUdpq92Zxca8Eha331wMOlf6rTN+vi2dlA==" }, "parent-module": { "version": "1.0.1", diff --git a/package.json b/package.json index c8b3af7174..b21854e231 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ "lodash.clonedeep": "^4.5.0", "lodash.isequal": "^4.5.0", "lodash.merge": "^4.5.0", - "parchment": "^2.0.1", + "parchment": "^3.0.0-alpha.1", "quill-delta": "^5.1.0" }, "devDependencies": { diff --git a/themes/snow.ts b/themes/snow.ts index 7781510a25..943d20f16f 100644 --- a/themes/snow.ts +++ b/themes/snow.ts @@ -51,7 +51,6 @@ class SnowTooltip extends BaseTooltip { if (range == null) return; if (range.length === 0 && source === Emitter.sources.USER) { const [link, offset] = this.quill.scroll.descendant( - // @ts-expect-error LinkBlot, range.index, );