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