瀏覽代碼

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
Faustino Kialungila 3 年之前
父節點
當前提交
89471094ce
共有 2 個文件被更改,包括 20 次插入14 次删除
  1. 14 6
      src/actions/actionClipboard.tsx
  2. 6 8
      src/components/App.tsx

+ 14 - 6
src/actions/actionClipboard.tsx

@@ -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",
@@ -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,
+    );
+
+    const text = selectedElements.reduce((acc, element) => {
+      if (isTextElement(element)) {
+        return `${acc}${element.text}\n\n`;
+      }
+      return acc;
+    }, "");
     copyTextToSystemClipboard(text);
     return {
       commitToHistory: false,

+ 6 - 8
src/components/App.tsx

@@ -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) {
@@ -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") {
@@ -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)) &&