UNPKG

8.88 kBTypeScriptView Raw
1// Type definitions for Quill 2.0
2// Project: https://github.com/quilljs/quill/, http://quilljs.com
3// Definitions by: Sumit <https://github.com/sumitkm>
4// Guillaume <https://github.com/guillaume-ro-fr>
5// James Garbutt <https://github.com/43081j>
6// Aniello Falcone <https://github.com/AnielloFalcone>
7// Mohammad Hossein Amri <https://github.com/mhamri>
8// Marco Mantovani <https://github.com/TheLand>
9// Ameer Hamoodi <https://github.com/AmeerHamoodi>
10// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
11// TypeScript Version: 2.9
12
13import { Blot } from "parchment/dist/src/blot/abstract/blot";
14import Delta = require("quill-delta");
15
16/**
17 * A stricter type definition would be:
18 *
19 * type DeltaOperation ({ insert: any } | { delete: number } | { retain: number }) & OptionalAttributes;
20 *
21 * But this would break a lot of existing code as it would require manual discrimination of the union types.
22 */
23export type DeltaOperation =
24 & { insert?: any; delete?: number | undefined; retain?: number | undefined }
25 & OptionalAttributes;
26interface SourceMap {
27 API: "api";
28 SILENT: "silent";
29 USER: "user";
30}
31export type Sources = "api" | "user" | "silent";
32
33export interface Key {
34 key: string | number;
35 shortKey?: boolean | null | undefined;
36 shiftKey?: boolean | null | undefined;
37 altKey?: boolean | null | undefined;
38 metaKey?: boolean | null | undefined;
39 ctrlKey?: boolean | null | undefined;
40}
41
42export interface StringMap {
43 [key: string]: any;
44}
45
46export interface OptionalAttributes {
47 attributes?: StringMap | undefined;
48}
49
50export type TextChangeHandler = (delta: Delta, oldContents: Delta, source: Sources) => any;
51export type SelectionChangeHandler = (range: RangeStatic, oldRange: RangeStatic, source: Sources) => any;
52export type EditorChangeHandler =
53 | ((name: "text-change", delta: Delta, oldContents: Delta, source: Sources) => any)
54 | ((name: "selection-change", range: RangeStatic, oldRange: RangeStatic, source: Sources) => any);
55
56export interface KeyboardStatic {
57 addBinding(key: Key, callback: (range: RangeStatic, context: any) => void): void;
58 addBinding(key: Key, context: any, callback: (range: RangeStatic, context: any) => void): void;
59}
60
61export type ClipboardMatcherCallback = (node: any, delta: Delta) => Delta;
62export type ClipboardMatcherNode = string | number;
63
64export interface ClipboardStatic {
65 matchers: Array<[ClipboardMatcherNode, ClipboardMatcherCallback]>;
66 convert(content?: { html?: string | undefined; text?: string | undefined }, formats?: StringMap): Delta;
67 addMatcher(selectorOrNodeType: ClipboardMatcherNode, callback: ClipboardMatcherCallback): void;
68 dangerouslyPasteHTML(html: string, source?: Sources): void;
69 dangerouslyPasteHTML(index: number, html: string, source?: Sources): void;
70}
71
72export interface QuillOptionsStatic {
73 debug?: string | boolean | undefined;
74 modules?: StringMap | undefined;
75 placeholder?: string | undefined;
76 readOnly?: boolean | undefined;
77 theme?: string | undefined;
78 formats?: string[] | undefined;
79 bounds?: HTMLElement | string | undefined;
80 scrollingContainer?: HTMLElement | string | undefined;
81 strict?: boolean | undefined;
82}
83
84export interface BoundsStatic {
85 bottom: number;
86 left: number;
87 right: number;
88 top: number;
89 height: number;
90 width: number;
91}
92
93export interface RangeStatic {
94 index: number;
95 length: number;
96}
97
98export class RangeStatic implements RangeStatic {
99 constructor();
100 index: number;
101 length: number;
102}
103
104export interface History {
105 clear(): void;
106 cutoff(): void;
107 undo(): void;
108 redo(): void;
109}
110
111export interface EventEmitter {
112 on(eventName: "text-change", handler: TextChangeHandler): EventEmitter;
113 on(eventName: "selection-change", handler: SelectionChangeHandler): EventEmitter;
114 on(eventName: "editor-change", handler: EditorChangeHandler): EventEmitter;
115 once(eventName: "text-change", handler: TextChangeHandler): EventEmitter;
116 once(eventName: "selection-change", handler: SelectionChangeHandler): EventEmitter;
117 once(eventName: "editor-change", handler: EditorChangeHandler): EventEmitter;
118 off(eventName: "text-change", handler: TextChangeHandler): EventEmitter;
119 off(eventName: "selection-change", handler: SelectionChangeHandler): EventEmitter;
120 off(eventName: "editor-change", handler: EditorChangeHandler): EventEmitter;
121}
122
123export class Quill implements EventEmitter {
124 /**
125 * Internal API
126 */
127 root: HTMLDivElement;
128 clipboard: ClipboardStatic;
129 scroll: Blot;
130 keyboard: KeyboardStatic;
131 history: History;
132 constructor(container: string | Element, options?: QuillOptionsStatic);
133 deleteText(index: number, length: number, source?: Sources): Delta;
134 disable(): void;
135 enable(enabled?: boolean): void;
136 isEnabled(): boolean;
137 getContents(index?: number, length?: number): Delta;
138 getLength(): number;
139 getText(index?: number, length?: number): string;
140 insertEmbed(index: number, type: string, value: any, source?: Sources): Delta;
141 insertText(index: number, text: string, source?: Sources): Delta;
142 insertText(index: number, text: string, format: string, value: any, source?: Sources): Delta;
143 insertText(index: number, text: string, formats: StringMap, source?: Sources): Delta;
144 /**
145 * @deprecated Remove in 2.0. Use clipboard.dangerouslyPasteHTML(index: number, html: string, source: Sources)
146 */
147 pasteHTML(index: number, html: string, source?: Sources): string;
148 /**
149 * @deprecated Remove in 2.0. Use clipboard.dangerouslyPasteHTML(html: string, source: Sources): void;
150 */
151 pasteHTML(html: string, source?: Sources): string;
152 setContents(delta: Delta, source?: Sources): Delta;
153 setText(text: string, source?: Sources): Delta;
154 update(source?: Sources): void;
155 updateContents(delta: Delta, source?: Sources): Delta;
156
157 static readonly sources: SourceMap;
158
159 format(name: string, value: any, source?: Sources): Delta;
160 formatLine(index: number, length: number, source?: Sources): Delta;
161 formatLine(index: number, length: number, format: string, value: any, source?: Sources): Delta;
162 formatLine(index: number, length: number, formats: StringMap, source?: Sources): Delta;
163 formatText(index: number, length: number, source?: Sources): Delta;
164 formatText(index: number, length: number, format: string, value: any, source?: Sources): Delta;
165 formatText(index: number, length: number, formats: StringMap, source?: Sources): Delta;
166 formatText(range: RangeStatic, format: string, value: any, source?: Sources): Delta;
167 formatText(range: RangeStatic, formats: StringMap, source?: Sources): Delta;
168 getFormat(range?: RangeStatic): StringMap;
169 getFormat(index: number, length?: number): StringMap;
170 removeFormat(index: number, length: number, source?: Sources): Delta;
171
172 blur(): void;
173 focus(): void;
174 getBounds(index: number, length?: number): BoundsStatic;
175 getSelection(focus: true): RangeStatic;
176 getSelection(focus?: false): RangeStatic | null;
177 hasFocus(): boolean;
178 setSelection(index: number, length: number, source?: Sources): void;
179 setSelection(range: RangeStatic, source?: Sources): void;
180
181 // static methods: debug, import, register, find
182 static debug(level: string | boolean): void;
183 static import(path: string): any;
184 static register(path: string, def: any, suppressWarning?: boolean): void;
185 static register(defs: StringMap, suppressWarning?: boolean): void;
186 static find(domNode: Node, bubble?: boolean): Quill | any;
187
188 addContainer(classNameOrDomNode: string | Node, refNode?: Node): any;
189 getModule(name: string): any;
190
191 // Blot interface is not exported on Parchment
192 getIndex(blot: any): number;
193 getLeaf(index: number): any;
194 getLine(index: number): [any, number];
195 getLines(index?: number, length?: number): any[];
196 getLines(range: RangeStatic): any[];
197
198 // EventEmitter methods
199 on(eventName: "text-change", handler: TextChangeHandler): EventEmitter;
200 on(eventName: "selection-change", handler: SelectionChangeHandler): EventEmitter;
201 on(eventName: "editor-change", handler: EditorChangeHandler): EventEmitter;
202 once(eventName: "text-change", handler: TextChangeHandler): EventEmitter;
203 once(eventName: "selection-change", handler: SelectionChangeHandler): EventEmitter;
204 once(eventName: "editor-change", handler: EditorChangeHandler): EventEmitter;
205 off(eventName: "text-change", handler: TextChangeHandler): EventEmitter;
206 off(eventName: "selection-change", handler: SelectionChangeHandler): EventEmitter;
207 off(eventName: "editor-change", handler: EditorChangeHandler): EventEmitter;
208}
209
210export default Quill;