edit: static edit link
[minimedit.git] / edit.js
diff --git a/edit.js b/edit.js
index c5d6ce3198610da8a490456c0a479c9a61d888cb..21028dc6f4b7d65c8584429e0cc0267c8af5b0b4 100644 (file)
--- a/edit.js
+++ b/edit.js
@@ -3,7 +3,13 @@ CKEDITOR.plugins.add('inlinesave', {
                editor.addCommand( 'inlinesave', {
                        exec: function (editor) {
                                var pagename = window.location.pathname.replace(/\/$/, '/index');
-                               var data = 'body='+encodeURIComponent(editor.getData());
+                               var body = editor.getData().replace(/^(\t*).{73,}/mg, function (line, indent) {
+                                       // wrap long line after each sentence
+                                       var dots = '(?:.{24,72}|.{73,}?)'; // chars before punctuation
+                                       var wrap = new RegExp('('+dots+'[.;:!?]) (?=[A-Z(<])', 'g'); // separate lines
+                                       return line.replace(wrap, '$1\n'+indent+'\t');
+                               });
+                               var data = 'body='+encodeURIComponent(body);
                                ajaxpost = new XMLHttpRequest();
                                ajaxpost.open('POST', '/edit'+pagename, true);
                                ajaxpost.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
@@ -42,7 +48,7 @@ CKEDITOR.on('instanceCreated', function (event) {
        editor.on('configLoaded', function () {
                var config = editor.config;
                config.language = 'nl';
-               config.extraPlugins = 'sourcedialog,inlinesave';
+               config.extraPlugins = 'sourcedialog,inlinesave,placeholder';
                config.format_tags = 'h2;h3;h4;p';
                config.allowedContent = true;
                config.entities = false; // keep unicode
@@ -54,48 +60,37 @@ CKEDITOR.on('instanceCreated', function (event) {
                        ['Format'],
                        ['BulletedList', 'NumberedList', '-', 'Blockquote'],
                        ['Bold', 'Italic', 'Underline', 'Strike', 'RemoveFormat', '-', 'Anchor', 'Link'],
-                       ['HorizontalRule', 'Table', 'Image'],
+                       ['HorizontalRule', 'Table', 'Image', 'CreatePlaceholder'],
                ];
+               config.toolbarCanCollapse = true;
+               config.floatSpacePreferRight = true;
+               config.floatSpaceDockedOffsetY = 0;
+               config.startupFocus = true;
 
                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('static')[0];
 if (pagebody) {
-       var editlink = document.createElement('a');
-       editlink.style.cursor = 'pointer';
-       editlink.appendChild(document.createTextNode('Wijzig'));
+       var editlink = document.querySelector('a[href="#edit"]');
        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';
+               editlink.style.fontWeight = 'bold';
+               editlink.href = '';
+               editlink.onclick = undefined;
+               pagebody.setAttribute('contenteditable', true);
+               pagebody.innerHTML = pagebody.innerHTML
+                       .replace(/<!--BLOCK:([^-]*)-->[^]*?<!--\/-->/g, '$1');
+               CKEDITOR.inline(pagebody);
+               document.body.className = 'edit';
                return false;
        };
-       document.querySelector('header ul').appendChild(editlink);
+       if (window.location.hash == '#edit') {
+               editlink.onclick();
+       }
 }