summaryrefslogtreecommitdiff
path: root/addons/web_editor/static/src/js/wysiwyg/wysiwyg_snippets.js
blob: 73801dc93f58f75f6a3a6fca4b98b786e59a57ae (plain)
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
odoo.define('web_editor.wysiwyg.snippets', function (require) {
'use strict';
var editor = require('web_editor.editor');
var Wysiwyg = require('web_editor.wysiwyg');


Wysiwyg.include({
    init: function (parent, options) {
        this._super.apply(this, arguments);
        this.Editor = editor.Class;
        if (!this.options.toolbarHandler) {
            this.options.toolbarHandler = $('#web_editor-top-edit');
        }
    },
    start: async function () {
        if (this.options.snippets) {
            var self = this;
            this.editor = new (this.Editor)(this, this.options);
            this.$editor = this.editor.rte.editable();
            const $body = this.$editor[0] ? this.$editor[0].ownerDocument.body : document.body;
            await this.editor.prependTo($body);
            this._relocateEditorBar();
            this.$el.on('content_changed', function (e) {
                self.trigger_up('wysiwyg_change');
            });
        } else {
            return this._super();
        }
    },

    //--------------------------------------------------------------------------
    // Private
    //--------------------------------------------------------------------------

    /**
     * @private
     */
    _relocateEditorBar: function () {
        if (!this.options.toolbarHandler.length) {
            this.options.toolbarHandler = $('.o_we_snippet_text_tools');
        }
        this.options.toolbarHandler.append(this.editor.$el);

        // TODO the next four lines are a huge hack: since the editor.$el
        // is repositioned, the snippetsMenu elements are not at the
        // correct position anymore if it was repositioned outside of it...
        // the whole logic has to be refactored... hopefully not needed anymore
        // with editor team changes
        if (this.editor.snippetsMenu && !this.editor.snippetsMenu.$el.has(this.options.toolbarHandler).length) {
            this.editor.snippetsMenu.$el.insertAfter(this.options.toolbarHandler);
            this.editor.snippetsMenu.$snippetEditorArea.insertAfter(this.editor.snippetsMenu.$el);
        }
    },
});

});