feat(connection): add copy connection name
This commit is contained in:
1 parent
7ee335e38a
commit
f2ce70dd68
4 files changed
+49
-9
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { selectedConnectionDeleteTargets, selectedConnectionDuplicateTargets } from "@/lib/sidebar/sidebarConnectionSelection";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { copySelectedConnectionsToClipboards, selectedConnectionDeleteTargets, selectedConnectionDuplicateTargets } from "@/lib/sidebar/sidebarConnectionSelection";
|
||||
import type { TreeNode } from "@/types/database";
|
||||
|
||||
function node(id: string, type: TreeNode["type"] = "connection"): TreeNode {
|
||||
@@ -32,4 +32,22 @@ describe("sidebar connection selection", () => {
|
||||
|
||||
expect(selectedConnectionDuplicateTargets(current, selected)).toEqual(selectedConnectionDeleteTargets(current, selected));
|
||||
});
|
||||
|
||||
it("copies selected connection names to the system clipboard for the sidebar copy shortcut", () => {
|
||||
const copyConnectionsToTreeClipboard = vi.fn(() => 2);
|
||||
const copyToSystemClipboard = vi.fn(() => Promise.resolve());
|
||||
|
||||
expect(copySelectedConnectionsToClipboards([node("conn-1"), node("conn-2")], copyConnectionsToTreeClipboard, copyToSystemClipboard)).toBe(2);
|
||||
expect(copyConnectionsToTreeClipboard).toHaveBeenCalledWith(["conn-1", "conn-2"]);
|
||||
expect(copyToSystemClipboard).toHaveBeenCalledWith("conn-1\nconn-2");
|
||||
});
|
||||
|
||||
it("keeps the tree clipboard copy when system clipboard access is denied", async () => {
|
||||
const copyConnectionsToTreeClipboard = vi.fn(() => 1);
|
||||
const copyToSystemClipboard = vi.fn(() => Promise.reject(new Error("denied")));
|
||||
|
||||
expect(copySelectedConnectionsToClipboards([node("conn-1")], copyConnectionsToTreeClipboard, copyToSystemClipboard)).toBe(1);
|
||||
await Promise.resolve();
|
||||
expect(copyConnectionsToTreeClipboard).toHaveBeenCalledWith(["conn-1"]);
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,5 @@
|
||||
import type { TreeNode } from "@/types/database";
|
||||
import { copyNameForTreeNode } from "@/lib/sidebar/treeNodeClick";
|
||||
|
||||
type ConnectionTreeNode = TreeNode & { connectionId: string };
|
||||
|
||||
@@ -39,6 +40,18 @@ export function selectedConnectionClipboardNodes(selectedNodes: TreeNode[]): Con
|
||||
return selectedNodes;
|
||||
}
|
||||
|
||||
export function copySelectedConnectionsToClipboards(selectedNodes: TreeNode[], copyConnectionsToTreeClipboard: (connectionIds: string[]) => number, copyToSystemClipboard: (text: string) => Promise<void>): number {
|
||||
const connectionNodes = selectedConnectionClipboardNodes(selectedNodes);
|
||||
if (connectionNodes.length === 0) return 0;
|
||||
|
||||
const copiedCount = copyConnectionsToTreeClipboard(connectionNodes.map((node) => node.connectionId));
|
||||
if (copiedCount > 0) {
|
||||
// Connection duplication uses the tree clipboard, so keep it available even if OS clipboard access is denied.
|
||||
void copyToSystemClipboard(connectionNodes.map(copyNameForTreeNode).join("\n")).catch(() => {});
|
||||
}
|
||||
return copiedCount;
|
||||
}
|
||||
|
||||
export function connectionPasteTargetGroupId(node: TreeNode | null | undefined, groupIdForConnection: (connectionId: string) => string | null): string | null {
|
||||
if (!node) return null;
|
||||
if (node.type === "connection-group") return node.id;
|
||||
|
||||
Reference in new issue
Block a user