edit: leave standalone placeholders as is
[minimedit.git] / edit.js
diff --git a/edit.js b/edit.js
index 78bf6dcfc3cf7f9ea1a6d57547216bc9edfe0d1b..1485394ab029618a9fcddc0736d02e6b3b2e65ca 100644 (file)
--- a/edit.js
+++ b/edit.js
@@ -2,10 +2,22 @@ CKEDITOR.plugins.add('inlinesave', {
        init: function(editor) {
                editor.addCommand( 'inlinesave', {
                        exec: function (editor) {
-                               var pagename = window.location.pathname.replace(/\.html$/, '').replace(/\/$/, '/index');
-                               var data = 'body='+encodeURIComponent(editor.getData());
+                               var pagename = window.location.pathname.replace(/\/$/, '/index');
+                               var body = editor.getData();
+                               // empty line is equivalent to a paragraph break
+                               body = body.replace(/<br \/>\s*<br \/>/g, '<p>');
+                               // wrap long line after each sentence
+                               body = body.replace(/^(\t*).{73,}/mg, function (line, indent) {
+                                       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');
+                               });
+                               // treat standalone placeholders as block elements
+                               body = body.replace(/<p>(\[\[.*\]\])<\/p>/g, '$1');
+
+                               var data = 'body='+encodeURIComponent(body);
                                ajaxpost = new XMLHttpRequest();
-                               ajaxpost.open('POST', '/edit.php'+pagename, true);
+                               ajaxpost.open('POST', '/edit'+pagename, true);
                                ajaxpost.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                                ajaxpost.onreadystatechange = function () {
                                        if (ajaxpost.readyState != 4)
@@ -13,7 +25,7 @@ CKEDITOR.plugins.add('inlinesave', {
                                        if (ajaxpost.status != 200)
                                                alert('Foutcode '+ajaxpost.status+' bij opslaan: '+ajaxpost.responseText);
                                        else
-                                               alert('Pagina is goed opgeslagen');
+                                               editor.resetDirty();
                                };
                                ajaxpost.send(data);
                        },
@@ -21,58 +33,105 @@ 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') {
+       switch (event.data.name) {
+       case '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 = '';
+               break;
+       case 'link':
+               // remove unneeded widgets from the Link Info tab
+               var infotab = event.data.definition.getContents('info');
+               infotab.remove('linkType');
+               break;
        }
 });
 
 CKEDITOR.on('instanceCreated', function (event) {
        var editor = event.editor;
+       var pastefilter = 'h2 h3 p ul ol li blockquote em i strong b; a[!href]; img[alt,!src]';
+
+       editor.on('paste', function (e) {
+               var html = e.data.dataValue;
+               if (!/<[^>]* style="/.test(html) && !/<font/.test(html)) return;
+
+               // force pasteFilter on contents containing styling attributes
+               var filter = new CKEDITOR.filter(pastefilter),
+                       fragment = CKEDITOR.htmlParser.fragment.fromHtml(html),
+                       writer = new CKEDITOR.htmlParser.basicWriter();
+               filter.applyTo(fragment);
+               fragment.writeHtml(writer);
+               e.data.dataValue = writer.getHtml();
+       });
+
        editor.on('configLoaded', function () {
                var config = editor.config;
                config.language = 'nl';
-               config.extraPlugins = 'sourcedialog,inlinesave';
+               config.extraPlugins = 'sourcedialog,inlinesave,placeholder,image2,uploadimage';
                config.format_tags = 'h2;h3;h4;p';
                config.allowedContent = true;
                config.entities = false; // keep unicode
-               config.filebrowserImageUploadUrl = '/edit.php?type=img';
-               config.forcePasteAsPlainText = true;
+               config.filebrowserImageUploadUrl = '/edit?output=ckescript';
+               config.uploadUrl = '/edit?output=ckjson';
+               config.image2_alignClasses = ['left', 'center', 'right'];
+               config.image2_disableResizer = true;
+               config.stylesSet = [
+                       { name: 'Kolom', element: 'div', attributes: { 'class': 'col' } },
+                       { name: 'Rechts', element: 'div', attributes: { 'class': 'right' } },
+               ];
+               config.pasteFilter = pastefilter;
+               config.contentsCss = document.styleSheets[0].href;
                config.toolbar = [
-                       ['Inlinesave', '-', 'ShowBlocks', 'Sourcedialog', '-', 'Undo', 'Redo'],
-                       ['Format'],
-                       ['BulletedList', 'NumberedList', '-', 'Blockquote'],
-                       ['Bold', 'Italic', 'Underline', 'Strike', 'RemoveFormat', '-', 'Link'],
-                       ['HorizontalRule', 'Table', 'Image'],
+                       ['Inlinesave', '-', 'Undo', 'Redo'],
+                       ['Format', 'BulletedList', 'NumberedList', 'CreateDiv', 'Table', 'Blockquote'],
+                       ['HorizontalRule', 'CreatePlaceholder', 'Image'],
+                       ['Bold', 'Italic', 'Link'],
+                       ['Sourcedialog'],
                ];
+               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,
-               });
-       });
+       window.onbeforeunload = function () {
+               if (editor.checkDirty()) {
+                       return 'Pagina verlaten zonder wijzigingen op te slaan?'; // message ignored in modern browsers
+               }
+       };
+});
 
-var pagebody = document.getElementsByClassName('article')[0];
-pagebody.setAttribute('contenteditable', 'true');
+       CKEDITOR.disableAutoInline = true;
 
-document.body.className = 'edit';
+// add edit link to menu
+var pagebody = document.getElementsByClassName('static')[0];
+if (pagebody) {
+       var editlink = document.querySelector('a[href="#edit"]');
+       editlink.onclick = function (e) {
+               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;
+       };
+       if (window.location.hash == '#edit') {
+               editlink.onclick();
+       }
+}