Cleanup some squire code

This commit is contained in:
the-djmaze 2023-08-28 12:05:02 +02:00
parent e9770e17f6
commit 6b42620ad3

View file

@ -220,52 +220,36 @@ const
}, },
fixCursor = (node, root) => { fixCursor = (node, root) => {
// In Webkit and Gecko, block level elements are collapsed and let fixer = null;
// unfocusable if they have no content (:empty). To remedy this, a <BR> must be if (node instanceof Text) {
// inserted. In Opera and IE, we just need a textnode in order for the return node;
// cursor to appear.
let self = root.__squire__;
let originalNode = node;
let fixer, child;
if (node === root) {
if (!(child = node.firstChild) || child.nodeName === 'BR') {
fixer = self.createDefaultBlock();
if (child) {
child.replaceWith(fixer);
} }
else {
node.append(fixer);
}
node = fixer;
fixer = null;
}
}
if (node.nodeType === TEXT_NODE) {
return originalNode;
}
if (isInline(node)) { if (isInline(node)) {
let child = node.firstChild;
if (cantFocusEmptyTextNodes) {
while (child && child instanceof Text && !child.data) {
node.removeChild(child);
child = node.firstChild; child = node.firstChild;
while (isWebKit && child?.nodeType === TEXT_NODE && !child.data) { }
child.remove();
child = node.firstChild;
} }
if (!child) { if (!child) {
fixer = self._addZWS(); if (cantFocusEmptyTextNodes) {
fixer = document.createTextNode(ZWS);
} else {
fixer = document.createTextNode("");
} }
// } else if (!node.querySelector('BR')) { }
// } else if (!node.innerText.trim().length) { } else if (node instanceof Element && !node.querySelector("BR")) {
} else if (node.matches(':empty')) { fixer = createElement("BR");
fixer = createElement('BR'); let parent = node;
while ((child = node.lastElementChild) && !isInline(child)) { let child;
node = child; while ((child = parent.lastElementChild) && !isInline(child)) {
parent = child;
} }
} }
if (fixer) { if (fixer) {
try { try {
node.append(fixer); node.appendChild(fixer);
} catch (error) { } catch (error) {
didError({ didError({
name: 'Squire: fixCursor ' + error, name: 'Squire: fixCursor ' + error,
@ -274,8 +258,7 @@ const
}); });
} }
} }
return node;
return originalNode;
}, },
// Recursively examine container nodes and wrap any inline children. // Recursively examine container nodes and wrap any inline children.
@ -959,10 +942,7 @@ const
let startBlock = getStartBlockOfRange(range, root); let startBlock = getStartBlockOfRange(range, root);
let nodeAfterCursor; let nodeAfterCursor;
if (!startBlock) { if (startBlock) {
return false;
}
// If in the middle or end of a text node, we're not at the boundary. // If in the middle or end of a text node, we're not at the boundary.
if (startContainer.nodeType === TEXT_NODE) { if (startContainer.nodeType === TEXT_NODE) {
if (startOffset) { if (startOffset) {
@ -971,14 +951,10 @@ const
nodeAfterCursor = startContainer; nodeAfterCursor = startContainer;
} else { } else {
nodeAfterCursor = getNodeAfter(startContainer, startOffset); nodeAfterCursor = getNodeAfter(startContainer, startOffset);
if (nodeAfterCursor && !root.contains(nodeAfterCursor)) {
nodeAfterCursor = null;
}
// The cursor was right at the end of the document // The cursor was right at the end of the document
if (!nodeAfterCursor) { if (!nodeAfterCursor || !root.contains(nodeAfterCursor)) {
nodeAfterCursor = getNodeBefore(startContainer, startOffset); nodeAfterCursor = getNodeBefore(startContainer, startOffset);
if (nodeAfterCursor.nodeType === TEXT_NODE && if (nodeAfterCursor.nodeType === TEXT_NODE && nodeAfterCursor.length) {
nodeAfterCursor.length) {
return false; return false;
} }
} }
@ -989,6 +965,7 @@ const
contentWalker.currentNode = nodeAfterCursor; contentWalker.currentNode = nodeAfterCursor;
return !contentWalker.previousNode(); return !contentWalker.previousNode();
}
}, },
rangeDoesEndAtBlockBoundary = (range, root) => { rangeDoesEndAtBlockBoundary = (range, root) => {