UNPKG

8.74 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 = { insert?: any; delete?: number | undefined; retain?: number | undefined } & OptionalAttributes;
24interface SourceMap {
25 API: "api";
26 SILENT: "silent";
27 USER: "user";
28}
29export type Sources = "api" | "user" | "silent";
30
31export interface Key {
32 key: string | number;
33 shortKey?: boolean | null | undefined;
34 shiftKey?: boolean | null | undefined;
35 altKey?: boolean | null | undefined;
36 metaKey?: boolean | null | undefined;
37 ctrlKey?: boolean | null | undefined;
38}
39
40export interface StringMap {
41 [key: string]: any;
42}
43
44export interface OptionalAttributes {
45 attributes?: StringMap | undefined;
46}
47
48export type TextChangeHandler = (delta: Delta, oldContents: Delta, source: Sources) => any;
49export type SelectionChangeHandler = (range: RangeStatic, oldRange: RangeStatic, source: Sources) => any;
50export type EditorChangeHandler =
51 | ((name: "text-change", delta: Delta, oldContents: Delta, source: Sources) => any)
52 | ((name: "selection-change", range: RangeStatic, oldRange: RangeStatic, source: Sources) => any);
53
54export interface KeyboardStatic {
55 addBinding(key: Key, callback: (range: RangeStatic, context: any) => void): void;
56 addBinding(key: Key, context: any, callback: (range: RangeStatic, context: any) => void): void;
57}
58
59export type ClipboardMatcherCallback = (node: any, delta: Delta) => Delta;
60export type ClipboardMatcherNode = string | number;
61
62export interface ClipboardStatic {
63 matchers: Array<[ClipboardMatcherNode, ClipboardMatcherCallback]>;
64 convert(content?: { html?: string | undefined; text?: string | undefined }, formats?: StringMap): Delta;
65 addMatcher(selectorOrNodeType: ClipboardMatcherNode, callback: ClipboardMatcherCallback): void;
66 dangerouslyPasteHTML(html: string, source?: Sources): void;
67 dangerouslyPasteHTML(index: number, html: string, source?: Sources): void;
68}
69
70export interface QuillOptionsStatic {
71 debug?: string | boolean | undefined;
72 modules?: StringMap | undefined;
73 placeholder?: string | undefined;
74 readOnly?: boolean | undefined;
75 theme?: string | undefined;
76 formats?: string[] | undefined;
77 bounds?: HTMLElement | string | undefined;
78 scrollingContainer?: HTMLElement | string | undefined;
79 strict?: boolean | undefined;
80}
81
82export interface BoundsStatic {
83 bottom: number;
84 left: number;
85 right: number;
86 top: number;
87 height: number;
88 width: number;
89}
90
91export interface RangeStatic {
92 index: number;
93 length: number;
94}
95
96export class RangeStatic implements RangeStatic {
97 constructor();
98 index: number;
99 length: number;
100}
101
102export interface EventEmitter {
103 on(eventName: "text-change", handler: TextChangeHandler): EventEmitter;
104 on(eventName: "selection-change", handler: SelectionChangeHandler): EventEmitter;
105 on(eventName: "editor-change", handler: EditorChangeHandler): EventEmitter;
106 once(eventName: "text-change", handler: TextChangeHandler): EventEmitter;
107 once(eventName: "selection-change", handler: SelectionChangeHandler): EventEmitter;
108 once(eventName: "editor-change", handler: EditorChangeHandler): EventEmitter;
109 off(eventName: "text-change", handler: TextChangeHandler): EventEmitter;
110 off(eventName: "selection-change", handler: SelectionChangeHandler): EventEmitter;
111 off(eventName: "editor-change", handler: EditorChangeHandler): EventEmitter;
112}
113
114export class Quill implements EventEmitter {
115 /**
116 * Internal API
117 */
118 root: HTMLDivElement;
119 clipboard: ClipboardStatic;
120 scroll: Blot;
121 keyboard: KeyboardStatic;
122 constructor(container: string | Element, options?: QuillOptionsStatic);
123 deleteText(index: number, length: number, source?: Sources): Delta;
124 disable(): void;
125 enable(enabled?: boolean): void;
126 isEnabled(): boolean;
127 getContents(index?: number, length?: number): Delta;
128 getLength(): number;
129 getText(index?: number, length?: number): string;
130 insertEmbed(index: number, type: string, value: any, source?: Sources): Delta;
131 insertText(index: number, text: string, source?: Sources): Delta;
132 insertText(index: number, text: string, format: string, value: any, source?: Sources): Delta;
133 insertText(index: number, text: string, formats: StringMap, source?: Sources): Delta;
134 /**
135 * @deprecated Remove in 2.0. Use clipboard.dangerouslyPasteHTML(index: number, html: string, source: Sources)
136 */
137 pasteHTML(index: number, html: string, source?: Sources): string;
138 /**
139 * @deprecated Remove in 2.0. Use clipboard.dangerouslyPasteHTML(html: string, source: Sources): void;
140 */
141 pasteHTML(html: string, source?: Sources): string;
142 setContents(delta: Delta, source?: Sources): Delta;
143 setText(text: string, source?: Sources): Delta;
144 update(source?: Sources): void;
145 updateContents(delta: Delta, source?: Sources): Delta;
146
147 static readonly sources: SourceMap;
148
149 format(name: string, value: any, source?: Sources): Delta;
150 formatLine(index: number, length: number, source?: Sources): Delta;
151 formatLine(index: number, length: number, format: string, value: any, source?: Sources): Delta;
152 formatLine(index: number, length: number, formats: StringMap, source?: Sources): Delta;
153 formatText(index: number, length: number, source?: Sources): Delta;
154 formatText(index: number, length: number, format: string, value: any, source?: Sources): Delta;
155 formatText(index: number, length: number, formats: StringMap, source?: Sources): Delta;
156 formatText(range: RangeStatic, format: string, value: any, source?: Sources): Delta;
157 formatText(range: RangeStatic, formats: StringMap, source?: Sources): Delta;
158 getFormat(range?: RangeStatic): StringMap;
159 getFormat(index: number, length?: number): StringMap;
160 removeFormat(index: number, length: number, source?: Sources): Delta;
161
162 blur(): void;
163 focus(): void;
164 getBounds(index: number, length?: number): BoundsStatic;
165 getSelection(focus: true): RangeStatic;
166 getSelection(focus?: false): RangeStatic | null;
167 hasFocus(): boolean;
168 setSelection(index: number, length: number, source?: Sources): void;
169 setSelection(range: RangeStatic, source?: Sources): void;
170
171 // static methods: debug, import, register, find
172 static debug(level: string | boolean): void;
173 static import(path: string): any;
174 static register(path: string, def: any, suppressWarning?: boolean): void;
175 static register(defs: StringMap, suppressWarning?: boolean): void;
176 static find(domNode: Node, bubble?: boolean): Quill | any;
177
178 addContainer(classNameOrDomNode: string | Node, refNode?: Node): any;
179 getModule(name: string): any;
180
181 // Blot interface is not exported on Parchment
182 getIndex(blot: any): number;
183 getLeaf(index: number): any;
184 getLine(index: number): [any, number];
185 getLines(index?: number, length?: number): any[];
186 getLines(range: RangeStatic): any[];
187
188 // EventEmitter methods
189 on(eventName: "text-change", handler: TextChangeHandler): EventEmitter;
190 on(eventName: "selection-change", handler: SelectionChangeHandler): EventEmitter;
191 on(eventName: "editor-change", handler: EditorChangeHandler): EventEmitter;
192 once(eventName: "text-change", handler: TextChangeHandler): EventEmitter;
193 once(eventName: "selection-change", handler: SelectionChangeHandler): EventEmitter;
194 once(eventName: "editor-change", handler: EditorChangeHandler): EventEmitter;
195 off(eventName: "text-change", handler: TextChangeHandler): EventEmitter;
196 off(eventName: "selection-change", handler: SelectionChangeHandler): EventEmitter;
197 off(eventName: "editor-change", handler: EditorChangeHandler): EventEmitter;
198}
199
200export default Quill;