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

fix: Copy to clipboard all text nodes as text #5014

Merged
merged 4 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
20 changes: 14 additions & 6 deletions src/actions/actionClipboard.tsx
Expand Up @@ -4,9 +4,8 @@ import { copyTextToSystemClipboard, copyToClipboard } from "../clipboard";
import { actionDeleteSelected } from "./actionDeleteSelected";
import { getSelectedElements } from "../scene/selection";
import { exportCanvas } from "../data/index";
import { getNonDeletedElements } from "../element";
import { getNonDeletedElements, isTextElement } from "../element";
import { t } from "../i18n";
import { ExcalidrawTextElement } from "../element/types";

export const actionCopy = register({
name: "copy",
Expand Down Expand Up @@ -131,10 +130,19 @@ export const actionCopyAsPng = register({
export const copyAllTextNodesAsText = register({
name: "copyAllTextNodesAsText",
trackEvent: { category: "element" },
perform: (elements) => {
const text = (
getNonDeletedElements(elements) as ExcalidrawTextElement[]
).reduce((acc, element) => `${acc}${element.text}\n`, "");
perform: (elements, appState) => {
const selectedElements = getSelectedElements(
getNonDeletedElements(elements),
appState,
true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be false since we are not including bound text elements

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but we should include bound text elements?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I thought!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Earlier since we decided to show only for text elements that's where I thought it could cause confusion for bound text, but now we are showing for mixed elements so let's include bound text too

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

);

const text = selectedElements.reduce((acc, element) => {
if (isTextElement(element)) {
return `${acc}${element.text}\n\n`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be single newline ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, we actually want to have two newlines so we can distinguish the nodes!

}
return acc;
}, "");
copyTextToSystemClipboard(text);
return {
commitToHistory: false,
Expand Down
14 changes: 6 additions & 8 deletions src/components/App.tsx
Expand Up @@ -5476,7 +5476,10 @@ class App extends React.Component<AppProps, AppState> {

const elements = this.scene.getElements();

const isTextNodesOnly = elements.every((element) => isTextElement(element));
const selectedElements = getSelectedElements(
this.scene.getElements(),
this.state,
);

const options: ContextMenuOption[] = [];
if (probablySupportsClipboardBlob && elements.length > 0) {
Expand All @@ -5487,11 +5490,7 @@ class App extends React.Component<AppProps, AppState> {
options.push(actionCopyAsSvg);
}

if (
probablySupportsClipboardWriteText &&
elements.length > 0 &&
isTextNodesOnly
) {
if (probablySupportsClipboardWriteText && selectedElements.length > 0) {
options.push(copyAllTextNodesAsText);
}
if (type === "canvas") {
Expand Down Expand Up @@ -5538,8 +5537,7 @@ class App extends React.Component<AppProps, AppState> {
elements.length > 0 &&
actionCopyAsSvg,
probablySupportsClipboardWriteText &&
elements.length > 0 &&
isTextNodesOnly &&
selectedElements.length > 0 &&
copyAllTextNodesAsText,
((probablySupportsClipboardBlob && elements.length > 0) ||
(probablySupportsClipboardWriteText && elements.length > 0)) &&
Expand Down