summaryrefslogtreecommitdiff
path: root/addons/web_editor/static/src/js/wysiwyg/plugin/table.js
diff options
context:
space:
mode:
authorRobot Odoo <xmo+roboto@odoo.com>2019-01-17 19:17:22 +0100
committerGitHub <noreply@github.com>2019-01-17 19:17:22 +0100
commit22efdfbecc1e8c688000be98903a5a355d1ece8a (patch)
treee097625171b2b67ed5f5288ced67446608b9f633 /addons/web_editor/static/src/js/wysiwyg/plugin/table.js
parentf50fbc6d168f0d17fa1e932b4ecc968a92b02b65 (diff)
parenteb0ea8efbe932c140302e9f8da33a839bcd73146 (diff)
[IMP] web_editor,*: Refactoring the wysiwyg editor and 'html' field
_Task #1878502_ - The refactoring allows for the update of the _Summernote_ library. - A new structure was created by transforming all the plugins in the library using the odoo inheritance system. Plugins are easier to implement with the `AbstractPlugin` to add Odoo behaviors. - The cleanup allowed us to remove some of the code that was in `web_editor` but only used in `website` or in `mass_mailing`. Some parts are still in `web_editor` but will be moved at a later time. - The refactoring also had to be done in order to create a set of unit tests. Each behavior can be tested, including the behaviors performed as a consequence of keyboard interactions (for instance: Enter, Tab...). - From now on, the methods of the library (in this case _Summernote_) can no longer be called by other modules or files. Only the `wysiwyg` widgets can access it, to simplify the updating process. The `wysiwyg` object serves as an interface. A lot of tests are done, ensuring the consistency of outside behavior. - Depending on the options the snippets will be loaded or not, the editor will be in an iframe or not... all of this is transparent from the outside. - Regarding iframes, all controllers related to editing have been removed: the new API no longer needs them. This speeds up loading, eases testing and removes complexity for the same features. **PUBLIC FEATURES** There are several public methods on the `Wysiwyg` class: - `Wysiwyg.prepare (WidgetParent)` which returns a deferred resolved when the library (xml, lazy, assets...) is loaded. - `Wysiwyg.getRange (DOM)` returns the range (selection in the dom) - `Wysiwyg.setRange (startNode, startOffset, endNode, endOffset)` which creates a range (selection in the dom) - `Wysiwyg.setRangeFromNode (DOM, options)` that creates a range from an element (option available to select all, start or end) A jQuery selector was added: `:o_editable`, which indicates whether the current element is editable. That is, if it is contained in a tag with the attribute `contentEditable = "true"` or in a tag with the class `o_editable`. Several methods are also present: - `focusIn`: makes a focus and places the cursor at the beginning of the element - `focusInEnd`: makes a focus and places the cursor at the end of the element - `selectContent`: makes a focus and selects the content **HTML FIELD** The HTML field can receive different options: - `style-inline:` {boolean} transforms a class into an inline style when saving and vice versa when reading. - `no-attachment`: {boolean} prevents the use of attachments (in media dialog) - `cssEdit`: {xml_id} to use a template containing the css to loaded in an iframe when editing - `cssReadonly`: {xml_id} to use a template containing the css to load into an iframe when viewing in readonly - `snippets`: {xml_id} snippets template (can be used with or without `cssEdit`) - `wrapper`: {template} qweb static template (containing a tag: `id = "wrapper"`) that will include the content during editing (removed on save) **MASS MAILING** A widget was created for `mass mailing`. There are now two fields: `body_html` and `body_arch`. `body_arch` contains the code with the class without conversion into inline style, useful when editing and one with the inline style that is visible in readonly mode and sent by email. Advantage: no spreading errors, able to update css/theme, able to do more changes when converting to inline style so that a maximum of mail clients have an impeccable rendering. **REMAINING KNOW ISSUES** These will be fixed in a subsequent commit. - A few committed changes made during the development of this refactoring were lost in the process and should be reapplied. - Some minor (mostly range) issues remain with Firefox. -- I confirm I have signed the CLA and read the PR guidelines at www.odoo.com/submit-pr closes odoo/odoo#29775
Diffstat (limited to 'addons/web_editor/static/src/js/wysiwyg/plugin/table.js')
-rw-r--r--addons/web_editor/static/src/js/wysiwyg/plugin/table.js244
1 files changed, 244 insertions, 0 deletions
diff --git a/addons/web_editor/static/src/js/wysiwyg/plugin/table.js b/addons/web_editor/static/src/js/wysiwyg/plugin/table.js
new file mode 100644
index 00000000000..b015faee43f
--- /dev/null
+++ b/addons/web_editor/static/src/js/wysiwyg/plugin/table.js
@@ -0,0 +1,244 @@
+odoo.define('web_editor.wysiwyg.plugin.table', function (require) {
+'use strict';
+
+var Plugins = require('web_editor.wysiwyg.plugins');
+var registry = require('web_editor.wysiwyg.plugin.registry');
+
+var dom = $.summernote.dom;
+
+
+var TablePlugin = Plugins.table.extend({
+
+ initialize: function () {
+ this._super.apply(this, arguments);
+ var self = this;
+ // We need setTimeout to make sure to initialize after HelperPlugin and HistoryPlugin
+ setTimeout(function () {
+ // contentEditable fail for image and font in table
+ // user must use right arrow the number of space but without feedback
+ self.$editable.find('td:has(img, span.fa)').each(function () {
+ if (this.firstChild && !this.firstChild.tagName) {
+ var startSpace = self.context.invoke('HelperPlugin.getRegex', 'startSpace');
+ this.firstChild.textContent = this.firstChild.textContent.replace(startSpace, ' ');
+ }
+ if (this.lastChild && !this.lastChild.tagName) {
+ var endSpace = self.context.invoke('HelperPlugin.getRegex', 'endSpace');
+ this.lastChild.textContent = this.lastChild.textContent.replace(endSpace, ' ');
+ }
+ });
+ self.context.invoke('HistoryPlugin.clear');
+ });
+ },
+
+ //--------------------------------------------------------------------------
+ // Public
+ //--------------------------------------------------------------------------
+
+ /**
+ * Add a new col and
+ * wrap contents of the new cells in p elements.
+ *
+ * @see summernote library
+ *
+ * @override
+ * @param {WrappedRange} rng
+ * @param {String('left'|'right')} position
+ */
+ addCol: function (rng, position) {
+ this._super.apply(this, arguments);
+ var cell = dom.ancestor(rng.commonAncestor(), dom.isCell);
+ var table = dom.ancestor(cell, function (n) {
+ return n.tagName === 'TABLE';
+ });
+ var newColIndex = $(cell)[position === 'right' ? 'next' : 'prev']('td').index() + 1;
+ $(table).find('td:nth-child(' + newColIndex + ')').contents().wrap('<p></p>');
+ },
+ /**
+ * Add a new row and
+ * wrap contents of the new cells in p elements.
+ *
+ * @see summernote library
+ *
+ * @override
+ * @param {WrappedRange} rng
+ * @param {String('top'|'bottom')} position
+ */
+ addRow: function (rng, position) {
+ this._super.apply(this, arguments);
+ var row = dom.ancestor(rng.commonAncestor(), dom.isCell).parentElement;
+ $(row)[position === 'bottom' ? 'next' : 'prev']('tr').find('td').contents().wrap('<p></p>');
+ },
+ /**
+ * Create empty table element and
+ * wrap the contents of all cells in p elements.
+ *
+ * @see summernote library
+ *
+ * @override
+ * @param {Number} rowCount
+ * @param {Number} colCount
+ * @returns {Node} table
+ */
+ createTable: function () {
+ var table = this._super.apply(this, arguments);
+ $(table).find('td').contents().wrap('<p></p>');
+ return table;
+ },
+ /**
+ * @see summernote library
+ *
+ * @override
+ */
+ deleteCol: function () {
+ var range = this.context.invoke('editor.createRange');
+
+ // Delete the last remaining column === delete the table
+ var cell = dom.ancestor(range.commonAncestor(), dom.isCell);
+ if (cell && !cell.previousElementSibling && !cell.nextElementSibling) {
+ return this.deleteTable();
+ }
+ var neighbor = cell.previousElementSibling || cell.nextElementSibling;
+
+ this._super.apply(this, arguments);
+
+ // Put the range back on the previous or next cell after deleting
+ // to allow chain-removing
+ range = this.context.invoke('editor.createRange');
+ if (range.sc.tagName !== 'TD' && neighbor) {
+ range = this.context.invoke('editor.setRange', neighbor, 0);
+ range.normalize().select();
+ }
+ },
+ /**
+ * @see summernote library
+ *
+ * @override
+ */
+ deleteRow: function () {
+ var range = this.context.invoke('editor.createRange');
+
+ // Delete the last remaining row === delete the table
+ var row = dom.ancestor(range.commonAncestor(), function (n) {
+ return n.tagName === 'TR';
+ });
+ if (row && !row.previousElementSibling && !row.nextElementSibling) {
+ return this.deleteTable();
+ }
+ var neighbor = row.previousElementSibling || row.nextElementSibling;
+
+ this._super.apply(this, arguments);
+
+ // Put the range back on the previous or next row after deleting
+ // to allow chain-removing
+ range = this.context.invoke('editor.createRange');
+ if (range.sc.tagName !== 'TR' && neighbor) {
+ range = this.context.invoke('editor.setRange', neighbor, 0);
+ range.normalize().select();
+ }
+ },
+ /**
+ * Delete the table in range.
+ */
+ deleteTable: function () {
+ var range = this.context.invoke('editor.createRange');
+ var cell = dom.ancestor(range.commonAncestor(), dom.isCell);
+ var table = $(cell).closest('table')[0];
+
+ var point = this.context.invoke('HelperPlugin.removeBlockNode', table);
+ range = this.context.invoke('editor.setRange', point.node, point.offset);
+ range.normalize().select();
+ },
+ /**
+ * Insert a table.
+ * Note: adds <p><br></p> before/after the table if the table
+ * has nothing brefore/after it, so as to allow the carret to move there.
+ *
+ * @param {String} dim dimension of table (ex : "5x5")
+ */
+ insertTable: function (dim) {
+ var dimension = dim.split('x');
+ var table = this.createTable(dimension[0], dimension[1], this.options);
+ this.context.invoke('HelperPlugin.insertBlockNode', table);
+ var p;
+ if (!table.previousElementSibling) {
+ p = this.document.createElement('p');
+ $(p).append(this.document.createElement('br'));
+ $(table).before(p);
+ }
+ if (!table.nextElementSibling) {
+ p = this.document.createElement('p');
+ $(p).append(this.document.createElement('br'));
+ $(table).after(p);
+ }
+ var range = this.context.invoke('editor.setRange', $(table).find('td')[0], 0);
+ range.normalize().select();
+ this.context.invoke('editor.saveRange');
+ },
+});
+
+
+var TablePopover = Plugins.tablePopover.extend({
+ events: _.defaults({
+ 'summernote.scroll': '_onScroll',
+ }, Plugins.tablePopover.prototype.events),
+
+ /**
+ * Update the table's popover and its position.
+ *
+ * @override
+ * @param {Node} target
+ * @returns {false|Node} the selected cell (on which to display the popover)
+ */
+ update: function (target) {
+ if (!target || this.context.isDisabled()) {
+ return false;
+ }
+ var cell = dom.ancestor(target, dom.isCell);
+ if (!!cell && this.options.isEditableNode(cell)) {
+ var pos = $(cell).offset();
+ var posContainer = $(this.options.container).offset();
+ pos.left = pos.left - posContainer.left + 10;
+ pos.top = pos.top - posContainer.top + $(cell).outerHeight() - 4;
+
+ this.lastPos = this.context.invoke('HelperPlugin.makePoint', target, $(target).offset());
+
+ this.$popover.css({
+ display: 'block',
+ left: pos.left,
+ top: pos.top,
+ });
+ } else {
+ this.hide();
+ }
+ return cell;
+ },
+ /**
+ * Update the target table and its popover.
+ *
+ * @private
+ */
+ _onScroll: function () {
+ var range = this.context.invoke('editor.createRange');
+ var target = dom.ancestor(range.sc, dom.isCell);
+ if (!target || target === this.editable) {
+ return;
+ }
+ if (this.lastPos && this.lastPos.target === target && $(target).offset()) {
+ var newTop = $(target).offset().top;
+ var movement = this.lastPos.offset.top - newTop;
+ if (movement && this.mousePosition) {
+ this.mousePosition.pageY -= movement;
+ }
+ }
+ return this.update(target);
+ },
+});
+
+
+registry.add('TablePlugin', TablePlugin);
+registry.add('TablePopover', TablePopover);
+registry.add('tablePopover', null);
+
+return TablePlugin;
+
+});