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

* fix: Copy to clipboard all text nodes as text

* fix: support copying text even if there are selected elements that are no text

* patch: makes paragraphs betwen texts of each element

* patch: allow copying text for bound text
This commit is contained in:
Faustino Kialungila 2022-04-05 21:48:59 +02:00 committed by GitHub
parent 670ceafc84
commit 89471094ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 14 deletions

View File

@ -4,9 +4,8 @@ import { copyTextToSystemClipboard, copyToClipboard } 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";
import { getNonDeletedElements } from "../element"; import { getNonDeletedElements, isTextElement } from "../element";
import { t } from "../i18n"; import { t } from "../i18n";
import { ExcalidrawTextElement } from "../element/types";
export const actionCopy = register({ export const actionCopy = register({
name: "copy", name: "copy",
@ -131,10 +130,19 @@ export const actionCopyAsPng = register({
export const copyAllTextNodesAsText = register({ export const copyAllTextNodesAsText = register({
name: "copyAllTextNodesAsText", name: "copyAllTextNodesAsText",
trackEvent: { category: "element" }, trackEvent: { category: "element" },
perform: (elements) => { perform: (elements, appState) => {
const text = ( const selectedElements = getSelectedElements(
getNonDeletedElements(elements) as ExcalidrawTextElement[] getNonDeletedElements(elements),
).reduce((acc, element) => `${acc}${element.text}\n`, ""); appState,
true,
);
const text = selectedElements.reduce((acc, element) => {
if (isTextElement(element)) {
return `${acc}${element.text}\n\n`;
}
return acc;
}, "");
copyTextToSystemClipboard(text); copyTextToSystemClipboard(text);
return { return {
commitToHistory: false, commitToHistory: false,

View File

@ -5476,7 +5476,10 @@ class App extends React.Component<AppProps, AppState> {
const elements = this.scene.getElements(); const elements = this.scene.getElements();
const isTextNodesOnly = elements.every((element) => isTextElement(element)); const selectedElements = getSelectedElements(
this.scene.getElements(),
this.state,
);
const options: ContextMenuOption[] = []; const options: ContextMenuOption[] = [];
if (probablySupportsClipboardBlob && elements.length > 0) { if (probablySupportsClipboardBlob && elements.length > 0) {
@ -5487,11 +5490,7 @@ class App extends React.Component<AppProps, AppState> {
options.push(actionCopyAsSvg); options.push(actionCopyAsSvg);
} }
if ( if (probablySupportsClipboardWriteText && selectedElements.length > 0) {
probablySupportsClipboardWriteText &&
elements.length > 0 &&
isTextNodesOnly
) {
options.push(copyAllTextNodesAsText); options.push(copyAllTextNodesAsText);
} }
if (type === "canvas") { if (type === "canvas") {
@ -5538,8 +5537,7 @@ class App extends React.Component<AppProps, AppState> {
elements.length > 0 && elements.length > 0 &&
actionCopyAsSvg, actionCopyAsSvg,
probablySupportsClipboardWriteText && probablySupportsClipboardWriteText &&
elements.length > 0 && selectedElements.length > 0 &&
isTextNodesOnly &&
copyAllTextNodesAsText, copyAllTextNodesAsText,
((probablySupportsClipboardBlob && elements.length > 0) || ((probablySupportsClipboardBlob && elements.length > 0) ||
(probablySupportsClipboardWriteText && elements.length > 0)) && (probablySupportsClipboardWriteText && elements.length > 0)) &&