Skip to content

Commit

Permalink
Enable strictNullChecks
Browse files Browse the repository at this point in the history
  • Loading branch information
luin committed Jun 8, 2023
1 parent 27d9a6e commit 3d684d9
Show file tree
Hide file tree
Showing 24 changed files with 205 additions and 77 deletions.
6 changes: 5 additions & 1 deletion blots/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Block extends BlockBlot {
}
if (value.length === 0) return;
const lines = value.split('\n');
const text = lines.shift();
const text = lines.shift() as string;
if (text.length > 0) {
if (index < this.length() - 1 || this.children.tail == null) {
super.insertAt(Math.min(index, this.length() - 1), text);
Expand All @@ -66,6 +66,7 @@ class Block extends BlockBlot {
// eslint-disable-next-line @typescript-eslint/no-this-alias
let block: Blot | this = this;
lines.reduce((lineIndex, line) => {
// @ts-expect-error Fix me later
block = block.split(lineIndex, true);
block.insertAt(0, line);
return line.length;
Expand Down Expand Up @@ -114,6 +115,7 @@ class Block extends BlockBlot {
this.parent.insertBefore(clone, this);
return this;
}
// @ts-expect-error Fix me later
this.parent.insertBefore(clone, this.next);
return clone;
}
Expand Down Expand Up @@ -169,9 +171,11 @@ class BlockEmbed extends EmbedBlot {
});
const ref = this.split(index);
blocks.forEach(block => {
// @ts-expect-error Fix me later
this.parent.insertBefore(block, ref);
});
if (text) {
// @ts-expect-error Fix me later
this.parent.insertBefore(this.scroll.create('text', text), ref);
}
}
Expand Down
3 changes: 3 additions & 0 deletions blots/cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class Cursor extends EmbedBlot {

remove() {
super.remove();
// @ts-expect-error Fix me later
this.parent = null;
}

Expand All @@ -79,6 +80,7 @@ class Cursor extends EmbedBlot {
this.domNode.lastChild != null &&
this.domNode.lastChild !== this.textNode
) {
// @ts-expect-error Fix me later
this.domNode.parentNode.insertBefore(
this.domNode.lastChild,
this.domNode,
Expand Down Expand Up @@ -171,6 +173,7 @@ class Cursor extends EmbedBlot {
// And then "x" will be inserted after `<a/>`:
// <span class="ql-cursor"><a>\uFEFF</a>d{I}</span>
optimize(context?: unknown) {
// @ts-expect-error Fix me later
super.optimize(context);

let { parent } = this;
Expand Down
1 change: 1 addition & 0 deletions blots/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class Embed extends EmbedBlot {
};
} else {
textNode = document.createTextNode(text);
// @ts-expect-error Fix me later
this.parent.insertBefore(this.scroll.create(textNode), this.next);
range = {
startNode: textNode,
Expand Down
6 changes: 4 additions & 2 deletions blots/scroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ class Scroll extends ScrollBlot {
}
const ref =
last.children.head instanceof Break ? null : last.children.head;
// @ts-expect-error
first.moveChildren(last, ref);
// @ts-expect-error
first.remove();
}
this.optimize();
Expand Down Expand Up @@ -162,7 +164,7 @@ class Scroll extends ScrollBlot {
blotIndex: number,
blotLength: number,
) => {
let lines = [];
let lines: (Block | BlockEmbed)[] = [];
let lengthLeft = blotLength;
blot.children.forEachAt(
blotIndex,
Expand All @@ -181,7 +183,7 @@ class Scroll extends ScrollBlot {
return getLines(this, index, length);
}

optimize(context: { [key: string]: any }): void;
optimize(context?: { [key: string]: any }): void;
optimize(
mutations?: MutationRecord[],
context?: { [key: string]: any },
Expand Down
38 changes: 25 additions & 13 deletions core/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import { Range } from './selection';

const ASCII = /^[ -~]*$/;

type SelectionInfo = {
newRange: Range;
oldRange: Range;
};

class Editor {
scroll: Scroll;
delta: Delta;
Expand Down Expand Up @@ -158,8 +163,8 @@ class Editor {
}

getFormat(index: number, length = 0): Record<string, unknown> {
let lines = [];
let leaves = [];
let lines: (Block | BlockEmbed)[] = [];
let leaves: LeafBlot[] = [];
if (length === 0) {
this.scroll.path(index).forEach(path => {
const [blot] = path;
Expand Down Expand Up @@ -189,10 +194,13 @@ class Editor {

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

getText(index: number, length: number): string {
Expand Down Expand Up @@ -226,7 +234,7 @@ class Editor {
if (this.scroll.children.length === 0) return true;
if (this.scroll.children.length > 1) return false;
const blot = this.scroll.children.head;
if (blot.statics.blotName !== Block.blotName) return false;
if (blot?.statics.blotName !== Block.blotName) return false;
const block = blot as Block;
if (block.children.length > 1) return false;
return block.children.head instanceof Break;
Expand All @@ -250,18 +258,25 @@ class Editor {
return this.applyDelta(delta);
}

update(change: Delta, mutations = [], selectionInfo = undefined): Delta {
update(
change: Delta | null,
mutations: MutationRecord[] = [],
selectionInfo: SelectionInfo | undefined = undefined,
): Delta {
const oldDelta = this.delta;
if (
mutations.length === 1 &&
mutations[0].type === 'characterData' &&
// @ts-expect-error Fix me later
mutations[0].target.data.match(ASCII) &&
this.scroll.find(mutations[0].target)
) {
// Optimization for character changes
const textBlot = this.scroll.find(mutations[0].target);
const formats = bubbleFormats(textBlot);
// @ts-expect-error Fix me later
const index = textBlot.offset(this.scroll);
// @ts-expect-error Fix me later
const oldValue = mutations[0].oldValue.replace(CursorBlot.CONTENTS, '');
const oldText = new Delta().insert(oldValue);
// @ts-expect-error
Expand Down Expand Up @@ -333,7 +348,7 @@ function convertHTML(blot, index, length, isRoot = false) {
if (blot.children) {
// TODO fix API
if (blot.statics.blotName === 'list-container') {
const items = [];
const items: any[] = [];
blot.children.forEachAt(index, length, (child, offset, childLength) => {
const formats = child.formats();
items.push({
Expand All @@ -346,7 +361,7 @@ function convertHTML(blot, index, length, isRoot = false) {
});
return convertListHTML(items, -1, []);
}
const parts = [];
const parts: string[] = [];
blot.children.forEachAt(index, length, (child, offset, childLength) => {
parts.push(convertHTML(child, offset, childLength));
});
Expand Down Expand Up @@ -405,10 +420,7 @@ function normalizeDelta(delta: Delta) {
}, new Delta());
}

function shiftRange(
{ index, length }: { index: number; length: number },
amount: number,
) {
function shiftRange({ index, length }: Range, amount: number) {
return new Range(index + amount, length);
}

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

function debug(method: string, ...args: unknown[]) {
if (levels.indexOf(method) <= levels.indexOf(level)) {
console[method](...args); // eslint-disable-line no-console
function debug(method: DebugLevel, ...args: unknown[]) {
if (level) {
if (levels.indexOf(method) <= levels.indexOf(level)) {
console[method](...args); // eslint-disable-line no-console
}
}
}

function namespace(ns: string): Record<typeof levels[number], typeof debug> {
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>);
}

namespace.level = newLevel => {
namespace.level = (newLevel: DebugLevel | false) => {
level = newLevel;
};
debug.level = namespace.level;
Expand Down
13 changes: 7 additions & 6 deletions core/quill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import Uploader from '../modules/uploader';
import Editor from './editor';
import Emitter, { EmitterSource } from './emitter';
import instances from './instances';
import logger from './logger';
import logger, { DebugLevel } from './logger';
import Module from './module';
import Selection, { Range } from './selection';
import Theme, { ThemeConstructor } from './theme';
Expand All @@ -27,7 +27,7 @@ Parchment.ParentBlot.uiClass = 'ql-ui';

interface Options {
theme?: string;
debug?: string | boolean;
debug?: DebugLevel | boolean;
registry?: Parchment.Registry;
readOnly?: boolean;
container?: HTMLElement | string;
Expand Down Expand Up @@ -69,7 +69,7 @@ class Quill {
'core/theme': Theme,
};

static debug(limit: string | boolean) {
static debug(limit: DebugLevel | boolean) {
if (limit === true) {
limit = 'log';
}
Expand Down Expand Up @@ -231,11 +231,11 @@ class Quill {
this.allowReadOnlyEdits = false;
}

addContainer(container: string, refNode?: Node): HTMLDivElement;
addContainer(container: HTMLElement, refNode?: Node): HTMLElement;
addContainer(container: string, refNode?: Node | null): HTMLDivElement;
addContainer(container: HTMLElement, refNode?: Node | null): HTMLElement;
addContainer(
container: string | HTMLElement,
refNode = null,
refNode: Node | null = null,
): HTMLDivElement | HTMLElement {
if (typeof container === 'string') {
const className = container;
Expand Down Expand Up @@ -875,6 +875,7 @@ function overload(
}
// Handle format being object, two format name/value strings or excluded
if (typeof name === 'object') {
// @ts-expect-error Fix me later
formats = name;
// @ts-expect-error
source = value;
Expand Down
9 changes: 9 additions & 0 deletions core/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ class Selection {
// TODO Give blot ability to not split
if (blot instanceof LeafBlot) {
const after = blot.split(nativeRange.start.offset);
// @ts-expect-error Fix me later
blot.parent.insertBefore(this.cursor, after);
} else {
// @ts-expect-error TODO: nativeRange.start.node doesn't seem to match function signature
Expand Down Expand Up @@ -253,13 +254,15 @@ class Selection {
const indexes = positions.map(position => {
const [node, offset] = position;
const blot = this.scroll.find(node, true);
// @ts-expect-error Fix me later
const index = blot.offset(this.scroll);
if (offset === 0) {
return index;
}
if (blot instanceof LeafBlot) {
return index + blot.index(node, offset);
}
// @ts-expect-error Fix me later
return index + blot.length();
});
const end = Math.min(Math.max(...indexes), this.scroll.length() - 1);
Expand Down Expand Up @@ -289,6 +292,7 @@ class Selection {
node = node.childNodes[offset];
offset = 0;
} else if (node.childNodes.length === offset) {
// @ts-expect-error Fix me later
node = node.lastChild;
if (node instanceof Text) {
offset = node.data.length;
Expand Down Expand Up @@ -351,6 +355,7 @@ class Selection {
startNode != null &&
(this.root.parentNode == null ||
startNode.parentNode == null ||
// @ts-expect-error Fix me later
endNode.parentNode == null)
) {
return;
Expand All @@ -369,19 +374,23 @@ class Selection {
endOffset !== native.endOffset
) {
if (startNode instanceof Element && startNode.tagName === 'BR') {
// @ts-expect-error Fix me later
startOffset = Array.from(startNode.parentNode.childNodes).indexOf(
startNode,
);
startNode = startNode.parentNode;
}
if (endNode instanceof Element && endNode.tagName === 'BR') {
// @ts-expect-error Fix me later
endOffset = Array.from(endNode.parentNode.childNodes).indexOf(
endNode,
);
endNode = endNode.parentNode;
}
const range = document.createRange();
// @ts-expect-error Fix me later
range.setStart(startNode, startOffset);
// @ts-expect-error Fix me later
range.setEnd(endNode, endOffset);
selection.removeAllRanges();
selection.addRange(range);
Expand Down
7 changes: 5 additions & 2 deletions e2e/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
export const SHORTKEY = process.platform === 'darwin' ? 'Meta' : 'Control';

export function getSelectionInTextNode() {
const { anchorNode, anchorOffset, focusNode, focusOffset } =
document.getSelection();
const selection = document.getSelection();
if (!selection) {
throw new Error('Selection is null');
}
const { anchorNode, anchorOffset, focusNode, focusOffset } = selection;
return JSON.stringify([
(anchorNode as Text).data,
anchorOffset,
Expand Down
6 changes: 6 additions & 0 deletions formats/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,15 @@ class TableRow extends Container {
next: this | null;

checkMerge() {
// @ts-expect-error
if (super.checkMerge() && this.next.children.head != null) {
// @ts-expect-error
const thisHead = this.children.head.formats();
// @ts-expect-error
const thisTail = this.children.tail.formats();
// @ts-expect-error
const nextHead = this.next.children.head.formats();
// @ts-expect-error
const nextTail = this.next.children.tail.formats();
return (
thisHead.table === thisTail.table &&
Expand Down Expand Up @@ -168,6 +173,7 @@ class TableContainer extends Container {
if (body == null || body.children.head == null) return;
body.children.forEach(row => {
const ref = row.children.at(index);
// @ts-expect-error
const value = TableCell.formats(row.children.head.domNode);
const cell = this.scroll.create(TableCell.blotName, value);
row.insertBefore(cell, ref);
Expand Down

0 comments on commit 3d684d9

Please sign in to comment.