preview-image-actions.fixture.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import React, { useEffect, useState } from "react";
  2. import { createRoot } from "react-dom/client";
  3. import { ImagePreviewContent, SvgPreviewContent } from "@/previewPaneImageContent.js";
  4. import { resolvePreviewPaneImageFileReference } from "@/PreviewPane.js";
  5. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  6. import {
  7. WORKSPACE_FILE_ADD_TO_CHAT_EVENT,
  8. createWorkspaceFileComposerMention,
  9. } from "@/lib/workspaceFileDrag.js";
  10. import "@/styles.css";
  11. const params = new URLSearchParams(location.search);
  12. const locale = params.get("locale") === "en-US" ? "en-US" : "zh-CN";
  13. localStorage.setItem("zcode-locale-preference", locale);
  14. document.documentElement.className =
  15. params.get("theme") === "light" ? "theme-zai-light" : "theme-zai-dark dark";
  16. const svg =
  17. '<svg xmlns="http://www.w3.org/2000/svg" width="480" height="300"><rect width="480" height="300" fill="white"/><circle cx="140" cy="150" r="70" fill="#d1fae5" stroke="#059669" stroke-width="3"/><path d="M300 80l70 40v80l-70 40-70-40v-80z" fill="#e0e7ff" stroke="#6366f1" stroke-width="3"/></svg>';
  18. const canvas = document.createElement("canvas");
  19. canvas.width = 64;
  20. canvas.height = 48;
  21. canvas.getContext("2d")!.fillRect(0, 0, 32, 48);
  22. const kind = params.get("kind") === "png" ? "png" : "svg";
  23. const source = {
  24. type: "file" as const,
  25. title: `drawing.${kind}`,
  26. path: `${params.get("source") === "outside" ? "/outside" : "/project"}/drawing.${kind}`,
  27. workspacePath: "/project",
  28. };
  29. const ref = resolvePreviewPaneImageFileReference(source, "/project", "/project")!;
  30. function Fixture() {
  31. const [draft, setDraft] = useState("existing draft");
  32. useEffect(() => {
  33. const onAdd = (event: Event) => {
  34. event.preventDefault();
  35. const mention = createWorkspaceFileComposerMention((event as CustomEvent).detail, "/project");
  36. setDraft((text) => `${text} ${mention.markdown}`);
  37. };
  38. window.addEventListener(WORKSPACE_FILE_ADD_TO_CHAT_EVENT, onAdd);
  39. return () => window.removeEventListener(WORKSPACE_FILE_ADD_TO_CHAT_EVENT, onAdd);
  40. }, []);
  41. return (
  42. <ZCodeIntlProvider initialLocale={locale}>
  43. <div style={{ height: "calc(100% - 52px)" }}>
  44. {kind === "svg" ? (
  45. <SvgPreviewContent title={ref.name} svgContent={svg} fileReference={ref} />
  46. ) : (
  47. <ImagePreviewContent
  48. title={ref.name}
  49. imageSource={canvas.toDataURL()}
  50. fileReference={ref}
  51. />
  52. )}
  53. </div>
  54. <textarea aria-label="Draft" readOnly value={draft} style={{ width: "100%", height: 52 }} />
  55. </ZCodeIntlProvider>
  56. );
  57. }
  58. createRoot(document.getElementById("root")!).render(<Fixture />);