diff --git a/types/quill/index.d.ts b/types/quill/index.d.ts index cf7440f428..f8ee7c5d1f 100644 --- a/types/quill/index.d.ts +++ b/types/quill/index.d.ts @@ -7,7 +7,16 @@ 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 = StringMap & OptionalAttributes; export interface KeyboardStatic { addBinding(key: Key, callback: (range: RangeStatic, context: any) => void): void; @@ -23,7 +32,7 @@ declare namespace Quill { export interface QuillOptionsStatic { debug?: string, - modules?: Formats, + modules?: StringMap, placeholder?: string, readOnly?: boolean, theme?: string, @@ -38,26 +47,26 @@ declare namespace Quill { } export interface DeltaStatic { - new (ops: Array) : DeltaStatic; - new (ops: any) : DeltaStatic; - ops?: Array; - 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(predicate: (op: DeltaOperation) => T) : T[]; + partition(predicate: (op: DeltaOperation) => boolean) : [DeltaOperation[], DeltaOperation[]]; + reduce(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) : DeltaStatic; + transform(other: DeltaStatic, priority: boolean) : DeltaStatic; + transformPosition(index: number) : DeltaStatic; } export interface RangeStatic { @@ -77,7 +86,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 +103,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; @@ -118,7 +127,7 @@ declare namespace 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 diff --git a/types/quill/quill-tests.ts b/types/quill/quill-tests.ts index eba77f8b68..dc7800d444 100644 --- a/types/quill/quill-tests.ts +++ b/types/quill/quill-tests.ts @@ -187,3 +187,140 @@ function test_PasteHTML2() var quillEditor = new Quill('#editor'); quillEditor.pasteHTML(5, '

Quill Rocks

'); } + +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'); + + a.transform(b, true); // new Delta().retain(1).insert('b').retain(5).insert('c'); + a.transform(b, false); // new Delta().insert('b').retain(6).insert('c'); + a.transform(5); +} + +function test_DeltatransformPosition() { + var delta = new Delta().retain(5).insert('a'); + delta.transformPosition(4); // 4 + delta.transformPosition(5); // 6 +}