Skip to content

Commit 9c680fa

Browse files
[IMP] apply todos: batch 2
1 parent ab32ef9 commit 9c680fa

File tree

7 files changed

+97
-111
lines changed

7 files changed

+97
-111
lines changed

addons/html_editor/static/src/main/toolbar/toolbar.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,11 @@ export function composeToolbarButton(userCommand, toolbarItem) {
8787
isAvailable: (selection) =>
8888
[userCommand.isAvailable, toolbarItem.isAvailable]
8989
.filter(Boolean)
90-
.every((predicate) => predicate(selection)),
90+
.every((predicate) => {
91+
console.log("heyy");
92+
return predicate(selection);
93+
}),
94+
9195
description: description instanceof Function ? description : () => description,
9296
};
9397
}

addons/mass_mailing/static/src/fields/html_field/mass_mailing_html_field.js

Lines changed: 43 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { MassMailingIframe } from "@mass_mailing/iframe/mass_mailing_iframe";
44
import { ThemeSelector } from "@mass_mailing/themes/theme_selector/theme_selector";
55
import { onWillUpdateProps, status, toRaw, useEffect, useRef } from "@odoo/owl";
66
import { useChildRef, useService } from "@web/core/utils/hooks";
7-
import { useTransition } from "@web/core/transition";
87
import { effect } from "@web/core/utils/reactive";
98
import { htmlField, HtmlField } from "@html_editor/fields/html_field";
109
import { normalizeHTML, parseHTML } from "@html_editor/utils/html";
@@ -41,22 +40,12 @@ export class MassMailingHtmlField extends HtmlField {
4140
this.iframeRef = useChildRef();
4241
this.codeViewButtonRef = useRef("codeViewButtonRef");
4342

44-
// Use a transition to display the HtmlField only when the themes
45-
// service finished loading
46-
this.displayTransition = useTransition({
47-
name: "mass_mailing_html_field",
48-
initialVisibility: false,
49-
immediate: false,
50-
leaveDuration: 150,
51-
onLeave: () => {},
52-
});
53-
54-
// Force a full reload for MassMailingIframe on readonly change
5543
onWillUpdateProps((nextProps) => {
5644
if (
5745
this.props.readonly !== nextProps.readonly &&
5846
(this.props.readonly || nextProps.readonly)
5947
) {
48+
// Force a full reload for MassMailingIframe on readonly change
6049
this.state.key++;
6150
}
6251
if (nextProps.readonly) {
@@ -65,27 +54,30 @@ export class MassMailingHtmlField extends HtmlField {
6554
if (nextProps.record.isNew) {
6655
Object.assign(toRaw(this.state), {
6756
activeTheme: undefined,
57+
showCodeView: false,
6858
showThemeSelector: true,
69-
showCodeView: true,
7059
});
7160
}
7261
});
7362

74-
// Recompute the themeOptions when the html value changes on the record
7563
let currentKey;
7664
effect(
7765
(state) => {
7866
if (status(this) === "destroyed") {
7967
return;
8068
}
8169
if (state.key !== currentKey) {
82-
this.updateActiveTheme();
70+
// html value may have been reset from the server:
71+
// - await the new iframe
8372
this.resetIframe();
73+
// - ensure that the activeTheme is up to date.
74+
this.updateActiveTheme();
8475
currentKey = state.key;
8576
}
8677
},
8778
[this.state]
8879
);
80+
8981
// Evaluate if the themeSelector should be displayed
9082
effect(
9183
(state) => {
@@ -95,25 +87,26 @@ export class MassMailingHtmlField extends HtmlField {
9587
const activeTheme = state.activeTheme;
9688
const showThemeSelector = state.showThemeSelector;
9789
if (!activeTheme && !showThemeSelector && !this.props.readonly) {
98-
// Always show the theme selector when the theme is unknown
90+
// Show the ThemeSelector when the theme is unknown and the content can be
91+
// changed (invalid value).
9992
state.showThemeSelector = true;
10093
} else if ((activeTheme && showThemeSelector) || this.props.readonly) {
10194
state.showThemeSelector = false;
10295
}
10396
if (showThemeSelector && toRaw(state.showCodeView)) {
104-
// Ensure that the code view is always disabled when the theme selector
105-
// is displayed
97+
// Never show the CodeView with the ThemeSelector.
10698
state.showCodeView = false;
10799
}
108100
},
109101
[this.state]
110102
);
111-
// Sets the iframe height
103+
112104
useEffect(
113105
() => {
114106
if (!this.codeViewRef.el) {
115107
return;
116108
}
109+
// Set the initial textArea height.
117110
this.codeViewRef.el.style.height = this.codeViewRef.el.scrollHeight + "px";
118111
},
119112
() => [this.codeViewRef.el]
@@ -129,6 +122,10 @@ export class MassMailingHtmlField extends HtmlField {
129122
this.lastIframeLoaded = this.keepLastIframe.add(this.iframeLoaded);
130123
}
131124

125+
onIframeLoad(iframeLoaded) {
126+
this.iframeLoaded.resolve(iframeLoaded);
127+
}
128+
132129
updateActiveTheme() {
133130
const activeTheme = this.themeService.getThemeName(this.value);
134131
if (toRaw(this.state).activeTheme !== activeTheme) {
@@ -157,18 +154,15 @@ export class MassMailingHtmlField extends HtmlField {
157154
}
158155

159156
/**
160-
* Contrary to the super method, toggling the codeView in mass_mailing
161-
* does not keep the editor alive, as the editor does properly support
162-
* reinsertion. Instead, a new editor will be reconstructed from the
163-
* codeView value, and all plugins will be instantiated from scratch.
157+
* Content reinsertion as done in the super method is not properly supported
158+
* by the editor (corrupted state). This override forces the creation of
159+
* a new editor instead, and all plugins will be instantiated from scratch.
164160
* @override
165161
*/
166162
async toggleCodeView() {
167163
await this.commitChanges();
168164
this.state.showCodeView = !this.state.showCodeView;
169-
if (!this.state.showCodeView && this.editor) {
170-
this.state.key++;
171-
}
165+
this.state.key++;
172166
}
173167

174168
/**
@@ -193,22 +187,26 @@ export class MassMailingHtmlField extends HtmlField {
193187

194188
getBuilderConfig() {
195189
const config = super.getConfig();
196-
// All plugins for the html builder are defined in mass_mailing_builder
190+
// All plugins for the html Builder are defined in MassMailingBuilder
197191
delete config.Plugins;
198192
return {
199193
...config,
200-
allowMediaDialogVideo: false, // videos aren't allowed in mails
201-
withBuilder: true,
202-
toggleThemeSelector: (show) => this.toggleThemeSelector(show),
203194
mobileBreakpoint: "md",
204195
};
205196
}
206197

207198
getSimpleEditorConfig() {
199+
const config = super.getConfig();
200+
const codeViewCommand = [config.resources?.user_commands]
201+
.filter(Boolean)
202+
.flat()
203+
.find((cmd) => cmd.id === "codeview");
204+
if (codeViewCommand) {
205+
codeViewCommand.isAvailable = () => this.env.debug;
206+
}
208207
return {
209-
...super.getConfig(),
208+
...config,
210209
Plugins: [...MAIN_EDITOR_PLUGINS, DynamicPlaceholderPlugin],
211-
toggleThemeSelector: (show) => this.toggleThemeSelector(show),
212210
};
213211
}
214212

@@ -224,33 +222,15 @@ export class MassMailingHtmlField extends HtmlField {
224222
};
225223
}
226224

227-
onIframeLoad(iframeLoaded) {
228-
this.iframeLoaded.resolve(iframeLoaded);
229-
}
230-
231-
toggleThemeSelector(show = true) {
232-
this.state.showThemeSelector = show;
233-
}
234-
235-
/**
236-
* Process the content at a specifically designed location to avoid
237-
* interference with the UI.
238-
* @override
239-
*/
240-
insertForInlineProcessing(el) {
241-
const processingContainer = this.iframeRef.el.contentDocument.querySelector(
242-
".o_mass_mailing_processing_container"
243-
);
244-
processingContainer.append(el);
245-
}
246-
247225
onTextareaInput(ev) {
226+
this.onChange();
248227
ev.target.style.height = ev.target.scrollHeight + "px";
249228
}
250229

251230
/**
252231
* Ensure that the inlineField has its first value set (in case the
253-
* body_arch was set manually without using this widget).
232+
* field value was set manually without using this widget).
233+
* @override
254234
*/
255235
async _commitChanges({ urgent }) {
256236
if (
@@ -269,20 +249,21 @@ export class MassMailingHtmlField extends HtmlField {
269249
* Complete rewrite of `updateValue` to ensure that both the field and the
270250
* inlineField are saved at the same time. Depends on the iframe to compute
271251
* the style of the inlineField.
272-
* TODO EGGMAIL: this is too slow for urgent save. We should display the
273-
* save confirmation popup like in website on beforeUnload, unlike other
274-
* html_fields in form views.
275252
* @override
276253
*/
277254
async updateValue(value) {
278255
await this.lastIframeLoaded;
279256
this.lastValue = normalizeHTML(value, this.clearElementToCompare.bind(this));
280257
this.isDirty = false;
281258
const shouldRestoreDisplayNone = this.iframeRef.el.classList.contains("d-none");
259+
// d-none must be removed for style computation.
282260
this.iframeRef.el.classList.remove("d-none");
283261
const processingEl = this.iframeRef.el.contentDocument.createElement("DIV");
284262
processingEl.append(parseHTML(this.iframeRef.el.contentDocument, value));
285-
this.insertForInlineProcessing(processingEl);
263+
const processingContainer = this.iframeRef.el.contentDocument.querySelector(
264+
".o_mass_mailing_processing_container"
265+
);
266+
processingContainer.append(processingEl);
286267
const cssRules = getCSSRules(this.iframeRef.el.contentDocument);
287268
await toInline(processingEl, cssRules);
288269
const inlineValue = processingEl.innerHTML;
@@ -305,11 +286,16 @@ export const massMailingHtmlField = {
305286
component: MassMailingHtmlField,
306287
extractProps({ attrs, options }) {
307288
const props = htmlField.extractProps(...arguments);
289+
props.editorConfig.allowVideo = false;
290+
props.editorConfig.baseContainers = ["P"];
308291
Object.assign(props, {
309292
filterTemplates: options.filterTemplates,
310293
inlineField: options["inline_field"],
311294
migrateHTML: false,
312295
embeddedComponents: false,
296+
isCollaborative: false,
297+
sandboxedPreview: false,
298+
codeview: true,
313299
});
314300
return props;
315301
},

addons/mass_mailing/static/src/fields/html_field/mass_mailing_html_field.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
<t t-if="state.showCodeView">
77
<div class="d-flex">
88
<textarea name="codeView" t-ref="codeView" class="o_codeview" t-att-value="this.value"
9-
t-on-change="onChange" t-on-input="onTextareaInput"/>
9+
t-on-input="onTextareaInput"/>
1010
<div t-if="state.showCodeView" t-ref="codeViewButtonRef"
11-
class="btn-group o_mass_mailing_code_view" t-on-click="toggleCodeView">
12-
<button class="o_codeview_btn btn btn-primary">
11+
class="btn-group o_mass_mailing_code_view">
12+
<button class="o_codeview_btn btn btn-primary" t-on-click="toggleCodeView" accesskey="d">
1313
<i class="fa fa-code" />
1414
</button>
1515
</div>

0 commit comments

Comments
 (0)