previewPaneMarkdownContent.tsx 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { useMemo, useRef } from "react";
  2. import { MarkdownSelectionTooltip } from "@/v4/MarkdownSelectionTooltip.js";
  3. import type { MarkdownSelectionTarget } from "@/lib/conversationSelectionReference.js";
  4. import { MessageResponse } from "@/components/ai-elements/message.js";
  5. import type { CodePreviewSettings } from "@/lib/codePreviewSettings.js";
  6. import type { Theme } from "@/useTheme.js";
  7. interface MarkdownPreviewContentProps {
  8. content: string;
  9. sourceKey?: string;
  10. sourceTitle?: string;
  11. sourcePath?: string;
  12. selectionTarget?: MarkdownSelectionTarget;
  13. workspacePath?: string;
  14. /** 应用主题(store 耦合剥离):透传给 markdown 渲染,缺省按 "system" 兜底。 */
  15. theme?: Theme;
  16. /** 代码预览设置(store 耦合剥离):透传给 markdown 渲染,需保持引用稳定。 */
  17. codePreviewSettings?: CodePreviewSettings;
  18. onOpenBrowserUrl?: (url: string) => void;
  19. }
  20. export function MarkdownPreviewContent({
  21. content,
  22. sourceKey,
  23. sourceTitle,
  24. sourcePath,
  25. selectionTarget,
  26. workspacePath,
  27. theme,
  28. codePreviewSettings,
  29. onOpenBrowserUrl,
  30. }: MarkdownPreviewContentProps) {
  31. const rootRef = useRef<HTMLDivElement>(null);
  32. const selectionScope = useMemo(
  33. () => ({}),
  34. [content, sourceKey, sourcePath, selectionTarget?.workspaceKey, selectionTarget?.sessionId],
  35. );
  36. return (
  37. <div
  38. ref={rootRef}
  39. data-markdown-preview="true"
  40. className="w-full h-full bg-background overflow-auto"
  41. >
  42. {selectionTarget && sourceKey ? (
  43. <MarkdownSelectionTooltip
  44. scopeKey={selectionScope}
  45. rootRef={rootRef}
  46. sourceKey={sourceKey}
  47. sourceTitle={sourceTitle ?? sourceKey}
  48. sourcePath={sourcePath}
  49. target={selectionTarget}
  50. />
  51. ) : null}
  52. <div className="min-h-full bg-background p-4">
  53. <MessageResponse
  54. className="min-w-0 break-words"
  55. workspacePath={workspacePath}
  56. theme={theme}
  57. codePreviewSettings={codePreviewSettings}
  58. onOpenExternalUrl={onOpenBrowserUrl}
  59. >
  60. {content}
  61. </MessageResponse>
  62. </div>
  63. </div>
  64. );
  65. }