import from fckeditorcompat
authorSamuel Trégouët <samuel.tregouet@logilab.fr>
Tue, 15 Mar 2016 17:08:31 +0100
changeset 1 df7eb5ac23b5
parent 0 04653b4e17e2
child 2 bd0cfeca3507
import from fckeditorcompat
__init__.py
data/ckeditor-config.js
data/initwysiwyg.js
migration/postcreate.py
--- a/__init__.py	Tue Mar 15 16:53:08 2016 +0100
+++ b/__init__.py	Tue Mar 15 17:08:31 2016 +0100
@@ -2,3 +2,32 @@
 
 WYSIWYG js editor with ckeditor
 """
+
+from logilab.common.decorators import monkeypatch, cached
+
+from cubicweb.web.webconfig import WebConfiguration
+from cubicweb.web.request import _CubicWebRequestBase
+
+
+@monkeypatch(_CubicWebRequestBase)
+@cached  # so it's writed only once
+def fckeditor_config(self):
+    fckeditor_url = '//cdn.ckeditor.com/4.5.7/standard/ckeditor.js'
+    self.add_js(fckeditor_url, localfile=False)
+    self.add_js('initwysiwyg.js')
+    self.add_onload('buildWysiwygEditors()')
+    self.html_headers.define_var('fcklang', self.lang)
+    self.html_headers.define_var('fckconfigpath',
+                                 self.data_url('ckeditor-config.js'))
+
+@monkeypatch(WebConfiguration)
+def fckeditor_installed(self):
+    if self.uiprops is None:
+        return False
+    return True
+
+
+@monkeypatch(WebConfiguration)
+def cwproperty_definitions(self):
+    for key, pdef in super(WebConfiguration, self).cwproperty_definitions():
+        yield key, pdef
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/data/ckeditor-config.js	Tue Mar 15 17:08:31 2016 +0100
@@ -0,0 +1,34 @@
+
+CKEDITOR.editorConfig = function( config ) {
+    // Define changes to default configuration here.
+    // For complete reference see:
+    // http://docs.ckeditor.com/#!/api/CKEDITOR.config
+
+    // The toolbar groups arrangement, optimized for two toolbar rows.
+    config.toolbarGroups = [
+        { name: 'clipboard',   groups: [ 'clipboard', 'undo' ] },
+        { name: 'editing',     groups: [ 'find', 'selection', 'spellchecker' ] },
+        { name: 'links' },
+        { name: 'insert' },
+        { name: 'forms' },
+        { name: 'tools' },
+        { name: 'document',	   groups: [ 'mode', 'document', 'doctools' ] },
+        { name: 'others' },
+        '/',
+        { name: 'basicstyles', groups: [ 'basicstyles', 'cleanup' ] },
+        { name: 'paragraph',   groups: [ 'list', 'indent', 'blocks', 'align', 'bidi' ] },
+        { name: 'styles' },
+        { name: 'colors' },
+        { name: 'about' }
+    ];
+
+    // Remove some buttons provided by the standard plugins, which are
+    // not needed in the Standard(s) toolbar.
+    config.removeButtons = 'Underline,Subscript,Superscript';
+
+    // Set the most common block elements.
+    config.format_tags = 'p;h1;h2;h3;pre';
+
+    // Simplify the dialog windows.
+    config.removeDialogTabs = 'image:advanced;link:advanced';
+};
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/data/initwysiwyg.js	Tue Mar 15 17:08:31 2016 +0100
@@ -0,0 +1,53 @@
+function buildWysiwygEditors(parent) {
+    jQuery('textarea').each(function() {
+        var cw_type = this.getAttribute('cubicweb:type');
+        if (cw_type === 'fckeditor' || cw_type === 'wysiwyg') {
+            // mark editor as instanciated, we may be called a number of times
+            // (see _postAjaxLoad)
+            this.setAttribute('cubicweb:type', 'ckeditor');
+            if (typeof CKEDITOR != "undefined") {
+                var fck = CKEDITOR.replace(this.id, {
+                    customConfig: fckconfigpath,
+                    defaultLangguage: fcklang
+                });
+            } else {
+                cw.log('fckeditor could not be found.');
+            }
+        }
+    });
+}
+
+
+cw.utils.formContents = function formContents(elem) {
+    var $elem, array, names, values;
+    $elem = cw.jqNode(elem);
+    array = $elem.serializeArray();
+
+    if (typeof CKEDITOR !== 'undefined') {
+        $elem.find('textarea').each(function (idx, textarea) {
+            var ck = CKEDITOR.instances[textarea.id];
+            if (ck) {
+                array = jQuery.map(array, function (dict) {
+                    if (dict.name === textarea.name) {
+                        // filter out the textarea's - likely empty - value ...
+                        return null;
+                    }
+                    return dict;
+                });
+                // ... so we can put the HTML coming from FCKeditor instead.
+                array.push({
+                    name: textarea.name,
+                    value: ck.getData()
+                });
+            }
+        });
+    }
+
+    names = [];
+    values = [];
+    jQuery.each(array, function (idx, dict) {
+        names.push(dict.name);
+        values.push(dict.value);
+    });
+    return [names, values];
+};
--- a/migration/postcreate.py	Tue Mar 15 16:53:08 2016 +0100
+++ b/migration/postcreate.py	Tue Mar 15 17:08:31 2016 +0100
@@ -21,5 +21,5 @@
 You could setup site properties or a workflow here for example.
 """
 
-# Example of site property change
-#set_property('ui.site-title', "<sitename>")
+set_property('ui.fckeditor', True)
+set_property('ui.default-text-format', 'text/html')