login/pass: error messages below page title
[minimedit.git] / edit / page.js
index 4e389dbe624f0765ad66a31115016527085bdf2c..8aa2890b8d3c426c1155a0264ad7a04fb44e68b7 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,14 +18,21 @@ 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((?:[Dd]hr|[Mm](?:w|e?vr|r|r?ss?)|[A-Z])\.)\s+(?=[A-Zdtv])/g, '$1&nbsp;');
                                // wrap long line after each sentence
                                body = body.replace(/^(\t*).{73,}/mg, function (line, indent) {
+                                       if (/^<meta /.test(line)) return line; // ignore metadata headers
                                        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');
+                               // strip redundant domain in relative urls
+                               var hostlink = 'https?://' + window.location.hostname.replace(/\W/g, '\\$&');
+                               hostlink = new RegExp('(<a [^>]*href="|<img [^>]*src=")' + hostlink + '(?=/)', 'gi');
+                               body = body.replace(hostlink, '$1');
 
                                var data = 'body='+encodeURIComponent(body);
                                var ajaxpost = new XMLHttpRequest();
@@ -41,7 +58,7 @@ CKEDITOR.plugins.add('inlinesave', {
                                ajaxpost.send(data);
                        },
                });
-               editor.setKeystroke(CKEDITOR.CTRL + 83 /*S*/, 'inlinesave');
+               editor.setKeystroke(CKEDITOR.CTRL + 'S'.charCodeAt(0), 'inlinesave');
                editor.ui.addButton( 'Inlinesave', {
                        command: 'inlinesave',
                        label: editor.lang.save.toolbar,
@@ -103,31 +120,46 @@ CKEDITOR.on('instanceCreated', function (event) {
                var config = editor.config;
                config.language = 'nl';
                config.extraPlugins = 'inlinesave,placeholder,image2,uploadimage';
-               config.format_tags = 'h2;h3;h4;p';
-               config.allowedContent = true;
+               config.removePlugins = 'image,exportpdf'; // conflicts with imag2
+               config.allowedContent = {
+                       $1: {
+                               elements: CKEDITOR.dtd,
+                               attributes: true,
+                               styles: true,
+                               classes: true,
+                       },
+               };
+               config.disallowedContent = 'img[width,height]';
                config.entities = false; // keep unicode
-               config.filebrowserImageUploadUrl = '/edit/page?output=ckescript';
-               config.uploadUrl = '/edit/page?output=ckjson';
+               config.filebrowserImageUploadUrl = '/edit/page?output=ckjson';
                config.image2_alignClasses = ['left', 'center', 'right'];
+               config.image2_prefillDimensions = false;
                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: 'Paginakop', element: 'h2' },
+                       { name: 'Paragraafkop', element: 'h3' },
+                       { name: 'Alineakop', element: 'h4' },
 
-                       { name: 'Kadertekst', element: 'aside' },
-                       { name: 'Uitgelijnd', element: 'div', attributes: { 'class': 'right' } },
+                       { name: 'Gerelateerd', element: 'aside' },
+                       { name: 'Voetnoot', element: 'div', attributes: { 'class': 'right' } },
                        { name: 'Kolom', element: 'div', attributes: { 'class': 'col' } },
+                       { name: 'Navigatie', element: 'p', attributes: { 'class': 'nav' } },
                        { name: 'Waarschuwing', element: 'div', attributes: { 'class': 'warn' } },
+
+                       { name: 'Klein', element: 'small' },
+                       { name: 'Aanhaling', element: 'q' },
+                       { name: 'Doorstreept', element: 's' },
+                       { name: 'Gemarkeerd', element: 'mark' },
+                       { name: 'Rechts', element: 'span', attributes: { 'class': 'right' } },
+                       { name: 'Attributie', element: 'em', attributes: { 'class': 'right' } },
+                       { name: 'Ingelogd', element: 'span', attributes: { 'class': 'login' } },
+                       { name: 'Uitgelogd', element: 'span', attributes: { 'class': 'logout' } },
                ];
                config.pasteFilter = pastefilter;
-               config.contentsCss = document.styleSheets[0].href;
+               config.contentsCss = [...document.styleSheets].map(e => e.href).filter(Boolean);
                config.toolbar = [
                        ['Inlinesave', '-', 'Undo', 'Redo'],
-                       ['Format', 'Styles'],
-                       ['Bold', 'Italic', 'Link'],
+                       ['Styles', 'Bold', 'Italic', 'Link'],
                        ['BulletedList', 'NumberedList', 'Blockquote'],
                        ['Table', 'CreateDiv'],
                        ['Image', 'HorizontalRule', 'CreatePlaceholder'],
@@ -151,17 +183,19 @@ CKEDITOR.on('instanceCreated', function (event) {
        };
 });
 
-       CKEDITOR.disableAutoInline = true;
-
-// add edit link to menu
-var pagebody = document.getElementsByClassName('static')[0];
 if (pagebody) {
-       var editlink = document.querySelector('a[href="#edit"]');
-       if (editlink)
+       // add edit link to menu
+       var editdiv = document.querySelector('header .login > p');
+       if (!editdiv) return;
+       var editlink = editdiv.appendChild(document.createElement('a'));
+       editlink.append('\u270D');
+       editlink.className = 'icon icon-edit';
+       editlink.href = '#edit';
        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 +214,15 @@ if (pagebody) {
        }
 }
 
+}
+
+var ckesrc = document.currentScript.getAttribute('data-ckesrc');
+document.addEventListener('DOMContentLoaded', function (e) {
+       pagebody = editorcontents();
+       if (!pagebody) return;
+       pagebody = pagebody.cloneNode(true);
+       var editorinc = document.createElement('script');
+       editorinc.addEventListener('load', editorsetup);
+       editorinc.src = ckesrc;
+       document.getElementsByTagName('head')[0].appendChild(editorinc);
+});