]> BookStack Code Mirror - bookstack/blob - resources/assets/js/pages/page-form.js
Updated all application urls to allow path prefix.
[bookstack] / resources / assets / js / pages / page-form.js
1 var mceOptions = module.exports = {
2     selector: '#html-editor',
3     content_css: [
4         window.baseUrl('/css/styles.css'),
5         window.baseUrl('/libs/material-design-iconic-font/css/material-design-iconic-font.min.css')
6     ],
7     body_class: 'page-content',
8     relative_urls: false,
9     statusbar: false,
10     menubar: false,
11     paste_data_images: false,
12     extended_valid_elements: 'pre[*]',
13     automatic_uploads: false,
14     valid_children: "-div[p|pre|h1|h2|h3|h4|h5|h6|blockquote]",
15     plugins: "image table textcolor paste link fullscreen imagetools code customhr autosave lists",
16     imagetools_toolbar: 'imageoptions',
17     toolbar: "undo redo | styleselect | bold italic underline strikethrough superscript subscript | forecolor backcolor | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | table image-insert link hr | removeformat code fullscreen",
18     content_style: "body {padding-left: 15px !important; padding-right: 15px !important; margin:0!important; margin-left:auto!important;margin-right:auto!important;}",
19     style_formats: [
20         {title: "Header 1", format: "h1"},
21         {title: "Header 2", format: "h2"},
22         {title: "Header 3", format: "h3"},
23         {title: "Paragraph", format: "p", exact: true, classes: ''},
24         {title: "Blockquote", format: "blockquote"},
25         {title: "Code Block", icon: "code", format: "pre"},
26         {title: "Inline Code", icon: "code", inline: "code"},
27         {title: "Callouts", items: [
28             {title: "Success", block: 'p', exact: true, attributes : {'class' : 'callout success'}},
29             {title: "Info", block: 'p', exact: true, attributes : {'class' : 'callout info'}},
30             {title: "Warning", block: 'p', exact: true, attributes : {'class' : 'callout warning'}},
31             {title: "Danger", block: 'p', exact: true, attributes : {'class' : 'callout danger'}}
32         ]}
33     ],
34     style_formats_merge: false,
35     formats: {
36         alignleft: {selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img', classes: 'align-left'},
37         aligncenter: {selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img', classes: 'align-center'},
38         alignright: {selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img', classes: 'align-right'},
39     },
40     file_browser_callback: function (field_name, url, type, win) {
41         window.ImageManager.showExternal(function (image) {
42             win.document.getElementById(field_name).value = image.url;
43             if ("createEvent" in document) {
44                 var evt = document.createEvent("HTMLEvents");
45                 evt.initEvent("change", false, true);
46                 win.document.getElementById(field_name).dispatchEvent(evt);
47             } else {
48                 win.document.getElementById(field_name).fireEvent("onchange");
49             }
50             var html = '<a href="' + image.url + '" target="_blank">';
51             html += '<img src="' + image.thumbs.display + '" alt="' + image.name + '">';
52             html += '</a>';
53             win.tinyMCE.activeEditor.execCommand('mceInsertContent', false, html);
54         });
55     },
56     paste_preprocess: function (plugin, args) {
57         var content = args.content;
58         if (content.indexOf('<img src="file://') !== -1) {
59             args.content = '';
60         }
61     },
62     extraSetups: [],
63     setup: function (editor) {
64
65         for (var i = 0; i < mceOptions.extraSetups.length; i++) {
66             mceOptions.extraSetups[i](editor);
67         }
68
69         (function () {
70             var wrap;
71
72             function hasTextContent(node) {
73                 return node && !!( node.textContent || node.innerText );
74             }
75
76             editor.on('dragstart', function () {
77                 var node = editor.selection.getNode();
78
79                 if (node.nodeName === 'IMG') {
80                     wrap = editor.dom.getParent(node, '.mceTemp');
81
82                     if (!wrap && node.parentNode.nodeName === 'A' && !hasTextContent(node.parentNode)) {
83                         wrap = node.parentNode;
84                     }
85                 }
86             });
87
88             editor.on('drop', function (event) {
89                 var dom = editor.dom,
90                     rng = tinymce.dom.RangeUtils.getCaretRangeFromPoint(event.clientX, event.clientY, editor.getDoc());
91
92                 // Don't allow anything to be dropped in a captioned image.
93                 if (dom.getParent(rng.startContainer, '.mceTemp')) {
94                     event.preventDefault();
95                 } else if (wrap) {
96                     event.preventDefault();
97
98                     editor.undoManager.transact(function () {
99                         editor.selection.setRng(rng);
100                         editor.selection.setNode(wrap);
101                         dom.remove(wrap);
102                     });
103                 }
104
105                 wrap = null;
106             });
107         })();
108
109         // Image picker button
110         editor.addButton('image-insert', {
111             title: 'My title',
112             icon: 'image',
113             tooltip: 'Insert an image',
114             onclick: function () {
115                 window.ImageManager.showExternal(function (image) {
116                     var html = '<a href="' + image.url + '" target="_blank">';
117                     html += '<img src="' + image.thumbs.display + '" alt="' + image.name + '">';
118                     html += '</a>';
119                     editor.execCommand('mceInsertContent', false, html);
120                 });
121             }
122         });
123
124         // Paste image-uploads
125         editor.on('paste', function (e) {
126             if (e.clipboardData) {
127                 var items = e.clipboardData.items;
128                 if (items) {
129                     for (var i = 0; i < items.length; i++) {
130                         if (items[i].type.indexOf("image") !== -1) {
131
132                             var file = items[i].getAsFile();
133                             var formData = new FormData();
134                             var ext = 'png';
135                             var xhr = new XMLHttpRequest();
136
137                             if (file.name) {
138                                 var fileNameMatches = file.name.match(/\.(.+)$/);
139                                 if (fileNameMatches) {
140                                     ext = fileNameMatches[1];
141                                 }
142                             }
143
144                             var id = "image-" + Math.random().toString(16).slice(2);
145                             editor.execCommand('mceInsertContent', false, '<img src="/loading.gif" id="' + id + '">');
146
147                             var remoteFilename = "image-" + Date.now() + "." + ext;
148                             formData.append('file', file, remoteFilename);
149                             formData.append('_token', document.querySelector('meta[name="token"]').getAttribute('content'));
150
151                             xhr.open('POST', window.baseUrl('/images/gallery/upload'));
152                             xhr.onload = function () {
153                                 if (xhr.status === 200 || xhr.status === 201) {
154                                     var result = JSON.parse(xhr.responseText);
155                                     editor.dom.setAttrib(id, 'src', result.url);
156                                 } else {
157                                     console.log('An error occured uploading the image');
158                                     console.log(xhr.responseText);
159                                     editor.dom.remove(id);
160                                 }
161                             };
162                             xhr.send(formData);
163                         }
164                     }
165                 }
166
167             }
168         });
169     }
170 };