edit: replace custom paragraph breaks by sentence wrapping
[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 dot = '(?:[^<]|<[^>]*>)'; // one character
9                                         var dots = '(?:'+dot+'{24,72}|'+dot+'{73,}?)'; // chars before punctuation
10                                         var wrap = new RegExp('('+dots+'[.;:!?]) (?=[A-Z])', 'g'); // separate lines
11                                         return line.replace(wrap, '$1\n'+indent+'\t');
12                                 });
13                                 var data = 'body='+encodeURIComponent(body);
14                                 ajaxpost = new XMLHttpRequest();
15                                 ajaxpost.open('POST', '/edit'+pagename, true);
16                                 ajaxpost.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
17                                 ajaxpost.onreadystatechange = function () {
18                                         if (ajaxpost.readyState != 4)
19                                                 return; // not done yet
20                                         if (ajaxpost.status != 200)
21                                                 alert('Foutcode '+ajaxpost.status+' bij opslaan: '+ajaxpost.responseText);
22                                         else
23                                                 alert('Pagina is goed opgeslagen');
24                                 };
25                                 ajaxpost.send(data);
26                         },
27                 });
28                 editor.ui.addButton( 'Inlinesave', {
29                         command: 'inlinesave',
30                         label: editor.lang.save.toolbar,
31                         icon: 'save',
32                 });
33         }
34 });
35
36 CKEDITOR.on('dialogDefinition', function (event) {
37         if (event.data.name === 'table') {
38                 // override initial attribute values
39                 var infoTab = event.data.definition.getContents('info');
40                 infoTab.get('txtWidth').default = '';
41                 infoTab.get('txtBorder').default = '0';
42                 infoTab.get('txtCellSpace').default = '';
43                 infoTab.get('txtCellPad').default = '';
44         }
45 });
46
47 CKEDITOR.on('instanceCreated', function (event) {
48         var editor = event.editor;
49         editor.on('configLoaded', function () {
50                 var config = editor.config;
51                 config.language = 'nl';
52                 config.extraPlugins = 'sourcedialog,inlinesave,placeholder';
53                 config.format_tags = 'h2;h3;h4;p';
54                 config.allowedContent = true;
55                 config.entities = false; // keep unicode
56                 config.filebrowserImageUploadUrl = '/edit?type=img';
57                 config.forcePasteAsPlainText = true;
58                 config.contentsCss = '/excelsior.css';
59                 config.toolbar = [
60                         ['Inlinesave', '-', 'ShowBlocks', 'Sourcedialog', '-', 'Undo', 'Redo'],
61                         ['Format'],
62                         ['BulletedList', 'NumberedList', '-', 'Blockquote'],
63                         ['Bold', 'Italic', 'Underline', 'Strike', 'RemoveFormat', '-', 'Anchor', 'Link'],
64                         ['HorizontalRule', 'Table', 'Image', 'CreatePlaceholder'],
65                 ];
66                 config.toolbarCanCollapse = true;
67                 config.floatSpacePreferRight = true;
68                 config.floatSpaceDockedOffsetY = 0;
69                 config.startupFocus = true;
70
71                 config.disableObjectResizing = true;
72                 document.execCommand('enableObjectResizing', false, false); // workaround in inline mode; ff bug?
73         });
74 });
75
76         CKEDITOR.disableAutoInline = true;
77
78 // add edit link to menu
79 var pagebody = document.getElementsByClassName('static')[0];
80 if (pagebody) {
81         var editlink = document.createElement('a');
82         editlink.style.cursor = 'pointer';
83         editlink.appendChild(document.createTextNode('Wijzig'));
84         editlink.href = '#edit';
85         editlink.onclick = function (e) {
86                 editlink.style.fontWeight = 'bold';
87                 editlink.href = '';
88                 editlink.onclick = undefined;
89                 pagebody.setAttribute('contenteditable', true);
90                 pagebody.innerHTML = pagebody.innerHTML
91                         .replace(/<!--BLOCK:([^-]*)-->[^]*?<!--\/-->/g, '$1');
92                 CKEDITOR.inline(pagebody);
93                 document.body.className = 'edit';
94                 return false;
95         };
96         if (window.location.hash == '#edit') {
97                 editlink.onclick();
98         }
99         document.querySelector('header ul').appendChild(editlink);
100 }
101