Skip to content

Commit

Permalink
Upgrade Parchment to fix typings
Browse files Browse the repository at this point in the history
  • Loading branch information
luin committed Jun 16, 2023
1 parent afcee5d commit 89c0525
Show file tree
Hide file tree
Showing 14 changed files with 29 additions and 49 deletions.
23 changes: 10 additions & 13 deletions blots/block.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions blots/cursor.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
7 changes: 4 additions & 3 deletions blots/scroll.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
6 changes: 0 additions & 6 deletions core/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -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();
Expand All @@ -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) || {};
Expand Down Expand Up @@ -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 => {
Expand Down
10 changes: 3 additions & 7 deletions core/quill.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -94,10 +90,10 @@ class Quill {
static register(
path:
| string
| BlotConstructor
| Parchment.BlotConstructor
| Parchment.Attributor
| Record<string, unknown>,
target?: BlotConstructor | Parchment.Attributor | boolean,
target?: Parchment.BlotConstructor | Parchment.Attributor | boolean,
overwrite = false,
) {
if (typeof path !== 'string') {
Expand Down Expand Up @@ -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);
}

Expand Down
1 change: 0 additions & 1 deletion formats/bold.ts
Original file line number Diff line number Diff line change
@@ -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'];
Expand Down
1 change: 0 additions & 1 deletion formats/header.ts
Original file line number Diff line number Diff line change
@@ -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'];
Expand Down
1 change: 0 additions & 1 deletion formats/script.ts
Original file line number Diff line number Diff line change
@@ -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'];
Expand Down
5 changes: 2 additions & 3 deletions formats/table.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -132,8 +132,7 @@ class TableContainer extends Container {
children: LinkedList<TableBody>;

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);
Expand Down
3 changes: 1 addition & 2 deletions modules/syntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,7 @@ class Syntax extends Module<SyntaxOptions> {
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);
Expand Down
2 changes: 0 additions & 2 deletions modules/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ class Table extends Module {
}

balanceTables() {
// @ts-expect-error
this.quill.scroll.descendants(TableContainer).forEach(table => {
// @ts-expect-error
table.balanceCells();
});
}
Expand Down
13 changes: 7 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"lodash.clonedeep": "^4.5.0",
"lodash.isequal": "^4.5.0",
"lodash.merge": "^4.5.0",
"parchment": "^2.0.1",
"parchment": "github:quilljs/parchment#24e95c28384b26006488e1a8961691c2249ba1c4",
"quill-delta": "^5.1.0"
},
"devDependencies": {
Expand Down
1 change: 0 additions & 1 deletion themes/snow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand Down

0 comments on commit 89c0525

Please sign in to comment.