Skip to content

Commit

Permalink
Turn on noImplicitAny and fix all errors
Browse files Browse the repository at this point in the history
  • Loading branch information
bmakuh committed Jul 28, 2023
1 parent 837e4c9 commit 794d8b5
Show file tree
Hide file tree
Showing 49 changed files with 3,742 additions and 5,121 deletions.
29 changes: 17 additions & 12 deletions blots/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Blot,
EmbedBlot,
LeafBlot,
Parent,
Scope,
} from 'parchment';
import Delta from 'quill-delta';
Expand All @@ -13,7 +14,7 @@ import TextBlot from './text';

const NEWLINE_LENGTH = 1;

class Block extends BlockBlot {
class Block extends BlockBlot implements Blot {
cache: { delta?: Delta | null; length?: number } = {};

delta(): Delta {
Expand All @@ -28,7 +29,7 @@ class Block extends BlockBlot {
this.cache = {};
}

formatAt(index, length, name, value) {
formatAt(index: number, length: number, name: string, value: unknown) {
if (length <= 0) return;
if (this.scroll.query(name, Scope.BLOCK)) {
if (index + length === this.length()) {
Expand Down Expand Up @@ -73,7 +74,7 @@ class Block extends BlockBlot {
}, index + text.length);
}

insertBefore(blot, ref) {
insertBefore(blot: Blot, ref: Blot | null) {
const { head } = this.children;
super.insertBefore(blot, ref);
if (head instanceof Break) {
Expand All @@ -89,26 +90,26 @@ class Block extends BlockBlot {
return this.cache.length;
}

moveChildren(target, ref?) {
moveChildren(target: Parent, ref?: Blot) {
super.moveChildren(target, ref);
this.cache = {};
}

optimize(context) {
optimize(context: { [key: string]: any }) {
super.optimize(context);
this.cache = {};
}

path(index) {
path(index: number) {
return super.path(index, true);
}

removeChild(child) {
removeChild(child: Blot) {
super.removeChild(child);
this.cache = {};
}

split(index, force = false) {
split(index: number, force: boolean | undefined = false): Blot | null {
if (force && (index === 0 || index >= this.length() - NEWLINE_LENGTH)) {
const clone = this.clone();
if (index === 0) {
Expand Down Expand Up @@ -145,15 +146,15 @@ class BlockEmbed extends EmbedBlot {
});
}

format(name, value) {
format(name: string, value: unknown) {
const attribute = this.scroll.query(name, Scope.BLOCK_ATTRIBUTE);
if (attribute != null) {
// @ts-expect-error TODO: Scroll#query() should return Attributor when scope is attribute
this.attributes.attribute(attribute, value);
}
}

formatAt(index, length, name, value) {
formatAt(index: number, length: number, name: string, value: unknown) {
this.format(name, value);
}

Expand Down Expand Up @@ -195,9 +196,13 @@ function blockDelta(blot: BlockBlot, filter = true) {
.insert('\n', bubbleFormats(blot));
}

function bubbleFormats(blot, formats = {}, filter = true) {
function bubbleFormats(
blot: Blot | LeafBlot | BlockBlot | Parent,
formats: Record<string, unknown> = {},
filter = true,
): Record<string, unknown> {
if (blot == null) return formats;
if (typeof blot.formats === 'function') {
if ('formats' in blot && typeof blot.formats === 'function') {
formats = {
...formats,
...blot.formats(),
Expand Down
13 changes: 7 additions & 6 deletions blots/cursor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { EmbedBlot, Parent, Scope, ScrollBlot } from 'parchment';
import Selection from '../core/selection';
import TextBlot from './text';
import { EmbedContext } from './embed';

class Cursor extends EmbedBlot {
static blotName = 'cursor';
Expand All @@ -16,7 +17,7 @@ class Cursor extends EmbedBlot {
textNode: Text;
savedLength: number;

constructor(scroll: ScrollBlot, domNode, selection: Selection) {
constructor(scroll: ScrollBlot, domNode: HTMLElement, selection: Selection) {
super(scroll, domNode);
this.selection = selection;
this.textNode = document.createTextNode(Cursor.CONTENTS);
Expand All @@ -29,7 +30,7 @@ class Cursor extends EmbedBlot {
if (this.parent != null) this.parent.removeChild(this);
}

format(name, value) {
format(name: string, value: unknown) {
if (this.savedLength !== 0) {
super.format(name, value);
return;
Expand All @@ -51,7 +52,7 @@ class Cursor extends EmbedBlot {
}
}

index(node, offset) {
index(node: Text, offset: number) {
if (node === this.textNode) return 0;
return super.index(node, offset);
}
Expand All @@ -70,7 +71,7 @@ class Cursor extends EmbedBlot {
this.parent = null;
}

restore() {
restore(): EmbedContext | null {
if (this.selection.composing || this.parent == null) return null;
const range = this.selection.getNativeRange();
// Browser may push down styles/nodes inside the cursor blot.
Expand Down Expand Up @@ -121,7 +122,7 @@ class Cursor extends EmbedBlot {
this.remove();
if (range) {
// calculate selection to restore
const remapOffset = (node, offset) => {
const remapOffset = (node: Node, offset: number) => {
if (prevTextBlot && node === prevTextBlot.domNode) {
return offset;
}
Expand All @@ -148,7 +149,7 @@ class Cursor extends EmbedBlot {
return null;
}

update(mutations, context) {
update(mutations: MutationRecord[], context: EmbedContext) {
if (
mutations.some(mutation => {
return (
Expand Down
22 changes: 15 additions & 7 deletions blots/embed.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import { EmbedBlot } from 'parchment';
import { EmbedBlot, ScrollBlot } from 'parchment';
import TextBlot from './text';

const GUARD_TEXT = '\uFEFF';

export interface EmbedContext {
startNode: Node | Text;
startOffset: number;
endNode?: Node | Text;
endOffset?: number;
range?: EmbedContext;
}

class Embed extends EmbedBlot {
contentNode: HTMLSpanElement;
leftGuard: Text;
rightGuard: Text;

constructor(scroll, node) {
constructor(scroll: ScrollBlot, node: Node) {
super(scroll, node);
this.contentNode = document.createElement('span');
this.contentNode.setAttribute('contenteditable', 'false');
Expand All @@ -22,14 +30,14 @@ class Embed extends EmbedBlot {
this.domNode.appendChild(this.rightGuard);
}

index(node, offset) {
index(node: Node, offset: number) {
if (node === this.leftGuard) return 0;
if (node === this.rightGuard) return 1;
return super.index(node, offset);
}

restore(node) {
let range;
restore(node: Text): EmbedContext | null {
let range = null;
let textNode;
const text = node.data.split(GUARD_TEXT).join('');
if (node === this.leftGuard) {
Expand Down Expand Up @@ -69,14 +77,14 @@ class Embed extends EmbedBlot {
return range;
}

update(mutations, context) {
update(mutations: MutationRecord[], context: EmbedContext) {
mutations.forEach(mutation => {
if (
mutation.type === 'characterData' &&
(mutation.target === this.leftGuard ||
mutation.target === this.rightGuard)
) {
const range = this.restore(mutation.target);
const range = this.restore(mutation.target as Text);
if (range) context.range = range;
}
});
Expand Down
10 changes: 5 additions & 5 deletions blots/inline.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { EmbedBlot, InlineBlot, Scope } from 'parchment';
import { BlotConstructor, EmbedBlot, InlineBlot, Scope } from 'parchment';
import Break from './break';
import Text from './text';

class Inline extends InlineBlot {
static allowedChildren = [Inline, Break, EmbedBlot, Text];
static allowedChildren: BlotConstructor[] = [Inline, Break, EmbedBlot, Text];
// Lower index means deeper in the DOM tree, since not found (-1) is for embeds
static order = [
'cursor',
Expand All @@ -17,7 +17,7 @@ class Inline extends InlineBlot {
'code', // Must be higher
];

static compare(self, other) {
static compare(self: string, other: string) {
const selfIndex = Inline.order.indexOf(self);
const otherIndex = Inline.order.indexOf(other);
if (selfIndex >= 0 || otherIndex >= 0) {
Expand All @@ -32,7 +32,7 @@ class Inline extends InlineBlot {
return 1;
}

formatAt(index, length, name, value) {
formatAt(index: number, length: number, name: string, value: unknown) {
if (
Inline.compare(this.statics.blotName, name) < 0 &&
this.scroll.query(name, Scope.BLOT)
Expand All @@ -46,7 +46,7 @@ class Inline extends InlineBlot {
}
}

optimize(context) {
optimize(context: { [key: string]: any }) {
super.optimize(context);
if (
this.parent instanceof Inline &&
Expand Down
39 changes: 23 additions & 16 deletions blots/scroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,19 @@ class Scroll extends ScrollBlot {
this.update(mutations);
}

emitMount(blot) {
emitMount(blot: Blot) {
this.emitter.emit(Emitter.events.SCROLL_BLOT_MOUNT, blot);
}

emitUnmount(blot) {
emitUnmount(blot: Blot) {
this.emitter.emit(Emitter.events.SCROLL_BLOT_UNMOUNT, blot);
}

emitEmbedUpdate(blot, change) {
emitEmbedUpdate(blot: Blot, change: Delta) {
this.emitter.emit(Emitter.events.SCROLL_EMBED_UPDATE, blot, change);
}

deleteAt(index, length) {
deleteAt(index: number, length: number) {
const [first, offset] = this.line(index);
const [last] = this.line(index + length);
super.deleteAt(index, length);
Expand All @@ -108,12 +108,12 @@ class Scroll extends ScrollBlot {
this.domNode.setAttribute('contenteditable', enabled ? 'true' : 'false');
}

formatAt(index, length, format, value) {
formatAt(index: number, length: number, format: string, value: unknown) {
super.formatAt(index, length, format, value);
this.optimize();
}

handleDragStart(event) {
handleDragStart(event: DragEvent) {
event.preventDefault();
}

Expand Down Expand Up @@ -175,11 +175,14 @@ class Scroll extends ScrollBlot {
this.insertAt(lineEndIndex - 1, '\n');
}

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

index = lineEndIndex;
}
Expand Down Expand Up @@ -322,7 +325,7 @@ class Scroll extends ScrollBlot {
updateEmbedAt(index: number, key: string, change: unknown) {
// Currently it only supports top-level embeds (BlockEmbed).
// We can update `ParentBlot` in parchment to support inline embeds.
const [blot] = this.descendant(b => b instanceof BlockEmbed, index);
const [blot] = this.descendant((b: Blot) => b instanceof BlockEmbed, index);
if (blot && blot.statics.blotName === key && isUpdatable(blot)) {
blot.updateContent(change);
}
Expand Down Expand Up @@ -427,17 +430,21 @@ function insertInlineContents(
const text = op.insert;
parent.insertAt(index, text);
const [leaf] = parent.descendant(LeafBlot, index);
const formats = bubbleFormats(leaf);
attributes = AttributeMap.diff(formats, attributes) || {};
if (leaf != null) {
const formats = bubbleFormats(leaf);
attributes = AttributeMap.diff(formats, attributes) || {};
}
} else if (typeof op.insert === 'object') {
const key = Object.keys(op.insert)[0]; // There should only be one key
if (key == null) return index;
parent.insertAt(index, key, op.insert[key]);
const isInlineEmbed = parent.scroll.query(key, Scope.INLINE) != null;
if (isInlineEmbed) {
const [leaf] = parent.descendant(LeafBlot, index);
const formats = bubbleFormats(leaf);
attributes = AttributeMap.diff(formats, attributes) || {};
if (leaf != null) {
const formats = bubbleFormats(leaf);
attributes = AttributeMap.diff(formats, attributes) || {};
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion blots/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Text extends TextBlot {}
function escapeText(text: string) {
return text.replace(/[&<>"']/g, s => {
// https://lodash.com/docs#escape
const entityMap = {
const entityMap: Record<string, string> = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
Expand Down
Loading

0 comments on commit 794d8b5

Please sign in to comment.