Skip to content

Commit e2585b0

Browse files
GoamanZynton
authored andcommitted
[FIX] deleteContentForward on google chrome
Sometimes Google Chrome wrongly triggers an input event with `data` being `null` on `deleteContentForward` and `insertParagraph`. Luckily Chrome provides the proper signal with the event `beforeinput`. A fix had already been made for enter but not for `deleteContentForward`.
1 parent d2ca74e commit e2585b0

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

src/editor.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ export class OdooEditor extends EventTarget {
229229

230230
this.addDomListener(this.editable, 'keydown', this._onKeyDown);
231231
this.addDomListener(this.editable, 'input', this._onInput);
232+
this.addDomListener(this.editable, 'beforeinput', this._onBeforeInput);
232233
this.addDomListener(this.editable, 'mousedown', this._onMouseDown);
233234
this.addDomListener(this.editable, 'mouseup', this._onMouseup);
234235
this.addDomListener(this.editable, 'paste', this._onPaste);
@@ -1652,6 +1653,10 @@ export class OdooEditor extends EventTarget {
16521653
// Handlers
16531654
//--------------------------------------------------------------------------
16541655

1656+
_onBeforeInput(ev) {
1657+
this._lastBeforeInputType = ev.inputType;
1658+
}
1659+
16551660
/**
16561661
* If backspace/delete input, rollback the operation and handle the
16571662
* operation ourself. Needed for mobile, used for desktop for consistency.
@@ -1665,21 +1670,28 @@ export class OdooEditor extends EventTarget {
16651670
const cursor = this._historySteps[this._historySteps.length - 1].cursor;
16661671
const { focusOffset, focusNode, anchorNode, anchorOffset } = cursor || {};
16671672
const wasCollapsed = !cursor || (focusNode === anchorNode && focusOffset === anchorOffset);
1673+
1674+
// Sometimes google chrome wrongly triggers an input event with `data`
1675+
// being `null` on `deleteContentForward` `insertParagraph`. Luckily,
1676+
// chrome provide the proper signal with the event `beforeinput`.
1677+
const isChromeDeleteforward =
1678+
ev.inputType === 'insertText' &&
1679+
ev.data === null &&
1680+
this._lastBeforeInputType === 'deleteContentForward';
1681+
const isChromeInsertParagraph =
1682+
ev.inputType === 'insertText' &&
1683+
ev.data === null &&
1684+
this._lastBeforeInputType === 'insertParagraph';
16681685
if (this.keyboardType === KEYBOARD_TYPES.PHYSICAL || !wasCollapsed) {
16691686
if (ev.inputType === 'deleteContentBackward') {
16701687
this.historyRollback();
16711688
ev.preventDefault();
16721689
this._applyCommand('oDeleteBackward');
1673-
} else if (ev.inputType === 'deleteContentForward') {
1690+
} else if (ev.inputType === 'deleteContentForward' || isChromeDeleteforward) {
16741691
this.historyRollback();
16751692
ev.preventDefault();
16761693
this._applyCommand('oDeleteForward');
1677-
} else if (
1678-
ev.inputType === 'insertParagraph' ||
1679-
(ev.inputType === 'insertText' && ev.data === null)
1680-
) {
1681-
// Sometimes the browser wrongly triggers an insertText
1682-
// input event with null data on enter.
1694+
} else if (ev.inputType === 'insertParagraph' || isChromeInsertParagraph) {
16831695
this.historyRollback();
16841696
ev.preventDefault();
16851697
if (this._applyCommand('oEnter') === UNBREAKABLE_ROLLBACK_CODE) {

0 commit comments

Comments
 (0)