(core) Add cut, copy, and paste to context menu

Summary:
On supported browsers, the new context menu commands work exactly as they do
via keyboard shortcuts. On unsupported browsers, an unavailable command
modal is shown with a suggestion to use keyboard shortcuts instead.

Test Plan: Browser tests.

Reviewers: jarek

Reviewed By: jarek

Differential Revision: https://phab.getgrist.com/D3867
This commit is contained in:
George Gevoian
2023-04-28 02:20:28 -07:00
parent c6ec8339f5
commit 18ad39cba3
17 changed files with 468 additions and 97 deletions

View File

@@ -3,9 +3,20 @@ import {get as getBrowserGlobals} from 'app/client/lib/browserGlobals';
const G = getBrowserGlobals('document', 'window');
/**
* Copy some text to the clipboard, by hook or by crook.
* Copy text or data to the clipboard.
*/
export async function copyToClipboard(txt: string) {
export async function copyToClipboard(data: string | ClipboardItem) {
if (typeof data === 'string') {
await copyTextToClipboard(data);
} else {
await copyDataToClipboard(data);
}
}
/**
* Copy text to the clipboard.
*/
async function copyTextToClipboard(txt: string) {
// If present and we have permission to use it, the navigator.clipboard interface
// is convenient. This method works in non-headless tests, and regular chrome
// and firefox.
@@ -36,3 +47,36 @@ export async function copyToClipboard(txt: string) {
G.document.getSelection().addRange(selection);
}
}
/**
* Copy data to the clipboard.
*/
async function copyDataToClipboard(data: ClipboardItem) {
if (!G.window.navigator?.clipboard?.write) {
throw new Error('navigator.clipboard.write is not supported on this browser');
}
await G.window.navigator.clipboard.write([data]);
}
/**
* Read text from the clipboard.
*/
export function readTextFromClipboard(): Promise<string> {
if (!G.window.navigator?.clipboard?.readText) {
throw new Error('navigator.clipboard.readText is not supported on this browser');
}
return G.window.navigator.clipboard.readText();
}
/**
* Read data from the clipboard.
*/
export function readDataFromClipboard(): Promise<ClipboardItem[]> {
if (!G.window.navigator?.clipboard?.read) {
throw new Error('navigator.clipboard.read is not supported on this browser');
}
return G.window.navigator.clipboard.read();
}