6dcb705cee1c72e649530d8b508ce518889338cc
[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().replace(/^(\t*).{73,}/mg, function (line, indent) {
7                                         // wrap long line after each sentence
8                                         var dots = '(?:.{24,72}|.{73,}?)'; // chars before punctuation
9                                         var wrap = new RegExp('('+dots+'[.;:!?]) (?=[A-Z(<])', 'g'); // separate lines
10                                         return line.replace(wrap, '$1\n'+indent+'\t');
11                                 });
12                                 var data = 'body='+encodeURIComponent(body);
13                                 ajaxpost = new XMLHttpRequest();
14                                 ajaxpost.open('POST', '/edit'+pagename, true);
15                                 ajaxpost.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
16                                 ajaxpost.onreadystatechange = function () {
17                                         if (ajaxpost.readyState != 4)
18                                                 return; // not done yet
19                                         if (ajaxpost.status != 200)
20                                                 alert('Foutcode '+ajaxpost.status+' bij opslaan: '+ajaxpost.responseText);
21                                         else
22                                                 editor.resetDirty();
23                                 };
24                                 ajaxpost.send(data);
25                         },
26                 });
27                 editor.ui.addButton( 'Inlinesave', {
28                         command: 'inlinesave',
29                         label: editor.lang.save.toolbar,
30                         icon: 'save',
31                 });
32         }
33 });
34
35 CKEDITOR.on('dialogDefinition', function (event) {
36         if (event.data.name === 'table') {
37                 // override initial attribute values
38                 var infoTab = event.data.definition.getContents('info');
39                 infoTab.get('txtWidth').default = '';
40                 infoTab.get('txtBorder').default = '0';
41                 infoTab.get('txtCellSpace').default = '';
42                 infoTab.get('txtCellPad').default = '';
43         }
44 });
45
46 CKEDITOR.on('instanceCreated', function (event) {
47         var editor = event.editor;
48         var pastefilter = 'h2 h3 p ul ol li blockquote em i strong b; a[!href]; img[alt,!src]';
49
50         editor.on('paste', function (e) {
51                 var html = e.data.dataValue;
52                 if (!/<[^>]* style="/.test(html) && !/<font/.test(html)) return;
53
54                 // force pasteFilter on contents containing styling attributes
55                 var filter = new CKEDITOR.filter(pastefilter),
56                         fragment = CKEDITOR.htmlParser.fragment.fromHtml(html),
57                         writer = new CKEDITOR.htmlParser.basicWriter();
58                 filter.applyTo(fragment);
59                 fragment.writeHtml(writer);
60                 e.data.dataValue = writer.getHtml();
61         });
62
63         editor.on('configLoaded', function () {
64                 var config = editor.config;
65                 config.language = 'nl';
66                 config.extraPlugins = 'sourcedialog,inlinesave,placeholder';
67                 config.format_tags = 'h2;h3;h4;p';
68                 config.allowedContent = true;
69                 config.entities = false; // keep unicode
70                 config.filebrowserImageUploadUrl = '/edit?type=img';
71                 config.pasteFilter = pastefilter;
72                 config.contentsCss = document.styleSheets[0].href;
73                 config.toolbar = [
74                         ['Inlinesave', '-', 'Undo', 'Redo'],
75                         ['Format', 'BulletedList', 'NumberedList', 'Blockquote'],
76                         ['Bold', 'Italic', 'Link'],
77                         ['HorizontalRule', 'Table', 'Image'],
78                         ['CreatePlaceholder', 'Sourcedialog'],
79                 ];
80                 config.toolbarCanCollapse = true;
81                 config.floatSpacePreferRight = true;
82                 config.floatSpaceDockedOffsetY = 0;
83                 config.startupFocus = true;
84
85                 config.disableObjectResizing = true;
86                 document.execCommand('enableObjectResizing', false, false); // workaround in inline mode; ff bug?
87         });
88
89         window.onbeforeunload = function () {
90                 if (editor.checkDirty()) {
91                         return 'Pagina verlaten zonder wijzigingen op te slaan?'; // message ignored in modern browsers
92                 }
93         };
94 });
95
96         CKEDITOR.disableAutoInline = true;
97
98 // add edit link to menu
99 var pagebody = document.getElementsByClassName('static')[0];
100 if (pagebody) {
101         var editlink = document.querySelector('a[href="#edit"]');
102         editlink.onclick = function (e) {
103                 editlink.style.fontWeight = 'bold';
104                 editlink.href = '';
105                 editlink.onclick = undefined;
106                 pagebody.setAttribute('contenteditable', true);
107                 pagebody.innerHTML = pagebody.innerHTML
108                         .replace(/<!--BLOCK:([^-]*)-->[^]*?<!--\/-->/g, '$1');
109                 CKEDITOR.inline(pagebody);
110                 document.body.className = 'edit';
111                 return false;
112         };
113         if (window.location.hash == '#edit') {
114                 editlink.onclick();
115         }
116 }
117