Skip to content

Commit

Permalink
[FIX] web_editor: discard changes html field
Browse files Browse the repository at this point in the history
Issue:
======
Discard changes of form having html field doesn't remove the changes
applied in the html field.

Steps to reproduce the issue:
=============================
- Open any mail template
- Add modification on the template
- Click on discard changes

Origin of the issue:
====================
The function `this.props.update` is responsible of updating `_changes`
and updating the record which is called for usual input_field using
`useInputField` hook, but since this html field isn't of the same format
we didn't use it and it's only called in `commitChanges`

Solution:
=========
We update the value of in `_onWysiwygBlur` in case we are `inlineStyle`
so we don't commit but we save the changes for the discard to work
properly.

task-3453497

closes #149601

Signed-off-by: Antoine Guenet (age) <age@odoo.com>
  • Loading branch information
Mtaylorr committed Jan 30, 2024
1 parent a28a9c3 commit 3573167
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
4 changes: 3 additions & 1 deletion addons/web_editor/static/src/js/backend/html_field.js
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,9 @@ export class HtmlField extends Component {
}
_onWysiwygBlur() {
// Avoid save on blur if the html field is in inline mode.
if (!this.props.isInlineStyle) {
if (this.props.isInlineStyle) {
this.updateValue();
} else {
this.commitChanges();
}
}
Expand Down
42 changes: 42 additions & 0 deletions addons/web_editor/static/tests/html_field_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { MediaDialog } from "@web_editor/components/media_dialog/media_dialog";
import { parseHTML, setSelection } from "@web_editor/js/editor/odoo-editor/src/utils/utils";
import { onRendered } from "@odoo/owl";
import { wysiwygData } from "web_editor.test_utils";
import { insertText } from '@web_editor/js/editor/odoo-editor/test/utils'

// Legacy
import legacyEnv from 'web.commonEnv';
Expand Down Expand Up @@ -105,6 +106,47 @@ QUnit.module("WebEditor.HtmlField", ({ beforeEach }) => {
assert.equal(mediaDialog.props.resId, 2);
});

QUnit.test("discard html field changes in form", async (assert) => {
serverData.models.partner.records = [{ id: 1, txt: "<p>first</p>" }];
let wysiwyg;
const wysiwygPromise = makeDeferred();
patchWithCleanup(HtmlField.prototype, {
async startWysiwyg() {
await this._super(...arguments);
wysiwyg = this.wysiwyg;
wysiwygPromise.resolve();
},
});
await makeView({
type: "form",
resId: 1,
resModel: "partner",
serverData,
arch: `
<form>
<field name="txt" widget="html" options="{'style-inline' : true}"/>
</form>`,
});
await wysiwygPromise;
const editor = wysiwyg.odooEditor;
const editable = editor.editable;
editor.testMode = true;
assert.strictEqual(editable.innerHTML, `<p>first</p>`);
const paragraph = editable.querySelector("p");
await setSelection(paragraph, 0);
await insertText(editor, "a");
assert.strictEqual(editable.innerHTML, `<p>afirst</p>`);
// For blur event here to call _onWysiwygBlur function in html_field
await editable.dispatchEvent(new Event("blur", { bubbles: true, cancelable: true }));
// Wait for the updates to be saved , if we don't wait the update of the value will
// be done after the call for discardChanges since it uses some async functions.
await new Promise((r) => setTimeout(r, 100));
const discardButton = target.querySelector(".o_form_button_cancel");
assert.ok(discardButton);
await click(discardButton);
assert.strictEqual(editable.innerHTML, `<p>first</p>`);
});

QUnit.module('Sandboxed Preview');

QUnit.test("complex html is automatically in sandboxed preview mode", async (assert) => {
Expand Down

0 comments on commit 3573167

Please sign in to comment.