Skip to content

Commit

Permalink
Merge branch 'bmakuh-no-implicit-any' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
luin committed Aug 13, 2023
2 parents a9c25ec + f5e3b54 commit c5fa826
Show file tree
Hide file tree
Showing 51 changed files with 542 additions and 319 deletions.
28 changes: 17 additions & 11 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 @@ -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,27 @@ class Block extends BlockBlot {
return this.cache.length;
}

moveChildren(target, ref?) {
moveChildren(target: Parent, ref?: Blot | null) {
// @ts-expect-error Parchment types are wrong
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 +147,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 +197,13 @@ function blockDelta(blot: BlockBlot, filter = true) {
.insert('\n', bubbleFormats(blot));
}

function bubbleFormats(blot, formats = {}, filter = true) {
function bubbleFormats(
blot: Blot | null,
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 { EmbedContextRange } 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: Node, 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(): EmbedContextRange | 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: Record<string, unknown>) {
if (
mutations.some(mutation => {
return (
Expand Down
23 changes: 15 additions & 8 deletions blots/embed.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import { EmbedBlot } from 'parchment';
import { EmbedBlot, ScrollBlot } from 'parchment';
import TextBlot from './text';

const GUARD_TEXT = '\uFEFF';

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

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,15 +29,15 @@ 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;
let textNode;
restore(node: Text): EmbedContextRange | null {
let range: EmbedContextRange | null = null;
let textNode: Text;
const text = node.data.split(GUARD_TEXT).join('');
if (node === this.leftGuard) {
if (this.prev instanceof TextBlot) {
Expand Down Expand Up @@ -69,14 +76,14 @@ class Embed extends EmbedBlot {
return range;
}

update(mutations, context) {
update(mutations: MutationRecord[], context: Record<string, unknown>) {
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
20 changes: 10 additions & 10 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: unknown) {
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,15 +108,11 @@ 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) {
event.preventDefault();
}

insertAt(index: number, value: string, def?: unknown) {
if (index >= this.length()) {
if (def == null || this.scroll.query(value, Scope.BLOCK) == null) {
Expand Down Expand Up @@ -322,12 +318,16 @@ 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);
}
}

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

private deltaToRenderBlocks(delta: Delta) {
const renderBlocks: RenderBlock[] = [];

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 c5fa826

Please sign in to comment.