Merge pull request #17270 from icosahebron/quill-fixes

[quill] Update delta- and event-related declarations
This commit is contained in:
Nathan Shively-Sanders
2017-06-20 10:39:41 -07:00
committed by GitHub
3 changed files with 205 additions and 36 deletions

View File

@@ -7,7 +7,21 @@ declare namespace Quill {
type Key = { key: string, shortKey?: boolean };
type Sources = "api" | "user" | "silent";
type Formats = { [key: string]: any };
type StringMap = { [key: string]: any };
type OptionalAttributes = { attributes?: StringMap };
/**
* A stricter type definition would be:
*
* type DeltaOperation ({ insert: any } | { delete: number } | { retain: number }) & OptionalAttributes;
*
* But this would break a lot of existing code as it would require manual discrimination of the union types.
*/
type DeltaOperation = { insert?: any, delete?: number, retain?: number } & OptionalAttributes;
type TextChangeHandler = (delta: DeltaStatic, oldContents: DeltaStatic, source: Sources) => any;
type SelectionChangeHandler = (range: RangeStatic, oldRange: RangeStatic, source: Sources) => any;
type EditorChangeHandler = ((name: "text-change", delta: DeltaStatic, oldContents: DeltaStatic, source: Sources) => any)
| ((name: "selection-change", range: RangeStatic, oldRange: RangeStatic, source: Sources) => any);
export interface KeyboardStatic {
addBinding(key: Key, callback: (range: RangeStatic, context: any) => void): void;
@@ -23,7 +37,7 @@ declare namespace Quill {
export interface QuillOptionsStatic {
debug?: string,
modules?: Formats,
modules?: StringMap,
placeholder?: string,
readOnly?: boolean,
theme?: string,
@@ -38,26 +52,26 @@ declare namespace Quill {
}
export interface DeltaStatic {
new (ops: Array<any>) : DeltaStatic;
new (ops: any) : DeltaStatic;
ops?: Array<any>;
retain(length: number, attributes: any) : DeltaStatic;
new (ops?: DeltaOperation[] | { ops: DeltaOperation[] }) : DeltaStatic;
ops?: DeltaOperation[];
retain(length: number, attributes?: StringMap) : DeltaStatic;
delete(length: number) : DeltaStatic;
filter(predicate: any) : DeltaStatic;
forEach(predicate: any) : DeltaStatic;
insert(text: any, attributes: any): DeltaStatic;
map(predicate: any) : DeltaStatic;
partition(predicate: any) : DeltaStatic;
reduce(predicate: any, initial: number): DeltaStatic;
filter(predicate: (op: DeltaOperation) => boolean) : DeltaOperation[];
forEach(predicate: (op: DeltaOperation) => void) : void;
insert(text: any, attributes?: StringMap): DeltaStatic;
map<T>(predicate: (op: DeltaOperation) => T) : T[];
partition(predicate: (op: DeltaOperation) => boolean) : [DeltaOperation[], DeltaOperation[]];
reduce<T>(predicate: (acc: T, curr: DeltaOperation, idx: number, arr: DeltaOperation[]) => T, initial: T): T;
chop() : DeltaStatic;
length(): number;
slice(start: number, end: number): DeltaStatic;
compose(other: any): DeltaStatic;
slice(start?: number, end?: number): DeltaStatic;
compose(other: DeltaStatic): DeltaStatic;
concat(other: DeltaStatic): DeltaStatic;
diff(other: DeltaStatic, index: number) : DeltaStatic;
eachLine(predicate: any, newline: any) : DeltaStatic;
transform(other: any, priority: any) : DeltaStatic;
transformPosition(index: number, priority: any) : DeltaStatic;
diff(other: DeltaStatic, index?: number) : DeltaStatic;
eachLine(predicate: (line: DeltaStatic, attributes: StringMap, idx: number) => any, newline?: string) : DeltaStatic;
transform(index: number) : number;
transform(other: DeltaStatic, priority: boolean) : DeltaStatic;
transformPosition(index: number) : number;
}
export interface RangeStatic {
@@ -66,7 +80,19 @@ declare namespace Quill {
length: number;
}
export interface Quill {
export interface EventEmitter {
on(eventName: "text-change", handler: TextChangeHandler): EventEmitter;
on(eventName: "selection-change", handler: SelectionChangeHandler): EventEmitter;
on(eventName: "editor-change", handler: EditorChangeHandler): EventEmitter;
once(eventName: "text-change", handler: TextChangeHandler): EventEmitter;
once(eventName: "selection-change", handler: SelectionChangeHandler): EventEmitter;
once(eventName: "editor-change", handler: EditorChangeHandler): EventEmitter;
off(eventName: "text-change", handler: TextChangeHandler): EventEmitter;
off(eventName: "selection-change", handler: SelectionChangeHandler): EventEmitter;
off(eventName: "editor-change", handler: EditorChangeHandler): EventEmitter;
}
export interface Quill extends EventEmitter {
new (container: string | Element, options?: QuillOptionsStatic): Quill;
deleteText(index: number, length: number, source?: Sources): void;
disable(): void;
@@ -77,7 +103,7 @@ declare namespace Quill {
insertEmbed(index: number, type: string, value: any, source?: Sources): void;
insertText(index: number, text: string, source?: Sources): DeltaStatic;
insertText(index: number, text: string, format: string, value: any, source?: Sources): DeltaStatic;
insertText(index: number, text: string, formats: Formats, source?: Sources): DeltaStatic;
insertText(index: number, text: string, formats: StringMap, source?: Sources): DeltaStatic;
/**
* @deprecated Use clipboard.dangerouslyPasteHTML(index: number, html: string, source: Sources)
*/
@@ -94,12 +120,12 @@ declare namespace Quill {
format(name: string, value: any, source?: Sources): DeltaStatic;
formatLine(index: number, length: number, source?: Sources): DeltaStatic;
formatLine(index: number, length: number, format: string, value: any, source?: Sources): DeltaStatic;
formatLine(index: number, length: number, formats: Formats, source?: Sources): DeltaStatic;
formatLine(index: number, length: number, formats: StringMap, source?: Sources): DeltaStatic;
formatText(index: number, length: number, source?: Sources): DeltaStatic;
formatText(index: number, length: number, format: string, value: any, source?: Sources): DeltaStatic;
formatText(index: number, length: number, formats: Formats, source?: Sources): DeltaStatic;
getFormat(range?: RangeStatic): Formats;
getFormat(index: number, length?: number): Formats;
formatText(index: number, length: number, formats: StringMap, source?: Sources): DeltaStatic;
getFormat(range?: RangeStatic): StringMap;
getFormat(index: number, length?: number): StringMap;
removeFormat(index: number, length: number, source?: Sources): void;
blur(): void;
@@ -110,15 +136,10 @@ declare namespace Quill {
setSelection(index: number, length: number, source?: Sources): void;
setSelection(range: RangeStatic, source?: Sources): void;
on(eventName: string, callback: (<T>(delta: T, oldContents: T, source: string) => void) |
((name: string, ...args: any[]) => void)): Quill;
once(eventName: string, callback: (delta: DeltaStatic, source: string) => void): Quill;
off(eventName: string, callback: (delta: DeltaStatic, source: string) => void): Quill;
debug(level: string): void;
import(path: string): any;
register(path: string, def: any, suppressWarning?: boolean): void;
register(defs: Formats, suppressWarning?: boolean): void;
register(defs: StringMap, suppressWarning?: boolean): void;
addContainer(className: string, refNode?: any): any;
addContainer(domNode: any, refNode?: any): any;
getModule(name: string): any

View File

@@ -168,12 +168,22 @@ function test_addContainer()
quillEditor.addContainer('ql-custom');
}
function test_on_EventType1(){
var quillEditor = new Quill('#editor');
quillEditor.on('text-change', <T>(newDelta: T, oldDelta: T, source: string)=>{
// happened
});
function test_on_Events(){
var textChangeHandler = function(newDelta: Quill.DeltaStatic, oldDelta: Quill.DeltaStatic, source: string) { };
var selectionChangeHandler = function(newRange: Quill.RangeStatic, oldRange: Quill.RangeStatic, source: string) { };
var editorChangeHandler = function(name: string, ...args: any[]) { };
var quillEditor = new Quill('#editor');
quillEditor
.on('text-change', textChangeHandler)
.off('text-change', textChangeHandler)
.once('text-change', textChangeHandler)
.on('selection-change', selectionChangeHandler)
.off('selection-change', selectionChangeHandler)
.once('selection-change', selectionChangeHandler)
.on('editor-change', editorChangeHandler)
.off('editor-change', editorChangeHandler)
.once('editor-change', editorChangeHandler);
}
function test_PasteHTML()
@@ -187,3 +197,141 @@ function test_PasteHTML2()
var quillEditor = new Quill('#editor');
quillEditor.pasteHTML(5, '<h1>Quill Rocks</h1>');
}
function test_DeltaChaining() {
var delta = new Delta()
.insert('Hello', { bold: true })
.insert('World')
.delete(5)
.retain(5)
.retain(5, { color: '#0c6' });
}
function test_DeltaFilter() {
var delta = new Delta().insert('Hello', { bold: true })
.insert({ image: 'https://octodex.github.com/images/labtocat.png' })
.insert('World!');
var text = delta.filter(function(op) {
return typeof op.insert === 'string';
}).map(function(op) {
return op.insert;
}).join('');
}
function test_DeltaForEach() {
var delta = new Delta();
delta.forEach(function(op) {
console.log(op);
});
}
function test_DeltaMap() {
var delta = new Delta()
.insert('Hello', { bold: true })
.insert({ image: 'https://octodex.github.com/images/labtocat.png' })
.insert('World!');
var text = delta.map(function(op) {
if (typeof op.insert === 'string') {
return op.insert;
} else {
return '';
}
}).join('');
}
function test_DeltaPartition() {
var delta = new Delta().insert('Hello', { bold: true })
.insert({ image: 'https://octodex.github.com/images/labtocat.png' })
.insert('World!');
var results = delta.partition(function(op) {
return typeof op.insert === 'string';
});
var passed = results[0]; // [{ insert: 'Hello', attributes: { bold: true }}, { insert: 'World'}]
var failed = results[1]; // [{ insert: { image: 'https://octodex.github.com/images/labtocat.png' }}]
}
function test_DeltaReduce() {
var delta = new Delta().insert('Hello', { bold: true })
.insert({ image: 'https://octodex.github.com/images/labtocat.png' })
.insert('World!');
var length = delta.reduce(function(length, op) {
return length + (op.insert.length || 1);
}, 0);
}
function test_DeltaSlice() {
var delta = new Delta().insert('Hello', { bold: true }).insert(' World');
// {
// ops: [
// { insert: 'Hello', attributes: { bold: true } },
// { insert: ' World' }
// ]
// }
var copy = delta.slice();
console.log(copy.ops);
// { ops: [{ insert: 'World' }] }
var world = delta.slice(6);
console.log(world.ops);
// { ops: [{ insert: ' ' }] }
var space = delta.slice(5, 6);
console.log(space.ops);
}
function test_DeltaCompose() {
var a = new Delta().insert('abc');
var b = new Delta().retain(1).delete(1);
var composed = a.compose(b); // composed == new Delta().insert('ac');
}
function test_DeltaDiff() {
var a = new Delta().insert('Hello');
var b = new Delta().insert('Hello!');
var diff = a.diff(b); // { ops: [{ retain: 5 }, { insert: '!' }] }
// a.compose(diff) == b
var diff2 = a.diff(b, 0); // { ops: [{ retain: 5 }, { insert: '!' }] }
// a.compose(diff) == b
}
function test_DeltaEachLine() {
var delta = new Delta().insert('Hello\n\n')
.insert('World')
.insert({ image: 'octocat.png' })
.insert('\n', { align: 'right' })
.insert('!');
delta.eachLine(function(line, attributes, i) {
console.log(line, attributes, i);
// Can return false to exit loop early
});
// Should log:
// { ops: [{ insert: 'Hello' }] }, {}, 0
// { ops: [] }, {}, 1
// { ops: [{ insert: 'World' }, { insert: { image: 'octocat.png' } }] }, { align: 'right' }, 2
// { ops: [{ insert: '!' }] }, {}, 3
}
function test_DeltaTransform() {
var a = new Delta().insert('a');
var b = new Delta().insert('b').retain(5).insert('c');
var d1: Quill.DeltaStatic = a.transform(b, true); // new Delta().retain(1).insert('b').retain(5).insert('c');
var d2: Quill.DeltaStatic = a.transform(b, false); // new Delta().insert('b').retain(6).insert('c');
var n1: number = a.transform(5);
}
function test_DeltatransformPosition() {
var delta = new Delta().retain(5).insert('a');
var n1: number = delta.transformPosition(4); // 4
var n2: number = delta.transformPosition(5); // 6
}

View File

@@ -7,7 +7,7 @@
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": false,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"