edit: move contents editor to subdirectory
[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                                                 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 = '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/page?output=ckescript';
84                 config.uploadUrl = '/edit/page?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'],
96                         ['Bold', 'Italic', 'Link'],
97                         ['BulletedList', 'NumberedList', 'Blockquote'],
98                         ['Table', 'CreateDiv'],
99                         ['Image', 'HorizontalRule', 'CreatePlaceholder'],
100 //                      ['Sourcedialog'],
101                         ['closebtn'],
102                 ];
103                 config.toolbarCanCollapse = true;
104                 config.floatSpacePreferRight = true;
105                 config.floatSpaceDockedOffsetY = 0;
106                 config.startupFocus = true;
107
108                 config.disableObjectResizing = true;
109                 document.execCommand('enableObjectResizing', false, false); // workaround in inline mode; ff bug?
110         });
111
112         window.onbeforeunload = function () {
113                 if (editor.checkDirty()) {
114                         return 'Pagina verlaten zonder wijzigingen op te slaan?'; // message ignored in modern browsers
115                 }
116         };
117 });
118
119         CKEDITOR.disableAutoInline = true;
120
121 // add edit link to menu
122 var pagebody = document.getElementsByClassName('static')[0];
123 if (pagebody) {
124         var editlink = document.querySelector('a[href="#edit"]');
125         if (editlink)
126         editlink.onclick = function (e) {
127                 editlink.style.fontWeight = 'bold';
128                 editlink.href = '';
129                 editlink.onclick = undefined;
130                 pagebody.setAttribute('contenteditable', true);
131                 pagebody.innerHTML = pagebody.innerHTML
132                         .replace(/<!--BLOCK:(.*?)-->[^]*?<!--\/-->/g, '$1');
133                 CKEDITOR.inline(pagebody, { customConfig: '' });
134                 document.body.className = 'edit';
135                 return false;
136         };
137         if (window.location.hash == '#edit') {
138                 editlink.onclick();
139         }
140 }
141