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)) {
child = node.firstChild; let child = node.firstChild;
while (isWebKit && child?.nodeType === TEXT_NODE && !child.data) { if (cantFocusEmptyTextNodes) {
child.remove(); while (child && child instanceof Text && !child.data) {
child = node.firstChild; node.removeChild(child);
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 instanceof Element && !node.querySelector("BR")) {
// } else if (!node.innerText.trim().length) { fixer = createElement("BR");
} else if (node.matches(':empty')) { let parent = node;
fixer = createElement('BR'); let child;
while ((child = node.lastElementChild) && !isInline(child)) { while ((child = parent.lastElementChild) && !isInline(child)) {
node = 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,36 +942,30 @@ 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 (startContainer.nodeType === TEXT_NODE) {
if (startOffset) {
// If in the middle or end of a text node, we're not at the boundary.
if (startContainer.nodeType === TEXT_NODE) {
if (startOffset) {
return false;
}
nodeAfterCursor = startContainer;
} else {
nodeAfterCursor = getNodeAfter(startContainer, startOffset);
if (nodeAfterCursor && !root.contains(nodeAfterCursor)) {
nodeAfterCursor = null;
}
// The cursor was right at the end of the document
if (!nodeAfterCursor) {
nodeAfterCursor = getNodeBefore(startContainer, startOffset);
if (nodeAfterCursor.nodeType === TEXT_NODE &&
nodeAfterCursor.length) {
return false; return false;
} }
nodeAfterCursor = startContainer;
} else {
nodeAfterCursor = getNodeAfter(startContainer, startOffset);
// The cursor was right at the end of the document
if (!nodeAfterCursor || !root.contains(nodeAfterCursor)) {
nodeAfterCursor = getNodeBefore(startContainer, startOffset);
if (nodeAfterCursor.nodeType === TEXT_NODE && nodeAfterCursor.length) {
return false;
}
}
} }
// Otherwise, look for any previous content in the same block.
contentWalker = newContentWalker(getStartBlockOfRange(range, root));
contentWalker.currentNode = nodeAfterCursor;
return !contentWalker.previousNode();
} }
// Otherwise, look for any previous content in the same block.
contentWalker = newContentWalker(getStartBlockOfRange(range, root));
contentWalker.currentNode = nodeAfterCursor;
return !contentWalker.previousNode();
}, },
rangeDoesEndAtBlockBoundary = (range, root) => { rangeDoesEndAtBlockBoundary = (range, root) => {