d1f16bf3c2b705d3323de6fb7963d010d5b0d440
[minimedit.git] / edit / page.js
1 CKEDITOR.plugins.add('inlinesave', {
2         init: function(editor) {
3                 editor.addCommand( 'inlinesave', {
4                         exec: function (editor) {
5                                 var pagename = window.location.pathname;
6                                 var body = editor.getData();
7                                 // trim trailing whitespace in non-empty paragraphs
8                                 body = body.replace(/((?!<p>).{3})(?:\s|\u200B)+(?=<\/p>)/g, '$1');
9                                 // empty line is equivalent to a paragraph break
10                                 body = body.replace(/<br \/>\s*<br \/>/g, '<p>');
11                                 // keep names and preceding abbreviations together
12                                 body = body.replace(/\b((?:dhr|mw|me?vr|mr?s?)\.)\s+(?=[A-Z])/ig, '$1&nbsp;');
13                                 // wrap long line after each sentence
14                                 body = body.replace(/^(\t*).{73,}/mg, function (line, indent) {
15                                         var dots = '(?:.{24,72}|.{73,}?)'; // chars before punctuation
16                                         var wrap = new RegExp('('+dots+'[.:!?]) (?=[A-Z(<])', 'g'); // separate lines
17                                         return line.replace(wrap, '$1\n'+indent+'\t');
18                                 });
19                                 // treat standalone placeholders as block elements
20                                 body = body.replace(/<p>(\[\[.*\]\])<\/p>/g, '$1');
21
22                                 var data = 'body='+encodeURIComponent(body);
23                                 var ajaxpost = new XMLHttpRequest();
24                                 ajaxpost.open('POST', '/edit/page'+pagename, true);
25                                 ajaxpost.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
26                                 ajaxpost.onreadystatechange = function () {
27                                         if (ajaxpost.readyState != 4)
28                                                 return; // not done yet
29                                         if (ajaxpost.status == 200) {
30                                                 editor.resetDirty();
31                                                 new CKEDITOR.plugins.notification(editor, {
32                                                         message: 'Pagina is succesvol opgeslagen',
33                                                         type: 'success',
34                                                 }).show();
35                                         }
36                                         else {
37                                                 new CKEDITOR.plugins.notification(editor, {
38                                                         message: 'Foutcode '+ajaxpost.status+' bij opslaan: '+ajaxpost.responseText,
39                                                         type: 'warning',
40                                                 }).show();
41                                         }
42                                 };
43                                 ajaxpost.send(data);
44                         },
45                 });
46                 editor.setKeystroke(CKEDITOR.CTRL + 83 /*S*/, 'inlinesave');
47                 editor.ui.addButton( 'Inlinesave', {
48                         command: 'inlinesave',
49                         label: editor.lang.save.toolbar,
50                         icon: 'save',
51                 });
52         }
53 });
54
55 CKEDITOR.on('dialogDefinition', function (event) {
56         switch (event.data.name) {
57         case 'table':
58                 // override initial attribute values
59                 var infotab = event.data.definition.getContents('info');
60                 infotab.remove('txtWidth');
61                 infotab.remove('txtHeight');
62                 infotab.remove('txtBorder');
63                 infotab.remove('txtCellSpace');
64                 infotab.remove('txtCellPad');
65                 infotab.remove('cmbAlign');
66
67                 // horizontal repositioning of existing elements
68                 var hbox = {
69                         id: 'hboxDimensions',
70                         type: 'hbox',
71                         children: [ infotab.get('txtCols'), infotab.get('txtRows') ],
72                 };
73                 infotab.add(hbox, 'selHeaders');
74                 infotab.remove('txtCols');
75                 infotab.remove('txtRows');
76
77                 break;
78         case 'link':
79                 // hide unneeded widgets from the Link Info tab
80                 event.data.definition.getContents('info').get('linkType').hidden = true;
81                 let linktarget = event.data.definition.getContents('target').get('linkTargetType');
82                 linktarget.items = [ linktarget.items[0], linktarget.items[3] ]; // only _blank
83                 break;
84         }
85 });
86
87 CKEDITOR.on('instanceCreated', function (event) {
88         var editor = event.editor;
89         var pastefilter = 'h2 h3 p ul ol li blockquote em i strong b; a[!href]; img[alt,!src]';
90
91         editor.on('paste', function (e) {
92                 var html = e.data.dataValue;
93                 if (!/<[^>]* style="/.test(html) && !/<font/.test(html)) return;
94
95                 // force pasteFilter on contents containing styling attributes
96                 var filter = new CKEDITOR.filter(pastefilter),
97                         fragment = CKEDITOR.htmlParser.fragment.fromHtml(html),
98                         writer = new CKEDITOR.htmlParser.basicWriter();
99                 filter.applyTo(fragment);
100                 fragment.writeHtml(writer);
101                 e.data.dataValue = writer.getHtml();
102         });
103
104         editor.on('configLoaded', function () {
105                 var config = editor.config;
106                 config.language = 'nl';
107                 config.extraPlugins = 'inlinesave,placeholder,image2,uploadimage';
108                 config.format_tags = 'h2;h3;h4;p';
109                 config.allowedContent = true;
110                 config.entities = false; // keep unicode
111                 config.filebrowserImageUploadUrl = '/edit/page?output=ckescript';
112                 config.uploadUrl = '/edit/page?output=ckjson';
113                 config.image2_alignClasses = ['left', 'center', 'right'];
114                 config.image2_disableResizer = true;
115                 config.stylesSet = [
116                         { name: 'Klein', element: 'small' },
117                         { name: 'Zijkant', element: 'span', attributes: { 'class': 'right' } },
118                         { name: 'Attributie', element: 'em', attributes: { 'class': 'right' } },
119                         { name: 'Quote', element: 'q' },
120                         { name: 'Gemarkeerd', element: 'span', styles: { 'background-color': 'Yellow' } },
121
122                         { name: 'Kadertekst', element: 'aside' },
123                         { name: 'Uitgelijnd', element: 'div', attributes: { 'class': 'right' } },
124                         { name: 'Kolom', element: 'div', attributes: { 'class': 'col' } },
125                         { name: 'Waarschuwing', element: 'div', attributes: { 'class': 'warn' } },
126                 ];
127                 config.pasteFilter = pastefilter;
128                 config.contentsCss = document.styleSheets[0].href;
129                 config.toolbar = [
130                         ['Inlinesave', '-', 'Undo', 'Redo'],
131                         ['Format', 'Styles'],
132                         ['Bold', 'Italic', 'Link'],
133                         ['BulletedList', 'NumberedList', 'Blockquote'],
134                         ['Table', 'CreateDiv'],
135                         ['Image', 'HorizontalRule', 'CreatePlaceholder'],
136 //                      ['Sourcedialog'],
137                         ['closebtn'],
138                 ];
139                 config.toolbarCanCollapse = true;
140                 config.floatSpacePreferRight = true;
141                 config.floatSpaceDockedOffsetY = 0;
142                 config.title = false;
143                 config.startupFocus = true;
144
145                 config.disableObjectResizing = true;
146                 document.execCommand('enableObjectResizing', false, false); // workaround in inline mode; ff bug?
147         });
148
149         window.onbeforeunload = function () {
150                 if (editor.checkDirty()) {
151                         return 'Pagina verlaten zonder wijzigingen op te slaan?'; // message ignored in modern browsers
152                 }
153         };
154 });
155
156         CKEDITOR.disableAutoInline = true;
157
158 // add edit link to menu
159 var pagebody = document.getElementsByClassName('static')[0];
160 if (pagebody) {
161         var editlink = document.querySelector('a[href="#edit"]');
162         if (editlink)
163         editlink.onclick = function (e) {
164                 editlink.style.fontWeight = 'bold';
165                 editlink.href = '';
166                 editlink.onclick = undefined;
167                 pagebody.setAttribute('contenteditable', true);
168                 pagebody.querySelectorAll('[data-dyn]').forEach(function (el) {
169                         let blockname = el.getAttribute('data-dyn');
170                         if (!blockname) {
171                                 el.remove();
172                                 return;
173                         }
174                         el.outerHTML = '[[' + blockname + ']]';
175                 });
176                 CKEDITOR.inline(pagebody, { customConfig: '' });
177                 document.body.className = 'edit';
178                 return false;
179         };
180         if (window.location.hash == '#edit') {
181                 editlink.onclick();
182         }
183 }
184