diff --git a/packages/quill/src/core.ts b/packages/quill/src/core.ts index cdf5724768..5b8946ad5f 100644 --- a/packages/quill/src/core.ts +++ b/packages/quill/src/core.ts @@ -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, diff --git a/packages/quill/src/core/quill.ts b/packages/quill/src/core/quill.ts index b88a4f5fa4..5622856527 100644 --- a/packages/quill/src/core/quill.ts +++ b/packages/quill/src/core/quill.ts @@ -30,6 +30,19 @@ const debug = logger('quill'); const globalRegistry = new Parchment.Registry(); Parchment.ParentBlot.uiClass = 'ql-ui'; +type ClassToConstructor = { new (...args: any[]): T & object }; + +type RegistryTarget = Parchment.RegistryDefinition | Module | boolean; + +interface RegistryRecord { + [key: `blots/${string}`]: Parchment.BlotConstructor; + [key: `formats/${string}`]: Parchment.RegistryDefinition; + [key: `attributors/${string}`]: Parchment.Attributor; + [key: `modules/${string}`]: ClassToConstructor; + [key: `themes/${string}`]: ClassToConstructor; + [key: string]: any; +} + /** * Options for initializing a Quill instance */ @@ -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, - 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: Exclude, + 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); } @@ -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); + }); } } diff --git a/packages/quill/src/modules/syntax.ts b/packages/quill/src/modules/syntax.ts index ed8214e81f..c3d1a81959 100644 --- a/packages/quill/src/modules/syntax.ts +++ b/packages/quill/src/modules/syntax.ts @@ -80,7 +80,8 @@ class SyntaxCodeBlock extends CodeBlock { } } - replaceWith(name: string, value: unknown) { + // TODO maybe something wrong + replaceWith(name: string | Blot, value?: any) { this.formatAt(0, this.length(), CodeToken.blotName, false); return super.replaceWith(name, value); } @@ -180,7 +181,7 @@ class SyntaxCodeBlockContainer extends CodeBlockContainer { } } } -// @ts-expect-error + SyntaxCodeBlockContainer.allowedChildren = [SyntaxCodeBlock]; SyntaxCodeBlock.requiredContainer = SyntaxCodeBlockContainer; SyntaxCodeBlock.allowedChildren = [CodeToken, CursorBlot, TextBlot, BreakBlot]; @@ -206,7 +207,6 @@ class Syntax extends Module { static register() { Quill.register(CodeToken, true); - // @ts-expect-error Quill.register(SyntaxCodeBlock, true); Quill.register(SyntaxCodeBlockContainer, true); } diff --git a/packages/quill/src/quill.ts b/packages/quill/src/quill.ts index 0c3626fd8a..de2f0a609f 100644 --- a/packages/quill/src/quill.ts +++ b/packages/quill/src/quill.ts @@ -115,6 +115,7 @@ Quill.register( true, ); +export { default as Module } from './core/module.js'; export type { Bounds, DebugLevel,