Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Copy to clipboard all text nodes as text #5013

Merged
merged 2 commits into from Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/actions/actionClipboard.tsx
@@ -1,11 +1,12 @@
import { CODES, KEYS } from "../keys";
import { register } from "./register";
import { copyToClipboard } from "../clipboard";
import { copyTextToSystemClipboard, copyToClipboard } from "../clipboard";
import { actionDeleteSelected } from "./actionDeleteSelected";
import { getSelectedElements } from "../scene/selection";
import { exportCanvas } from "../data/index";
import { getNonDeletedElements } from "../element";
import { t } from "../i18n";
import { ExcalidrawTextElement } from "../element/types";

export const actionCopy = register({
name: "copy",
Expand Down Expand Up @@ -126,3 +127,18 @@ export const actionCopyAsPng = register({
contextItemLabel: "labels.copyAsPng",
keyTest: (event) => event.code === CODES.C && event.altKey && event.shiftKey,
});

export const copyAllTextNodesAsText = register({
name: "copyAllTextNodesAsText",
trackEvent: { category: "element" },
perform: (elements) => {
const text = (
getNonDeletedElements(elements) as ExcalidrawTextElement[]
).reduce((acc, element) => `${acc}${element.text}\n`, "");
copyTextToSystemClipboard(text);
return {
commitToHistory: false,
};
},
contextItemLabel: "labels.copyAllTextNodesAsText",
});
1 change: 1 addition & 0 deletions src/actions/index.ts
Expand Up @@ -75,6 +75,7 @@ export {
actionCut,
actionCopyAsPng,
actionCopyAsSvg,
copyAllTextNodesAsText,
} from "./actionClipboard";

export { actionToggleGridMode } from "./actionToggleGridMode";
Expand Down
1 change: 1 addition & 0 deletions src/actions/types.ts
Expand Up @@ -41,6 +41,7 @@ export type ActionName =
| "paste"
| "copyAsPng"
| "copyAsSvg"
| "copyAllTextNodesAsText"
| "sendBackward"
| "bringForward"
| "sendToBack"
Expand Down
15 changes: 15 additions & 0 deletions src/components/App.tsx
Expand Up @@ -11,6 +11,7 @@ import {
actionCopy,
actionCopyAsPng,
actionCopyAsSvg,
copyAllTextNodesAsText,
actionCopyStyles,
actionCut,
actionDeleteSelected,
Expand Down Expand Up @@ -5475,6 +5476,8 @@ class App extends React.Component<AppProps, AppState> {

const elements = this.scene.getElements();

const isTextNodesOnly = elements.every((element) => isTextElement(element));

const options: ContextMenuOption[] = [];
if (probablySupportsClipboardBlob && elements.length > 0) {
options.push(actionCopyAsPng);
Expand All @@ -5483,6 +5486,14 @@ class App extends React.Component<AppProps, AppState> {
if (probablySupportsClipboardWriteText && elements.length > 0) {
options.push(actionCopyAsSvg);
}

if (
probablySupportsClipboardWriteText &&
elements.length > 0 &&
isTextNodesOnly
) {
options.push(copyAllTextNodesAsText);
}
if (type === "canvas") {
const viewModeOptions = [
...options,
Expand Down Expand Up @@ -5526,6 +5537,10 @@ class App extends React.Component<AppProps, AppState> {
probablySupportsClipboardWriteText &&
elements.length > 0 &&
actionCopyAsSvg,
probablySupportsClipboardWriteText &&
elements.length > 0 &&
isTextNodesOnly &&
copyAllTextNodesAsText,
((probablySupportsClipboardBlob && elements.length > 0) ||
(probablySupportsClipboardWriteText && elements.length > 0)) &&
separator,
Expand Down
1 change: 1 addition & 0 deletions src/locales/en.json
Expand Up @@ -9,6 +9,7 @@
"copy": "Copy",
"copyAsPng": "Copy to clipboard as PNG",
"copyAsSvg": "Copy to clipboard as SVG",
"copyAllTextNodesAsText": "Copy to clipboard as a single text element",
"bringForward": "Bring forward",
"sendToBack": "Send to back",
"bringToFront": "Bring to front",
Expand Down