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

Migrate remaining files to TypeScript #3631

Merged
merged 21 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from 19 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
23 changes: 14 additions & 9 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"extends": ["airbnb-base", "airbnb-typescript/base", "prettier"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "./tsconfig.json"
},
Expand All @@ -8,33 +13,33 @@
"commonjs": true,
"es6": true
},
"plugins": ["prettier"],
"plugins": ["prettier", "@typescript-eslint"],
"settings": {
"import/resolver": {
"webpack": {
"config": "_develop/webpack.config.js"
}
}
},
"ignorePatterns": ["**/*.d.ts", "**/*.js"],
"rules": {
"arrow-parens": ["error", "as-needed"],
"class-methods-use-this": "off",
"import/no-cycle": "off",
"no-restricted-exports": "off",
"@typescript-eslint/indent": "off",
"@typescript-eslint/lines-between-class-members": "off",
"@typescript-eslint/space-before-function-paren": "off",
"@typescript-eslint/no-use-before-define": "off",
"@typescript-eslint/no-this-alias": "off",
"@typescript-eslint/quotes": "off",
"@typescript-eslint/comma-dangle": "off",
"import/no-extraneous-dependencies": [
"error",
{
"devDependencies": ["_develop/*.js", "test/**/*.js"]
}
],
"@typescript-eslint/ban-ts-comment": "off",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/ban-types": "off",
"@typescript-eslint/no-explicit-any": "off",
"no-param-reassign": "off",
"no-use-before-define": ["error", { "functions": false, "classes": false }],
"import/named": "error",
"max-classes-per-file": "off",
"prettier/prettier": "error"
}
Expand Down
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,16 @@
/docs/_site
/src
*.d.ts
*.js.map
*.log

blots/*.js
core/*.js
formats/*.js
modules/*.js
themes/*.js
ui/*.js
test/random.js

core.js
quill.js
10 changes: 10 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
*.ts
!*.d.ts
.*
Gemfile
Gemfile.lock
_develop
.github
.vscode
docs
test
6 changes: 3 additions & 3 deletions blots/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ const NEWLINE_LENGTH = 1;
class Block extends BlockBlot {
cache: { delta?: Delta | null; length?: number } = {};

delta() {
delta(): Delta {
if (this.cache.delta == null) {
this.cache.delta = blockDelta(this);
}
return this.cache.delta;
}

deleteAt(index, length) {
deleteAt(index: number, length: number) {
super.deleteAt(index, length);
this.cache = {};
}
Expand Down Expand Up @@ -153,7 +153,7 @@ class BlockEmbed extends EmbedBlot {
this.format(name, value);
}

insertAt(index, value, def) {
insertAt(index: number, value: string, def?: unknown) {
if (typeof value === 'string' && value.endsWith('\n')) {
const block = this.scroll.create(Block.blotName);
this.parent.insertBefore(block, index === 0 ? this : this.next);
Expand Down
7 changes: 6 additions & 1 deletion blots/scroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ interface UpdatableEmbed {
}

function isUpdatable(blot: Blot): blot is Blot & UpdatableEmbed {
return typeof ((blot as unknown) as any).updateContent === 'function';
return typeof (blot as unknown as any).updateContent === 'function';
}

class Scroll extends ScrollBlot {
Expand Down Expand Up @@ -181,6 +181,11 @@ class Scroll extends ScrollBlot {
return getLines(this, index, length);
}

optimize(context: { [key: string]: any }): void;
optimize(
mutations?: MutationRecord[],
context?: { [key: string]: any },
): void;
optimize(mutations = [], context = {}) {
if (this.batch) return;
super.optimize(mutations, context);
Expand Down
3 changes: 3 additions & 0 deletions core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import Clipboard from './modules/clipboard';
import History from './modules/history';
import Keyboard from './modules/keyboard';
import Uploader from './modules/uploader';
import Delta, { Op, OpIterator, AttributeMap } from 'quill-delta';

export { Delta, Op, OpIterator, AttributeMap };
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Export quill-delta so users don't need to install quill-delta on their side. This is useful to prevent multiple versions of quill-delta coexist in a project and Delta.registerEmbed() being called on a wrong Delta instance.

An alternative is to get Delta from Quill.import('delta') but due to a TypeScript limit, users have to use typeof Delta instead of Delta when referring the type.


Quill.register({
'blots/block': Block,
Expand Down
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
7 changes: 4 additions & 3 deletions core/emitter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as EventEmitter from 'eventemitter3';
import EventEmitter from 'eventemitter3';
import instances from './instances';
import logger from './logger';

Expand Down Expand Up @@ -27,7 +27,7 @@ class Emitter extends EventEmitter<string> {
SCROLL_EMBED_UPDATE: 'scroll-embed-update',
SELECTION_CHANGE: 'selection-change',
TEXT_CHANGE: 'text-change',
};
} as const;

static sources = {
API: 'api',
Expand Down Expand Up @@ -66,6 +66,7 @@ class Emitter extends EventEmitter<string> {
}
}

export type EmitterSource = typeof Emitter.sources[keyof typeof Emitter.sources];
export type EmitterSource =
typeof Emitter.sources[keyof typeof Emitter.sources];

export default Emitter;
4 changes: 2 additions & 2 deletions core/module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Quill from './quill';

class Module {
abstract class Module<T extends {} = {}> {
static DEFAULTS = {};

constructor(protected quill: Quill, protected options = {}) {}
constructor(protected quill: Quill, protected options: Partial<T> = {}) {}
}

export default Module;
Loading