edit/page: load ckeditor dependency from script include
[minimedit.git] / edit / page.js
index e2475a7e2de83140b89a36a8382bfd946f166973..ecf520453fb4ffffb6039895600d1725beb2f469 100644 (file)
@@ -1,11 +1,19 @@
+function editorsetup() {
+
+CKEDITOR.disableAutoInline = true;
+
 CKEDITOR.plugins.add('inlinesave', {
        init: function(editor) {
                editor.addCommand( 'inlinesave', {
                        exec: function (editor) {
-                               var pagename = window.location.pathname.replace(/\/$/, '/index');
+                               var pagename = window.location.pathname;
                                var body = editor.getData();
+                               // trim trailing whitespace in non-empty paragraphs
+                               body = body.replace(/((?!<p>).{3})(?:\s|\u200B)+(?=<\/p>)/g, '$1');
                                // empty line is equivalent to a paragraph break
                                body = body.replace(/<br \/>\s*<br \/>/g, '<p>');
+                               // keep names and preceding abbreviations together
+                               body = body.replace(/\b((?:dhr|mw|me?vr|mr?s?)\.)\s+(?=[A-Z])/ig, '$1&nbsp;');
                                // wrap long line after each sentence
                                body = body.replace(/^(\t*).{73,}/mg, function (line, indent) {
                                        var dots = '(?:.{24,72}|.{73,}?)'; // chars before punctuation
@@ -22,14 +30,24 @@ CKEDITOR.plugins.add('inlinesave', {
                                ajaxpost.onreadystatechange = function () {
                                        if (ajaxpost.readyState != 4)
                                                return; // not done yet
-                                       if (ajaxpost.status != 200)
-                                               alert('Foutcode '+ajaxpost.status+' bij opslaan: '+ajaxpost.responseText);
-                                       else
+                                       if (ajaxpost.status == 200) {
                                                editor.resetDirty();
+                                               new CKEDITOR.plugins.notification(editor, {
+                                                       message: 'Pagina is succesvol opgeslagen',
+                                                       type: 'success',
+                                               }).show();
+                                       }
+                                       else {
+                                               new CKEDITOR.plugins.notification(editor, {
+                                                       message: 'Foutcode '+ajaxpost.status+' bij opslaan: '+ajaxpost.responseText,
+                                                       type: 'warning',
+                                               }).show();
+                                       }
                                };
                                ajaxpost.send(data);
                        },
                });
+               editor.setKeystroke(CKEDITOR.CTRL + 83 /*S*/, 'inlinesave');
                editor.ui.addButton( 'Inlinesave', {
                        command: 'inlinesave',
                        label: editor.lang.save.toolbar,
@@ -42,15 +60,30 @@ CKEDITOR.on('dialogDefinition', function (event) {
        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 = '';
+               var infotab = event.data.definition.getContents('info');
+               infotab.remove('txtWidth');
+               infotab.remove('txtHeight');
+               infotab.remove('txtBorder');
+               infotab.remove('txtCellSpace');
+               infotab.remove('txtCellPad');
+               infotab.remove('cmbAlign');
+
+               // horizontal repositioning of existing elements
+               var hbox = {
+                       id: 'hboxDimensions',
+                       type: 'hbox',
+                       children: [ infotab.get('txtCols'), infotab.get('txtRows') ],
+               };
+               infotab.add(hbox, 'selHeaders');
+               infotab.remove('txtCols');
+               infotab.remove('txtRows');
+
                break;
        case 'link':
-               //TODO: remove unneeded widgets from the Link Info tab
-               var infotab = event.data.definition.getContents('info');
+               // hide unneeded widgets from the Link Info tab
+               event.data.definition.getContents('info').get('linkType').hidden = true;
+               let linktarget = event.data.definition.getContents('target').get('linkTargetType');
+               linktarget.items = [ linktarget.items[0], linktarget.items[3] ]; // only _blank
                break;
        }
 });
@@ -84,14 +117,22 @@ CKEDITOR.on('instanceCreated', function (event) {
                config.image2_alignClasses = ['left', 'center', 'right'];
                config.image2_disableResizer = true;
                config.stylesSet = [
+                       { name: 'Klein', element: 'small' },
+                       { name: 'Zijkant', element: 'span', attributes: { 'class': 'right' } },
+                       { name: 'Attributie', element: 'em', attributes: { 'class': 'right' } },
+                       { name: 'Quote', element: 'q' },
+                       { name: 'Gemarkeerd', element: 'span', styles: { 'background-color': 'Yellow' } },
+
+                       { name: 'Kadertekst', element: 'aside' },
+                       { name: 'Uitgelijnd', element: 'div', attributes: { 'class': 'right' } },
                        { name: 'Kolom', element: 'div', attributes: { 'class': 'col' } },
-                       { name: 'Rechts', element: 'div', attributes: { 'class': 'right' } },
+                       { name: 'Waarschuwing', element: 'div', attributes: { 'class': 'warn' } },
                ];
                config.pasteFilter = pastefilter;
                config.contentsCss = document.styleSheets[0].href;
                config.toolbar = [
                        ['Inlinesave', '-', 'Undo', 'Redo'],
-                       ['Format'],
+                       ['Format', 'Styles'],
                        ['Bold', 'Italic', 'Link'],
                        ['BulletedList', 'NumberedList', 'Blockquote'],
                        ['Table', 'CreateDiv'],
@@ -102,6 +143,7 @@ CKEDITOR.on('instanceCreated', function (event) {
                config.toolbarCanCollapse = true;
                config.floatSpacePreferRight = true;
                config.floatSpaceDockedOffsetY = 0;
+               config.title = false;
                config.startupFocus = true;
 
                config.disableObjectResizing = true;
@@ -115,11 +157,9 @@ CKEDITOR.on('instanceCreated', function (event) {
        };
 });
 
-       CKEDITOR.disableAutoInline = true;
-
-// add edit link to menu
 var pagebody = document.getElementsByClassName('static')[0];
 if (pagebody) {
+       // add edit link to menu
        var editlink = document.querySelector('a[href="#edit"]');
        if (editlink)
        editlink.onclick = function (e) {
@@ -127,8 +167,14 @@ if (pagebody) {
                editlink.href = '';
                editlink.onclick = undefined;
                pagebody.setAttribute('contenteditable', true);
-               pagebody.innerHTML = pagebody.innerHTML
-                       .replace(/<!--BLOCK:(.*?)-->[^]*?<!--\/-->/g, '$1');
+               pagebody.querySelectorAll('[data-dyn]').forEach(function (el) {
+                       let blockname = el.getAttribute('data-dyn');
+                       if (!blockname) {
+                               el.remove();
+                               return;
+                       }
+                       el.outerHTML = '[[' + blockname + ']]';
+               });
                CKEDITOR.inline(pagebody, { customConfig: '' });
                document.body.className = 'edit';
                return false;
@@ -138,3 +184,11 @@ if (pagebody) {
        }
 }
 
+}
+
+document.addEventListener('DOMContentLoaded', function (e) {
+       var editorinc = document.createElement('script');
+       editorinc.addEventListener('load', editorsetup);
+       editorinc.src = ckesrc;
+       document.getElementsByTagName('head')[0].appendChild(editorinc);
+});