Skip to content

Commit

Permalink
Misc improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
luin committed Aug 16, 2022
1 parent ac9c20d commit 78f5e99
Show file tree
Hide file tree
Showing 17 changed files with 800 additions and 625 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ core/*.js
formats/*.js
modules/*.js
themes/*.js
ui/*.js

core.js
quill.js
40 changes: 26 additions & 14 deletions core/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Editor {
this.delta = this.getDelta();
}

applyDelta(delta: Delta) {
applyDelta(delta: Delta): Delta {
this.scroll.update();
let scrollLength = this.scroll.length();
this.scroll.batchStart();
Expand Down Expand Up @@ -91,12 +91,16 @@ class Editor {
return this.update(normalizedDelta);
}

deleteText(index, length) {
deleteText(index: number, length: number): Delta {
this.scroll.deleteAt(index, length);
return this.update(new Delta().retain(index).delete(length));
}

formatLine(index, length, formats = {}) {
formatLine(
index: number,
length: number,
formats: Record<string, unknown> = {},
): Delta {
this.scroll.update();
Object.keys(formats).forEach(format => {
this.scroll.lines(index, Math.max(length, 1)).forEach(line => {
Expand All @@ -108,25 +112,29 @@ class Editor {
return this.update(delta);
}

formatText(index, length, formats = {}) {
formatText(
index: number,
length: number,
formats: Record<string, unknown> = {},
): Delta {
Object.keys(formats).forEach(format => {
this.scroll.formatAt(index, length, format, formats[format]);
});
const delta = new Delta().retain(index).retain(length, cloneDeep(formats));
return this.update(delta);
}

getContents(index, length) {
getContents(index: number, length: number): Delta {
return this.delta.slice(index, index + length);
}

getDelta() {
getDelta(): Delta {
return this.scroll.lines().reduce((delta, line) => {
return delta.concat(line.delta());
}, new Delta());
}

getFormat(index, length = 0) {
getFormat(index: number, length = 0): Record<string, unknown> {
let lines = [];
let leaves = [];
if (length === 0) {
Expand Down Expand Up @@ -156,27 +164,31 @@ class Editor {
return { ...lines, ...leaves };
}

getHTML(index, length) {
getHTML(index: number, length: number): string {
const [line, lineOffset] = this.scroll.line(index);
if (line.length() >= lineOffset + length) {
return convertHTML(line, lineOffset, length, true);
}
return convertHTML(this.scroll, index, length, true);
}

getText(index, length) {
getText(index: number, length: number): string {
return this.getContents(index, length)
.filter(op => typeof op.insert === 'string')
.map(op => op.insert)
.join('');
}

insertEmbed(index, embed, value) {
insertEmbed(index: number, embed: string, value: unknown): Delta {
this.scroll.insertAt(index, embed, value);
return this.update(new Delta().retain(index).insert({ [embed]: value }));
}

insertText(index, text, formats = {}) {
insertText(
index: number,
text: string,
formats: Record<string, unknown> = {},
): Delta {
text = text.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
this.scroll.insertAt(index, text);
Object.keys(formats).forEach(format => {
Expand All @@ -187,7 +199,7 @@ class Editor {
);
}

isBlank() {
isBlank(): boolean {
if (this.scroll.children.length === 0) return true;
if (this.scroll.children.length > 1) return false;
const blot = this.scroll.children.head;
Expand All @@ -197,7 +209,7 @@ class Editor {
return block.children.head instanceof Break;
}

removeFormat(index, length) {
removeFormat(index: number, length: number): Delta {
const text = this.getText(index, length);
const [line, offset] = this.scroll.line(index + length);
let suffixLength = 0;
Expand All @@ -215,7 +227,7 @@ class Editor {
return this.applyDelta(delta);
}

update(change, mutations = [], selectionInfo = undefined) {
update(change: Delta, mutations = [], selectionInfo = undefined): Delta {
const oldDelta = this.delta;
if (
mutations.length === 1 &&
Expand Down
34 changes: 19 additions & 15 deletions core/quill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ class Quill {
return modify.call(
this,
() => {
// @ts-expect-error
return this.editor.deleteText(index, length);
},
source,
Expand Down Expand Up @@ -394,6 +395,7 @@ class Quill {
return modify.call(
this,
() => {
// @ts-expect-error
return this.editor.formatText(index, length, formats);
},
source,
Expand Down Expand Up @@ -559,19 +561,19 @@ class Quill {
return this.scroll.isEnabled();
}

off(event: string, ...args: unknown[]) {
// @ts-expect-error
return this.emitter.off(event, ...args);
off(...args: Parameters<typeof Emitter['prototype']['off']>) {
return this.emitter.off(...args);
}

on(
event: typeof Emitter['events']['TEXT_CHANGE'],
handler: (delta: Delta, oldContent: Delta, source: EmitterSource) => void,
): this;
): Emitter;
on(
event: typeof Emitter['events']['SELECTION_CHANGE'],
handler: (range: Range, oldRange: Range, source: EmitterSource) => void,
): this;
): Emitter;
// @ts-expect-error
on(
event: typeof Emitter['events']['EDITOR_CHANGE'],
handler: (
Expand All @@ -584,16 +586,14 @@ class Quill {
EmitterSource,
]
) => void,
): this;
on(event: string, ...args: unknown[]): this;
on(event: string, ...args: unknown[]): this {
// @ts-expect-error
return this.emitter.on(event, ...args);
): Emitter;
on(event: string, ...args: unknown[]): Emitter;
on(...args: Parameters<typeof Emitter['prototype']['on']>): Emitter {
return this.emitter.on(...args);
}

once(event: string, ...args: unknown[]) {
// @ts-expect-error
return this.emitter.once(event, ...args);
once(...args: Parameters<typeof Emitter['prototype']['once']>) {
return this.emitter.once(...args);
}

removeFormat(...args: Parameters<typeof overload>) {
Expand All @@ -612,7 +612,10 @@ class Quill {
this.selection.scrollIntoView(this.scrollingContainer);
}

setContents(delta: Delta | Op[], source = Emitter.sources.API) {
setContents(
delta: Delta | Op[],
source: EmitterSource = Emitter.sources.API,
) {
return modify.call(
this,
() => {
Expand All @@ -630,6 +633,7 @@ class Quill {
);
}
setSelection(range: Range | null, source?: EmitterSource): void;
setSelection(index: number, source?: EmitterSource): void;
setSelection(index: number, length?: number, source?: EmitterSource): void;
setSelection(index: number, source?: EmitterSource): void;
setSelection(
Expand All @@ -650,7 +654,7 @@ class Quill {
}
}

setText(text, source = Emitter.sources.API) {
setText(text: string, source: EmitterSource = Emitter.sources.API) {
const delta = new Delta().insert(text);
return this.setContents(delta, source);
}
Expand Down
3 changes: 1 addition & 2 deletions modules/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class Clipboard extends Module<ClipboardOptions> {
const delta = this.convert({ html: index, text: '' });
// @ts-expect-error
this.quill.setContents(delta, html);
this.quill.setSelection(0, 0, Quill.sources.SILENT);
this.quill.setSelection(0, Quill.sources.SILENT);
} else {
const paste = this.convert({ html, text: '' });
this.quill.updateContents(
Expand Down Expand Up @@ -207,7 +207,6 @@ class Clipboard extends Module<ClipboardOptions> {
// range.length contributes to delta.length()
this.quill.setSelection(
delta.length() - range.length,
0,
Quill.sources.SILENT,
);
this.quill.scrollIntoView();
Expand Down
2 changes: 1 addition & 1 deletion modules/keyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class Keyboard extends Module<KeyboardOptions> {
return binding.key === evt.key || binding.key === evt.which;
}

bindings: Record<string, NormalizedBinding[]> = {};
bindings: Record<string, NormalizedBinding[]>;

constructor(quill: Quill, options: Partial<KeyboardOptions>) {
super(quill, options);
Expand Down
2 changes: 1 addition & 1 deletion modules/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
tableId,
} from '../formats/table';

class Table extends Module<{}> {
class Table extends Module {
static register() {
Quill.register(TableCell);
Quill.register(TableRow);
Expand Down
12 changes: 3 additions & 9 deletions modules/tableEmbed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ export type CellData = {
export interface TableData {
rows?: Delta['ops'];
columns?: Delta['ops'];
cells?: {
[identity: string]: CellData;
};
cells?: Record<string, CellData>;
}

const parseCellIdentity = (identity: string) => {
Expand Down Expand Up @@ -61,11 +59,7 @@ const compactCellData = ({ content, attributes }) => {
};

const compactTableData = ({ rows, columns, cells }) => {
const data: {
rows?: Delta['ops'];
columns?: Delta['ops'];
cells?: Record<string, CellData>;
} = {};
const data: TableData = {};
if (rows.length() > 0) {
data.rows = rows.ops;
}
Expand Down Expand Up @@ -231,7 +225,7 @@ export const tableHandler = {
},
};

class TableEmbed extends Module<{}> {
class TableEmbed extends Module {
static register() {
Delta.registerEmbed('table-embed', tableHandler);
}
Expand Down
Loading

0 comments on commit 78f5e99

Please sign in to comment.