edit: toggle editor dynamically
[minimedit.git] / edit.js
diff --git a/edit.js b/edit.js
index fda6bf95741812e344958417f95e835c7dcb599b..0db437427c0305dc980dc1697d5e290fabf58aa4 100644 (file)
--- a/edit.js
+++ b/edit.js
@@ -2,17 +2,18 @@ CKEDITOR.plugins.add('inlinesave', {
        init: function(editor) {
                editor.addCommand( 'inlinesave', {
                        exec: function (editor) {
-                               var pagename = window.location.pathname.replace(/^\//, '') || 'index';
-                               var data = 'page='+encodeURIComponent(pagename)+'&body='+encodeURIComponent(editor.getData());
+                               var pagename = window.location.pathname.replace(/\/$/, '/index');
+                               var data = 'body='+encodeURIComponent(editor.getData());
                                ajaxpost = new XMLHttpRequest();
-                               ajaxpost.open('POST', 'edit.php', true);
+                               ajaxpost.open('POST', '/edit'+pagename, true);
                                ajaxpost.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                                ajaxpost.onreadystatechange = function () {
                                        if (ajaxpost.readyState != 4)
                                                return; // not done yet
                                        if (ajaxpost.status != 200)
                                                alert('Foutcode '+ajaxpost.status+' bij opslaan: '+ajaxpost.responseText);
-                                       //else alert('ok: '+ajaxpost.responseText);
+                                       else
+                                               alert('Pagina is goed opgeslagen');
                                };
                                ajaxpost.send(data);
                        },
@@ -20,11 +21,22 @@ CKEDITOR.plugins.add('inlinesave', {
                editor.ui.addButton( 'Inlinesave', {
                        command: 'inlinesave',
                        label: editor.lang.save.toolbar,
-                       icon: this.path + '../save/icons/save.png',
+                       icon: 'save',
                });
        }
 });
 
+CKEDITOR.on('dialogDefinition', function (event) {
+       if (event.data.name === 'table') {
+               // override initial attribute values
+               var infoTab = event.data.definition.getContents('info');
+               infoTab.get('txtWidth').default = '';
+               infoTab.get('txtBorder').default = '0';
+               infoTab.get('txtCellSpace').default = '';
+               infoTab.get('txtCellPad').default = '';
+       }
+});
+
 CKEDITOR.on('instanceCreated', function (event) {
        var editor = event.editor;
        editor.on('configLoaded', function () {
@@ -34,17 +46,56 @@ CKEDITOR.on('instanceCreated', function (event) {
                config.format_tags = 'h2;h3;h4;p';
                config.allowedContent = true;
                config.entities = false; // keep unicode
+               config.filebrowserImageUploadUrl = '/edit?type=img';
+               config.forcePasteAsPlainText = true;
+               config.contentsCss = '/excelsior.css';
                config.toolbar = [
                        ['Inlinesave', '-', 'ShowBlocks', 'Sourcedialog', '-', 'Undo', 'Redo'],
                        ['Format'],
                        ['BulletedList', 'NumberedList', '-', 'Blockquote'],
-                       ['Bold', 'Italic', 'Underline', 'Strike', '-', 'Link'],
+                       ['Bold', 'Italic', 'Underline', 'Strike', 'RemoveFormat', '-', 'Anchor', 'Link'],
                        ['HorizontalRule', 'Table', 'Image'],
                ];
-               config.enterMode = CKEDITOR.ENTER_BR; // results in <p>; ENTER_P does it twice
+
+               config.disableObjectResizing = true;
+               document.execCommand('enableObjectResizing', false, false); // workaround in inline mode; ff bug?
        });
 });
 
+       CKEDITOR.on('instanceReady', function (event) {
+               var editor = event.editor;
+               var writer = editor.dataProcessor.writer;
+               writer.selfClosingEnd = '>';
+               writer.setRules( 'p', {
+                       breakAfterOpen: true,
+                       breakBeforeClose: true,
+               });
+       });
+
+       CKEDITOR.disableAutoInline = true;
+
+// add edit link to menu
 var pagebody = document.getElementsByClassName('article')[0];
-pagebody.setAttribute('contenteditable', 'true');
+if (pagebody) {
+       var editlink = document.createElement('a');
+       editlink.style.cursor = 'pointer';
+       editlink.appendChild(document.createTextNode('Wijzig'));
+       editlink.onclick = function (e) {
+               var toggled = editlink.style.fontWeight;
+               editlink.style.fontWeight = toggled ? '' : 'bold';
+               pagebody.setAttribute('contenteditable', !toggled);
+               if (toggled) {
+                       for (name in CKEDITOR.instances) {
+                               CKEDITOR.instances[name].destroy()
+                       }
+               }
+               else {
+                       CKEDITOR.inline(pagebody);
+                       pagebody.focus();
+               }
+               document.body.className = toggled ? '' : 'edit';
+               return false;
+       };
+       document.querySelector('header ul').appendChild(editlink);
+}