Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix function overloads for formatText #4086

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/quill/src/core.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Quill from './core/quill.js';
import Quill, { Parchment, Range } from './core/quill.js';
import type {
DebugLevel,
EmitterSource,
ExpandedQuillOptions,
QuillOptions,
} from './core/quill.js';
Expand All @@ -22,8 +23,8 @@ import Delta, { Op, OpIterator, AttributeMap } from 'quill-delta';
import Input from './modules/input.js';
import UINode from './modules/uiNode.js';

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

Quill.register({
'blots/block': Block,
Expand Down
23 changes: 15 additions & 8 deletions packages/quill/src/core/quill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,13 @@ class Quill {
length: number,
name: string,
value: unknown,
source: EmitterSource,
source?: EmitterSource,
): Delta;
formatText(
index: number,
length: number,
formats: Record<string, unknown>,
source?: EmitterSource,
): Delta;
formatText(
index: number | { index: number; length: number },
Expand Down Expand Up @@ -568,24 +574,24 @@ class Quill {
);
}

insertText(index: number, text: string, source: EmitterSource): Delta;
insertText(index: number, text: string, source?: EmitterSource): Delta;
insertText(
index: number,
text: string,
formats: Record<string, unknown>,
source: EmitterSource,
source?: EmitterSource,
): Delta;
insertText(
index: number,
text: string,
name: string,
value: unknown,
source: EmitterSource,
source?: EmitterSource,
): Delta;
insertText(
index: number,
text: string,
name: string | Record<string, unknown> | EmitterSource,
name?: string | Record<string, unknown> | EmitterSource,
value?: unknown,
source?: EmitterSource,
): Delta {
Expand Down Expand Up @@ -647,8 +653,8 @@ class Quill {
return this.emitter.once(...args);
}

removeFormat(...args: Parameters<typeof overload>) {
const [index, length, , source] = overload(...args);
removeFormat(index: number, length: number, source?: EmitterSource) {
[index, length, , source] = overload(index, length, source);
return modify.call(
this,
() => {
Expand Down Expand Up @@ -1027,6 +1033,7 @@ function shiftRange(
return new Range(start, end - start);
}

export type { DebugLevel };
export type { DebugLevel, EmitterSource };
export { Parchment, Range };

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

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

export type { DebugLevel, ExpandedQuillOptions, QuillOptions };
export type { DebugLevel, EmitterSource, ExpandedQuillOptions, QuillOptions };
export { Parchment, Range };

export default Quill;
211 changes: 211 additions & 0 deletions packages/quill/test/types/quill.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
import { assertType, expectTypeOf } from 'vitest';
import Quill from '../../src/quill.js';
import type { EmitterSource, Parchment, Range } from '../../src/quill.js';
import Delta from 'quill-delta';
import type { default as Block, BlockEmbed } from '../../src/blots/block.js';

const quill = new Quill('#editor');

{
quill.deleteText(0, 1);
quill.deleteText(0, 1, 'api');
quill.deleteText({ index: 0, length: 1 });
quill.deleteText({ index: 0, length: 1 }, 'api');
}

{
assertType<Delta>(quill.getContents());
assertType<Delta>(quill.getContents(1));
assertType<Delta>(quill.getContents(1, 2));
}

{
assertType<number>(quill.getLength());
}

{
assertType<string>(quill.getSemanticHTML());
assertType<string>(quill.getSemanticHTML(1));
assertType<string>(quill.getSemanticHTML(1, 2));
}

{
quill.insertEmbed(10, 'image', 'https://example.com/logo.png');
quill.insertEmbed(10, 'image', 'https://example.com/logo.png', 'api');
}

{
quill.insertText(0, 'Hello');
quill.insertText(0, 'Hello', 'api');
quill.insertText(0, 'Hello', 'bold', true);
quill.insertText(0, 'Hello', 'bold', true, 'api');
quill.insertText(5, 'Quill', {
color: '#ffff00',
italic: true,
});
quill.insertText(
5,
'Quill',
{
color: '#ffff00',
italic: true,
},
'api',
);
}

{
quill.enable();
quill.enable(true);
}

{
quill.disable();
}

{
assertType<boolean>(quill.editReadOnly(() => true));
assertType<string>(quill.editReadOnly(() => 'success'));
}

{
quill.setText('Hello World!');
quill.setText('Hello World!', 'api');
}

{
quill.updateContents([{ insert: 'Hello World!' }]);
quill.updateContents([{ insert: 'Hello World!' }], 'api');
quill.updateContents(new Delta().insert('Hello World!'));
quill.updateContents(new Delta().insert('Hello World!'), 'api');
}

{
quill.setContents([{ insert: 'Hello World!\n' }]);
quill.setContents([{ insert: 'Hello World!\n' }], 'api');
quill.setContents(new Delta().insert('Hello World!\n'));
quill.setContents(new Delta().insert('Hello World!\n'), 'api');
}

{
quill.format('bold', true);
quill.format('bold', true, 'api');
}

{
quill.formatText(0, 1, 'bold', true);
quill.formatText(0, 1, 'bold', true, 'api');
quill.formatText(0, 5, {
bold: false,
color: 'rgb(0, 0, 255)',
});
quill.formatText(
0,
5,
{
bold: false,
color: 'rgb(0, 0, 255)',
},
'api',
);
}

{
quill.formatLine(0, 1, 'bold', true);
quill.formatLine(0, 1, 'bold', true, 'api');
quill.formatLine(0, 5, {
bold: false,
color: 'rgb(0, 0, 255)',
});
quill.formatLine(
0,
5,
{
bold: false,
color: 'rgb(0, 0, 255)',
},
'api',
);
}

{
quill.getFormat();
quill.getFormat(1);
quill.getFormat(1, 10);
quill.getFormat({ index: 1, length: 1 });
}

{
quill.removeFormat(3, 2);
quill.removeFormat(3, 2, 'user');
}

{
quill.getBounds(3, 2);
}

{
quill.getSelection();
quill.getSelection(true);
}

{
quill.setSelection(1, 2);
quill.setSelection(1, 2, 'api');
quill.setSelection({ index: 1, length: 2 });
quill.setSelection({ index: 1, length: 2 }, 'api');
}

{
quill.scrollSelectionIntoView();
}

{
quill.blur();
}

{
quill.focus();
}

{
assertType<boolean>(quill.hasFocus());
}

{
quill.update();
quill.update('user');
}

{
quill.scrollRectIntoView({ left: 0, right: 0, top: 0, bottom: 0 });
}

{
quill.on('text-change', (delta, oldDelta, source) => {
expectTypeOf<Delta>(delta);
expectTypeOf<Delta>(oldDelta);
expectTypeOf<EmitterSource>(source);
});
}

{
quill.on('selection-change', (range, oldRange, source) => {
expectTypeOf<Range>(range);
expectTypeOf<Range>(oldRange);
expectTypeOf<EmitterSource>(source);
});
}

{
assertType<[Parchment.LeafBlot | null, number]>(quill.getLeaf(0));
}

{
assertType<[BlockEmbed | Block | null, number]>(quill.getLine(0));
}

{
assertType<(BlockEmbed | Block)[]>(quill.getLines(0));
assertType<(BlockEmbed | Block)[]>(quill.getLines(0, 10));
}
47 changes: 30 additions & 17 deletions packages/quill/test/unit/core/quill.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,20 +120,36 @@ describe('Quill', () => {
);
});

test('formatText()', () => {
const { quill, oldDelta } = setup();
// @ts-expect-error
quill.formatText(3, 2, 'bold', true);
const change = new Delta().retain(3).retain(2, { bold: true });
expect(quill.root.innerHTML).toMatchInlineSnapshot(
'"<p>012<strong>3<em>4</em></strong><em>5</em>67</p>"',
);
expect(quill.emitter.emit).toHaveBeenCalledWith(
Emitter.events.TEXT_CHANGE,
change,
oldDelta,
Emitter.sources.API,
);
describe('formatText()', () => {
test('single format', () => {
const { quill, oldDelta } = setup();
quill.formatText(3, 2, 'bold', true);
const change = new Delta().retain(3).retain(2, { bold: true });
expect(quill.root.innerHTML).toMatchInlineSnapshot(
'"<p>012<strong>3<em>4</em></strong><em>5</em>67</p>"',
);
expect(quill.emitter.emit).toHaveBeenCalledWith(
Emitter.events.TEXT_CHANGE,
change,
oldDelta,
Emitter.sources.API,
);
});

test('format object', () => {
const { quill, oldDelta } = setup();
quill.formatText(3, 2, { bold: true });
const change = new Delta().retain(3).retain(2, { bold: true });
expect(quill.root.innerHTML).toMatchInlineSnapshot(
'"<p>012<strong>3<em>4</em></strong><em>5</em>67</p>"',
);
expect(quill.emitter.emit).toHaveBeenCalledWith(
Emitter.events.TEXT_CHANGE,
change,
oldDelta,
Emitter.sources.API,
);
});
});

test('insertEmbed()', () => {
Expand All @@ -155,7 +171,6 @@ describe('Quill', () => {

test('insertText()', () => {
const { quill, oldDelta } = setup();
// @ts-expect-error
quill.insertText(5, '|', 'bold', true);
const change = new Delta()
.retain(5)
Expand Down Expand Up @@ -205,7 +220,6 @@ describe('Quill', () => {

test('removeFormat()', () => {
const { quill, oldDelta } = setup();
// @ts-expect-error
quill.removeFormat(5, 1);
const change = new Delta().retain(5).retain(1, { italic: null });
expect(quill.root.innerHTML).toMatchInlineSnapshot(
Expand Down Expand Up @@ -261,7 +275,6 @@ describe('Quill', () => {

test('api text insert', () => {
const { quill, oldDelta } = setup();
// @ts-expect-error
quill.insertText(2, '!');
const delta = new Delta().retain(2).insert('!');
expect(quill.emitter.emit).toHaveBeenCalledWith(
Expand Down
Loading
Loading