now browsers with literally no clipboard support are ok

Signed-off-by: Victor Seiji Hariki <victorseijih@gmail.com>
This commit is contained in:
Victor Seiji Hariki 2022-12-03 11:40:40 -03:00
parent 464ab31431
commit eea14e1cc9

View file

@ -54,7 +54,9 @@ const selectTransformTool = () =>
state.snapToGrid = true; state.snapToGrid = true;
state.keepAspectRatio = true; state.keepAspectRatio = true;
state.useClipboard = !!navigator.clipboard.write; // Use it by default if supported state.useClipboard = !!(
navigator.clipboard && navigator.clipboard.write
); // Use it by default if supported
state.original = null; state.original = null;
state.dragging = null; state.dragging = null;
@ -489,10 +491,11 @@ const selectTransformTool = () =>
// Send to clipboard // Send to clipboard
state.clipboard.copy.toBlob((blob) => { state.clipboard.copy.toBlob((blob) => {
const item = new ClipboardItem({"image/png": blob}); const item = new ClipboardItem({"image/png": blob});
navigator.clipboard.write([item]).catch((e) => { navigator.clipboard &&
console.warn("Error sending to clipboard"); navigator.clipboard.write([item]).catch((e) => {
console.warn(e); console.warn("Error sending to clipboard");
}); console.warn(e);
});
}); });
} }
}; };
@ -501,24 +504,25 @@ const selectTransformTool = () =>
state.ctrlvcb = (evn) => { state.ctrlvcb = (evn) => {
if (state.useClipboard) { if (state.useClipboard) {
// If we use the clipboard, do some proccessing of clipboard data (ugly but kind of minimum required) // If we use the clipboard, do some proccessing of clipboard data (ugly but kind of minimum required)
navigator.clipboard.read().then((items) => { navigator.clipboard &&
for (const item of items) { navigator.clipboard.read().then((items) => {
for (const type of item.types) { for (const item of items) {
if (type.startsWith("image/")) { for (const type of item.types) {
item.getType(type).then((blob) => { if (type.startsWith("image/")) {
// Converts blob to image item.getType(type).then((blob) => {
const url = window.URL || window.webkitURL; // Converts blob to image
const image = document.createElement("img"); const url = window.URL || window.webkitURL;
image.src = url.createObjectURL(file); const image = document.createElement("img");
tools.stamp.enable({ image.src = url.createObjectURL(file);
image, tools.stamp.enable({
back: tools.selecttransform.enable, image,
back: tools.selecttransform.enable,
});
}); });
}); }
} }
} }
} });
});
} else if (state.clipboard.copy) { } else if (state.clipboard.copy) {
// Use internal clipboard // Use internal clipboard
const image = document.createElement("img"); const image = document.createElement("img");
@ -562,7 +566,7 @@ const selectTransformTool = () =>
"Use clipboard" "Use clipboard"
); );
state.ctxmenu.useClipboardLabel = clipboardCheckbox.label; state.ctxmenu.useClipboardLabel = clipboardCheckbox.label;
if (!navigator.clipboard.write) if (!(navigator.clipboard && navigator.clipboard.write))
clipboardCheckbox.checkbox.disabled = true; // Disable if not available clipboardCheckbox.checkbox.disabled = true; // Disable if not available
// Some useful actions to do with selection // Some useful actions to do with selection