Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve typings for attributor #3954

Merged
merged 5 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 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 packages/quill/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"dependencies": {
"eventemitter3": "^5.0.1",
"lodash-es": "^4.17.21",
"parchment": "^3.0.0-alpha.1",
"parchment": "github:quilljs/parchment#9d148df41cc3d62ab07c25ebcead46a3d513d2c6",
"quill-delta": "^5.1.0"
},
"devDependencies": {
Expand Down
7 changes: 6 additions & 1 deletion packages/quill/scripts/build
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ set -e

DIST=dist

TMPDIR=$(mktemp -d 2>/dev/null || mktemp -d -t 'mytmpdir')
npx tsc --declaration --emitDeclarationOnly --outDir $TMPDIR

rm -rf $DIST
npx tsc --declaration --emitDeclarationOnly --outDir $DIST
mkdir $DIST
mv $TMPDIR/src/* $DIST
rm -rf $TMPDIR
npx babel src --out-dir $DIST --copy-files --no-copy-ignored --extensions .ts --source-maps
npx webpack -- --mode $1
cp package.json $DIST
Expand Down
8 changes: 0 additions & 8 deletions packages/quill/src/blots/scroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,12 +440,4 @@ function insertInlineContents(
}, index);
}

export interface ScrollConstructor {
new (
registry: Registry,
domNode: HTMLDivElement,
options: { emitter: Emitter },
): Scroll;
}

export default Scroll;
16 changes: 10 additions & 6 deletions packages/quill/src/core/quill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import Delta from 'quill-delta';
import type { BlockEmbed } from '../blots/block';
import type Block from '../blots/block';
import type Scroll from '../blots/scroll';
import type { ScrollConstructor } from '../blots/scroll';
import type Clipboard from '../modules/clipboard';
import type History from '../modules/history';
import type Keyboard from '../modules/keyboard';
Expand Down Expand Up @@ -120,6 +119,8 @@ class Quill {
this.imports[path] = target;
if (
(path.startsWith('blots/') || path.startsWith('formats/')) &&
target &&
typeof target !== 'boolean' &&
// @ts-expect-error
target.blotName !== 'abstract'
) {
Expand Down Expand Up @@ -167,13 +168,16 @@ class Quill {
this.root = this.addContainer('ql-editor');
this.root.classList.add('ql-blank');
this.emitter = new Emitter();
// @ts-expect-error TODO: fix BlotConstructor
const ScrollBlot = this.options.registry.query(
Parchment.ScrollBlot.blotName,
) as ScrollConstructor;
const scrollBlotName = Parchment.ScrollBlot.blotName;
const ScrollBlot = this.options.registry.query(scrollBlotName);
if (!ScrollBlot || !('blotName' in ScrollBlot)) {
throw new Error(
`Cannot initialize Quill without "${scrollBlotName}" blot`,
);
}
this.scroll = new ScrollBlot(this.options.registry, this.root, {
emitter: this.emitter,
});
}) as Scroll;
this.editor = new Editor(this.scroll);
this.selection = new Selection(this.scroll, this.emitter);
this.composition = new Composition(this.scroll, this.emitter);
Expand Down
1 change: 0 additions & 1 deletion packages/quill/src/formats/bold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ class Bold extends Inline {
static tagName = ['STRONG', 'B'];

static create() {
// @ts-expect-error
return super.create();
}

Expand Down
2 changes: 1 addition & 1 deletion packages/quill/src/formats/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ClassAttributor, Scope, StyleAttributor } from 'parchment';

class ColorAttributor extends StyleAttributor {
value(domNode: HTMLElement) {
let value = super.value(domNode);
let value = super.value(domNode) as string;
if (!value.startsWith('rgb(')) return value;
value = value.replace(/^[^\d]+/, '').replace(/[^\d]+$/, '');
const hex = value
Expand Down
3 changes: 1 addition & 2 deletions packages/quill/src/formats/indent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ class IndentAttributor extends ClassAttributor {
return super.canAdd(node, value) || super.canAdd(node, parseInt(value, 10));
}

// @ts-expect-error TODO: ClassAttributor may support numbers
value(node) {
value(node: HTMLElement) {
return parseInt(super.value(node), 10) || undefined; // Don't return NaN
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/quill/src/formats/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Link extends Inline {
static PROTOCOL_WHITELIST = ['http', 'https', 'mailto', 'tel', 'sms'];

static create(value: string) {
const node = super.create(value) as Element;
const node = super.create(value) as HTMLElement;
node.setAttribute('href', this.sanitize(value));
node.setAttribute('rel', 'noopener noreferrer');
node.setAttribute('target', '_blank');
Expand Down
3 changes: 1 addition & 2 deletions packages/quill/src/formats/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ ListContainer.tagName = 'OL';

class ListItem extends Block {
static create(value: string) {
// @ts-expect-error
const node = super.create() as Element;
const node = super.create() as HTMLElement;
node.setAttribute('data-list', value);
return node;
}
Expand Down
3 changes: 1 addition & 2 deletions packages/quill/src/formats/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ class TableCell extends Block {
static tagName = 'TD';

static create(value: string) {
// @ts-expect-error
const node = super.create() as Element;
const node = super.create() as HTMLElement;
if (value) {
node.setAttribute('data-row', value);
} else {
Expand Down
1 change: 0 additions & 1 deletion packages/quill/src/modules/syntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ class SyntaxCodeBlock extends CodeBlock {
static create(value: unknown) {
const domNode = super.create(value);
if (typeof value === 'string') {
// @ts-expect-error
domNode.setAttribute('data-language', value);
}
return domNode;
Expand Down
3 changes: 2 additions & 1 deletion packages/quill/test/unit/__helpers__/factory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Registry } from 'parchment';
import type { Attributor } from 'parchment';

import Block from '../../../src/blots/block';
import Break from '../../../src/blots/break';
Expand All @@ -14,7 +15,7 @@ export const createRegistry = (formats: unknown[] = []) => {
const registry = new Registry();

formats.forEach((format) => {
registry.register(format);
registry.register(format as Attributor);
});
registry.register(Block);
registry.register(Break);
Expand Down
30 changes: 10 additions & 20 deletions packages/quill/test/unit/modules/toolbar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,10 @@ describe('Toolbar', () => {
) as HTMLButtonElement;
quill.setSelection(7);
expect(boldButton.classList.contains('ql-active')).toBe(true);
// @ts-expect-error -- TODO fix later
expect(boldButton.attributes['aria-pressed'].value).toBe('true');
expect(boldButton.getAttribute('aria-pressed')).toBe('true');
quill.setSelection(2);
expect(boldButton.classList.contains('ql-active')).toBe(false);
// @ts-expect-error -- TODO fix later
expect(boldButton.attributes['aria-pressed'].value).toBe('false');
expect(boldButton.getAttribute('aria-pressed')).toBe('false');
});

test('link', () => {
Expand All @@ -193,12 +191,10 @@ describe('Toolbar', () => {
) as HTMLButtonElement;
quill.setSelection(12);
expect(linkButton.classList.contains('ql-active')).toBe(true);
// @ts-expect-error -- TODO fix later
expect(linkButton.attributes['aria-pressed'].value).toBe('true');
expect(linkButton.getAttribute('aria-pressed')).toBe('true');
quill.setSelection(2);
expect(linkButton.classList.contains('ql-active')).toBe(false);
// @ts-expect-error -- TODO fix later
expect(linkButton.attributes['aria-pressed'].value).toBe('false');
expect(linkButton.getAttribute('aria-pressed')).toBe('false');
});

test('dropdown', () => {
Expand Down Expand Up @@ -227,24 +223,18 @@ describe('Toolbar', () => {
quill.setSelection(17);
expect(centerButton.classList.contains('ql-active')).toBe(true);
expect(leftButton.classList.contains('ql-active')).toBe(false);
// @ts-expect-error -- TODO fix later
expect(centerButton.attributes['aria-pressed'].value).toBe('true');
// @ts-expect-error -- TODO fix later
expect(leftButton.attributes['aria-pressed'].value).toBe('false');
expect(centerButton.getAttribute('aria-pressed')).toBe('true');
expect(leftButton.getAttribute('aria-pressed')).toBe('false');
quill.setSelection(2);
expect(centerButton.classList.contains('ql-active')).toBe(false);
expect(leftButton.classList.contains('ql-active')).toBe(true);
// @ts-expect-error -- TODO fix later
expect(centerButton.attributes['aria-pressed'].value).toBe('false');
// @ts-expect-error -- TODO fix later
expect(leftButton.attributes['aria-pressed'].value).toBe('true');
expect(centerButton.getAttribute('aria-pressed')).toBe('false');
expect(leftButton.getAttribute('aria-pressed')).toBe('true');
quill.blur();
expect(centerButton.classList.contains('ql-active')).toBe(false);
expect(leftButton.classList.contains('ql-active')).toBe(false);
// @ts-expect-error -- TODO fix later
expect(centerButton.attributes['aria-pressed'].value).toBe('false');
// @ts-expect-error -- TODO fix later
expect(leftButton.attributes['aria-pressed'].value).toBe('false');
expect(centerButton.getAttribute('aria-pressed')).toBe('false');
expect(leftButton.getAttribute('aria-pressed')).toBe('false');
});
});
});
4 changes: 2 additions & 2 deletions packages/quill/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
"resolveJsonModule": true,
"declaration": false,
"module": "ES2020",
"moduleResolution": "node",
"moduleResolution": "bundler",
"strictNullChecks": true,
"noImplicitAny": true,
"noUnusedLocals": true
},
"include": ["src/**/*"]
"include": ["src/**/*", "test/**/*"]
}
28 changes: 16 additions & 12 deletions scripts/release.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,19 +152,23 @@ exec(`npm publish --tag ${distTag}${dryRun ? " --dry-run" : ""}`, {
/*
* Create GitHub release
*/
const filename = `release-note-${version}-${(Math.random() * 1000) | 0}.txt`;
fs.writeFileSync(filename, releaseNots);
try {
const prereleaseFlag = distTag === "latest" ? "--latest" : " --prerelease";
const releaseCommand = `gh release create v${version} ${prereleaseFlag} -t "Version ${version}" --notes-file "${filename}" --draft`;
if (dryRun) {
console.log(`Skipping: "${releaseCommand}" in dry-run mode`);
console.log(`Release note:\n${releaseNots}`);
} else {
exec(releaseCommand);
if (version === "experimental") {
console.log("Skipping GitHub release for experimental version");
} else {
const filename = `release-note-${version}-${(Math.random() * 1000) | 0}.txt`;
fs.writeFileSync(filename, releaseNots);
try {
const prereleaseFlag = distTag === "latest" ? "--latest" : " --prerelease";
const releaseCommand = `gh release create v${version} ${prereleaseFlag} -t "Version ${version}" --notes-file "${filename}" --draft`;
if (dryRun) {
console.log(`Skipping: "${releaseCommand}" in dry-run mode`);
console.log(`Release note:\n${releaseNots}`);
} else {
exec(releaseCommand);
}
} finally {
fs.unlinkSync(filename);
}
} finally {
fs.unlinkSync(filename);
}

/*
Expand Down