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