page: retain conditional login elements in editor
[minimedit.git] / edit / page.js
index 4e389dbe624f0765ad66a31115016527085bdf2c..ccc1115eaeda6798426c0266cb06c3e2ed727555 100644 (file)
@@ -1,3 +1,13 @@
+var pagebody;
+
+function editorcontents() {
+       return document.getElementsByClassName('static')[0];
+}
+
+function editorsetup() {
+
+CKEDITOR.disableAutoInline = true;
+
 CKEDITOR.plugins.add('inlinesave', {
        init: function(editor) {
                editor.addCommand( 'inlinesave', {
@@ -8,6 +18,8 @@ CKEDITOR.plugins.add('inlinesave', {
                                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
@@ -116,6 +128,8 @@ CKEDITOR.on('instanceCreated', function (event) {
                        { name: 'Attributie', element: 'em', attributes: { 'class': 'right' } },
                        { name: 'Quote', element: 'q' },
                        { name: 'Gemarkeerd', element: 'span', styles: { 'background-color': 'Yellow' } },
+                       { name: 'Ingelogd', element: 'span', attributes: { 'class': 'login' } },
+                       { name: 'Uitgelogd', element: 'span', attributes: { 'class': 'logout' } },
 
                        { name: 'Kadertekst', element: 'aside' },
                        { name: 'Uitgelijnd', element: 'div', attributes: { 'class': 'right' } },
@@ -151,17 +165,15 @@ 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) {
                editlink.style.fontWeight = 'bold';
                editlink.href = '';
                editlink.onclick = undefined;
+               document.body.replaceChild(pagebody, editorcontents());
                pagebody.setAttribute('contenteditable', true);
                pagebody.querySelectorAll('[data-dyn]').forEach(function (el) {
                        let blockname = el.getAttribute('data-dyn');
@@ -180,3 +192,12 @@ if (pagebody) {
        }
 }
 
+}
+
+document.addEventListener('DOMContentLoaded', function (e) {
+       pagebody = editorcontents().cloneNode(true);
+       var editorinc = document.createElement('script');
+       editorinc.addEventListener('load', editorsetup);
+       editorinc.src = ckesrc;
+       document.getElementsByTagName('head')[0].appendChild(editorinc);
+});