refactor(web): clarify preview link action
This commit is contained in:
1 parent
3bb0b9c484
commit
061ad09329
3 files changed
+17
-17
No files matched your search
@@ -1421,7 +1421,7 @@ function ChatMarkdown({
|
||||
href,
|
||||
position: { x: event.clientX, y: event.clientY },
|
||||
showContextMenu: (items, position) => api.contextMenu.show(items, position),
|
||||
openInBrowser: async (target) => {
|
||||
openInPreview: async (target) => {
|
||||
const result = await openExternalLinkInPreview(target);
|
||||
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
|
||||
reportMarkdownActionFailure(
|
||||
|
||||
@@ -2,16 +2,16 @@ import { describe, expect, it, vi } from "vite-plus/test";
|
||||
|
||||
import { isExternalWebLink, showExternalLinkContextMenu } from "./externalLinkContextMenu";
|
||||
|
||||
function createHarness(selection: "open-in-browser" | "open-external" | "copy-link" | null) {
|
||||
function createHarness(selection: "open-in-preview" | "open-external" | "copy-link" | null) {
|
||||
const showContextMenu = vi.fn().mockResolvedValue(selection);
|
||||
const openInBrowser = vi.fn().mockResolvedValue(undefined);
|
||||
const openInPreview = vi.fn().mockResolvedValue(undefined);
|
||||
const openExternal = vi.fn().mockResolvedValue(undefined);
|
||||
const copyLink = vi.fn().mockResolvedValue(undefined);
|
||||
const reportFailure = vi.fn();
|
||||
|
||||
return {
|
||||
showContextMenu,
|
||||
openInBrowser,
|
||||
openInPreview,
|
||||
openExternal,
|
||||
copyLink,
|
||||
reportFailure,
|
||||
@@ -30,13 +30,13 @@ describe("external chat link context menu", () => {
|
||||
|
||||
expect(harness.showContextMenu).toHaveBeenCalledWith(
|
||||
[
|
||||
{ id: "open-in-browser", label: "Open in integrated browser" },
|
||||
{ id: "open-in-preview", label: "Open in integrated browser" },
|
||||
{ id: "open-external", label: "Open in system browser" },
|
||||
{ id: "copy-link", label: "Copy Link" },
|
||||
],
|
||||
{ x: 12, y: 24 },
|
||||
);
|
||||
expect(harness.openInBrowser).not.toHaveBeenCalled();
|
||||
expect(harness.openInPreview).not.toHaveBeenCalled();
|
||||
expect(harness.openExternal).not.toHaveBeenCalled();
|
||||
expect(harness.copyLink).not.toHaveBeenCalled();
|
||||
});
|
||||
@@ -48,12 +48,12 @@ describe("external chat link context menu", () => {
|
||||
await showExternalLinkContextMenu({ href, position: { x: 1, y: 2 }, ...harness });
|
||||
|
||||
expect(harness.copyLink).toHaveBeenCalledWith(href);
|
||||
expect(harness.openInBrowser).not.toHaveBeenCalled();
|
||||
expect(harness.openInPreview).not.toHaveBeenCalled();
|
||||
expect(harness.openExternal).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it.each([
|
||||
["open-in-browser" as const, "openInBrowser" as const],
|
||||
["open-in-preview" as const, "openInPreview" as const],
|
||||
["open-external" as const, "openExternal" as const],
|
||||
])("preserves the %s action", async (selection, expectedCallback) => {
|
||||
const harness = createHarness(selection);
|
||||
@@ -91,13 +91,13 @@ describe("external chat link context menu", () => {
|
||||
});
|
||||
|
||||
expect(harness.reportFailure).toHaveBeenCalledWith("show-link-context-menu", cause);
|
||||
expect(harness.openInBrowser).not.toHaveBeenCalled();
|
||||
expect(harness.openInPreview).not.toHaveBeenCalled();
|
||||
expect(harness.openExternal).not.toHaveBeenCalled();
|
||||
expect(harness.copyLink).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it.each([
|
||||
["open-in-browser" as const, "openInBrowser" as const, "open-link-in-preview"],
|
||||
["open-in-preview" as const, "openInPreview" as const, "open-link-in-preview"],
|
||||
["open-external" as const, "openExternal" as const, "open-link-external"],
|
||||
])("reports a failed %s action", async (selection, callback, operation) => {
|
||||
const harness = createHarness(selection);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { ContextMenuItem } from "@t3tools/contracts";
|
||||
|
||||
export type ExternalLinkContextMenuAction = "open-in-browser" | "open-external" | "copy-link";
|
||||
export type ExternalLinkContextMenuAction = "open-in-preview" | "open-external" | "copy-link";
|
||||
|
||||
export type ExternalLinkContextMenuFailureOperation =
|
||||
| "show-link-context-menu"
|
||||
@@ -9,13 +9,13 @@ export type ExternalLinkContextMenuFailureOperation =
|
||||
| "copy-link";
|
||||
|
||||
const FAILURE_OPERATION_BY_ACTION = {
|
||||
"open-in-browser": "open-link-in-preview",
|
||||
"open-in-preview": "open-link-in-preview",
|
||||
"open-external": "open-link-external",
|
||||
"copy-link": "copy-link",
|
||||
} as const satisfies Record<ExternalLinkContextMenuAction, ExternalLinkContextMenuFailureOperation>;
|
||||
|
||||
const EXTERNAL_LINK_CONTEXT_MENU_ITEMS = [
|
||||
{ id: "open-in-browser", label: "Open in integrated browser" },
|
||||
{ id: "open-in-preview", label: "Open in integrated browser" },
|
||||
{ id: "open-external", label: "Open in system browser" },
|
||||
{ id: "copy-link", label: "Copy Link" },
|
||||
] as const satisfies readonly ContextMenuItem<ExternalLinkContextMenuAction>[];
|
||||
@@ -27,7 +27,7 @@ interface ShowExternalLinkContextMenuOptions {
|
||||
items: readonly ContextMenuItem<ExternalLinkContextMenuAction>[],
|
||||
position: { readonly x: number; readonly y: number },
|
||||
) => Promise<ExternalLinkContextMenuAction | null>;
|
||||
readonly openInBrowser: (href: string) => Promise<void>;
|
||||
readonly openInPreview: (href: string) => Promise<void>;
|
||||
readonly openExternal: (href: string) => Promise<void>;
|
||||
readonly copyLink: (href: string) => Promise<unknown>;
|
||||
readonly reportFailure: (
|
||||
@@ -49,7 +49,7 @@ export async function showExternalLinkContextMenu({
|
||||
href,
|
||||
position,
|
||||
showContextMenu,
|
||||
openInBrowser,
|
||||
openInPreview,
|
||||
openExternal,
|
||||
copyLink,
|
||||
reportFailure,
|
||||
@@ -63,8 +63,8 @@ export async function showExternalLinkContextMenu({
|
||||
}
|
||||
|
||||
try {
|
||||
if (action === "open-in-browser") {
|
||||
await openInBrowser(href);
|
||||
if (action === "open-in-preview") {
|
||||
await openInPreview(href);
|
||||
} else if (action === "open-external") {
|
||||
await openExternal(href);
|
||||
} else if (action === "copy-link") {
|
||||
|
||||
Reference in new issue
Block a user