Skip to content

Commit 0daf766

Browse files
committed
respect storageType on copy&paste
Instead of observing the clipboard, we first put the data URI into the pad text, so it is uploaded by contentCollector. When Etherpad starts using the Clipboard API we should use it too.
1 parent bf9e8bd commit 0daf766

1 file changed

Lines changed: 30 additions & 1 deletion

File tree

static/js/contentCollection.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,47 @@
11
'use strict';
22

3+
const {_handleNewLines, _isValid, uploadFile} = require('ep_image_upload/static/js/toolbar');
4+
35
// When an image is detected give it a lineAttribute
46
// of Image with the URL to the iamge
57
exports.collectContentImage = (hookName, {node, state: {lineAttributes}, tname}) => {
68
if (tname === 'div' || tname === 'p') delete lineAttributes.img;
79
if (tname !== 'img') return;
8-
lineAttributes.img =
10+
const imageData =
911
// Client-side. This will also be used for server-side HTML imports once jsdom adds support
1012
// for HTMLImageElement.currentSrc.
1113
node.currentSrc ||
1214
// Server-side HTML imports using jsdom v16.6.0 (Etherpad v1.8.15).
1315
node.src ||
1416
// Server-side HTML imports using cheerio (Etherpad <= v1.8.14).
1517
(node.attribs && node.attribs.src);
18+
19+
if (typeof window !== 'undefined' && clientVars.ep_image_upload.storageType === 'local') {
20+
if (/^http/.test(imageData)) {
21+
// an uploaded image is copied, place a copy in the desired line
22+
lineAttributes.img = imageData;
23+
return;
24+
}
25+
26+
const padeditor = require('ep_etherpad-lite/static/js/pad_editor').padeditor;
27+
28+
const match = imageData.match(/data:(?<mime>[^;]+);base64,(?<data>.*)/);
29+
if (!match || !match.groups.data || !match.groups.mime) return;
30+
31+
// decode from internal base64 rep
32+
const decodedData = Uint8Array.from(window.atob(match.groups.data), (c) => c.charCodeAt(0));
33+
34+
// check if size is within limits and mime type is supported
35+
const extension = _isValid({size: decodedData.length, type: match.groups.mime});
36+
if (!extension) return;
37+
38+
const blob = new Blob([decodedData], {type: match.groups.mime});
39+
40+
// image.* is a temporary name not used on the server
41+
uploadFile(padeditor, blob, `image.${extension}`);
42+
} else {
43+
lineAttributes.img = imageData;
44+
}
1645
};
1746

1847
exports.collectContentPre = (name, context) => {

0 commit comments

Comments
 (0)