[FIX] web_editor : ensure break line consistency on delete

When delete backward/forward OR deleteRange,
we need to ensure the break lines stay consistent with the user action.

Some cases where not handled correctly.

Mainly when a <p> tags was removed and not directly adjacent to another block element.
In those case we kept too much or too little break lines.

@see tests added in `test/spec/editor.test.js`

task-2604627

X-original-commit: 9f14422ffceadd48b76ff460e4080e0cf5bc6fa2
Part-of: odoo/odoo#89210
This commit is contained in:
Sébastien Geelen (sge)
2022-04-21 07:59:43 +02:00
parent 4bd57e41a4
commit fcc035bcaa
2 changed files with 18 additions and 1 deletions
@@ -1312,7 +1312,9 @@ export class OdooEditor extends EventTarget {
let start = range.startContainer;
let end = range.endContainer;
// Let the DOM split and delete the range.
const doJoin = closestBlock(start) !== closestBlock(range.commonAncestorContainer);
const doJoin =
closestBlock(start) !== closestBlock(range.commonAncestorContainer) ||
closestBlock(end) !== closestBlock(range.commonAncestorContainer) ;
let next = nextLeaf(end, this.editable);
const splitEndTd = closestElement(end, 'td') && end.nextSibling;
const contents = range.extractContents();
@@ -163,6 +163,7 @@ HTMLElement.prototype.oDeleteBackward = function (offset, alreadyMoved = false,
}
let node = this.childNodes[offset];
const nextSibling = this.nextSibling;
let currentNodeIndex = offset;
// `offsetLimit` will ensure we never move nodes that were not initialy in the element
@@ -194,6 +195,20 @@ HTMLElement.prototype.oDeleteBackward = function (offset, alreadyMoved = false,
const { cType } = getState(cursorNode, cursorOffset, DIRECTIONS.LEFT);
if (cType & CTGROUPS.BLOCK && (!alreadyMoved || cType === CTYPES.BLOCK_OUTSIDE)) {
cursorNode.oDeleteBackward(cursorOffset, alreadyMoved, cursorOffset + currentNodeIndex - offset);
} else if (!alreadyMoved) {
// When removing a block node adjacent to a inline node,
// we need to ensure the block node induced line break are kept with a <br>.
// ex : <div>a<span>b</span><p>[]c</p>d</div> => deleteBakward
// => <div>a<span>b</span>[]c<br>d</div>
// In this case we cannot simply merge the <p> content into the div parent
// or we would loose the line break located after the <p>.
const cursorNodeNode = cursorNode.childNodes[cursorOffset];
const cursorNodeRightNode = cursorNodeNode ? cursorNodeNode.nextSibling : undefined;
if (cursorNodeRightNode &&
cursorNodeRightNode.nodeType === Node.TEXT_NODE &&
nextSibling === cursorNodeRightNode) {
moveDest[0].insertBefore(document.createElement('br'), cursorNodeRightNode);
}
}
}
};