Skip to content

Commit

Permalink
Fixes in system clipboard paste.
Browse files Browse the repository at this point in the history
  • Loading branch information
dariuszdawidowski committed Aug 16, 2024
1 parent 010d575 commit 9734501
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
28 changes: 22 additions & 6 deletions app/metaviz-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,8 @@ class Metaviz {
// Browser features
features: {
nativeFileSystemApi: false,
clipboardApi: false
clipboardApi: false,
clipboardAllowed: false
},

// Passed browser features compability
Expand Down Expand Up @@ -753,13 +754,28 @@ class Metaviz {
* Optional features support
*/

// https://caniuse.com/#feat=native-filesystem-api
// Filesystem API: https://caniuse.com/#feat=native-filesystem-api
this.system.features.nativeFileSystemApi = ('showOpenFilePicker' in window);

// https://caniuse.com/async-clipboard (full read/write support) Note: local file supports api but always ask about permission which is unacceptable
this.system.features.clipboardApi = (this.agent.protocol != 'file' && this.agent.protocol != 'http') ? ('readText' in navigator.clipboard) && ('read' in navigator.clipboard) : false;
if (!this.system.features.clipboardApi) console.warn('Feature clipboardApi require a https protocol.')

// Clipboard API: https://caniuse.com/async-clipboard (full read/write support) Note: local file supports api but always ask about permission which is unacceptable
this.system.features.clipboardApi = (this.agent.protocol != 'file') ? ('readText' in navigator.clipboard) && ('read' in navigator.clipboard) : false;
if (this.system.features.clipboardApi) {
let remember = '';
navigator.clipboard.readText().then((text) => {
remember = text;
const compare = crypto.randomUUID();
navigator.clipboard.writeText(compare).then(() => {
navigator.clipboard.readText().then((text) => {
if (text === compare) this.system.features.clipboardAllowed = true;
});
navigator.clipboard.writeText(remember);
});
});
}
else if (this.agent.protocol == 'file') {
console.warn('Feature clipboardApi require a http(s) protocol.');
}

/**
* Mandatory features support
*/
Expand Down
20 changes: 6 additions & 14 deletions editor/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -855,19 +855,11 @@ class MetavizContextMenu extends TotalProMenu {
super.show({left: args.x - container.x, top: args.y - container.y});

// Permissions: Clipboard
if (metaviz.system.features.clipboardApi) {
const compare = crypto.randomUUID();
navigator.clipboard.writeText(compare).then(() => {
navigator.clipboard.readText().then((text) => {
if (text === compare) this.panel.left.find('menu-settings-allow-clipboard')?.set(true);
navigator.clipboard.writeText('');
}).catch((err) => {
this.panel.left.find('menu-settings-allow-clipboard')?.set(false);
navigator.clipboard.writeText('');
});
}).catch((err) => {
this.panel.left.find('menu-settings-allow-clipboard')?.set(false);
});
if (metaviz.system.features.clipboardApi && metaviz.system.features.clipboardAllowed) {
this.panel.left.find('menu-settings-allow-clipboard')?.set(true);
}
else {
this.panel.left.find('menu-settings-allow-clipboard')?.set(false);
}

}
Expand Down Expand Up @@ -935,7 +927,7 @@ class MetavizContextMenu extends TotalProMenu {
*/

async checkClipboardToPaste() {
if (metaviz.system.features.clipboardApi) {
if (metaviz.system.features.clipboardApi && metaviz.system.features.clipboardAllowed) {
try {
const items = await navigator.clipboard.read();
const text = await navigator.clipboard.readText();
Expand Down

0 comments on commit 9734501

Please sign in to comment.