2020-04-14 23:16:11 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2020-04-14 23:16:11 +00:00
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 13:57:16 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2020-04-14 23:16:11 +00:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Copy plaintext to user's clipboard
|
|
|
|
* It will overwrite user's selection range
|
|
|
|
* In certain browsers it may only work if triggered by a user action or may ask user for permissions
|
|
|
|
* Tries to use new async clipboard API if available
|
|
|
|
* @param text the plaintext to put in the user's clipboard
|
|
|
|
*/
|
2021-10-15 14:30:53 +00:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
|
|
|
|
2020-04-14 23:16:11 +00:00
|
|
|
export async function copyPlaintext(text: string): Promise<boolean> {
|
|
|
|
try {
|
2022-05-03 21:04:37 +00:00
|
|
|
if (navigator?.clipboard?.writeText) {
|
2020-04-14 23:16:11 +00:00
|
|
|
await navigator.clipboard.writeText(text);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
const textArea = document.createElement("textarea");
|
|
|
|
textArea.value = text;
|
|
|
|
|
|
|
|
// Avoid scrolling to bottom
|
|
|
|
textArea.style.top = "0";
|
|
|
|
textArea.style.left = "0";
|
|
|
|
textArea.style.position = "fixed";
|
|
|
|
|
|
|
|
document.body.appendChild(textArea);
|
2023-02-24 15:28:40 +00:00
|
|
|
const selection = document.getSelection()!;
|
2020-04-14 23:16:11 +00:00
|
|
|
const range = document.createRange();
|
|
|
|
// range.selectNodeContents(textArea);
|
|
|
|
range.selectNode(textArea);
|
|
|
|
selection.removeAllRanges();
|
|
|
|
selection.addRange(range);
|
|
|
|
|
|
|
|
const successful = document.execCommand("copy");
|
|
|
|
selection.removeAllRanges();
|
|
|
|
document.body.removeChild(textArea);
|
|
|
|
return successful;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2021-10-15 14:30:53 +00:00
|
|
|
logger.error("copyPlaintext failed", e);
|
2020-04-14 23:16:11 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
export function selectText(target: Element): void {
|
2020-04-14 23:16:11 +00:00
|
|
|
const range = document.createRange();
|
|
|
|
range.selectNodeContents(target);
|
|
|
|
|
2023-02-24 15:28:40 +00:00
|
|
|
const selection = window.getSelection()!;
|
2020-04-14 23:16:11 +00:00
|
|
|
selection.removeAllRanges();
|
|
|
|
selection.addRange(range);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy rich text to user's clipboard
|
|
|
|
* It will overwrite user's selection range
|
|
|
|
* In certain browsers it may only work if triggered by a user action or may ask user for permissions
|
|
|
|
* @param ref pointer to the node to copy
|
|
|
|
*/
|
2023-02-28 10:51:27 +00:00
|
|
|
export function copyNode(ref?: Element | null): boolean {
|
|
|
|
if (!ref) return false;
|
2020-04-14 23:16:11 +00:00
|
|
|
selectText(ref);
|
|
|
|
return document.execCommand("copy");
|
|
|
|
}
|
2021-06-02 09:42:17 +00:00
|
|
|
|
2022-04-15 18:12:40 +00:00
|
|
|
/**
|
|
|
|
* Returns text which has been selected by the user
|
|
|
|
* @returns the selected text
|
|
|
|
*/
|
|
|
|
export function getSelectedText(): string {
|
2023-02-24 15:28:40 +00:00
|
|
|
return window.getSelection()!.toString();
|
2022-04-15 18:12:40 +00:00
|
|
|
}
|
2023-05-25 08:32:20 +00:00
|
|
|
|
2024-06-26 09:34:07 +00:00
|
|
|
export const graphemeSegmenter = new Intl.Segmenter();
|
|
|
|
|
2023-05-25 08:32:20 +00:00
|
|
|
/**
|
|
|
|
* Returns the first grapheme in the given string,
|
|
|
|
* especially useful for strings containing emoji, will not break compound emoji up.
|
|
|
|
* @param str string to parse
|
|
|
|
* @returns the first grapheme or an empty string if given an empty string
|
|
|
|
*/
|
|
|
|
export function getFirstGrapheme(str: string): string {
|
2024-06-26 09:34:07 +00:00
|
|
|
const result = graphemeSegmenter.segment(str)[Symbol.iterator]().next();
|
|
|
|
return result.done ? "" : result.value.segment;
|
2023-05-25 08:32:20 +00:00
|
|
|
}
|