From fac09ae974ebb7bc552ac0b9f59836b4b89e9e34 Mon Sep 17 00:00:00 2001 From: Nicolas Bayet Date: Wed, 6 Sep 2023 10:26:10 +0200 Subject: [PATCH] [IMP] web_editor,mass_mailing,website: lazy load wysiwyg In order to improve the loading time of the odoo backend, only load the wysiwyg before starting an html_field. task-3493014 Part-of: odoo/odoo#134464 --- addons/mail/__manifest__.py | 4 +++ addons/mass_mailing/__manifest__.py | 4 +-- .../static/src/js/mass_mailing_html_field.js | 10 +++---- .../static/src/js/mass_mailing_wysiwyg.js | 5 ++-- addons/project/__manifest__.py | 1 - addons/web_editor/__manifest__.py | 8 +++--- .../static/src/js/backend/html_field.js | 26 +++++++++++++++---- .../static/src/js/backend/html_field.xml | 9 ++++--- .../js/frontend/loadWysiwygFromTextarea.js | 6 ++--- .../static/src/js/frontend/loader.js | 17 ------------ .../static/src/js/wysiwyg/wysiwyg.js | 4 +-- addons/website/__manifest__.py | 8 ++++-- .../static/src/components/editor/editor.js | 4 +-- .../static/src/components/editor/editor.xml | 25 +++++++++--------- .../wysiwyg_adapter/wysiwyg_adapter.js | 7 +++-- .../static/src/services/website_service.js | 2 +- addons/website_blog/__manifest__.py | 4 ++- addons/website_sale/__manifest__.py | 4 ++- 18 files changed, 79 insertions(+), 69 deletions(-) delete mode 100644 addons/web_editor/static/src/js/frontend/loader.js diff --git a/addons/mail/__manifest__.py b/addons/mail/__manifest__.py index e13a8131df1..cd2f6eca044 100644 --- a/addons/mail/__manifest__.py +++ b/addons/mail/__manifest__.py @@ -134,6 +134,7 @@ For more specific needs, you may also assign custom-defined actions 'mail/static/src/core/web/**/*', 'mail/static/src/**/common/**/*', 'mail/static/src/**/web/**/*', + ('remove', 'mail/static/src/core/web/wysiwyg.js'), ('remove', 'mail/static/src/**/*.dark.scss'), # discuss (loaded last to fix dependencies) ('remove', 'mail/static/src/discuss/**/*'), @@ -144,6 +145,9 @@ For more specific needs, you may also assign custom-defined actions ('remove', 'mail/static/src/discuss/**/*.dark.scss'), 'mail/static/src/views/fields/**/*', ], + 'web_editor.backend_assets_wysiwyg': [ + 'mail/static/src/core/web/wysiwyg.js', + ], "web.assets_web_dark": [ 'mail/static/src/**/*.dark.scss', ], diff --git a/addons/mass_mailing/__manifest__.py b/addons/mass_mailing/__manifest__.py index b9a86a6a15b..194a050f931 100644 --- a/addons/mass_mailing/__manifest__.py +++ b/addons/mass_mailing/__manifest__.py @@ -96,7 +96,7 @@ ('include', 'mass_mailing.assets_mail_themes'), ('include', 'web.assets_frontend'), ('after', 'web/static/lib/bootstrap/scss/_variables.scss', 'mass_mailing/static/src/scss/mass_mailing.ui.scss'), - ('include', 'web_editor.assets_wysiwyg'), + ('include', 'web_editor.backend_assets_wysiwyg'), ('include', 'web_editor.assets_legacy_wysiwyg'), 'mass_mailing/static/src/scss/mass_mailing_mail.scss', @@ -115,7 +115,7 @@ 'mass_mailing/static/src/xml/mailing_portal_subscription_feedback.xml', 'mass_mailing/static/src/xml/mailing_portal_subscription_form.xml', ], - 'web_editor.assets_wysiwyg': [ + 'web_editor.backend_assets_wysiwyg': [ 'mass_mailing/static/src/js/mass_mailing_wysiwyg.js', 'mass_mailing/static/src/scss/mass_mailing.wysiwyg.scss', ], diff --git a/addons/mass_mailing/static/src/js/mass_mailing_html_field.js b/addons/mass_mailing/static/src/js/mass_mailing_html_field.js index 9639c360285..f4892a1efaf 100644 --- a/addons/mass_mailing/static/src/js/mass_mailing_html_field.js +++ b/addons/mass_mailing/static/src/js/mass_mailing_html_field.js @@ -12,7 +12,6 @@ import { useService } from "@web/core/utils/hooks"; import { HtmlField, htmlField } from "@web_editor/js/backend/html_field"; import { MassMailingMobilePreviewDialog } from "./mass_mailing_mobile_preview"; import { getRangePosition } from '@web_editor/js/editor/odoo-editor/src/utils/utils'; -import { MassMailingWysiwyg } from '@mass_mailing/js/mass_mailing_wysiwyg'; import { utils as uiUtils } from "@web/core/ui/ui_service"; import { useSubEnv, status } from "@odoo/owl"; @@ -24,10 +23,6 @@ export class MassMailingHtmlField extends HtmlField { inlineField: { type: String, optional: true }, iframeHtmlClass: { type: String, optional: true }, } - static components = { - ...HtmlField.components, - Wysiwyg: MassMailingWysiwyg, - } setup() { super.setup(); @@ -616,6 +611,11 @@ export class MassMailingHtmlField extends HtmlField { } await super._setupReadonlyIframe(); } + async _lazyloadWysiwyg() { + await super._lazyloadWysiwyg(...arguments); + const wysiwygModule = await odoo.loader.modules.get('@mass_mailing/js/mass_mailing_wysiwyg'); + this.Wysiwyg = wysiwygModule.MassMailingWysiwyg; + } } export const massMailingHtmlField = { diff --git a/addons/mass_mailing/static/src/js/mass_mailing_wysiwyg.js b/addons/mass_mailing/static/src/js/mass_mailing_wysiwyg.js index 949997bbec7..dc15f2f85fd 100644 --- a/addons/mass_mailing/static/src/js/mass_mailing_wysiwyg.js +++ b/addons/mass_mailing/static/src/js/mass_mailing_wysiwyg.js @@ -1,10 +1,10 @@ /** @odoo-module **/ +import { loadBundle } from "@web/core/assets"; import { attachComponent } from "@web/legacy/utils"; import { Wysiwyg } from "@web_editor/js/wysiwyg/wysiwyg"; import { closestElement } from "@web_editor/js/editor/odoo-editor/src/OdooEditor"; import { Toolbar } from "@web_editor/js/editor/toolbar"; -import { requireWysiwygLegacyModule } from "@web_editor/js/frontend/loader"; import "@web_editor/js/wysiwyg/wysiwyg_iframe"; export class MassMailingWysiwyg extends Wysiwyg { @@ -114,7 +114,8 @@ export class MassMailingWysiwyg extends Wysiwyg { * @override */ async _createSnippetsMenuInstance(options={}) { - const { MassMailingSnippetsMenu } = await requireWysiwygLegacyModule('@mass_mailing/js/snippets.editor'); + await loadBundle('web_editor.assets_legacy_wysiwyg'); + const { MassMailingSnippetsMenu } = await odoo.loader.modules.get('@mass_mailing/js/snippets.editor'); return new MassMailingSnippetsMenu(this, Object.assign({ wysiwyg: this, selectorEditableArea: '.o_editable', diff --git a/addons/project/__manifest__.py b/addons/project/__manifest__.py index dd2db436234..07590c7515e 100644 --- a/addons/project/__manifest__.py +++ b/addons/project/__manifest__.py @@ -208,7 +208,6 @@ 'web_editor/static/src/scss/web_editor.common.scss', 'web_editor/static/src/scss/web_editor.backend.scss', - 'web_editor/static/src/js/frontend/loader.js', 'web_editor/static/src/js/backend/**/*', 'web_editor/static/src/xml/backend.xml', diff --git a/addons/web_editor/__manifest__.py b/addons/web_editor/__manifest__.py index 61720b42bb1..bea5f58054b 100644 --- a/addons/web_editor/__manifest__.py +++ b/addons/web_editor/__manifest__.py @@ -170,6 +170,9 @@ Odoo Web Editor widget. 'web_editor/static/src/xml/wysiwyg.xml', 'web_editor/static/src/xml/wysiwyg_colorpicker.xml', ], + 'web_editor.backend_assets_wysiwyg': [ + ('include', 'web_editor.assets_wysiwyg'), + ], 'web.assets_backend': [ 'web_editor/static/src/js/editor/odoo-editor/src/base_style.scss', 'web_editor/static/lib/vkbeautify/**/*', @@ -183,11 +186,8 @@ Odoo Web Editor widget. 'web_editor/static/src/scss/web_editor.common.scss', 'web_editor/static/src/scss/web_editor.backend.scss', - 'web_editor/static/src/js/frontend/loader.js', 'web_editor/static/src/js/backend/**/*', 'web_editor/static/src/xml/backend.xml', - - ('include', 'web_editor.assets_wysiwyg'), ], "web.assets_web_dark": [ 'web_editor/static/src/scss/odoo-editor/powerbox.dark.scss', @@ -210,7 +210,6 @@ Odoo Web Editor widget. 'web_editor/static/src/scss/web_editor.common.scss', 'web_editor/static/src/scss/web_editor.frontend.scss', - 'web_editor/static/src/js/frontend/loader.js', 'web_editor/static/src/js/frontend/loadWysiwygFromTextarea.js', ], @@ -239,6 +238,7 @@ Odoo Web Editor widget. 'web.qunit_suite_tests': [ ('include', 'web_editor.assets_legacy_wysiwyg'), + ('include', 'web_editor.backend_assets_wysiwyg'), 'web_editor/static/tests/**/*', 'web_editor/static/src/js/editor/odoo-editor/test/utils.js' diff --git a/addons/web_editor/static/src/js/backend/html_field.js b/addons/web_editor/static/src/js/backend/html_field.js index 642cc60e7c1..db24ae00f8e 100644 --- a/addons/web_editor/static/src/js/backend/html_field.js +++ b/addons/web_editor/static/src/js/backend/html_field.js @@ -4,7 +4,6 @@ import { registry } from "@web/core/registry"; import { _t } from "@web/core/l10n/translation"; import { standardFieldProps } from "@web/views/fields/standard_field_props"; import { QWebPlugin } from '@web_editor/js/backend/QWebPlugin'; -import { MoveNodePlugin } from '@web_editor/js/wysiwyg/MoveNodePlugin'; import { TranslationButton } from "@web/views/fields/translation_button"; import { useDynamicPlaceholder } from "@web/views/fields/dynamic_placeholder_hook"; import { @@ -18,7 +17,7 @@ import { getRangePosition } from '@web_editor/js/editor/odoo-editor/src/utils/utils'; import { toInline } from '@web_editor/js/backend/convert_inline'; -import { getBundle } from '@web/core/assets'; +import { getBundle, loadBundle } from '@web/core/assets'; import { Component, useRef, @@ -31,13 +30,16 @@ import { status, } from "@odoo/owl"; import { uniqueId } from '@web/core/utils/functions'; -import { Wysiwyg, stripHistoryIds } from '../wysiwyg/wysiwyg'; +// Ensure `@web/views/fields/html/html_field` is loaded first as this module +// must override the html field in the registry. +import '@web/views/fields/html/html_field'; + +let stripHistoryIds; export class HtmlField extends Component { static template = "web_editor.HtmlField"; static components = { TranslationButton, - Wysiwyg, }; static defaultProps = { dynamicPlaceholder: false }; static props = { @@ -92,6 +94,7 @@ export class HtmlField extends Component { if (this.props.cssReadonlyAssetId) { this.cssReadonlyAsset = await getBundle(this.props.cssReadonlyAssetId); } + await this._lazyloadWysiwyg(); }); this._lastRecordInfo = { res_model: this.props.record.resModel, @@ -239,6 +242,7 @@ export class HtmlField extends Component { res_id: this.props.record.resId, }, fieldId: this.props.id, + editorPlugins: [...(wysiwygOptions.editorPlugins || []), QWebPlugin, this.MoveNodePlugin], }; } /** @@ -393,6 +397,19 @@ export class HtmlField extends Component { } } } + async _lazyloadWysiwyg() { + // In some bundle (eg. `web.qunit_suite_tests`), the following module is already included. + let wysiwygModule = await odoo.loader.modules.get('@web_editor/js/wysiwyg/wysiwyg'); + this.MoveNodePlugin = (await odoo.loader.modules.get('@web_editor/js/wysiwyg/MoveNodePlugin'))?.MoveNodePlugin; + // Otherwise, load the module. + if (!wysiwygModule) { + await loadBundle('web_editor.backend_assets_wysiwyg'); + wysiwygModule = await odoo.loader.modules.get('@web_editor/js/wysiwyg/wysiwyg'); + this.MoveNodePlugin = (await odoo.loader.modules.get('@web_editor/js/wysiwyg/MoveNodePlugin')).MoveNodePlugin; + } + stripHistoryIds = wysiwygModule.stripHistoryIds; + this.Wysiwyg = wysiwygModule.Wysiwyg; + } _isDirty() { const strippedPropValue = stripHistoryIds(String(this.props.record.data[this.props.name])); const strippedEditingValue = stripHistoryIds(this.getEditingValue()); @@ -685,7 +702,6 @@ export const htmlField = { minHeight: options.minHeight, maxHeight: options.maxHeight, resizable: 'resizable' in options ? options.resizable : false, - editorPlugins: [QWebPlugin, MoveNodePlugin], }; if ('collaborative' in options) { wysiwygOptions.collaborative = options.collaborative; diff --git a/addons/web_editor/static/src/js/backend/html_field.xml b/addons/web_editor/static/src/js/backend/html_field.xml index db8c78091c2..9d9d6bebe62 100644 --- a/addons/web_editor/static/src/js/backend/html_field.xml +++ b/addons/web_editor/static/src/js/backend/html_field.xml @@ -16,10 +16,11 @@