Skip to content

Commit

Permalink
perf: type safety
Browse files Browse the repository at this point in the history
  • Loading branch information
hzgotb committed Apr 28, 2024
1 parent 3454595 commit 7e46419
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 25 deletions.
1 change: 1 addition & 0 deletions packages/quill/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import Delta, { Op, OpIterator, AttributeMap } from 'quill-delta';
import Input from './modules/input.js';
import UINode from './modules/uiNode.js';

export { default as Module } from './core/module.js';
export { Delta, Op, OpIterator, AttributeMap, Parchment, Range };
export type {
Bounds,
Expand Down
61 changes: 37 additions & 24 deletions packages/quill/src/core/quill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ const debug = logger('quill');
const globalRegistry = new Parchment.Registry();
Parchment.ParentBlot.uiClass = 'ql-ui';

type ClassToConstructor<T> = { new (...args: any[]): T & object };

type RegistryTarget = Parchment.RegistryDefinition | Module | boolean;

interface RegistryRecord {
[key: `blots/${string}`]: Parchment.BlotConstructor;
[key: `formats/${string}`]: Parchment.BlotConstructor | Parchment.Attributor;
[key: `attributors/${string}`]: Parchment.Attributor;
[key: `modules/${string}`]: ClassToConstructor<Module>;
[key: `themes/${string}`]: ClassToConstructor<Theme>;
[key: string]: any;
}

/**
* Options for initializing a Quill instance
*/
Expand Down Expand Up @@ -121,28 +134,21 @@ class Quill {
return this.imports[name];
}

static register(record: RegistryRecord, overwrite?: boolean): void;
static register(
path:
| string
| Parchment.BlotConstructor
| Parchment.Attributor
| Record<string, unknown>,
target?: Parchment.BlotConstructor | Parchment.Attributor | boolean,
overwrite = false,
) {
if (typeof path !== 'string') {
const name = 'attrName' in path ? path.attrName : path.blotName;
if (typeof name === 'string') {
// register(Blot | Attributor, overwrite)
// @ts-expect-error
this.register(`formats/${name}`, path, target);
} else {
Object.keys(path).forEach((key) => {
// @ts-expect-error
this.register(key, path[key], target);
});
}
} else {
target: Omit<RegistryTarget, 'boolean' | 'Module'>,
overwrite?: boolean,
): void;
static register(
path: string,
target: RegistryTarget,
overwrite?: boolean,
): void;
static register(...args: any[]): void {
const path = typeof args[0] === 'string' ? args[0] : '';
const target = path ? args[1] : args[0];
const overwrite = (path ? args[2] : args[1]) ?? false;
if (path) {
if (this.imports[path] != null && !overwrite) {
debug.warn(`Overwriting ${path} with`, target);
}
Expand All @@ -151,16 +157,23 @@ class Quill {
(path.startsWith('blots/') || path.startsWith('formats/')) &&
target &&
typeof target !== 'boolean' &&
// @ts-expect-error
target.blotName !== 'abstract'
) {
globalRegistry.register(target);
}
// @ts-expect-error
if (typeof target.register === 'function') {
// @ts-expect-error
target.register(globalRegistry);
}
return;
}
const name: string | undefined = target['attrName'] ?? target['blotName'];
if (typeof name === 'string') {
// register(Blot | Attributor, overwrite)
this.register(`formats/${name}`, target, overwrite);
} else {
Object.keys(target).forEach((key) => {
this.register(key, target[key], overwrite);
});
}
}

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 @@ -206,7 +206,6 @@ class Syntax extends Module<SyntaxOptions> {

static register() {
Quill.register(CodeToken, true);
// @ts-expect-error
Quill.register(SyntaxCodeBlock, true);
Quill.register(SyntaxCodeBlockContainer, true);
}
Expand Down
1 change: 1 addition & 0 deletions packages/quill/src/quill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ Quill.register(
true,
);

export { default as Module } from './core/module.js';
export type {
Bounds,
DebugLevel,
Expand Down

0 comments on commit 7e46419

Please sign in to comment.