fix: Draft comment on text gets into a strange state when unfocused (#5153)

This commit is contained in:
Tom Moor
2023-04-05 09:02:26 -04:00
committed by GitHub
parent 3ca86bcc0c
commit 2458085eed
2 changed files with 219 additions and 13 deletions

View File

@@ -49,20 +49,42 @@ export default class ProsemirrorHelper {
* @returns True if the editor is empty
*/
static trim(doc: Node) {
const first = doc.firstChild;
const last = doc.lastChild;
const firstIsEmpty =
first &&
ProsemirrorHelper.toPlainText(first, doc.type.schema).trim() === "";
const lastIsEmpty =
last &&
ProsemirrorHelper.toPlainText(last, doc.type.schema).trim() === "";
const firstIsLast = first === last;
const { schema } = doc.type;
let index = 0,
start = 0,
end = doc.nodeSize - 2,
isEmpty;
return doc.cut(
firstIsEmpty ? first.nodeSize : 0,
lastIsEmpty && !firstIsLast ? doc.nodeSize - last.nodeSize : undefined
);
if (doc.childCount <= 1) {
return doc;
}
isEmpty = true;
while (isEmpty) {
const node = doc.maybeChild(index++);
if (!node) {
break;
}
isEmpty = ProsemirrorHelper.toPlainText(node, schema).trim() === "";
if (isEmpty) {
start += node.nodeSize;
}
}
index = doc.childCount - 1;
isEmpty = true;
while (isEmpty) {
const node = doc.maybeChild(index--);
if (!node) {
break;
}
isEmpty = ProsemirrorHelper.toPlainText(node, schema).trim() === "";
if (isEmpty) {
end -= node.nodeSize;
}
}
return doc.cut(start, end);
}
/**