From 4e4ed9b0980e74838ffc57cde81e5ee0d8b569f1 Mon Sep 17 00:00:00 2001 From: "Rodopho Cammarosano de Lima (rcdl)" Date: Thu, 19 Jan 2023 16:40:39 +0000 Subject: [PATCH] [FIX] web_editor: save a historyStep upon cut Before this commit, _onClipboardCut removed the selected text but did not register it as a history step. Other than not storing history properly, this caused unexpected behavior when followed by an action that leads to a historyRollback on the current step, such as those in _onInput. task-3142995 closes odoo/odoo#111927 X-original-commit: 451f1a2641b5b85ea75d9f1e8a8d1b528de031b6 Signed-off-by: David Monjoie (dmo) --- .../js/editor/odoo-editor/src/OdooEditor.js | 2 ++ .../odoo-editor/test/spec/copyPaste.test.js | 28 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/addons/web_editor/static/src/js/editor/odoo-editor/src/OdooEditor.js b/addons/web_editor/static/src/js/editor/odoo-editor/src/OdooEditor.js index 4c399d37f58..cc32eaaf090 100644 --- a/addons/web_editor/static/src/js/editor/odoo-editor/src/OdooEditor.js +++ b/addons/web_editor/static/src/js/editor/odoo-editor/src/OdooEditor.js @@ -3266,7 +3266,9 @@ export class OdooEditor extends EventTarget { _onClipboardCut(clipboardEvent) { this._onClipboardCopy(clipboardEvent); + this._recordHistorySelection(); this.deleteRange(); + this.historyStep(); } _onClipboardCopy(clipboardEvent) { if (!this.isSelectionInEditable()) { diff --git a/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/copyPaste.test.js b/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/copyPaste.test.js index 5b154395f16..5b5c7aae742 100644 --- a/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/copyPaste.test.js +++ b/addons/web_editor/static/src/js/editor/odoo-editor/test/spec/copyPaste.test.js @@ -2,6 +2,7 @@ import { BasicEditor, testEditor, triggerEvent, + undo, } from "../utils.js"; import {CLIPBOARD_WHITELISTS} from "../../src/OdooEditor.js"; @@ -122,6 +123,33 @@ describe('Cut', () => { contentAfter: '

[]

', }); }); + it('should cut selection and register it as a history step', async () => { + await testEditor(BasicEditor, { + contentBefore: '

a[bcd]e

', + stepFunction: async editor => { + const historyStepsCount = editor._historySteps.length; + triggerEvent(editor.editable, 'cut', { clipboardData: new DataTransfer() }); + window.chai.expect(editor._historySteps.length).to.be.equal(historyStepsCount + 1); + undo(editor); + }, + contentAfter: '

a[bcd]e

', + }); + }); + it('should not restore cut content when cut followed by delete forward', async () => { + await testEditor(BasicEditor, { + contentBefore: '

a[]bcde

', + stepFunction: async editor => { + // Set selection to a[bcd]e. + const selection = editor.document.getSelection(); + selection.extend(selection.anchorNode, 4); + triggerEvent(editor.editable, 'cut', { clipboardData: new DataTransfer() }); + triggerEvent(editor.editable, 'input', { + inputType: 'deleteContentForward' + }); + }, + contentAfter: '

a[]

', + }); + }); }); });