Skip to content

Commit

Permalink
respect storageType on copy&paste
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
webzwo0i committed Nov 29, 2021
1 parent 38a8a71 commit af2b141
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion static/js/contentCollection.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,47 @@
'use strict';

const {_isValid, uploadFile} = require('ep_image_upload/static/js/toolbar');

// When an image is detected give it a lineAttribute
// of Image with the URL to the iamge
exports.collectContentImage = (hookName, {node, state: {lineAttributes}, tname}) => {
if (tname === 'div' || tname === 'p') delete lineAttributes.img;
if (tname !== 'img') return;
lineAttributes.img =
const imageData =
// Client-side. This will also be used for server-side HTML imports once jsdom adds support
// for HTMLImageElement.currentSrc.
node.currentSrc ||
// Server-side HTML imports using jsdom v16.6.0 (Etherpad v1.8.15).
node.src ||
// Server-side HTML imports using cheerio (Etherpad <= v1.8.14).
(node.attribs && node.attribs.src);

if (typeof window !== 'undefined' && clientVars.ep_image_upload.storageType === 'local') {
if (/^http/.test(imageData)) {
// an uploaded image is copied, place a copy in the desired line
lineAttributes.img = imageData;
return;
}

const padeditor = require('ep_etherpad-lite/static/js/pad_editor').padeditor;

const match = imageData.match(/data:([^;]+);base64,(.*)/);
if (!match || !match[1] || !match[2]) return;

// decode from internal base64 rep
const decodedData = Uint8Array.from(window.atob(match[2]), (c) => c.charCodeAt(0));

// check if size is within limits and mime type is supported
const extension = _isValid({size: decodedData.length, type: match[1]});
if (!extension) return;

const blob = new Blob([decodedData], {type: match[1]});

// image.* is a temporary name not used on the server
uploadFile(padeditor, blob, `image.${extension}`);
} else {
lineAttributes.img = imageData;
}
};

exports.collectContentPre = (name, context) => {
Expand Down

0 comments on commit af2b141

Please sign in to comment.