edit: static edit link
[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                                                 alert('Pagina is goed opgeslagen');
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         editor.on('configLoaded', function () {
49                 var config = editor.config;
50                 config.language = 'nl';
51                 config.extraPlugins = 'sourcedialog,inlinesave,placeholder';
52                 config.format_tags = 'h2;h3;h4;p';
53                 config.allowedContent = true;
54                 config.entities = false; // keep unicode
55                 config.filebrowserImageUploadUrl = '/edit?type=img';
56                 config.forcePasteAsPlainText = true;
57                 config.contentsCss = '/excelsior.css';
58                 config.toolbar = [
59                         ['Inlinesave', '-', 'ShowBlocks', 'Sourcedialog', '-', 'Undo', 'Redo'],
60                         ['Format'],
61                         ['BulletedList', 'NumberedList', '-', 'Blockquote'],
62                         ['Bold', 'Italic', 'Underline', 'Strike', 'RemoveFormat', '-', 'Anchor', 'Link'],
63                         ['HorizontalRule', 'Table', 'Image', 'CreatePlaceholder'],
64                 ];
65                 config.toolbarCanCollapse = true;
66                 config.floatSpacePreferRight = true;
67                 config.floatSpaceDockedOffsetY = 0;
68                 config.startupFocus = true;
69
70                 config.disableObjectResizing = true;
71                 document.execCommand('enableObjectResizing', false, false); // workaround in inline mode; ff bug?
72         });
73 });
74
75         CKEDITOR.disableAutoInline = true;
76
77 // add edit link to menu
78 var pagebody = document.getElementsByClassName('static')[0];
79 if (pagebody) {
80         var editlink = document.querySelector('a[href="#edit"]');
81         editlink.onclick = function (e) {
82                 editlink.style.fontWeight = 'bold';
83                 editlink.href = '';
84                 editlink.onclick = undefined;
85                 pagebody.setAttribute('contenteditable', true);
86                 pagebody.innerHTML = pagebody.innerHTML
87                         .replace(/<!--BLOCK:([^-]*)-->[^]*?<!--\/-->/g, '$1');
88                 CKEDITOR.inline(pagebody);
89                 document.body.className = 'edit';
90                 return false;
91         };
92         if (window.location.hash == '#edit') {
93                 editlink.onclick();
94         }
95 }
96