edit: leave standalone placeholders as is
[minimedit.git] / edit.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                                 ajaxpost = new XMLHttpRequest();
20                                 ajaxpost.open('POST', '/edit'+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                                                 alert('Foutcode '+ajaxpost.status+' bij opslaan: '+ajaxpost.responseText);
27                                         else
28                                                 editor.resetDirty();
29                                 };
30                                 ajaxpost.send(data);
31                         },
32                 });
33                 editor.ui.addButton( 'Inlinesave', {
34                         command: 'inlinesave',
35                         label: editor.lang.save.toolbar,
36                         icon: 'save',
37                 });
38         }
39 });
40
41 CKEDITOR.on('dialogDefinition', function (event) {
42         switch (event.data.name) {
43         case 'table':
44                 // override initial attribute values
45                 var infoTab = event.data.definition.getContents('info');
46                 infoTab.get('txtWidth').default = '';
47                 infoTab.get('txtBorder').default = '0';
48                 infoTab.get('txtCellSpace').default = '';
49                 infoTab.get('txtCellPad').default = '';
50                 break;
51         case 'link':
52                 // remove unneeded widgets from the Link Info tab
53                 var infotab = event.data.definition.getContents('info');
54                 infotab.remove('linkType');
55                 break;
56         }
57 });
58
59 CKEDITOR.on('instanceCreated', function (event) {
60         var editor = event.editor;
61         var pastefilter = 'h2 h3 p ul ol li blockquote em i strong b; a[!href]; img[alt,!src]';
62
63         editor.on('paste', function (e) {
64                 var html = e.data.dataValue;
65                 if (!/<[^>]* style="/.test(html) && !/<font/.test(html)) return;
66
67                 // force pasteFilter on contents containing styling attributes
68                 var filter = new CKEDITOR.filter(pastefilter),
69                         fragment = CKEDITOR.htmlParser.fragment.fromHtml(html),
70                         writer = new CKEDITOR.htmlParser.basicWriter();
71                 filter.applyTo(fragment);
72                 fragment.writeHtml(writer);
73                 e.data.dataValue = writer.getHtml();
74         });
75
76         editor.on('configLoaded', function () {
77                 var config = editor.config;
78                 config.language = 'nl';
79                 config.extraPlugins = 'sourcedialog,inlinesave,placeholder,image2,uploadimage';
80                 config.format_tags = 'h2;h3;h4;p';
81                 config.allowedContent = true;
82                 config.entities = false; // keep unicode
83                 config.filebrowserImageUploadUrl = '/edit?output=ckescript';
84                 config.uploadUrl = '/edit?output=ckjson';
85                 config.image2_alignClasses = ['left', 'center', 'right'];
86                 config.image2_disableResizer = true;
87                 config.stylesSet = [
88                         { name: 'Kolom', element: 'div', attributes: { 'class': 'col' } },
89                         { name: 'Rechts', element: 'div', attributes: { 'class': 'right' } },
90                 ];
91                 config.pasteFilter = pastefilter;
92                 config.contentsCss = document.styleSheets[0].href;
93                 config.toolbar = [
94                         ['Inlinesave', '-', 'Undo', 'Redo'],
95                         ['Format', 'BulletedList', 'NumberedList', 'CreateDiv', 'Table', 'Blockquote'],
96                         ['HorizontalRule', 'CreatePlaceholder', 'Image'],
97                         ['Bold', 'Italic', 'Link'],
98                         ['Sourcedialog'],
99                 ];
100                 config.toolbarCanCollapse = true;
101                 config.floatSpacePreferRight = true;
102                 config.floatSpaceDockedOffsetY = 0;
103                 config.startupFocus = true;
104
105                 config.disableObjectResizing = true;
106                 document.execCommand('enableObjectResizing', false, false); // workaround in inline mode; ff bug?
107         });
108
109         window.onbeforeunload = function () {
110                 if (editor.checkDirty()) {
111                         return 'Pagina verlaten zonder wijzigingen op te slaan?'; // message ignored in modern browsers
112                 }
113         };
114 });
115
116         CKEDITOR.disableAutoInline = true;
117
118 // add edit link to menu
119 var pagebody = document.getElementsByClassName('static')[0];
120 if (pagebody) {
121         var editlink = document.querySelector('a[href="#edit"]');
122         editlink.onclick = function (e) {
123                 editlink.style.fontWeight = 'bold';
124                 editlink.href = '';
125                 editlink.onclick = undefined;
126                 pagebody.setAttribute('contenteditable', true);
127                 pagebody.innerHTML = pagebody.innerHTML
128                         .replace(/<!--BLOCK:([^-]*)-->[^]*?<!--\/-->/g, '$1');
129                 CKEDITOR.inline(pagebody);
130                 document.body.className = 'edit';
131                 return false;
132         };
133         if (window.location.hash == '#edit') {
134                 editlink.onclick();
135         }
136 }
137