diff options
| author | Robot Odoo <xmo+roboto@odoo.com> | 2019-01-17 19:17:22 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-01-17 19:17:22 +0100 |
| commit | 22efdfbecc1e8c688000be98903a5a355d1ece8a (patch) | |
| tree | e097625171b2b67ed5f5288ced67446608b9f633 /addons/web_editor/static/lib/summernote/src/js/summernote.js | |
| parent | f50fbc6d168f0d17fa1e932b4ecc968a92b02b65 (diff) | |
| parent | eb0ea8efbe932c140302e9f8da33a839bcd73146 (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/lib/summernote/src/js/summernote.js')
| -rw-r--r-- | addons/web_editor/static/lib/summernote/src/js/summernote.js | 328 |
1 files changed, 0 insertions, 328 deletions
diff --git a/addons/web_editor/static/lib/summernote/src/js/summernote.js b/addons/web_editor/static/lib/summernote/src/js/summernote.js deleted file mode 100644 index 329ba7fa126..00000000000 --- a/addons/web_editor/static/lib/summernote/src/js/summernote.js +++ /dev/null @@ -1,328 +0,0 @@ -define([ - 'summernote/core/agent', - 'summernote/core/list', - 'summernote/core/dom', - 'summernote/core/range', - 'summernote/defaults', - 'summernote/EventHandler', - 'summernote/Renderer', - 'summernote/core/key' // ODOO: change for override -], function (agent, list, dom, range, - defaults, EventHandler, Renderer, key) { - - // jQuery namespace for summernote - /** - * @class $.summernote - * - * summernote attribute - * - * @mixin defaults - * @singleton - * - */ - $.summernote = $.summernote || {}; - - // extends default settings - // - $.summernote.version - // - $.summernote.options - // - $.summernote.lang - $.extend($.summernote, defaults); - - var renderer = new Renderer(); - var eventHandler = new EventHandler(); - - $.extend($.summernote, { - /** @property {Renderer} */ - renderer: renderer, - /** @property {EventHandler} */ - eventHandler: eventHandler, - /** - * @property {Object} core - * @property {core.agent} core.agent - * @property {core.dom} core.dom - * @property {core.range} core.range - */ - core: { - agent: agent, - list : list, - dom: dom, - range: range, - key: key // ODOO: change for override - }, - /** - * @property {Object} - * pluginEvents event list for plugins - * event has name and callback function. - * - * ``` - * $.summernote.addPlugin({ - * events : { - * 'hello' : function(layoutInfo, value, $target) { - * console.log('event name is hello, value is ' + value ); - * } - * } - * }) - * ``` - * - * * event name is data-event property. - * * layoutInfo is a summernote layout information. - * * value is data-value property. - */ - pluginEvents: {}, - - plugins : [] - }); - - /** - * @method addPlugin - * - * add Plugin in Summernote - * - * Summernote can make a own plugin. - * - * ### Define plugin - * ``` - * // get template function - * var tmpl = $.summernote.renderer.getTemplate(); - * - * // add a button - * $.summernote.addPlugin({ - * buttons : { - * // "hello" is button's namespace. - * "hello" : function(lang, options) { - * // make icon button by template function - * return tmpl.iconButton(options.iconPrefix + 'header', { - * // callback function name when button clicked - * event : 'hello', - * // set data-value property - * value : 'hello', - * hide : true - * }); - * } - * - * }, - * - * events : { - * "hello" : function(layoutInfo, value) { - * // here is event code - * } - * } - * }); - * ``` - * ### Use a plugin in toolbar - * - * ``` - * $("#editor").summernote({ - * ... - * toolbar : [ - * // display hello plugin in toolbar - * ['group', [ 'hello' ]] - * ] - * ... - * }); - * ``` - * - * - * @param {Object} plugin - * @param {Object} [plugin.buttons] define plugin button. for detail, see to Renderer.addButtonInfo - * @param {Object} [plugin.dialogs] define plugin dialog. for detail, see to Renderer.addDialogInfo - * @param {Object} [plugin.events] add event in $.summernote.pluginEvents - * @param {Object} [plugin.langs] update $.summernote.lang - * @param {Object} [plugin.options] update $.summernote.options - */ - $.summernote.addPlugin = function (plugin) { - - // save plugin list - $.summernote.plugins.push(plugin); - - if (plugin.buttons) { - $.each(plugin.buttons, function (name, button) { - renderer.addButtonInfo(name, button); - }); - } - - if (plugin.dialogs) { - $.each(plugin.dialogs, function (name, dialog) { - renderer.addDialogInfo(name, dialog); - }); - } - - if (plugin.events) { - $.each(plugin.events, function (name, event) { - $.summernote.pluginEvents[name] = event; - }); - } - - if (plugin.langs) { - $.each(plugin.langs, function (locale, lang) { - if ($.summernote.lang[locale]) { - $.extend($.summernote.lang[locale], lang); - } - }); - } - - if (plugin.options) { - $.extend($.summernote.options, plugin.options); - } - }; - - /* - * extend $.fn - */ - $.fn.extend({ - /** - * @method - * Initialize summernote - * - create editor layout and attach Mouse and keyboard events. - * - * ``` - * $("#summernote").summernote( { options ..} ); - * ``` - * - * @member $.fn - * @param {Object|String} options reference to $.summernote.options - * @return {this} - */ - summernote: function () { - // check first argument's type - // - {String}: External API call {{module}}.{{method}} - // - {Object}: init options - var type = $.type(list.head(arguments)); - var isExternalAPICalled = type === 'string'; - var hasInitOptions = type === 'object'; - - // extend default options with custom user options - var options = hasInitOptions ? list.head(arguments) : {}; - - options = $.extend({}, $.summernote.options, options); - options.icons = $.extend({}, $.summernote.options.icons, options.icons); - - // Include langInfo in options for later use, e.g. for image drag-n-drop - // Setup language info with en-US as default - options.langInfo = $.extend(true, {}, $.summernote.lang['en-US'], $.summernote.lang[options.lang]); - - // override plugin options - if (!isExternalAPICalled && hasInitOptions) { - for (var i = 0, len = $.summernote.plugins.length; i < len; i++) { - var plugin = $.summernote.plugins[i]; - - if (options.plugin[plugin.name]) { - $.summernote.plugins[i] = $.extend(true, plugin, options.plugin[plugin.name]); - } - } - } - - this.each(function (idx, holder) { - var $holder = $(holder); - - // if layout isn't created yet, createLayout and attach events - if (!renderer.hasNoteEditor($holder)) { - renderer.createLayout($holder, options); - - var layoutInfo = renderer.layoutInfoFromHolder($holder); - $holder.data('layoutInfo', layoutInfo); - - eventHandler.attach(layoutInfo, options); - eventHandler.attachCustomEvent(layoutInfo, options); - } - }); - - var $first = this.first(); - if ($first.length) { - var layoutInfo = renderer.layoutInfoFromHolder($first); - - // external API - if (isExternalAPICalled) { - var moduleAndMethod = list.head(list.from(arguments)); - var args = list.tail(list.from(arguments)); - - // TODO now external API only works for editor - var params = [moduleAndMethod, layoutInfo.editable()].concat(args); - return eventHandler.invoke.apply(eventHandler, params); - } else if (options.focus) { - // focus on first editable element for initialize editor - layoutInfo.editable().focus(); - } - } - - return this; - }, - - /** - * @method - * - * get the HTML contents of note or set the HTML contents of note. - * - * * get contents - * ``` - * var content = $("#summernote").code(); - * ``` - * * set contents - * - * ``` - * $("#summernote").code(html); - * ``` - * - * @member $.fn - * @param {String} [html] - HTML contents(optional, set) - * @return {this|String} - context(set) or HTML contents of note(get). - */ - code: function (html) { - // get the HTML contents of note - if (html === undefined) { - var $holder = this.first(); - if (!$holder.length) { - return; - } - - var layoutInfo = renderer.layoutInfoFromHolder($holder); - var $editable = layoutInfo && layoutInfo.editable(); - - if ($editable && $editable.length) { - var isCodeview = eventHandler.invoke('codeview.isActivated', layoutInfo); - eventHandler.invoke('codeview.sync', layoutInfo); - return isCodeview ? layoutInfo.codable().val() : - layoutInfo.editable().html(); - } - return dom.value($holder); - } - - // set the HTML contents of note - this.each(function (i, holder) { - var layoutInfo = renderer.layoutInfoFromHolder($(holder)); - var $editable = layoutInfo && layoutInfo.editable(); - if ($editable) { - $editable.html(html); - } - }); - - return this; - }, - - /** - * @method - * - * destroy Editor Layout and detach Key and Mouse Event - * - * @member $.fn - * @return {this} - */ - destroy: function () { - this.each(function (idx, holder) { - var $holder = $(holder); - - if (!renderer.hasNoteEditor($holder)) { - return; - } - - var info = renderer.layoutInfoFromHolder($holder); - var options = info.editor().data('options'); - - eventHandler.detach(info, options); - renderer.removeLayout($holder, info, options); - }); - - return this; - } - }); -}); |
