edit/page: remove all styling attributes in table editor
[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.ui.addButton( 'Inlinesave', {
43                         command: 'inlinesave',
44                         label: editor.lang.save.toolbar,
45                         icon: 'save',
46                 });
47         }
48 });
49
50 CKEDITOR.on('dialogDefinition', function (event) {
51         switch (event.data.name) {
52         case 'table':
53                 // override initial attribute values
54                 var infotab = event.data.definition.getContents('info');
55                 infotab.remove('txtWidth');
56                 infotab.remove('txtHeight');
57                 infotab.remove('txtBorder');
58                 infotab.remove('txtCellSpace');
59                 infotab.remove('txtCellPad');
60                 infotab.remove('cmbAlign');
61                 break;
62         case 'link':
63                 // hide unneeded widgets from the Link Info tab
64                 event.data.definition.getContents('info').get('linkType').hidden = true;
65                 let linktarget = event.data.definition.getContents('target').get('linkTargetType');
66                 linktarget.items = [ linktarget.items[0], linktarget.items[3] ]; // only _blank
67                 break;
68         }
69 });
70
71 CKEDITOR.on('instanceCreated', function (event) {
72         var editor = event.editor;
73         var pastefilter = 'h2 h3 p ul ol li blockquote em i strong b; a[!href]; img[alt,!src]';
74
75         editor.on('paste', function (e) {
76                 var html = e.data.dataValue;
77                 if (!/<[^>]* style="/.test(html) && !/<font/.test(html)) return;
78
79                 // force pasteFilter on contents containing styling attributes
80                 var filter = new CKEDITOR.filter(pastefilter),
81                         fragment = CKEDITOR.htmlParser.fragment.fromHtml(html),
82                         writer = new CKEDITOR.htmlParser.basicWriter();
83                 filter.applyTo(fragment);
84                 fragment.writeHtml(writer);
85                 e.data.dataValue = writer.getHtml();
86         });
87
88         editor.on('configLoaded', function () {
89                 var config = editor.config;
90                 config.language = 'nl';
91                 config.extraPlugins = 'inlinesave,placeholder,image2,uploadimage';
92                 config.format_tags = 'h2;h3;h4;p';
93                 config.allowedContent = true;
94                 config.entities = false; // keep unicode
95                 config.filebrowserImageUploadUrl = '/edit/page?output=ckescript';
96                 config.uploadUrl = '/edit/page?output=ckjson';
97                 config.image2_alignClasses = ['left', 'center', 'right'];
98                 config.image2_disableResizer = true;
99                 config.stylesSet = [
100                         { name: 'Klein', element: 'small' },
101                         { name: 'Zijkant', element: 'span', attributes: { 'class': 'right' } },
102                         { name: 'Attributie', element: 'em', attributes: { 'class': 'right' } },
103                         { name: 'Quote', element: 'q' },
104                         { name: 'Gemarkeerd', element: 'span', styles: { 'background-color': 'Yellow' } },
105
106                         { name: 'Kadertekst', element: 'aside' },
107                         { name: 'Uitgelijnd', element: 'div', attributes: { 'class': 'right' } },
108                         { name: 'Kolom', element: 'div', attributes: { 'class': 'col' } },
109                         { name: 'Waarschuwing', element: 'div', attributes: { 'class': 'warn' } },
110                 ];
111                 config.pasteFilter = pastefilter;
112                 config.contentsCss = document.styleSheets[0].href;
113                 config.toolbar = [
114                         ['Inlinesave', '-', 'Undo', 'Redo'],
115                         ['Format', 'Styles'],
116                         ['Bold', 'Italic', 'Link'],
117                         ['BulletedList', 'NumberedList', 'Blockquote'],
118                         ['Table', 'CreateDiv'],
119                         ['Image', 'HorizontalRule', 'CreatePlaceholder'],
120 //                      ['Sourcedialog'],
121                         ['closebtn'],
122                 ];
123                 config.toolbarCanCollapse = true;
124                 config.floatSpacePreferRight = true;
125                 config.floatSpaceDockedOffsetY = 0;
126                 config.title = false;
127                 config.startupFocus = true;
128
129                 config.disableObjectResizing = true;
130                 document.execCommand('enableObjectResizing', false, false); // workaround in inline mode; ff bug?
131         });
132
133         window.onbeforeunload = function () {
134                 if (editor.checkDirty()) {
135                         return 'Pagina verlaten zonder wijzigingen op te slaan?'; // message ignored in modern browsers
136                 }
137         };
138 });
139
140         CKEDITOR.disableAutoInline = true;
141
142 // add edit link to menu
143 var pagebody = document.getElementsByClassName('static')[0];
144 if (pagebody) {
145         var editlink = document.querySelector('a[href="#edit"]');
146         if (editlink)
147         editlink.onclick = function (e) {
148                 editlink.style.fontWeight = 'bold';
149                 editlink.href = '';
150                 editlink.onclick = undefined;
151                 pagebody.setAttribute('contenteditable', true);
152                 pagebody.innerHTML = pagebody.innerHTML
153                         .replace(/<!--BLOCK:(.*?)-->[^]*?<!--\/-->/g, '$1');
154                 CKEDITOR.inline(pagebody, { customConfig: '' });
155                 document.body.className = 'edit';
156                 return false;
157         };
158         if (window.location.hash == '#edit') {
159                 editlink.onclick();
160         }
161 }
162