Skip to content

Commit

Permalink
Expose types for Quill options
Browse files Browse the repository at this point in the history
  • Loading branch information
luin committed Mar 23, 2024
1 parent 83402d2 commit 944ba8d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
6 changes: 6 additions & 0 deletions packages/quill/src/core.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import Quill from './core/quill.js';
import type {
DebugLevel,
ExpandedQuillOptions,
QuillOptions,
} from './core/quill.js';

import Block, { BlockEmbed } from './blots/block.js';
import Break from './blots/break.js';
Expand All @@ -18,6 +23,7 @@ import Input from './modules/input.js';
import UINode from './modules/uiNode.js';

export { Delta, Op, OpIterator, AttributeMap };
export type { DebugLevel, ExpandedQuillOptions, QuillOptions };

Quill.register({
'blots/block': Block,
Expand Down
45 changes: 35 additions & 10 deletions packages/quill/src/core/quill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,41 @@ const debug = logger('quill');
const globalRegistry = new Parchment.Registry();
Parchment.ParentBlot.uiClass = 'ql-ui';

interface Options {
/**
* Options for initializing a Quill instance
*/
export interface QuillOptions {
theme?: string;
debug?: DebugLevel | boolean;
registry?: Parchment.Registry;
/**
* Whether to disable the editing
* @default false
*/
readOnly?: boolean;

/**
* Placeholder text to display when the editor is empty
* @default ""
*/
placeholder?: string;
bounds?: HTMLElement | string | null;
modules?: Record<string, unknown>;

/**
* A list of formats that are recognized and can exist within the editor contents.
* `null` means all formats are allowed.
* @default null
*/
formats?: string[] | null;
}

interface ExpandedOptions extends Omit<Options, 'theme' | 'formats'> {
/**
* Similar to QuillOptions, but with all properties expanded to their default values,
* and all selectors resolved to HTMLElements.
*/
export interface ExpandedQuillOptions
extends Omit<QuillOptions, 'theme' | 'formats'> {
theme: ThemeConstructor;
registry: Parchment.Registry;
container: HTMLElement;
Expand All @@ -63,7 +86,7 @@ class Quill {
readOnly: false,
registry: globalRegistry,
theme: 'default',
} satisfies Partial<Options>;
} satisfies Partial<QuillOptions>;
static events = Emitter.events;
static sources = Emitter.sources;
static version = typeof QUILL_VERSION === 'undefined' ? 'dev' : QUILL_VERSION;
Expand Down Expand Up @@ -145,7 +168,7 @@ class Quill {
root: HTMLDivElement;
scroll: Scroll;
emitter: Emitter;
allowReadOnlyEdits: boolean;
protected allowReadOnlyEdits: boolean;
editor: Editor;
composition: Composition;
selection: Selection;
Expand All @@ -156,9 +179,9 @@ class Quill {
history: History;
uploader: Uploader;

options: ExpandedOptions;
options: ExpandedQuillOptions;

constructor(container: HTMLElement | string, options: Options = {}) {
constructor(container: HTMLElement | string, options: QuillOptions = {}) {
this.options = expandConfig(container, options);
this.container = this.options.container;
if (this.container == null) {
Expand Down Expand Up @@ -747,16 +770,16 @@ function expandModuleConfig(config: Record<string, unknown> | undefined) {
);
}

function omitUndefinedValuesFromOptions(obj: Options) {
function omitUndefinedValuesFromOptions(obj: QuillOptions) {
return Object.fromEntries(
Object.entries(obj).filter((entry) => entry[1] !== undefined),
);
}

function expandConfig(
containerOrSelector: HTMLElement | string,
options: Options,
): ExpandedOptions {
options: QuillOptions,
): ExpandedQuillOptions {
const container = resolveSelector(containerOrSelector);
if (!container) {
throw new Error('Invalid Quill container');
Expand All @@ -774,7 +797,7 @@ function expandConfig(
const { modules: quillModuleDefaults, ...quillDefaults } = Quill.DEFAULTS;
const { modules: themeModuleDefaults, ...themeDefaults } = theme.DEFAULTS;

const modules: ExpandedOptions['modules'] = merge(
const modules: ExpandedQuillOptions['modules'] = merge(
{},
expandModuleConfig(quillModuleDefaults),
expandModuleConfig(themeModuleDefaults),
Expand Down Expand Up @@ -1004,4 +1027,6 @@ function shiftRange(
return new Range(start, end - start);
}

export type { DebugLevel };

export { globalRegistry, expandConfig, overload, Quill as default };
3 changes: 3 additions & 0 deletions packages/quill/src/quill.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Quill from './core.js';
import type { DebugLevel, ExpandedQuillOptions, QuillOptions } from './core.js';

import { AlignClass, AlignStyle } from './formats/align.js';
import {
Expand Down Expand Up @@ -108,4 +109,6 @@ Quill.register(
true,
);

export type { DebugLevel, ExpandedQuillOptions, QuillOptions };

export default Quill;

0 comments on commit 944ba8d

Please sign in to comment.