inline-image-mention.fixture.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import React, { useMemo, useState } from "react";
  2. import { createRoot } from "react-dom/client";
  3. import { ConversationRowView } from "@/v4/ConversationRowView";
  4. import { ZCodeIntlProvider } from "@/i18n/IntlProvider";
  5. import { ServiceProvider } from "@/hooks/useServices";
  6. import { PlatformProvider } from "@/hooks/usePlatform";
  7. import { TooltipProvider } from "@/components/ui/tooltip";
  8. import { buildFileMentionMarkdown } from "@/mentions/mentionMarkdown";
  9. import "@/styles.css";
  10. const params = new URLSearchParams(location.search);
  11. const evidence: any[] = [];
  12. const releases = new Map<string, () => void>();
  13. (window as any).inlineEvidence = evidence;
  14. (window as any).resolvedOwners = [];
  15. (window as any).releaseRead = (owner: string) => releases.get(owner)?.();
  16. const mode = params.get("case");
  17. const label = mode === "ordinary" ? "README.md" : "小桥流水人家.jpg";
  18. const destination =
  19. mode === "ordinary" || mode === "disguised"
  20. ? "./README.md"
  21. : mode === "windows"
  22. ? "C:\\work\\images\\小桥流水人家.jpg"
  23. : "./images/小桥流水人家.jpg";
  24. const actualDestination =
  25. {
  26. absolute: "/data/images/小桥流水人家.jpg",
  27. "file-url": "file:///data/images/%E5%B0%8F%E6%A1%A5.jpg",
  28. traversal: "../outside.jpg",
  29. "nested-escape": "./x/../../outside.jpg",
  30. http: "https://example.test/image.jpg",
  31. }[mode ?? ""] ?? destination;
  32. const row = {
  33. kind: "userInput",
  34. rowId: 1,
  35. entityId: "inline-image-test",
  36. turnId: "turn-1",
  37. revision: 1,
  38. text: buildFileMentionMarkdown(actualDestination, label) + " 不错不错,再来一张",
  39. origin: "realUser",
  40. } as any;
  41. const platform = {
  42. canOpenLocalFiles: !params.has("mobile"),
  43. saveFile: async () => {
  44. evidence.push({ kind: "saveFile" });
  45. if (params.has("failSave")) return { success: false, error: "fixture save failed" };
  46. return params.has("cancelSave")
  47. ? { success: false, canceled: true }
  48. : { success: true, path: "/tmp/inline-image.svg" };
  49. },
  50. openInEditor: async (_id: string, path: string) => {
  51. evidence.push({ kind: "reveal", path });
  52. return { success: true };
  53. },
  54. openInFileManager: async (path: string) => {
  55. evidence.push({ kind: "reveal", path });
  56. return { success: true };
  57. },
  58. getFileApplications: async () => [
  59. {
  60. id: "preview",
  61. name: "Preview",
  62. isDefault: true,
  63. iconDataUrl:
  64. (window as any).applicationIcon ??
  65. "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='20' height='20'%3E%3Crect width='20' height='20' fill='skyblue'/%3E%3C/svg%3E",
  66. },
  67. ],
  68. openExternalFile: async () => ({ success: true }),
  69. } as any;
  70. document.documentElement.className = params.has("dark") ? "theme-zai-dark" : "theme-zai-light";
  71. function App() {
  72. const [owner, setOwner] = useState(params.has("remote") ? "remote-A" : "local");
  73. (window as any).switchOwner = setOwner;
  74. const context = useMemo(
  75. () => ({
  76. theme: params.has("dark") ? "dark" : "light",
  77. sessionId: "inline-image-session",
  78. workspacePath: mode === "windows" ? "C:\\work" : "/workspace",
  79. workspaceIdentity: owner === "local" ? undefined : `ssh:${owner}:/workspace`,
  80. workspaceRemoteSessionId: owner === "local" ? undefined : `session-${owner}`,
  81. }),
  82. [owner],
  83. );
  84. const services = useMemo(
  85. () => ({
  86. fileService: {
  87. readMediaPreview: async (request: any) => {
  88. evidence.push({ kind: "readMediaPreview", ...request, owner });
  89. if (params.has("defer"))
  90. await new Promise<void>((resolve) => releases.set(owner, resolve));
  91. if (params.has("fail")) throw Error("fixture missing file");
  92. const svg =
  93. (window as any).previewSample ??
  94. '<svg xmlns="http://www.w3.org/2000/svg" width="1200" height="900"><rect width="1200" height="900" fill="lightblue"/></svg>';
  95. (window as any).resolvedOwners.push(owner);
  96. return {
  97. path: request.path,
  98. mediaType: "image/svg+xml",
  99. dataBase64: btoa(unescape(encodeURIComponent(svg))),
  100. totalBytes: svg.length,
  101. };
  102. },
  103. },
  104. }),
  105. [owner],
  106. );
  107. return (
  108. <ServiceProvider services={services as any}>
  109. <ZCodeIntlProvider initialLocale="zh-CN">
  110. <PlatformProvider platform={platform}>
  111. <TooltipProvider>
  112. <main data-owner={owner} className="min-h-screen bg-background p-8 text-foreground">
  113. <h1 className="text-ui-xl">对话中的文件名形式图片引用</h1>
  114. <div className="mx-auto mt-20 max-w-3xl">
  115. <ConversationRowView row={row} context={context as any} />
  116. </div>
  117. </main>
  118. </TooltipProvider>
  119. </PlatformProvider>
  120. </ZCodeIntlProvider>
  121. </ServiceProvider>
  122. );
  123. }
  124. createRoot(document.getElementById("root")!).render(<App />);