Make more of the codebase conform to strict types (#10857)

This commit is contained in:
Michael Telatynski 2023-05-16 14:25:43 +01:00 committed by GitHub
parent 7f017a84c2
commit 6a3f59cc76
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
45 changed files with 127 additions and 121 deletions

View file

@ -147,7 +147,7 @@ function getTextAndOffsetToNode(
let foundNode = false;
let text = "";
function enterNodeCallback(node: HTMLElement): boolean {
function enterNodeCallback(node: Node): boolean {
if (!foundNode) {
if (node === selectionNode) {
foundNode = true;
@ -157,7 +157,7 @@ function getTextAndOffsetToNode(
// but for example while pasting in some browsers, they are still
// converted to BRs, so also take these into account when they
// are not the last element in the DIV.
if (node.tagName === "BR" && node.nextSibling) {
if (node instanceof HTMLElement && node.tagName === "BR" && node.nextSibling) {
if (!foundNode) {
offsetToNode += 1;
}
@ -173,12 +173,16 @@ function getTextAndOffsetToNode(
return true;
}
function leaveNodeCallback(node: HTMLElement): void {
function leaveNodeCallback(node: Node): void {
// if this is not the last DIV (which are only used as line containers atm)
// we don't just check if there is a nextSibling because sometimes the caret ends up
// after the last DIV and it creates a newline if you type then,
// whereas you just want it to be appended to the current line
if (node.tagName === "DIV" && (<HTMLElement>node.nextSibling)?.tagName === "DIV") {
if (
node instanceof HTMLElement &&
node.tagName === "DIV" &&
(<HTMLElement>node.nextSibling)?.tagName === "DIV"
) {
text += "\n";
if (!foundNode) {
offsetToNode += 1;

View file

@ -21,7 +21,7 @@ import { Caret } from "./caret";
export interface IHistory {
parts: SerializedPart[];
caret: Caret;
caret?: Caret;
}
export const MAX_STEP_LENGTH = 10;
@ -31,7 +31,7 @@ export default class HistoryManager {
private newlyTypedCharCount = 0;
private currentIndex = -1;
private changedSinceLastPush = false;
private lastCaret: Caret | null = null;
private lastCaret?: Caret;
private nonWordBoundarySinceLastPush = false;
private addedSinceLastPush = false;
private removedSinceLastPush = false;
@ -41,7 +41,7 @@ export default class HistoryManager {
this.newlyTypedCharCount = 0;
this.currentIndex = -1;
this.changedSinceLastPush = false;
this.lastCaret = null;
this.lastCaret = undefined;
this.nonWordBoundarySinceLastPush = false;
this.addedSinceLastPush = false;
this.removedSinceLastPush = false;
@ -85,7 +85,7 @@ export default class HistoryManager {
}
}
private pushState(model: EditorModel, caret: Caret): void {
private pushState(model: EditorModel, caret?: Caret): void {
// remove all steps after current step
while (this.currentIndex < this.stack.length - 1) {
this.stack.pop();
@ -93,7 +93,7 @@ export default class HistoryManager {
const parts = model.serializeParts();
this.stack.push({ parts, caret });
this.currentIndex = this.stack.length - 1;
this.lastCaret = null;
this.lastCaret = undefined;
this.changedSinceLastPush = false;
this.newlyTypedCharCount = 0;
this.nonWordBoundarySinceLastPush = false;
@ -102,7 +102,7 @@ export default class HistoryManager {
}
// needs to persist parts and caret position
public tryPush(model: EditorModel, caret: Caret, inputType?: string, diff?: IDiff): boolean {
public tryPush(model: EditorModel, caret?: Caret, inputType?: string, diff?: IDiff): boolean {
// ignore state restoration echos.
// these respect the inputType values of the input event,
// but are actually passed in from MessageEditor calling model.reset()