summaryrefslogtreecommitdiff
path: root/addons/web_editor/static
diff options
context:
space:
mode:
Diffstat (limited to 'addons/web_editor/static')
-rw-r--r--addons/web_editor/static/lib/summernote/src/js/EventHandler.js3
-rw-r--r--addons/web_editor/static/lib/summernote/src/js/module/Clipboard.js287
-rw-r--r--addons/web_editor/static/src/js/backend/fields.js4
-rw-r--r--addons/web_editor/static/src/js/editor/rte.js8
-rw-r--r--addons/web_editor/static/src/js/editor/rte.summernote.js4
-rw-r--r--addons/web_editor/static/src/js/editor/transcoder.js40
-rw-r--r--addons/web_editor/static/src/js/widgets/widgets.js4
7 files changed, 81 insertions, 269 deletions
diff --git a/addons/web_editor/static/lib/summernote/src/js/EventHandler.js b/addons/web_editor/static/lib/summernote/src/js/EventHandler.js
index 9701b8728d2..efa57f8111c 100644
--- a/addons/web_editor/static/lib/summernote/src/js/EventHandler.js
+++ b/addons/web_editor/static/lib/summernote/src/js/EventHandler.js
@@ -472,7 +472,8 @@ define([
onImageUpload: options.onImageUpload,
onImageUploadError: options.onImageUploadError,
onMediaDelete: options.onMediaDelete,
- onToolbarClick: options.onToolbarClick
+ onToolbarClick: options.onToolbarClick,
+ onUpload: options.onUpload,
});
var styleInfo = modules.editor.styleFromNode(layoutInfo.editable());
diff --git a/addons/web_editor/static/lib/summernote/src/js/module/Clipboard.js b/addons/web_editor/static/lib/summernote/src/js/module/Clipboard.js
index 955b49437a8..4cd9088da75 100644
--- a/addons/web_editor/static/lib/summernote/src/js/module/Clipboard.js
+++ b/addons/web_editor/static/lib/summernote/src/js/module/Clipboard.js
@@ -1,256 +1,41 @@
define([
- 'summernote/core/list',
- 'summernote/core/dom',
- 'summernote/core/key',
- 'summernote/core/agent',
- 'summernote/core/range'
+ 'summernote/core/list',
+ 'summernote/core/dom',
+ 'summernote/core/key',
+ 'summernote/core/agent',
+ 'summernote/core/range'
], function (list, dom, key, agent, range) {
- var Clipboard = function (handler) {
- var $paste;
-
- this.attach = function (layoutInfo) {
- // [workaround] getting image from clipboard
- // - IE11 and Firefox: CTRL+v hook
- // - Webkit: event.clipboardData
- if (agent.isMSIE && agent.browserVersion > 10) {
- $paste = $('<div />').attr('contenteditable', true).css({
- position : 'absolute',
- left : -100000,
- opacity : 0
- });
-
- layoutInfo.editable().on('keydown', function (e) {
- if (e.ctrlKey && e.keyCode === key.code.V) {
- handler.invoke('saveRange', layoutInfo.editable());
- $paste.focus();
-
- setTimeout(function () {
- pasteByHook(layoutInfo);
- }, 0);
- }
- });
-
- layoutInfo.editable().before($paste);
- } else {
- layoutInfo.editable().on('paste', pasteByEvent);
- }
- };
-
- var pasteByHook = function (layoutInfo) {
- var $editable = layoutInfo.editable();
- var node = $paste[0].firstChild;
-
- if (dom.isImg(node)) {
- var dataURI = node.src;
- var decodedData = atob(dataURI.split(',')[1]);
- var array = new Uint8Array(decodedData.length);
- for (var i = 0; i < decodedData.length; i++) {
- array[i] = decodedData.charCodeAt(i);
- }
-
- var blob = new Blob([array], { type : 'image/png' });
- blob.name = 'clipboard.png';
-
- handler.invoke('restoreRange', $editable);
- handler.invoke('focus', $editable);
- handler.insertImages(layoutInfo, [blob]);
- } else {
- var pasteContent = $('<div />').html($paste.html()).html();
- handler.invoke('restoreRange', $editable);
- handler.invoke('focus', $editable);
-
- if (pasteContent) {
- handler.invoke('pasteHTML', $editable, pasteContent);
- }
- }
-
- $paste.empty();
- };
-
- /**
- * paste by clipboard event
- *
- * @param {Event} event
- */
- var pasteByEvent = function (event) {
- var clipboardData = event.originalEvent.clipboardData;
- var layoutInfo = dom.makeLayoutInfo(event.currentTarget || event.target);
- var $editable = layoutInfo.editable();
-
- if (["INPUT", "TEXTAREA"].indexOf(event.target.tagName) !== -1) {
- return;
- }
-
- if (clipboardData && clipboardData.items && clipboardData.items.length) {
- var item = list.head(clipboardData.items);
- if (item.kind === 'file' && item.type.indexOf('image/') !== -1) {
- handler.insertImages(layoutInfo, [item.getAsFile()]);
- }
- handler.invoke('editor.afterCommand', $editable);
- }
-
- event.preventDefault();
-
- var html = clipboardData.getData("text/html");
- var $node = $('<div/>').html(html);
- // if copying source did not provide html, default to plain text
- if(!html) {
- $node.text(clipboardData.getData("text/plain")).html(function(_, html){
- return html.replace(/\r?\n/g,'<br>');
- });
- }
- pasteContent($node, layoutInfo, $editable);
- };
-
- /*
- remove undesirable tag
- filter classes and style attributes
- remove undesirable attributes
- */
- var filter_tag = function ($nodes, $editable) {
- return $nodes.each(function() {
- var $node = $(this);
-
- if ($node.attr('style')) {
- var style = _.filter(_.compact($node.attr('style').split(/\s*;\s*/)), function (style) {
- style = style.split(/\s*:\s*/);
- return /width|height|color|background-color|font-weight|text-align|font-style|text-decoration/i.test(style[0]) &&
- !(style[1] === 'initial' || style[1] === 'inherit' || $node.css(style[0]) === $editable.css(style[0]) ||
- (style[0] === 'background-color' && style[1] === 'rgb(255, 255, 255)') ||
- (style[0] === 'color' && style[1] === 'rgb(0, 0, 0)'));
- }).join(';');
- if (style.length) {
- $node.attr('style', style);
- } else {
- $node.removeAttr('style');
- }
- }
-
- if ($node.attr('class')) {
- var classes = _.filter($node.attr('class').split(/\s+/), function (style) {
- return /(^|\s)(fa|pull|text|bg)(\s|-|$)/.test(style);
- }).join(' ');
- if (classes.length) {
- $node.attr('class', classes);
- } else {
- $node.removeAttr('class');
- }
- }
- });
- };
-
- var pasteContent = function ($node, layoutInfo, $editable) {
- $node.find('meta, script, style').remove();
- filter_tag($node.find('*'), $editable).removeAttr('title', 'alt', 'id', 'contenteditable');
-
- /*
- remove unless span and unwant font
- */
- $node.find('span, font').filter(':not([class]):not([style])').each(function () {
- $(this).replaceWith($(this).contents());
- });
- $node.find('span + span').each(function () {
-
- if (dom.isText(this.previousSibling)) {
- if (dom.isVisibleText(this.previousSibling)) {
- return;
- } else { // keep space between 2 tags, but can merge the both tags
- $(this).prev().append(this.previousSibling);
- }
- }
- if ($(this).attr('class') === $(this).prev().attr('class') && $(this).attr('style') === $(this).prev().attr('style')) {
- $(this).prev().append($(this).contents());
- $(this).remove();
- }
- });
-
- // remove empty table row and td
- var $tdr;
- while(($tdr = $node.find('tr:empty, td:empty, th:empty, tbody:empty, t-head:empty, table:empty')) && $tdr.length) {
- $tdr.remove();
- }
-
- /*
- reset architecture HTML node and add <p> tag
- */
- var $arch = $('<div/>');
- var $last = $arch;
- $node.contents().each(function () {
- if (dom.isBR(this)) {
- $(this).remove();
- $last = $('<p/>');
- $arch.append($last);
- } else if (/h[0-9]+|li|table|p/i.test(this.tagName)) {
- $last = $('<p/>');
- $arch.append(this).append($last);
- } else if ($arch.is(':empty') && dom.isText(this)) {
- $last = $('<p/>').append(this);
- $arch.append($last);
- } else if (this.nodeType !== Node.COMMENT_NODE) {
- $last.append(this);
- }
- });
- $arch.find(':not([class]):not([style]):empty, p:empty').remove();
-
- /*
- history
- */
- $editable.data('NoteHistory').recordUndo($editable, "paste");
-
- /*
- remove selected content
- */
- var r = range.create();
- if (!r.isCollapsed()) {
- r = r.deleteContents();
- r.select();
- }
-
- // If only pasting a <p/> element in an unique <p/> element, only paste
- // the <p/> element text
- var $p = $arch.children('p');
- var onlyAP = ($p.length === 1 && $arch.children().length === 1);
- if (onlyAP) {
- var $p1 = $(r.sc).closest('p');
- var $p2 = $(r.ec).closest('p');
- if ($p1.length && $p2.length && $p1[0] === $p2[0]) {
- $arch.html($p.text());
- }
- }
-
- /*
- insert content
- */
- var $nodes = $();
- $editable.on('DOMNodeInserted', function (event) {
- $nodes = $nodes.add(event.originalEvent.target);
- });
- window.document.execCommand('insertHTML', false, $arch.html());
- $editable.off('DOMNodeInserted');
-
- /*
- clean insterted content
- */
- var $span = $nodes.filter('span');
- $span = $span.first().add($span.last());
- $span = $span.add($span.prev('span'));
- $span = $span.add($span.next('span'));
- filter_tag($span, $editable);
- $span.not('[span], [style]').each(function () {
- _.each(this.childNodes, function (node) {
- $(node.parentNode).after(node);
- });
- $(this).remove();
- });
- r = range.create();
- if (!dom.isText(r.ec)) {
- r = range.create(r.sc.childNodes[r.so], dom.nodeLength(r.sc.childNodes[r.so]));
- }
- r.clean().select();
-
- $editable.trigger('content_changed');
+ // ODOO override: use 0.8.10 version of this, adapted for the old summernote
+ // version odoo is using
+ var Clipboard = function (handler) {
+ /**
+ * paste by clipboard event
+ *
+ * @param {Event} event
+ */
+ var pasteByEvent = function (event) {
+ if (["INPUT", "TEXTAREA"].indexOf(event.target.tagName) !== -1) {
+ // ODOO override: from old summernote version
+ return;
+ }
+
+ var clipboardData = event.originalEvent.clipboardData;
+ var layoutInfo = dom.makeLayoutInfo(event.currentTarget || event.target);
+ var $editable = layoutInfo.editable();
+
+ if (clipboardData && clipboardData.items && clipboardData.items.length) {
+ var item = list.head(clipboardData.items);
+ if (item.kind === 'file' && item.type.indexOf('image/') !== -1) {
+ handler.insertImages(layoutInfo, [item.getAsFile()]);
+ }
+ handler.invoke('editor.afterCommand', $editable);
+ }
+ };
+
+ this.attach = function (layoutInfo) {
+ layoutInfo.editable().on('paste', pasteByEvent);
+ };
};
- };
- return Clipboard;
+ return Clipboard;
});
diff --git a/addons/web_editor/static/src/js/backend/fields.js b/addons/web_editor/static/src/js/backend/fields.js
index 479a26469a3..a636b7e5be3 100644
--- a/addons/web_editor/static/src/js/backend/fields.js
+++ b/addons/web_editor/static/src/js/backend/fields.js
@@ -163,7 +163,7 @@ var FieldTextHtmlSimple = basic_fields.DebouncedField.extend(TranslatableFieldMi
if (fieldNameAttachment) {
this.fieldNameAttachment = fieldNameAttachment;
this.attachments = [];
- summernoteConfig.onImageUpload = this._onImageUpload.bind(this);
+ summernoteConfig.onUpload = this._onUpload.bind(this);
summernoteConfig.getMediaDomain = this._getAttachmentsDomain.bind(this);
}
@@ -195,7 +195,7 @@ var FieldTextHtmlSimple = basic_fields.DebouncedField.extend(TranslatableFieldMi
*
* @private
*/
- _onImageUpload: function (attachments) {
+ _onUpload: function (attachments) {
var self = this;
attachments = _.filter(attachments, function (attachment) {
return !_.findWhere(self.attachments, {id: attachment.id});
diff --git a/addons/web_editor/static/src/js/editor/rte.js b/addons/web_editor/static/src/js/editor/rte.js
index 44f69fc2026..b4a42f843c8 100644
--- a/addons/web_editor/static/src/js/editor/rte.js
+++ b/addons/web_editor/static/src/js/editor/rte.js
@@ -270,7 +270,13 @@ var RTEWidget = Widget.extend({
$.fn.carousel = this.edit_bootstrap_carousel;
$(document).on('click.rte keyup.rte', function () {
- var $popover = $((range.create()||{}).sc).closest('[contenteditable]');
+ var current_range = {};
+ try {
+ current_range = range.create() || {};
+ } catch (e) {
+ // if range is on Restricted element ignore error
+ }
+ var $popover = $(current_range.sc).closest('[contenteditable]');
var popover_history = ($popover.data()||{}).NoteHistory;
if (!popover_history || popover_history === history) return;
var editor = $popover.parent('.note-editor');
diff --git a/addons/web_editor/static/src/js/editor/rte.summernote.js b/addons/web_editor/static/src/js/editor/rte.summernote.js
index 8d9d19be795..ce20c53ff49 100644
--- a/addons/web_editor/static/src/js/editor/rte.summernote.js
+++ b/addons/web_editor/static/src/js/editor/rte.summernote.js
@@ -403,7 +403,7 @@ eventHandler.modules.imageDialog.showImageDialog = function ($editable) {
media: media,
options: {
lastFilters: ['background'],
- onUpload: $editable.data('callbacks').onImageUpload,
+ onUpload: $editable.data('callbacks').onUpload,
},
});
return new $.Deferred().reject();
@@ -689,7 +689,7 @@ function summernote_mousedown(event) {
return;
}
var editables = $(".o_editable[contenteditable], .note-editable[contenteditable]");
- var r_editable = editables.has((r||{}).sc);
+ var r_editable = editables.has((r||{}).sc).addBack(editables.filter((r||{}).sc));
if (!r_editable.closest('.note-editor').is($editable) && !r_editable.filter('.o_editable').is(editables)) {
var saved_editable = editables.has((remember_selection||{}).sc);
if ($editable.length && !saved_editable.closest('.o_editable, .note-editor').is($editable)) {
diff --git a/addons/web_editor/static/src/js/editor/transcoder.js b/addons/web_editor/static/src/js/editor/transcoder.js
index 4fa30978f84..fb3e69eda29 100644
--- a/addons/web_editor/static/src/js/editor/transcoder.js
+++ b/addons/web_editor/static/src/js/editor/transcoder.js
@@ -227,6 +227,26 @@ function imgToFont($editable) {
});
}
+/*
+ * Utility function to apply function over descendants elements
+ *
+ * This is needed until the following issue of jQuery is solved:
+ * https://github.com./jquery/sizzle/issues/403
+ *
+ * @param {Element} node The root Element node
+ * @param {Function} func The function applied over descendants
+ */
+function applyOverDescendants(node, func) {
+ node = node.firstChild;
+ while (node) {
+ if (node.nodeType === 1) {
+ func(node);
+ applyOverDescendants(node, func);
+ }
+ node = node.nextSibling;
+ }
+}
+
/**
* Converts css style to inline style (leave the classes on elements but forces
* the style they give as inline style).
@@ -237,9 +257,9 @@ function classToStyle($editable) {
if (!rulesCache.length) {
getMatchedCSSRules($editable[0]);
}
- $editable.find('*').each(function () {
- var $target = $(this);
- var css = getMatchedCSSRules(this);
+ applyOverDescendants($editable[0], function (node) {
+ var $target = $(node);
+ var css = getMatchedCSSRules(node);
var style = $target.attr('style') || '';
_.each(css, function (v,k) {
if (!(new RegExp('(^|;)\s*' + k).test(style))) {
@@ -252,12 +272,12 @@ function classToStyle($editable) {
$target.attr('style', style);
}
// Apple Mail
- if (this.nodeName === 'TD' && !this.childNodes.length) {
- this.innerHTML = '&nbsp;';
+ if (node.nodeName === 'TD' && !node.childNodes.length) {
+ node.innerHTML = '&nbsp;';
}
// Outlook
- if (this.nodeName === 'A' && $target.hasClass('btn') && !$target.children().length) {
+ if (node.nodeName === 'A' && $target.hasClass('btn') && !$target.children().length) {
var $hack = $('<table class="o_outlook_hack"><tr><td></td></tr></table>');
$hack.find('td')
.attr('height', $target.outerHeight())
@@ -269,7 +289,7 @@ function classToStyle($editable) {
$target.after($hack);
$target.appendTo($hack.find('td'));
// the space add a line when it's a table but it's invisible when it's a link
- var node = $hack[0].previousSibling;
+ node = $hack[0].previousSibling;
if (node && node.nodeType === Node.TEXT_NODE && !node.textContent.match(/\S/)) {
$(node).remove();
}
@@ -297,9 +317,9 @@ function styleToClass($editable) {
var $c = $('<span/>').appendTo(document.body);
- $editable.find('*').each(function () {
- var $target = $(this);
- var css = getMatchedCSSRules(this);
+ applyOverDescendants($editable[0], function (node) {
+ var $target = $(node);
+ var css = getMatchedCSSRules(node);
var style = '';
_.each(css, function (v,k) {
if (!(new RegExp('(^|;)\s*' + k).test(style))) {
diff --git a/addons/web_editor/static/src/js/widgets/widgets.js b/addons/web_editor/static/src/js/widgets/widgets.js
index fc4892cfa64..5fd70d9cfee 100644
--- a/addons/web_editor/static/src/js/widgets/widgets.js
+++ b/addons/web_editor/static/src/js/widgets/widgets.js
@@ -381,13 +381,13 @@ var ImageWidget = MediaWidget.extend({
.sortBy(function (r) {
if (_.any(self.firstFilters, function (filter) {
var regex = new RegExp(filter, 'i');
- return r.name.match(regex) || r.datas_fname.match(regex);
+ return r.name.match(regex) || r.datas_fname && r.datas_fname.match(regex);
})) {
return -1;
}
if (_.any(self.lastFilters, function (filter) {
var regex = new RegExp(filter, 'i');
- return r.name.match(regex) || r.datas_fname.match(regex);
+ return r.name.match(regex) || r.datas_fname && r.datas_fname.match(regex);
})) {
return 1;
}