fix: more copyText fixes (#5016)

This commit is contained in:
David Luzar 2022-04-05 23:11:00 +02:00 committed by GitHub
parent 89471094ce
commit 77d789ed8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 33 additions and 17 deletions

View File

@ -1,6 +1,10 @@
import { CODES, KEYS } from "../keys"; import { CODES, KEYS } from "../keys";
import { register } from "./register"; import { register } from "./register";
import { copyTextToSystemClipboard, copyToClipboard } from "../clipboard"; import {
copyTextToSystemClipboard,
copyToClipboard,
probablySupportsClipboardWriteText,
} from "../clipboard";
import { actionDeleteSelected } from "./actionDeleteSelected"; import { actionDeleteSelected } from "./actionDeleteSelected";
import { getSelectedElements } from "../scene/selection"; import { getSelectedElements } from "../scene/selection";
import { exportCanvas } from "../data/index"; import { exportCanvas } from "../data/index";
@ -127,8 +131,8 @@ export const actionCopyAsPng = register({
keyTest: (event) => event.code === CODES.C && event.altKey && event.shiftKey, keyTest: (event) => event.code === CODES.C && event.altKey && event.shiftKey,
}); });
export const copyAllTextNodesAsText = register({ export const copyText = register({
name: "copyAllTextNodesAsText", name: "copyText",
trackEvent: { category: "element" }, trackEvent: { category: "element" },
perform: (elements, appState) => { perform: (elements, appState) => {
const selectedElements = getSelectedElements( const selectedElements = getSelectedElements(
@ -137,16 +141,24 @@ export const copyAllTextNodesAsText = register({
true, true,
); );
const text = selectedElements.reduce((acc, element) => { const text = selectedElements
if (isTextElement(element)) { .reduce((acc: string[], element) => {
return `${acc}${element.text}\n\n`; if (isTextElement(element)) {
} acc.push(element.text);
return acc; }
}, ""); return acc;
}, [])
.join("\n\n");
copyTextToSystemClipboard(text); copyTextToSystemClipboard(text);
return { return {
commitToHistory: false, commitToHistory: false,
}; };
}, },
contextItemLabel: "labels.copyAllTextNodesAsText", contextItemPredicate: (elements, appState) => {
return (
probablySupportsClipboardWriteText &&
getSelectedElements(elements, appState, true).some(isTextElement)
);
},
contextItemLabel: "labels.copyText",
}); });

View File

@ -75,7 +75,7 @@ export {
actionCut, actionCut,
actionCopyAsPng, actionCopyAsPng,
actionCopyAsSvg, actionCopyAsSvg,
copyAllTextNodesAsText, copyText,
} from "./actionClipboard"; } from "./actionClipboard";
export { actionToggleGridMode } from "./actionToggleGridMode"; export { actionToggleGridMode } from "./actionToggleGridMode";

View File

@ -41,7 +41,7 @@ export type ActionName =
| "paste" | "paste"
| "copyAsPng" | "copyAsPng"
| "copyAsSvg" | "copyAsSvg"
| "copyAllTextNodesAsText" | "copyText"
| "sendBackward" | "sendBackward"
| "bringForward" | "bringForward"
| "sendToBack" | "sendToBack"

View File

@ -11,7 +11,7 @@ import {
actionCopy, actionCopy,
actionCopyAsPng, actionCopyAsPng,
actionCopyAsSvg, actionCopyAsSvg,
copyAllTextNodesAsText, copyText,
actionCopyStyles, actionCopyStyles,
actionCut, actionCut,
actionDeleteSelected, actionDeleteSelected,
@ -5490,8 +5490,12 @@ class App extends React.Component<AppProps, AppState> {
options.push(actionCopyAsSvg); options.push(actionCopyAsSvg);
} }
if (probablySupportsClipboardWriteText && selectedElements.length > 0) { if (
options.push(copyAllTextNodesAsText); type === "element" &&
copyText.contextItemPredicate(elements, this.state) &&
probablySupportsClipboardWriteText
) {
options.push(copyText);
} }
if (type === "canvas") { if (type === "canvas") {
const viewModeOptions = [ const viewModeOptions = [
@ -5538,7 +5542,7 @@ class App extends React.Component<AppProps, AppState> {
actionCopyAsSvg, actionCopyAsSvg,
probablySupportsClipboardWriteText && probablySupportsClipboardWriteText &&
selectedElements.length > 0 && selectedElements.length > 0 &&
copyAllTextNodesAsText, copyText,
((probablySupportsClipboardBlob && elements.length > 0) || ((probablySupportsClipboardBlob && elements.length > 0) ||
(probablySupportsClipboardWriteText && elements.length > 0)) && (probablySupportsClipboardWriteText && elements.length > 0)) &&
separator, separator,

View File

@ -9,7 +9,7 @@
"copy": "Copy", "copy": "Copy",
"copyAsPng": "Copy to clipboard as PNG", "copyAsPng": "Copy to clipboard as PNG",
"copyAsSvg": "Copy to clipboard as SVG", "copyAsSvg": "Copy to clipboard as SVG",
"copyAllTextNodesAsText": "Copy to clipboard as a single text element", "copyText": "Copy to clipboard as text",
"bringForward": "Bring forward", "bringForward": "Bring forward",
"sendToBack": "Send to back", "sendToBack": "Send to back",
"bringToFront": "Bring to front", "bringToFront": "Bring to front",