1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
/*
Copyright 2021 Camptocamp SA (https://www.camptocamp.com).
@author Iván Todorovich <ivan.todorovich@camptocamp.com>
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
*/
odoo.define("web_widget_ckeditor.field_ckeditor", function (require) {
"use strict";
const core = require("web.core");
const session = require("web.session");
const config = require("web.config");
const ajax = require("web.ajax");
const rpc = require("web.rpc");
const basic_fields = require("web.basic_fields");
const field_registry = require("web.field_registry");
const _lt = core._lt;
const TranslatableFieldMixin = basic_fields.TranslatableFieldMixin;
// Load configuration for the editor
const getCKEditorConfigPromise = rpc.query({
model: "ir.config_parameter",
method: "get_web_widget_ckeditor_config",
});
// Load CKEditor localization files
async function loadCKEditorLanguageSource(languageCode) {
if (languageCode === "en") {
return;
}
const languageURL = `/web_widget_ckeditor/static/lib/ckeditor/build/translations/${languageCode}.js`;
try {
ajax.loadJS(languageURL);
} catch (error) {
console.warn("Unable to load CKEditor language: ", languageCode);
}
}
const CKEditorLanguageCode = session.user_context.lang.split("_")[0];
const loadCKEditorLanguagePromise = loadCKEditorLanguageSource(
CKEditorLanguageCode
);
const FieldHtmlCKEditor = basic_fields.DebouncedField.extend(
TranslatableFieldMixin,
{
description: _lt("Html (CKEditor)"),
className: "oe_form_field oe_form_field_html oe_form_field_html_ckeditor",
supportedFieldTypes: ["html"],
/**
* @override
*/
willStart: function () {
return Promise.all([
this._super.apply(this, arguments),
loadCKEditorLanguagePromise,
]);
},
/**
* @override
*/
destroy: function () {
if (this.ckeditor) {
this.ckeditor.destroy();
this.ckeditor = undefined;
}
return this._super();
},
// --------------------------------------------------------------------------
// Public
// --------------------------------------------------------------------------
/**
* @override
*/
activate: function () {
if (this.ckeditor) {
this.ckeditor.focus();
return true;
}
},
/**
* This function is similar to the one found in core's web_editor.FieldHtml.
*
* @override
*/
isSet: function () {
// Removing spaces & html spaces
const value =
this.value &&
this.value.split(" ").join("").replace(/\s/g, "");
return (
value &&
value !== "<p></p>" &&
value !== "<p><br></p>" &&
value.match(/\S/)
);
},
/**
* This function is similar to the one found in core's web_editor.FieldHtml.
*
* @override
*/
getFocusableElement: function () {
return this.$target || $();
},
/**
* Do not re-render this field if it was the origin of the onchange call.
* This function is similar to the one found in core's web_editor.FieldHtml.
*
* @override
*/
reset: function (record, event) {
this._reset(record, event);
const value = this._textToHtml(this.value);
if (!event || event.target !== this) {
if (this.mode === "edit") {
this.ckeditor.setData(value);
} else {
this.$content.html(value);
}
}
return Promise.resolve();
},
// --------------------------------------------------------------------------
// Private
// --------------------------------------------------------------------------
/**
* @override
*/
_getValue: function () {
if (this.mode === "edit" && this.ckeditor) {
return this.ckeditor.getData();
}
return this.$target.val();
},
/**
* Gets the CKEditor toolbar items configuration.
* If not found, returns the default configuration.
*/
_getCKEditorToolbarItems: async function () {
try {
const ckconfig = await getCKEditorConfigPromise;
if (ckconfig.toolbar) {
return ckconfig.toolbar.split(/[\s,]+/).filter((item) => item);
}
} catch (error) {
console.warn(
"Unable to use CKEditor toolbar configuration: ",
error
);
console.warn(
"Please check the value for ir.config_parameter 'web_widget_ckeditor.toolbar' is correct"
);
console.warn("Using default toolbar configuration");
}
return [
"heading",
"|",
"bold",
"italic",
"underline",
"removeFormat",
"|",
"fontSize",
"fontColor",
"fontBackgroundColor",
"|",
"bulletedList",
"numberedList",
"alignment",
"|",
"outdent",
"indent",
"pagebreak",
"|",
"link",
"imageUpload",
"blockQuote",
"insertTable",
"|",
"undo",
"redo",
];
},
/**
* Gets the CKEditor configuration.
* See for details:
* https://ckeditor.com/docs/ckeditor5/latest/api/module_core_editor_editorconfig-EditorConfig.html
*
* @returns EditorConfig
*/
_getCKEditorConfig: async function () {
const res = {
toolbar: {
items: await this._getCKEditorToolbarItems(),
shouldNotGroupWhenFull: true,
},
language: CKEditorLanguageCode,
image: {
toolbar: [
"imageTextAlternative",
"imageStyle:inline",
"imageStyle:block",
"imageStyle:side",
"linkImage",
],
},
table: {
contentToolbar: [
"tableColumn",
"tableRow",
"mergeTableCells",
"tableCellProperties",
"tableProperties",
],
},
link: {
addTargetToExternalLinks: true
},
};
if (config.isDebug()) {
res.toolbar.items.push("|");
res.toolbar.items.push("sourceEditing");
}
return res;
},
/**
* Create the CKEditor instance with the target (this.$target)
* then add the editable content (this.$content).
*
* @private
* @returns {$.Promise}
*/
_createCKEditorIntance: async function () {
const editorConfig = await this._getCKEditorConfig();
this.ckeditor = await window.ClassicEditor.create(
this.$target.get(0),
editorConfig
);
// Register event hooks
this.ckeditor.on("change", () => this._onChange());
this.ckeditor.ui.focusTracker.on(
"change:isFocused",
(ev, name, isFocused) => (isFocused ? null : this._onChange())
);
this._onLoadCKEditor();
},
/**
* @override
*/
_renderEdit: function () {
const value = this._textToHtml(this.value);
this.$target = $("<textarea>").val(value).hide();
this.$target.appendTo(this.$el);
return this._createCKEditorIntance();
},
/**
* @override
*/
_renderReadonly: function () {
const value = this._textToHtml(this.value);
this.$el.empty();
this.$content = $('<div class="o_readonly"/>').html(value);
this.$content.appendTo(this.$el);
},
/**
* This function is similar to the one found in core's web_editor.FieldHtml.
*
* @private
* @param {String} text
* @returns {String} the text converted to html
*/
_textToHtml: function (text) {
let value = text || "";
try {
// Crashes if text isn't html
$(text)[0].innerHTML; // eslint-disable-line
} catch (e) {
if (value.match(/^\s*$/)) {
value = "<p><br/></p>";
} else {
value =
"<p>" +
value.split(/<br\/?>/).join("<br/></p><p>") +
"</p>";
value = value
.replace(/<p><\/p>/g, "")
.replace("<p><p>", "<p>")
.replace("<p><p ", "<p ")
.replace("</p></p>", "</p>");
}
}
return value;
},
// --------------------------------------------------------------------------
// Handler
// --------------------------------------------------------------------------
/**
* Method called when the CKEditor instance is loaded.
*
* @private
*/
_onLoadCKEditor: function () {
const $button = this._renderTranslateButton();
$button.css({
"font-size": "15px",
position: "absolute",
right: "+5px",
top: "+5px",
});
this.$el.append($button);
},
/**
* Method called when ckeditor triggers a change.
*
* @private
* @param {OdooEvent} ev
*/
_onChange: function () {
this._doDebouncedAction.apply(this, arguments);
},
}
);
field_registry.add("ckeditor", FieldHtmlCKEditor);
return FieldHtmlCKEditor;
});
|