terminal-mobile-input.fixture.tsx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { useState } from "react";
  2. import { createRoot } from "react-dom/client";
  3. import type { IServiceAccessor, ITerminalService } from "@zcode/services";
  4. import { TerminalSession } from "@/terminal/TerminalSession.js";
  5. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  6. import "@/styles.css";
  7. const writes: string[] = [];
  8. const listeners = new Set<(data: string) => void>();
  9. const query = new URLSearchParams(location.search);
  10. const theme = query.get("theme") ?? "dark";
  11. document.documentElement.className = `${theme} theme-zai-${theme}`;
  12. const terminalService: ITerminalService = {
  13. async create() {
  14. return { id: "input-fixture", shell: "/bin/sh" };
  15. },
  16. async write({ data }) {
  17. writes.push(data);
  18. document.querySelector("output")!.textContent = JSON.stringify(writes.join(""));
  19. for (const listener of listeners) listener(data.replaceAll("\x7f", "\b \b"));
  20. },
  21. async resize() {},
  22. async dispose() {},
  23. onDynamicData() {
  24. return (listener) => {
  25. listeners.add(listener);
  26. return { dispose: () => listeners.delete(listener) };
  27. };
  28. },
  29. onDynamicExit() {
  30. return () => ({ dispose() {} });
  31. },
  32. };
  33. const services = { terminalService } as IServiceAccessor;
  34. const onShellLabelChange = () => {
  35. document.body.dataset.ready = "true";
  36. };
  37. const onOpenBrowserUrl = () => {};
  38. function Fixture() {
  39. const [mounted, setMounted] = useState(true);
  40. return (
  41. <ZCodeIntlProvider initialLocale="zh-CN">
  42. <button onClick={() => setMounted((value) => !value)}>Toggle</button>
  43. <button
  44. onClick={() => {
  45. for (const listener of listeners) listener("\x1b[5n");
  46. }}
  47. >
  48. PTY status
  49. </button>
  50. <output>""</output>
  51. <div style={{ height: 400 }}>
  52. {mounted && (
  53. <TerminalSession
  54. sessionId="input-fixture"
  55. services={services}
  56. cwd="/workspace/example"
  57. workspaceKey="input-fixture"
  58. persistentKey={query.has("persistent") ? "input-fixture" : undefined}
  59. isVisible
  60. isWindowsDesktop={query.has("windows")}
  61. onShellLabelChange={onShellLabelChange}
  62. onOpenBrowserUrl={onOpenBrowserUrl}
  63. />
  64. )}
  65. </div>
  66. </ZCodeIntlProvider>
  67. );
  68. }
  69. createRoot(document.getElementById("root")!).render(<Fixture />);