annotate src/plugins/dragdrop.js @ 47:16b61f228546 default tip

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 31 Oct 2022 10:41:24 -0600
parents ea32a44b5a6e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 /**
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 * SCEditor Drag and Drop Plugin
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 * http://www.sceditor.com/
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 *
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 * Copyright (C) 2017, Sam Clarke (samclarke.com)
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 *
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 * SCEditor is licensed under the MIT license:
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 * http://www.opensource.org/licenses/mit-license.php
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 *
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 * @author Sam Clarke
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 */
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 (function (sceditor) {
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 'use strict';
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 /**
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 * Place holder GIF shown while image is loading.
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 * @type {string}
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 * @private
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 */
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 var loadingGif = 'data:image/gif;base64,R0lGODlhlgBkAPABAH19ffb29iH5BAAK' +
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 'AAAAIf4aQ3JlYXRlZCB3aXRoIGFqYXhsb2FkLmluZm8AIf8LTkVUU0NBUEUyLjADAQA' +
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 'AACwAAAAAlgBkAAAC1YyPqcvtD6OctNqLs968+w+G4kiW5omm6sq27gvH8kzX9o3n+s' +
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 '73/g8MCofEovGITCqXzKbzCY1Kp9Sq9YrNarfcrvcLDovH5LL5jE6r1+y2+w2Py+f0u' +
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 'v2OvwD2fP6iD/gH6Pc2GIhg2JeQSNjGuLf4GMlYKIloefAIUEl52ZmJyaY5mUhqyFnq' +
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 'mQr6KRoaMKp66hbLumpQ69oK+5qrOyg4a6qYV2x8jJysvMzc7PwMHS09TV1tfY2drb3' +
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 'N3e39DR4uPk5ebn6Onq6+zt7u/g4fL99UAAAh+QQACgAAACwAAAAAlgBkAIEAAAB9fX' +
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 '329vYAAAAC3JSPqcvtD6OctNqLs968+w+G4kiW5omm6sq27gvH8kzX9o3n+s73/g8MC' +
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 'ofEovGITCqXzKbzCY1Kp9Sq9YrNarfcrvcLDovH5LL5jE6r1+y2+w2Py+f0uv2OvwD2' +
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 'fP4iABgY+CcoCNeHuJdQyLjIaOiWiOj4CEhZ+SbZd/nI2RipqYhQOThKGpAZCuBZyAr' +
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 'ZprpqSupaCqtaazmLCRqai7rb2av5W5wqSShcm8fc7PwMHS09TV1tfY2drb3N3e39DR' +
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 '4uPk5ebn6Onq6+zt7u/g4fLz9PX29/j5/vVAAAIfkEAAoAAAAsAAAAAJYAZACBAAAAf' +
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 'X199vb2AAAAAuCUj6nL7Q+jnLTai7PevPsPhuJIluaJpurKtu4Lx/JM1/aN5/rO9/4P' +
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 'DAqHxKLxiEwql8ym8wmNSqfUqvWKzWq33K73Cw6Lx+Sy+YxOq9fstvsNj8vn9Lr9jr8' +
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 'E9nz+AgAYGLjQVwhXiJgguAiYgGjo9tinyCjoKLn3hpmJUGmJsBmguUnpCXCJOZraaX' +
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 'oKShoJe9DqehCqKlnqiZobuzrbyvuIO8xqKpxIPKlwrPCbBx0tPU1dbX2Nna29zd3t/' +
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 'Q0eLj5OXm5+jp6uvs7e7v4OHy8/T19vf4+fr7/P379UAAAh+QQACgAAACwAAAAAlgBk' +
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 'AIEAAAB9fX329vYAAAAC4JSPqcvtD6OctNqLs968+w+G4kiW5omm6sq27gvH8kzX9o3' +
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 'n+s73/g8MCofEovGITCqXzKbzCY1Kp9Sq9YrNarfcrvcLDovH5LL5jE6r1+y2+w2Py+' +
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 'f0uv2OvwT2fP6iD7gAMEhICAeImIAYiFDoOPi22KcouZfw6BhZGUBZeYlp6LbJiTD6C' +
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 'Qqg6Vm6eQqqKtkZ24iaKtrKunpQa9tmmju7Wwu7KFtMi3oYDMzompkHHS09TV1tfY2d' +
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 'rb3N3e39DR4uPk5ebn6Onq6+zt7u/g4fLz9PX29/j5+vv8/f31QAADs=';
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 /**
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 * Basic check for browser support
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 * @type {boolean}
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 * @private
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 */
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 var isSupported = typeof window.FileReader !== 'undefined';
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 var base64DataUri = /data:[^;]+;base64,/i;
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 function base64DataUriToBlob(url) {
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 // 5 is length of "data:" prefix
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 var mime = url.substr(5, url.indexOf(';') - 5);
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 var data = atob(url.substr(url.indexOf(',') + 1));
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 /* global Uint8Array */
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 var binary = new Uint8Array(data.length);
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 for (var i = 0; i < data.length; i++) {
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 binary[i] = data[i].charCodeAt(0);
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 }
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 try {
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 return new Blob([binary], { type: mime });
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 } catch (e) {
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65 return null;
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 }
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 }
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
69 sceditor.plugins.dragdrop = function () {
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 if (!isSupported) {
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71 return;
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 }
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73
29
ea32a44b5a6e remove more oo
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
74 var base = {};
0
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 var opts;
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76 var editor;
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
77 var handleFile;
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
78 var container;
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79 var cover;
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
80 var placeholderId = 0;
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
81
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83 function hideCover() {
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84 cover.style.display = 'none';
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
85 container.className = container.className.replace(/(^| )dnd( |$)/g, '');
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
86 }
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
87
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88 function showCover() {
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
89 if (cover.style.display === 'none') {
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90 cover.style.display = 'block';
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
91 container.className += ' dnd';
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
92 }
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
93 }
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
94
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
95 function isAllowed(file) {
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
96 // FF sets type to application/x-moz-file until it has been dropped
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
97 if (file.type !== 'application/x-moz-file' && opts.allowedTypes &&
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
98 opts.allowedTypes.indexOf(file.type) < 0) {
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
99 return false;
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
100 }
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
101
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
102 return opts.isAllowed ? opts.isAllowed(file) : true;
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
103 };
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
104
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
105 function createHolder(toReplace) {
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
106 var placeholder = document.createElement('img');
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
107 placeholder.src = loadingGif;
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
108 placeholder.className = 'sceditor-ignore';
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
109 placeholder.id = 'sce-dragdrop-' + placeholderId++;
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
110
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
111 function replace(html) {
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
112 var node = editor
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
113 .getBody()
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
114 .ownerDocument
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
115 .getElementById(placeholder.id);
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
116
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
117 if (node) {
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
118 if (typeof html === 'string') {
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
119 node.insertAdjacentHTML('afterend', html);
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
120 }
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
121
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
122 node.parentNode.removeChild(node);
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
123 }
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
124 }
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
125
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
126 return function () {
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
127 if (toReplace) {
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
128 toReplace.parentNode.replaceChild(placeholder, toReplace);
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
129 } else {
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
130 editor.wysiwygEditorInsertHtml(placeholder.outerHTML);
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
131 }
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
132
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
133 return {
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
134 insert: function (html) {
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
135 replace(html);
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
136 },
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
137 cancel: replace
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
138 };
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
139 };
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
140 }
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
141
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
142 function handleDragOver(e) {
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
143 var dt = e.dataTransfer;
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
144 var files = dt.files.length || !dt.items ? dt.files : dt.items;
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
145
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
146 for (var i = 0; i < files.length; i++) {
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
147 // Dragging a string should be left to default
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
148 if (files[i].kind === 'string') {
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
149 return;
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
150 }
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
151 }
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
152
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
153 showCover();
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
154 e.preventDefault();
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
155 }
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
156
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
157 function handleDrop(e) {
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
158 var dt = e.dataTransfer;
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
159 var files = dt.files.length || !dt.items ? dt.files : dt.items;
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
160
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
161 hideCover();
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
162
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
163 for (var i = 0; i < files.length; i++) {
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
164 // Dragging a string should be left to default
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
165 if (files[i].kind === 'string') {
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
166 return;
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
167 }
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
168
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
169 if (isAllowed(files[i])) {
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
170 handleFile(files[i], createHolder());
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
171 }
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
172 }
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
173
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
174 e.preventDefault();
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
175 }
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
176
29
ea32a44b5a6e remove more oo
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
177 base.signalReady = function (ed) {
ea32a44b5a6e remove more oo
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
178 editor = ed;
0
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
179 opts = editor.opts.dragdrop || {};
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
180 handleFile = opts.handleFile;
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
181
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
182 container = editor.getContentAreaContainer().parentNode;
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
183
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
184 cover = container.appendChild(sceditor.dom.parseHTML(
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
185 '<div class="sceditor-dnd-cover" style="display: none">' +
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
186 '<p>' + editor._('Drop files here') + '</p>' +
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
187 '</div>'
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
188 ).firstChild);
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
189
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
190 container.addEventListener('dragover', handleDragOver);
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
191 container.addEventListener('dragleave', hideCover);
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
192 container.addEventListener('dragend', hideCover);
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
193 container.addEventListener('drop', handleDrop);
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
194
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
195 editor.getBody().addEventListener('dragover', handleDragOver);
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
196 editor.getBody().addEventListener('drop', hideCover);
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
197 };
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
198
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
199 base.signalPasteHtml = function (paste) {
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
200 if (!('handlePaste' in opts) || opts.handlePaste) {
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
201 var div = document.createElement('div');
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
202 div.innerHTML = paste.val;
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
203
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
204 var images = div.querySelectorAll('img');
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
205 for (var i = 0; i < images.length; i++) {
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
206 var image = images[i];
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
207
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
208 if (base64DataUri.test(image.src)) {
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
209 var file = base64DataUriToBlob(image.src);
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
210 if (file && isAllowed(file)) {
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
211 handleFile(file, createHolder(image));
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
212 } else {
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
213 image.parentNode.removeChild(image);
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
214 }
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
215 }
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
216 }
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
217
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
218 paste.val = div.innerHTML;
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
219 }
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
220 };
29
ea32a44b5a6e remove more oo
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
221
ea32a44b5a6e remove more oo
Franklin Schmidt <fschmidt@gmail.com>
parents: 4
diff changeset
222 return base;
0
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
223 };
4c4fc447baea start with sceditor-3.1.1
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
224 })(sceditor);