Fix strict strictNullChecks to src/editor/* (#10428

* Fix strict `strictNullChecks` to `src/editor/*`

* Fix autoComplete creation

* Fix dom regression

* Remove changes
This commit is contained in:
Florian Duros 2023-03-23 14:35:55 +01:00 committed by GitHub
parent e19127f8ad
commit e4dfb21e56
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 85 additions and 59 deletions

View file

@ -34,11 +34,11 @@ export function walkDOMDepthFirst(rootNode: Node, enterNodeCallback: Predicate,
} else {
while (node && !node.nextSibling && node !== rootNode) {
node = node.parentElement;
if (node !== rootNode) {
if (node && node !== rootNode) {
leaveNodeCallback(node);
}
}
if (node !== rootNode) {
if (node && node !== rootNode) {
node = node.nextSibling;
}
}
@ -57,10 +57,10 @@ export function getCaretOffsetAndText(
}
function tryReduceSelectionToTextNode(
selectionNode: Node,
selectionNode: Node | null,
selectionOffset: number,
): {
node: Node;
node: Node | null;
characterOffset: number;
} {
// if selectionNode is an element, the selected location comes after the selectionOffset-th child node,
@ -73,8 +73,8 @@ function tryReduceSelectionToTextNode(
if (childNodeCount) {
if (selectionOffset >= childNodeCount) {
selectionNode = selectionNode.lastChild;
if (selectionNode.nodeType === Node.TEXT_NODE) {
selectionOffset = selectionNode.textContent.length;
if (selectionNode?.nodeType === Node.TEXT_NODE) {
selectionOffset = selectionNode.textContent?.length || 0;
} else {
// this will select the last child node in the next iteration
selectionOffset = Number.MAX_SAFE_INTEGER;
@ -101,7 +101,7 @@ function tryReduceSelectionToTextNode(
function getSelectionOffsetAndText(
editor: HTMLDivElement,
selectionNode: Node,
selectionNode: Node | null,
selectionOffset: number,
): {
offset: DocumentOffset;
@ -115,14 +115,15 @@ function getSelectionOffsetAndText(
// gets the caret position details, ignoring and adjusting to
// the ZWS if you're typing in a caret node
function getCaret(node: Node, offsetToNode: number, offsetWithinNode: number): DocumentOffset {
function getCaret(node: Node | null, offsetToNode: number, offsetWithinNode: number): DocumentOffset {
// if no node is selected, return an offset at the start
if (!node) {
return new DocumentOffset(0, false);
}
let atNodeEnd = offsetWithinNode === node.textContent.length;
let atNodeEnd = offsetWithinNode === node.textContent?.length;
if (node.nodeType === Node.TEXT_NODE && isCaretNode(node.parentElement)) {
const zwsIdx = node.nodeValue.indexOf(CARET_NODE_CHAR);
const nodeValue = node.nodeValue || "";
const zwsIdx = nodeValue.indexOf(CARET_NODE_CHAR);
if (zwsIdx !== -1 && zwsIdx < offsetWithinNode) {
offsetWithinNode -= 1;
}
@ -138,7 +139,10 @@ function getCaret(node: Node, offsetToNode: number, offsetWithinNode: number): D
// gets the text of the editor as a string,
// and the offset in characters where the selectionNode starts in that string
// all ZWS from caret nodes are filtered out
function getTextAndOffsetToNode(editor: HTMLDivElement, selectionNode: Node): { offsetToNode: number; text: string } {
function getTextAndOffsetToNode(
editor: HTMLDivElement,
selectionNode: Node | null,
): { offsetToNode: number; text: string } {
let offsetToNode = 0;
let foundNode = false;
let text = "";