[IMP] web_ckeditor4: pre-commit execution

pull/2083/head
Ivàn Todorovich 2021-11-15 12:37:34 -03:00
parent 19289c7577
commit 6f0b6b3969
14 changed files with 244 additions and 233 deletions

View File

@ -0,0 +1 @@
__import__('pkg_resources').declare_namespace(__name__)

View File

@ -0,0 +1 @@
__import__('pkg_resources').declare_namespace(__name__)

View File

@ -0,0 +1 @@
../../../../web_ckeditor4

View File

@ -0,0 +1,6 @@
import setuptools
setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)

View File

@ -1,4 +1,3 @@
# -*- encoding: utf-8 -*-
############################################################################## ##############################################################################
# #
# OpenERP, Open Source Management Solution # OpenERP, Open Source Management Solution

View File

@ -1,29 +1,27 @@
# -*- coding: utf-8 -*-
# Copyright 2018 Therp BV <https://therp.nl> # Copyright 2018 Therp BV <https://therp.nl>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
{ {
'name': 'CKEditor 4.x widget', "name": "CKEditor 4.x widget",
'version': '10.0.1.0.0', "version": "10.0.1.0.0",
'author': "Therp BV,Odoo Community Association (OCA)", "author": "Therp BV,Odoo Community Association (OCA)",
'website': 'https://github.com/OCA/web', "website": "https://github.com/OCA/web",
'summary': 'Provides a widget for editing HTML fields using CKEditor 4.x', "summary": "Provides a widget for editing HTML fields using CKEditor 4.x",
"category": "Tools", "category": "Tools",
'license': 'AGPL-3', "license": "AGPL-3",
"depends": [ "depends": [
'web', "web",
], ],
'data': [ "data": [
'templates/assets.xml', "templates/assets.xml",
], ],
'css': [ "css": [
'static/src/css/web_ckeditor4.css', "static/src/css/web_ckeditor4.css",
], ],
'data': [ "data": [],
], "js": [
'js': [ "static/src/js/ckeditor_basepath.js",
'static/src/js/ckeditor_basepath.js', "static/lib/ckeditor/ckeditor.js",
'static/lib/ckeditor/ckeditor.js', "static/lib/ckeditor/config.js",
'static/lib/ckeditor/config.js',
# to debug ckeditor, comment the lines above, # to debug ckeditor, comment the lines above,
# do a # do a
# cd static/lib # cd static/lib
@ -86,8 +84,8 @@
# 'static/lib/trunk/core/skin.js', # 'static/lib/trunk/core/skin.js',
# 'static/lib/trunk/core/_bootstrap.js', # 'static/lib/trunk/core/_bootstrap.js',
# end of ckeditor debug # end of ckeditor debug
'static/src/js/web_ckeditor4.js', "static/src/js/web_ckeditor4.js",
], ],
'installable': True, "installable": True,
'auto_install': False, "auto_install": False,
} }

View File

@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
############################################################################## ##############################################################################
# #
# OpenERP, Open Source Management Solution # OpenERP, Open Source Management Solution

View File

@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
############################################################################## ##############################################################################
# #
# OpenERP, Open Source Management Solution # OpenERP, Open Source Management Solution
@ -19,25 +18,26 @@
# #
############################################################################## ##############################################################################
import re import re
from lxml.html import clean from lxml.html import clean
from openerp import models from openerp import models
class CkeditorMonkeypatch(models.AbstractModel): class CkeditorMonkeypatch(models.AbstractModel):
_name = 'ckeditor.monkeypatch' _name = "ckeditor.monkeypatch"
_description = 'Monkeypatches for CKEditor' _description = "Monkeypatches for CKEditor"
def _register_hook(self): def _register_hook(self):
marker = self._name.replace('.', '_') marker = self._name.replace(".", "_")
if not hasattr(clean, marker): if not hasattr(clean, marker):
# monkey patch lxml's html cleaner to allow image data urls # monkey patch lxml's html cleaner to allow image data urls
if hasattr(clean, '_is_javascript_scheme'): if hasattr(clean, "_is_javascript_scheme"):
# this is the case in lxml >= 3.3 # this is the case in lxml >= 3.3
_is_javascript_scheme = clean._is_javascript_scheme _is_javascript_scheme = clean._is_javascript_scheme
_is_image_dataurl = re.compile( _is_image_dataurl = re.compile(r"^data:image/.+;base64", re.I).search
r'^data:image/.+;base64', re.I).search clean._is_javascript_scheme = (
clean._is_javascript_scheme = lambda s:\ lambda s: None if _is_image_dataurl(s) else _is_javascript_scheme(s)
None if _is_image_dataurl(s) else _is_javascript_scheme(s) )
# TODO: do something else for 2.3.1 <= version <= 3.2, before data # TODO: do something else for 2.3.1 <= version <= 3.2, before data
# urls were not cleaned at all # urls were not cleaned at all
setattr(clean, marker, True) setattr(clean, marker, True)

View File

@ -1,4 +1,7 @@
.odoo .oe_form_field_text_ckeditor4.disabled, .odoo td.oe_form_field_text_ckeditor4, .odoo .oe_form_field_text_ckeditor4_raw.disabled.odoo, .odoo td.oe_form_field_text_ckeditor4_raw { .odoo .oe_form_field_text_ckeditor4.disabled,
.odoo td.oe_form_field_text_ckeditor4,
.odoo .oe_form_field_text_ckeditor4_raw.disabled.odoo,
.odoo td.oe_form_field_text_ckeditor4_raw {
/* here we need to reset odoo's styles to /* here we need to reset odoo's styles to
* have the HTML display as (probably) intended * have the HTML display as (probably) intended
*/ */

View File

@ -1 +1 @@
CKEDITOR_BASEPATH='/web_ckeditor4/static/lib/ckeditor/' CKEDITOR_BASEPATH = "/web_ckeditor4/static/lib/ckeditor/";

View File

@ -20,41 +20,40 @@
# #
############################################################################*/ ############################################################################*/
odoo.define('web_ckeditor4', function(require){ odoo.define("web_ckeditor4", function (require) {
"use strict"; "use strict";
var core = require('web.core'); var core = require("web.core");
var session = require('web.session'); var session = require("web.session");
var formats = require('web.formats'); var formats = require("web.formats");
var ckconfig = require('web_ckeditor4.config'); var ckconfig = require("web_ckeditor4.config");
var FieldCKEditor4 = core.form_widget_registry.get('text').extend({ var FieldCKEditor4 = core.form_widget_registry.get("text").extend({
ckeditor_config: function () { ckeditor_config: function () {
return { return {
removePlugins: this._getRemovePlugins(), removePlugins: this._getRemovePlugins(),
removeButtons: this._getRemoveButtons(), removeButtons: this._getRemoveButtons(),
filebrowserImageUploadUrl: 'dummy', filebrowserImageUploadUrl: "dummy",
extraPlugins: 'filebrowser', extraPlugins: "filebrowser",
// this is '#39' per default which screws up single quoted text in ${} // This is '#39' per default which screws up single quoted text in ${}
entities_additional: '' entities_additional: "",
}; };
}, },
ckeditor_filter: ckconfig.default_ckeditor_filter, ckeditor_filter: ckconfig.default_ckeditor_filter,
ckeditor_writer: ckconfig.default_ckeditor_writer, ckeditor_writer: ckconfig.default_ckeditor_writer,
_getRemovePlugins: function () { _getRemovePlugins: function () {
return 'iframe,flash,forms,smiley,pagebreak,stylescombo'; return "iframe,flash,forms,smiley,pagebreak,stylescombo";
}, },
_getRemoveButtons: function () { _getRemoveButtons: function () {
return ''; return "";
}, },
init: function () { init: function () {
this._super.apply(this, arguments); this._super.apply(this, arguments);
this.editor_lang = session.user_context.lang.split('_')[0]; this.editor_lang = session.user_context.lang.split("_")[0];
this.view.on("load_record", this, this._on_load_record); this.view.on("load_record", this, this._on_load_record);
}, },
start: function() start: function () {
{
this._super.apply(this, arguments); this._super.apply(this, arguments);
CKEDITOR.lang.load(this.editor_lang, 'en', function() {}); CKEDITOR.lang.load(this.editor_lang, "en", function () {});
}, },
_on_load_record: function () { _on_load_record: function () {
/* Fix widget not re-initialized on form discard. /* Fix widget not re-initialized on form discard.
@ -75,22 +74,19 @@ odoo.define('web_ckeditor4', function(require){
this.initialize_content(); this.initialize_content();
} }
}, },
initialize_content: function() initialize_content: function () {
{
var self = this; var self = this;
this._super.apply(this, arguments); this._super.apply(this, arguments);
if(!this.$el) if (!this.$el) {
{
return; return;
} else if (!this.get('effective_readonly') && !this.editor) { } else if (!this.get("effective_readonly") && !this.editor) {
this.editor = CKEDITOR.replace(this.$el.get(0), this.editor = CKEDITOR.replace(
this.$el.get(0),
_.extend( _.extend(
{ {
language: this.editor_lang, language: this.editor_lang,
on: on: {
{ change: function () {
'change': function()
{
self.store_dom_value(); self.store_dom_value();
}, },
}, },
@ -100,82 +96,72 @@ odoo.define('web_ckeditor4', function(require){
); );
} }
}, },
store_dom_value: function() store_dom_value: function () {
{ this.internal_set_value(
this.internal_set_value(this.editor ? this.editor.getData() : formats.parse_value(this.get('value'), this)); this.editor
? this.editor.getData()
: formats.parse_value(this.get("value"), this)
);
}, },
filter_html: function(value) filter_html: function (value) {
{ return ckconfig.filter_html(
return ckconfig.filter_html(value, this.ckeditor_filter, this.ckeditor_writer); value,
this.ckeditor_filter,
this.ckeditor_writer
);
}, },
render_value: function() render_value: function () {
{ if (this.get("effective_readonly")) {
if(this.get("effective_readonly")) this.$el.html(this.filter_html(this.get("value")));
{ } else if (this.editor) {
this.$el.html(this.filter_html(this.get('value')));
}
else
{
if(this.editor)
{
var self = this; var self = this;
if(this.editor.status != 'ready') if (this.editor.status != "ready") {
{ var instanceReady = function () {
var instanceReady = function() self.editor.setData(self.get("value") || "");
{ self.editor.removeListener("instanceReady", instanceReady);
self.editor.setData(self.get('value') || '');
self.editor.removeListener('instanceReady', instanceReady);
}; };
this.editor.on('instanceReady', instanceReady); this.editor.on("instanceReady", instanceReady);
} } else {
else self.editor.setData(self.get("value") || "");
{
self.editor.setData(self.get('value') || '');
}
} }
} }
}, },
destroy_content: function () { destroy_content: function () {
this._cleanup_editor(); this._cleanup_editor();
}, },
undelegateEvents: function() undelegateEvents: function () {
{
this._cleanup_editor(); this._cleanup_editor();
return this._super.apply(this, arguments); return this._super.apply(this, arguments);
}, },
_cleanup_editor: function() _cleanup_editor: function () {
{ if (this.editor && this.editor.status == "ready") {
if(this.editor && this.editor.status == 'ready')
{
CKEDITOR.remove(this.editor.name); CKEDITOR.remove(this.editor.name);
$('#cke_' + this.editor.name).remove(); $("#cke_" + this.editor.name).remove();
this.editor.removeAllListeners(); this.editor.removeAllListeners();
this.editor.destroy(); this.editor.destroy();
this.editor = null; this.editor = null;
} }
}, },
destroy: function() destroy: function () {
{
this.view.off("load_record", this, this._on_load_record); this.view.off("load_record", this, this._on_load_record);
this._cleanup_editor(); this._cleanup_editor();
this._super(); this._super();
} },
}); });
var FieldCKEditor4Raw = FieldCKEditor4.extend({ var FieldCKEditor4Raw = FieldCKEditor4.extend({
filter_html: function(value) filter_html: function (value) {
{
return value; return value;
} },
}); });
core.form_widget_registry.add('text_ckeditor4', FieldCKEditor4); core.form_widget_registry.add("text_ckeditor4", FieldCKEditor4);
core.form_widget_registry.add('text_ckeditor4_raw', FieldCKEditor4Raw); core.form_widget_registry.add("text_ckeditor4_raw", FieldCKEditor4Raw);
core.form_widget_registry.add('text_html', FieldCKEditor4); core.form_widget_registry.add("text_html", FieldCKEditor4);
core.form_widget_registry.add('html', FieldCKEditor4); core.form_widget_registry.add("html", FieldCKEditor4);
return { return {
'FieldCKEditor4': FieldCKEditor4, FieldCKEditor4: FieldCKEditor4,
'FieldCKEditor4Raw': FieldCKEditor4Raw FieldCKEditor4Raw: FieldCKEditor4Raw,
} };
}); });

View File

@ -20,11 +20,11 @@
# #
############################################################################*/ ############################################################################*/
odoo.define('web_ckeditor4.config', function (require) { odoo.define("web_ckeditor4.config", function (require) {
"use strict"; "use strict";
var ckeditor_addFunction_org = CKEDITOR.tools.addFunction; var ckeditor_addFunction_org = CKEDITOR.tools.addFunction;
// this is a quite complicated way to kind of monkey patch the private // This is a quite complicated way to kind of monkey patch the private
// method onDomReady of ckeditor's plugin wysiwigarea, which causes problems // method onDomReady of ckeditor's plugin wysiwigarea, which causes problems
// when the editor is about to be destroyed but because of OpenERP's // when the editor is about to be destroyed but because of OpenERP's
// architecture updated one last time with its current value // architecture updated one last time with its current value
@ -43,20 +43,24 @@ odoo.define('web_ckeditor4.config', function (require) {
return ckeditor_addFunction_org(fn, scope); return ckeditor_addFunction_org(fn, scope);
}; };
CKEDITOR.on('dialogDefinition', function (e) { CKEDITOR.on("dialogDefinition", function (e) {
_.each(e.data.definition.contents, function (element) { _.each(e.data.definition.contents, function (element) {
if (!element || element.filebrowser != 'uploadButton') { if (!element || element.filebrowser != "uploadButton") {
return return;
} }
_.each(element.elements, function (element) { _.each(element.elements, function (element) {
if (!element.onClick || element.type != 'fileButton') { if (!element.onClick || element.type != "fileButton") {
return return;
} }
var onClick_org = element.onClick; var onClick_org = element.onClick;
element.onClick = function (e1) { element.onClick = function (e1) {
onClick_org.apply(this, arguments); onClick_org.apply(this, arguments);
_.each($('#' + this.domId).closest('table') _.each(
.find('iframe').contents().find(':file') $("#" + this.domId)
.closest("table")
.find("iframe")
.contents()
.find(":file")
.get(0).files, .get(0).files,
function (file) { function (file) {
var reader = new FileReader(); var reader = new FileReader();
@ -64,12 +68,14 @@ odoo.define('web_ckeditor4.config', function (require) {
CKEDITOR.tools.callFunction( CKEDITOR.tools.callFunction(
e.editor._.filebrowserFn, e.editor._.filebrowserFn,
load_event.target.result, load_event.target.result,
''); ""
} );
};
reader.readAsDataURL(file); reader.readAsDataURL(file);
});
return false;
} }
);
return false;
};
}); });
}); });
}); });
@ -82,18 +88,17 @@ odoo.define('web_ckeditor4.config', function (require) {
}; };
var default_ckeditor_filter = new CKEDITOR.filter({ var default_ckeditor_filter = new CKEDITOR.filter({
'*': "*": {
{ attributes: "href,src,style,alt,width,height,dir",
attributes: 'href,src,style,alt,width,height,dir', styles: "*",
styles: '*', classes: "*",
classes: '*',
}, },
'html head title meta style body p div span a h1 h2 h3 h4 h5 img br hr table tr th td ul ol li dd dt strong pre b i': true, "html head title meta style body p div span a h1 h2 h3 h4 h5 img br hr table tr th td ul ol li dd dt strong pre b i": true,
}); });
var default_ckeditor_writer = new CKEDITOR.htmlParser.basicWriter(); var default_ckeditor_writer = new CKEDITOR.htmlParser.basicWriter();
return { return {
'filter_html': filter_html, filter_html: filter_html,
'default_ckeditor_filter': default_ckeditor_filter, default_ckeditor_filter: default_ckeditor_filter,
'default_ckeditor_writer': default_ckeditor_writer default_ckeditor_writer: default_ckeditor_writer,
} };
}); });

View File

@ -1,15 +1,14 @@
odoo.define('web_ckeditor4.FormView', function(require) { odoo.define("web_ckeditor4.FormView", function (require) {
"use strict"; "use strict";
var core = require('web.core'); var core = require("web.core");
var FormView = core.view_registry.get('form'); var FormView = core.view_registry.get("form");
FormView.include({ FormView.include({
can_be_discarded: function (message) { can_be_discarded: function (message) {
var self = this; var self = this;
var res = this._super().done(function () { var res = this._super().done(function () {
// if form can be discarded // If form can be discarded
// we want to destroy all ck4 editor instances // we want to destroy all ck4 editor instances
for (name in CKEDITOR.instances) { for (name in CKEDITOR.instances) {
if (self.fields.hasOwnProperty(name)) { if (self.fields.hasOwnProperty(name)) {
@ -18,8 +17,6 @@ odoo.define('web_ckeditor4.FormView', function(require) {
} }
}); });
return res; return res;
} },
}); });
}); });

View File

@ -1,20 +1,35 @@
<?xml version="1.0" encoding="UTF-8" ?> <?xml version="1.0" encoding="UTF-8" ?>
<odoo> <odoo>
<template id="assets_backend" name="web_ckeditor4 assets" <template
inherit_id="web.assets_backend"> id="assets_backend"
name="web_ckeditor4 assets"
inherit_id="web.assets_backend"
>
<xpath expr="." position="inside"> <xpath expr="." position="inside">
<script type="text/javascript" <script
src="/web_ckeditor4/static/src/js/ckeditor_basepath.js" /> type="text/javascript"
<script type="text/javascript" src="/web_ckeditor4/static/src/js/ckeditor_basepath.js"
src="/web_ckeditor4/static/lib/ckeditor/ckeditor.js" /> />
<script type="text/javascript" <script
src="/web_ckeditor4/static/lib/ckeditor/config.js" /> type="text/javascript"
<script type="text/javascript" src="/web_ckeditor4/static/lib/ckeditor/ckeditor.js"
src="/web_ckeditor4/static/src/js/web_ckeditor4_config.js" /> />
<script type="text/javascript" <script
src="/web_ckeditor4/static/src/js/web_ckeditor4_formview.js" /> type="text/javascript"
<script type="text/javascript" src="/web_ckeditor4/static/lib/ckeditor/config.js"
src="/web_ckeditor4/static/src/js/web_ckeditor4.js" /> />
<script
type="text/javascript"
src="/web_ckeditor4/static/src/js/web_ckeditor4_config.js"
/>
<script
type="text/javascript"
src="/web_ckeditor4/static/src/js/web_ckeditor4_formview.js"
/>
<script
type="text/javascript"
src="/web_ckeditor4/static/src/js/web_ckeditor4.js"
/>
</xpath> </xpath>
</template> </template>
</odoo> </odoo>