Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/frontend/copy-paste-buffer.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45/*6An internal copy/paste buffer78MOTIVATION: There is no way to sync with the official operating system copy/paste buffer,9due to security restrictions. However, for some platforms (iPad, I'm looking at you!),10it's still very useful to have our own internal copy/paste buffer. This is it.11It stores a string right now. Who knows, maybe someboday it'll do interesting12richer content too.13*/1415let buffer = "";1617// TODO: get_buffer could be done via a permission request, though that is a potential security issue.18// See https://alligator.io/js/async-clipboard-api/19export function get_buffer(): string {20return buffer;21}2223export function set_buffer(s: string | undefined): void {24buffer = s ?? "";2526// In addition to saving the test to our own internal buffer (that get_buffer produces),27// we also attempt to write that buffer to the actual clipboard. In some cases this might28// work and produces a better user experience.29if (navigator.clipboard != null) {30// this is async -- requires at least chrome 66.31navigator.clipboard.writeText(buffer);32return;33}34// failing that, try to copy what is selected to the internal buffer...35try {36// https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand37// This ignores the input s above, since it operates on whatever is selected.38// NOTE: there might be no context in CoCalc where this will actually work, since39// it is supposed to be used in an event handler (like in the x11 xpra code?).40document.execCommand("copy");41} catch (_) {}42}434445