Skip to content

Commit

Permalink
Upgrade prettier and enforce styles
Browse files Browse the repository at this point in the history
  • Loading branch information
luin committed Aug 13, 2023
1 parent 577ebbf commit 636ec74
Show file tree
Hide file tree
Showing 44 changed files with 910 additions and 331 deletions.
11 changes: 1 addition & 10 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,12 @@
}
},
"ignorePatterns": ["website/**/*", "*.js"],
"rules": {
"arrow-parens": ["error", "as-needed"],
"no-use-before-define": ["error", { "functions": false, "classes": false }],
"prettier/prettier": "error"
},
"overrides": [
{
"files": ["test/helpers/**/*.js", "test/unit/**/*.js"],
"plugins": ["prettier"]
},
{
"files": ["**/*.ts"],
"extends": ["plugin:@typescript-eslint/recommended"],
"excludedFiles": "*.d.ts",
"plugins": ["prettier", "@typescript-eslint"],
"plugins": ["@typescript-eslint"],
"rules": {
"@typescript-eslint/consistent-type-imports": "error",
"@typescript-eslint/ban-ts-comment": "off",
Expand Down
4 changes: 2 additions & 2 deletions blots/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,13 @@ class BlockEmbed extends EmbedBlot {
}
const lines = value.split('\n');
const text = lines.pop();
const blocks = lines.map(line => {
const blocks = lines.map((line) => {
const block = this.scroll.create(Block.blotName);
block.insertAt(0, line);
return block;
});
const ref = this.split(index);
blocks.forEach(block => {
blocks.forEach((block) => {
// @ts-expect-error Fix me later
this.parent.insertBefore(block, ref);
});
Expand Down
2 changes: 1 addition & 1 deletion blots/cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class Cursor extends EmbedBlot {

update(mutations: MutationRecord[], context: Record<string, unknown>) {
if (
mutations.some(mutation => {
mutations.some((mutation) => {
return (
mutation.type === 'characterData' && mutation.target === this.textNode
);
Expand Down
4 changes: 2 additions & 2 deletions blots/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Embed extends EmbedBlot {
super(scroll, node);
this.contentNode = document.createElement('span');
this.contentNode.setAttribute('contenteditable', 'false');
Array.from(this.domNode.childNodes).forEach(childNode => {
Array.from(this.domNode.childNodes).forEach((childNode) => {
this.contentNode.appendChild(childNode);
});
this.leftGuard = document.createTextNode(GUARD_TEXT);
Expand Down Expand Up @@ -78,7 +78,7 @@ class Embed extends EmbedBlot {
}

update(mutations: MutationRecord[], context: Record<string, unknown>) {
mutations.forEach(mutation => {
mutations.forEach((mutation) => {
if (
mutation.type === 'characterData' &&
(mutation.target === this.leftGuard ||
Expand Down
14 changes: 7 additions & 7 deletions blots/scroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Scroll extends ScrollBlot {
this.batch = false;
this.optimize();
this.enable();
this.domNode.addEventListener('dragstart', e => this.handleDragStart(e));
this.domNode.addEventListener('dragstart', (e) => this.handleDragStart(e));
}

batchStart() {
Expand Down Expand Up @@ -165,7 +165,7 @@ class Scroll extends ScrollBlot {

const formats = bubbleFormats(this.line(index)[0]);
const attributes = AttributeMap.diff(formats, first.attributes) || {};
Object.keys(attributes).forEach(name => {
Object.keys(attributes).forEach((name) => {
this.formatAt(lineEndIndex - 1, 1, name, attributes[name]);
});

Expand All @@ -179,7 +179,7 @@ class Scroll extends ScrollBlot {
refBlotOffset = 0;
}

renderBlocks.forEach(renderBlock => {
renderBlocks.forEach((renderBlock) => {
if (renderBlock.type === 'block') {
const block = this.createBlock(
renderBlock.attributes,
Expand All @@ -192,7 +192,7 @@ class Scroll extends ScrollBlot {
renderBlock.value,
) as EmbedBlot;
this.insertBefore(blockEmbed, refBlot || undefined);
Object.keys(renderBlock.attributes).forEach(name => {
Object.keys(renderBlock.attributes).forEach((name) => {
blockEmbed.format(name, renderBlock.attributes[name]);
});
}
Expand Down Expand Up @@ -324,12 +324,12 @@ class Scroll extends ScrollBlot {
const renderBlocks: RenderBlock[] = [];

let currentBlockDelta = new Delta();
delta.forEach(op => {
delta.forEach((op) => {
const insert = op?.insert;
if (!insert) return;
if (typeof insert === 'string') {
const splitted = insert.split('\n');
splitted.slice(0, -1).forEach(text => {
splitted.slice(0, -1).forEach((text) => {
currentBlockDelta.insert(text, op.attributes);
renderBlocks.push({
type: 'block',
Expand Down Expand Up @@ -433,7 +433,7 @@ function insertInlineContents(
}
}
}
Object.keys(attributes).forEach(key => {
Object.keys(attributes).forEach((key) => {
parent.formatAt(index, length, key, attributes[key]);
});
return index + length;
Expand Down
2 changes: 1 addition & 1 deletion blots/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { TextBlot } from 'parchment';
class Text extends TextBlot {}

function escapeText(text: string) {
return text.replace(/[&<>"']/g, s => {
return text.replace(/[&<>"']/g, (s) => {
// https://lodash.com/docs#escape
const entityMap: Record<string, string> = {
'&': '&amp;',
Expand Down
9 changes: 6 additions & 3 deletions core/composition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ import Emitter from './emitter';
class Composition {
isComposing = false;

constructor(private scroll: Scroll, private emitter: Emitter) {
constructor(
private scroll: Scroll,
private emitter: Emitter,
) {
this.setupListeners();
}

private setupListeners() {
this.scroll.domNode.addEventListener('compositionstart', event => {
this.scroll.domNode.addEventListener('compositionstart', (event) => {
if (!this.isComposing) {
this.handleCompositionStart(event);
}
});

this.scroll.domNode.addEventListener('compositionend', event => {
this.scroll.domNode.addEventListener('compositionend', (event) => {
if (this.isComposing) {
this.handleCompositionEnd(event);
}
Expand Down
53 changes: 28 additions & 25 deletions core/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class Editor {
this.scroll.updateEmbedAt(index, key, op.retain[key]);
}
}
Object.keys(attributes).forEach(name => {
Object.keys(attributes).forEach((name) => {
this.scroll.formatAt(index, length, name, attributes[name]);
});
const prependedLength = isImplicitNewlinePrepended ? 1 : 0;
Expand Down Expand Up @@ -135,8 +135,8 @@ class Editor {
formats: Record<string, unknown> = {},
): Delta {
this.scroll.update();
Object.keys(formats).forEach(format => {
this.scroll.lines(index, Math.max(length, 1)).forEach(line => {
Object.keys(formats).forEach((format) => {
this.scroll.lines(index, Math.max(length, 1)).forEach((line) => {
line.format(format, formats[format]);
});
});
Expand All @@ -150,7 +150,7 @@ class Editor {
length: number,
formats: Record<string, unknown> = {},
): Delta {
Object.keys(formats).forEach(format => {
Object.keys(formats).forEach((format) => {
this.scroll.formatAt(index, length, format, formats[format]);
});
const delta = new Delta().retain(index).retain(length, cloneDeep(formats));
Expand All @@ -171,7 +171,7 @@ class Editor {
let lines: (Block | BlockEmbed)[] = [];
let leaves: LeafBlot[] = [];
if (length === 0) {
this.scroll.path(index).forEach(path => {
this.scroll.path(index).forEach((path) => {
const [blot] = path;
if (blot instanceof Block) {
lines.push(blot);
Expand All @@ -183,7 +183,7 @@ class Editor {
lines = this.scroll.lines(index, length);
leaves = this.scroll.descendants(LeafBlot, index, length);
}
const [lineFormats, leafFormats] = [lines, leaves].map(blots => {
const [lineFormats, leafFormats] = [lines, leaves].map((blots) => {
const blot = blots.shift();
if (blot == null) return {};
let formats = bubbleFormats(blot);
Expand Down Expand Up @@ -212,8 +212,8 @@ class Editor {

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

Expand All @@ -236,7 +236,7 @@ class Editor {
): Delta {
text = text.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
this.scroll.insertAt(index, text);
Object.keys(formats).forEach(format => {
Object.keys(formats).forEach((format) => {
this.scroll.formatAt(index, text.length, format, formats[format]);
});
return this.update(
Expand Down Expand Up @@ -415,23 +415,26 @@ function combineFormats(
formats: Record<string, unknown>,
combined: Record<string, unknown>,
): Record<string, unknown> {
return Object.keys(combined).reduce((merged, name) => {
if (formats[name] == null) return merged;
const combinedValue = combined[name];
if (combinedValue === formats[name]) {
merged[name] = combinedValue;
} else if (Array.isArray(combinedValue)) {
if (combinedValue.indexOf(formats[name]) < 0) {
merged[name] = combinedValue.concat([formats[name]]);
} else {
// If style already exists, don't add to an array, but don't lose other styles
return Object.keys(combined).reduce(
(merged, name) => {
if (formats[name] == null) return merged;
const combinedValue = combined[name];
if (combinedValue === formats[name]) {
merged[name] = combinedValue;
} else if (Array.isArray(combinedValue)) {
if (combinedValue.indexOf(formats[name]) < 0) {
merged[name] = combinedValue.concat([formats[name]]);
} else {
// If style already exists, don't add to an array, but don't lose other styles
merged[name] = combinedValue;
}
} else {
merged[name] = [combinedValue, formats[name]];
}
} else {
merged[name] = [combinedValue, formats[name]];
}
return merged;
}, {} as Record<string, unknown>);
return merged;
},
{} as Record<string, unknown>,
);
}

function getListType(type: string | undefined) {
Expand Down Expand Up @@ -462,7 +465,7 @@ function shiftRange({ index, length }: Range, amount: number) {

function splitOpLines(ops: Op[]) {
const split: Op[] = [];
ops.forEach(op => {
ops.forEach((op) => {
if (typeof op.insert === 'string') {
const lines = op.insert.split('\n');
lines.forEach((line, index) => {
Expand Down
6 changes: 3 additions & 3 deletions core/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import logger from './logger';
const debug = logger('quill:events');
const EVENTS = ['selectionchange', 'mousedown', 'mouseup', 'click'];

EVENTS.forEach(eventName => {
EVENTS.forEach((eventName) => {
document.addEventListener(eventName, (...args) => {
Array.from(document.querySelectorAll('.ql-container')).forEach(node => {
Array.from(document.querySelectorAll('.ql-container')).forEach((node) => {
const quill = instances.get(node);
if (quill && quill.emitter) {
quill.emitter.handleDOM(...args);
Expand Down Expand Up @@ -71,6 +71,6 @@ class Emitter extends EventEmitter<string> {
}

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

export default Emitter;
13 changes: 8 additions & 5 deletions core/logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const levels = ['error', 'warn', 'log', 'info'] as const;
export type DebugLevel = typeof levels[number];
export type DebugLevel = (typeof levels)[number];
let level: DebugLevel | false = 'warn';

function debug(method: DebugLevel, ...args: unknown[]) {
Expand All @@ -13,10 +13,13 @@ function debug(method: DebugLevel, ...args: unknown[]) {
function namespace(
ns: string,
): Record<DebugLevel, (...args: unknown[]) => void> {
return levels.reduce((logger, method) => {
logger[method] = debug.bind(console, method, ns);
return logger;
}, {} as Record<DebugLevel, (...args: unknown[]) => void>);
return levels.reduce(
(logger, method) => {
logger[method] = debug.bind(console, method, ns);
return logger;
},
{} as Record<DebugLevel, (...args: unknown[]) => void>,
);
}

namespace.level = (newLevel: DebugLevel | false) => {
Expand Down
5 changes: 4 additions & 1 deletion core/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import type Quill from './quill';
abstract class Module<T extends {} = {}> {
static DEFAULTS = {};

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

export default Module;
Loading

0 comments on commit 636ec74

Please sign in to comment.