@deepseek-ai/dsh-client-ui-primitives 0.2.2-alpha.3 → 0.2.2-rc.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.i18n.yaml +16 -16
- package/README.md +21 -14
- package/README.zh.md +21 -14
- package/lib/HoverCard.module.css +8 -3
- package/lib/JsonTree.module.css +7 -17
- package/lib/Menu.module.css +157 -37
- package/lib/Modal.module.css +16 -0
- package/lib/QuotePreview.module.css +37 -0
- package/lib/RiskConfirmation.module.css +11 -37
- package/lib/Toast.module.css +2 -3
- package/lib/Tooltip.module.css +2 -1
- package/lib/index.js +1447 -776
- package/lib/index.js.map +1 -0
- package/lib/markdown/CodeBlock.module.css +64 -0
- package/lib/markdown/MarkdownText.module.css +20 -1
- package/lib/types/AnimatedRows.d.ts +3 -0
- package/lib/types/AnimatedRows.d.ts.map +1 -0
- package/lib/types/AnimatedRows.js.map +1 -0
- package/lib/types/BrandWordmark.d.ts.map +1 -0
- package/lib/types/BrandWordmark.js.map +1 -0
- package/lib/types/Button.d.ts.map +1 -0
- package/lib/types/Button.js.map +1 -0
- package/lib/types/Checkbox.d.ts +3 -1
- package/lib/types/Checkbox.d.ts.map +1 -0
- package/lib/types/Checkbox.js.map +1 -0
- package/lib/types/CodeFileIcon.d.ts.map +1 -0
- package/lib/types/CodeFileIcon.js.map +1 -0
- package/lib/types/CodeToolbar.d.ts.map +1 -0
- package/lib/types/CodeToolbar.js.map +1 -0
- package/lib/types/CommandText.d.ts.map +1 -0
- package/lib/types/CommandText.js.map +1 -0
- package/lib/types/ConfigField.d.ts.map +1 -0
- package/lib/types/ConfigField.js.map +1 -0
- package/lib/types/ConnectionIndicator.d.ts.map +1 -0
- package/lib/types/ConnectionIndicator.js.map +1 -0
- package/lib/types/DetailDocument.d.ts.map +1 -0
- package/lib/types/DetailDocument.js.map +1 -0
- package/lib/types/DiffBlock.d.ts.map +1 -0
- package/lib/types/DiffBlock.js.map +1 -0
- package/lib/types/DisclosureRow.d.ts.map +1 -0
- package/lib/types/DisclosureRow.js.map +1 -0
- package/lib/types/DraggableImage.d.ts.map +1 -0
- package/lib/types/DraggableImage.js.map +1 -0
- package/lib/types/FileTypeIcon.d.ts.map +1 -0
- package/lib/types/FileTypeIcon.js.map +1 -0
- package/lib/types/FishLogo.d.ts.map +1 -0
- package/lib/types/FishLogo.js.map +1 -0
- package/lib/types/FoldToggle.d.ts.map +1 -0
- package/lib/types/FoldToggle.js.map +1 -0
- package/lib/types/HoverCard.d.ts.map +1 -0
- package/lib/types/HoverCard.js.map +1 -0
- package/lib/types/HoverCardGroup.d.ts.map +1 -0
- package/lib/types/HoverCardGroup.js.map +1 -0
- package/lib/types/ImageLightbox.d.ts.map +1 -0
- package/lib/types/ImageLightbox.js.map +1 -0
- package/lib/types/ImagePreview.d.ts.map +1 -0
- package/lib/types/ImagePreview.js.map +1 -0
- package/lib/types/InlineEditor.d.ts.map +1 -0
- package/lib/types/InlineEditor.js.map +1 -0
- package/lib/types/Input.d.ts.map +1 -0
- package/lib/types/Input.js.map +1 -0
- package/lib/types/JsonTree.d.ts.map +1 -0
- package/lib/types/JsonTree.js.map +1 -0
- package/lib/types/LinkIcon.d.ts.map +1 -0
- package/lib/types/LinkIcon.js.map +1 -0
- package/lib/types/Menu.d.ts +26 -14
- package/lib/types/Menu.d.ts.map +1 -0
- package/lib/types/Menu.js.map +1 -0
- package/lib/types/MenuGroup.d.ts.map +1 -0
- package/lib/types/MenuGroup.js.map +1 -0
- package/lib/types/MenuRow.d.ts +37 -0
- package/lib/types/MenuRow.d.ts.map +1 -0
- package/lib/types/MenuRow.js.map +1 -0
- package/lib/types/MenuSurface.d.ts.map +1 -0
- package/lib/types/MenuSurface.js.map +1 -0
- package/lib/types/Modal.d.ts +3 -1
- package/lib/types/Modal.d.ts.map +1 -0
- package/lib/types/Modal.js.map +1 -0
- package/lib/types/PathLabel.d.ts.map +1 -0
- package/lib/types/PathLabel.js.map +1 -0
- package/lib/types/PermissionIcon.d.ts.map +1 -0
- package/lib/types/PermissionIcon.js.map +1 -0
- package/lib/types/Pill.d.ts.map +1 -0
- package/lib/types/Pill.js.map +1 -0
- package/lib/types/QuotePreview.d.ts +17 -4
- package/lib/types/QuotePreview.d.ts.map +1 -0
- package/lib/types/QuotePreview.js.map +1 -0
- package/lib/types/ReadBlock.d.ts.map +1 -0
- package/lib/types/ReadBlock.js.map +1 -0
- package/lib/types/ReferenceIcon.d.ts.map +1 -0
- package/lib/types/ReferenceIcon.js.map +1 -0
- package/lib/types/RiskConfirmation.d.ts.map +1 -0
- package/lib/types/RiskConfirmation.js.map +1 -0
- package/lib/types/SearchBlock.d.ts.map +1 -0
- package/lib/types/SearchBlock.js.map +1 -0
- package/lib/types/SegmentedControl.d.ts.map +1 -0
- package/lib/types/SegmentedControl.js.map +1 -0
- package/lib/types/SegmentedTabs.d.ts.map +1 -0
- package/lib/types/SegmentedTabs.js.map +1 -0
- package/lib/types/ShortcutKeys.d.ts.map +1 -0
- package/lib/types/ShortcutKeys.js.map +1 -0
- package/lib/types/SiteGlyph.d.ts.map +1 -0
- package/lib/types/SiteGlyph.js.map +1 -0
- package/lib/types/StateDot.d.ts.map +1 -0
- package/lib/types/StateDot.js.map +1 -0
- package/lib/types/SubmenuPanel.d.ts +19 -0
- package/lib/types/SubmenuPanel.d.ts.map +1 -0
- package/lib/types/SubmenuPanel.js.map +1 -0
- package/lib/types/Switch.d.ts.map +1 -0
- package/lib/types/Switch.js.map +1 -0
- package/lib/types/Tag.d.ts.map +1 -0
- package/lib/types/Tag.js.map +1 -0
- package/lib/types/TerminalBlock.d.ts.map +1 -0
- package/lib/types/TerminalBlock.js.map +1 -0
- package/lib/types/TextShimmer.d.ts.map +1 -0
- package/lib/types/TextShimmer.js.map +1 -0
- package/lib/types/Toast.d.ts.map +1 -0
- package/lib/types/Toast.js.map +1 -0
- package/lib/types/Tooltip.d.ts.map +1 -0
- package/lib/types/Tooltip.js.map +1 -0
- package/lib/types/UserMessageBubble.d.ts.map +1 -0
- package/lib/types/UserMessageBubble.js.map +1 -0
- package/lib/types/WebBlock.d.ts.map +1 -0
- package/lib/types/WebBlock.js.map +1 -0
- package/lib/types/ZoomControls.d.ts.map +1 -0
- package/lib/types/ZoomControls.js.map +1 -0
- package/lib/types/ansi.d.ts.map +1 -0
- package/lib/types/ansi.js.map +1 -0
- package/lib/types/clipboard.d.ts.map +1 -0
- package/lib/types/clipboard.js.map +1 -0
- package/lib/types/code-file-icon-artwork.d.ts.map +1 -0
- package/lib/types/code-file-icon-artwork.js.map +1 -0
- package/lib/types/code-file-types.d.ts.map +1 -0
- package/lib/types/code-file-types.js.map +1 -0
- package/lib/types/code-highlighting.d.ts.map +1 -0
- package/lib/types/code-highlighting.js.map +1 -0
- package/lib/types/darwin-desktop.d.ts.map +1 -0
- package/lib/types/darwin-desktop.js.map +1 -0
- package/lib/types/document-zoom.d.ts.map +1 -0
- package/lib/types/document-zoom.js.map +1 -0
- package/lib/types/file-drag-actions.d.ts.map +1 -0
- package/lib/types/file-drag-actions.js.map +1 -0
- package/lib/types/file-size.d.ts.map +1 -0
- package/lib/types/file-size.js.map +1 -0
- package/lib/types/focus.d.ts.map +1 -0
- package/lib/types/focus.js.map +1 -0
- package/lib/types/guide-artwork.d.ts.map +1 -0
- package/lib/types/guide-artwork.js.map +1 -0
- package/lib/types/head-tail-cap.d.ts.map +1 -0
- package/lib/types/head-tail-cap.js.map +1 -0
- package/lib/types/icons/artwork.d.ts +12 -0
- package/lib/types/icons/artwork.d.ts.map +1 -0
- package/lib/types/icons/artwork.js.map +1 -0
- package/lib/types/icons/index.d.ts +12 -0
- package/lib/types/icons/index.d.ts.map +1 -0
- package/lib/types/icons/index.js.map +1 -0
- package/lib/types/icons/props.d.ts.map +1 -0
- package/lib/types/icons/props.js.map +1 -0
- package/lib/types/icons/shared-artwork.d.ts.map +1 -0
- package/lib/types/icons/shared-artwork.js.map +1 -0
- package/lib/types/image-drag.d.ts.map +1 -0
- package/lib/types/image-drag.js.map +1 -0
- package/lib/types/index.d.ts +5 -4
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index.js.map +1 -0
- package/lib/types/input-modality.d.ts.map +1 -0
- package/lib/types/input-modality.js.map +1 -0
- package/lib/types/keyboard-composition.d.ts.map +1 -0
- package/lib/types/keyboard-composition.js.map +1 -0
- package/lib/types/markdown/CodeBlock.d.ts +8 -1
- package/lib/types/markdown/CodeBlock.d.ts.map +1 -0
- package/lib/types/markdown/CodeBlock.js.map +1 -0
- package/lib/types/markdown/FenceBlocks.d.ts +31 -0
- package/lib/types/markdown/FenceBlocks.d.ts.map +1 -0
- package/lib/types/markdown/FenceBlocks.js.map +1 -0
- package/lib/types/markdown/JsonBlock.d.ts.map +1 -0
- package/lib/types/markdown/JsonBlock.js.map +1 -0
- package/lib/types/markdown/MarkdownDelegate.d.ts +23 -1
- package/lib/types/markdown/MarkdownDelegate.d.ts.map +1 -0
- package/lib/types/markdown/MarkdownDelegate.js.map +1 -0
- package/lib/types/markdown/MarkdownText.d.ts +2 -0
- package/lib/types/markdown/MarkdownText.d.ts.map +1 -0
- package/lib/types/markdown/MarkdownText.js.map +1 -0
- package/lib/types/markdown/cjkFriendlyStrong.d.ts.map +1 -0
- package/lib/types/markdown/cjkFriendlyStrong.js.map +1 -0
- package/lib/types/markdown/copy-tex.d.ts +14 -0
- package/lib/types/markdown/copy-tex.d.ts.map +1 -0
- package/lib/types/markdown/copy-tex.js.map +1 -0
- package/lib/types/markdown/diff-fence.d.ts +58 -0
- package/lib/types/markdown/diff-fence.d.ts.map +1 -0
- package/lib/types/markdown/diff-fence.js.map +1 -0
- package/lib/types/markdown/file-link.d.ts.map +1 -0
- package/lib/types/markdown/file-link.js.map +1 -0
- package/lib/types/markdown/highlight.d.ts +18 -0
- package/lib/types/markdown/highlight.d.ts.map +1 -0
- package/lib/types/markdown/highlight.js.map +1 -0
- package/lib/types/markdown/incremental.d.ts.map +1 -0
- package/lib/types/markdown/incremental.js.map +1 -0
- package/lib/types/markdown/katex.d.ts.map +1 -0
- package/lib/types/markdown/katex.js.map +1 -0
- package/lib/types/markdown/local-image-syntax.d.ts.map +1 -0
- package/lib/types/markdown/local-image-syntax.js.map +1 -0
- package/lib/types/markdown/mathCompatibility.d.ts.map +1 -0
- package/lib/types/markdown/mathCompatibility.js.map +1 -0
- package/lib/types/markdown/parse.d.ts.map +1 -0
- package/lib/types/markdown/parse.js.map +1 -0
- package/lib/types/markdown/plain-text.d.ts.map +1 -0
- package/lib/types/markdown/plain-text.js.map +1 -0
- package/lib/types/markdown/quote-text.d.ts.map +1 -0
- package/lib/types/markdown/quote-text.js.map +1 -0
- package/lib/types/markdown/render.d.ts.map +1 -0
- package/lib/types/markdown/render.js.map +1 -0
- package/lib/types/markdown/useViewportHighlighting.d.ts.map +1 -0
- package/lib/types/markdown/useViewportHighlighting.js.map +1 -0
- package/lib/types/markdown/windows-path-destinations.d.ts.map +1 -0
- package/lib/types/markdown/windows-path-destinations.js.map +1 -0
- package/lib/types/menu-submenu-position.d.ts +15 -0
- package/lib/types/menu-submenu-position.d.ts.map +1 -0
- package/lib/types/menu-submenu-position.js.map +1 -0
- package/lib/types/overlay-top-margin.d.ts.map +1 -0
- package/lib/types/overlay-top-margin.js.map +1 -0
- package/lib/types/plugin-artwork.d.ts.map +1 -0
- package/lib/types/plugin-artwork.js.map +1 -0
- package/lib/types/pointer-grace.d.ts.map +1 -0
- package/lib/types/pointer-grace.js.map +1 -0
- package/lib/types/rank-by-name.d.ts.map +1 -0
- package/lib/types/rank-by-name.js.map +1 -0
- package/lib/types/relative-time.d.ts.map +1 -0
- package/lib/types/relative-time.js.map +1 -0
- package/lib/types/settings-form/SettingsForm.d.ts.map +1 -0
- package/lib/types/settings-form/SettingsForm.js.map +1 -0
- package/lib/types/settings-form/fields.d.ts.map +1 -0
- package/lib/types/settings-form/fields.js.map +1 -0
- package/lib/types/settings-form/form-model.d.ts.map +1 -0
- package/lib/types/settings-form/form-model.js.map +1 -0
- package/lib/types/tooltip-placement.d.ts.map +1 -0
- package/lib/types/tooltip-placement.js.map +1 -0
- package/lib/types/use-clipboard-copy.d.ts.map +1 -0
- package/lib/types/use-clipboard-copy.js.map +1 -0
- package/lib/types/useAnchoredMaxHeight.d.ts.map +1 -0
- package/lib/types/useAnchoredMaxHeight.js.map +1 -0
- package/lib/types/useDismissOnOutsidePointer.d.ts.map +1 -0
- package/lib/types/useDismissOnOutsidePointer.js.map +1 -0
- package/lib/types/useMenuSubmenu.d.ts +43 -0
- package/lib/types/useMenuSubmenu.d.ts.map +1 -0
- package/lib/types/useMenuSubmenu.js.map +1 -0
- package/lib/types/useModalLayer.d.ts.map +1 -0
- package/lib/types/useModalLayer.js.map +1 -0
- package/lib/types/useRowDragPreview.d.ts.map +1 -0
- package/lib/types/useRowDragPreview.js.map +1 -0
- package/lib/types/user-text.d.ts.map +1 -0
- package/lib/types/user-text.js.map +1 -0
- package/package.json +11 -8
- package/src/AnimatedRows.module.css +7 -0
- package/src/AnimatedRows.tsx +195 -0
- package/src/BrandWordmark.tsx +59 -0
- package/src/Button.module.css +84 -0
- package/src/Button.tsx +36 -0
- package/src/Checkbox.module.css +28 -0
- package/src/Checkbox.tsx +34 -0
- package/src/CodeCard.module.css +85 -0
- package/src/CodeFileIcon.tsx +28 -0
- package/src/CodeToolbar.tsx +62 -0
- package/src/CommandText.module.css +12 -0
- package/src/CommandText.tsx +46 -0
- package/src/ConfigField.module.css +12 -0
- package/src/ConfigField.tsx +53 -0
- package/src/ConnectionIndicator.module.css +120 -0
- package/src/ConnectionIndicator.tsx +103 -0
- package/src/DetailDocument.module.css +67 -0
- package/src/DetailDocument.tsx +120 -0
- package/src/DiffBlock.module.css +82 -0
- package/src/DiffBlock.tsx +221 -0
- package/src/DisclosureRow.module.css +115 -0
- package/src/DisclosureRow.tsx +124 -0
- package/src/DraggableImage.tsx +26 -0
- package/src/FileTypeIcon.module.css +50 -0
- package/src/FileTypeIcon.tsx +285 -0
- package/src/FishLogo.tsx +28 -0
- package/src/FoldToggle.tsx +33 -0
- package/src/HoverCard.module.css +108 -0
- package/src/HoverCard.tsx +356 -0
- package/src/HoverCardGroup.tsx +77 -0
- package/src/ImageLightbox.module.css +54 -0
- package/src/ImageLightbox.tsx +72 -0
- package/src/ImagePreview.module.css +24 -0
- package/src/ImagePreview.tsx +36 -0
- package/src/InlineEditor.module.css +23 -0
- package/src/InlineEditor.tsx +69 -0
- package/src/Input.module.css +38 -0
- package/src/Input.tsx +24 -0
- package/src/JsonTree.module.css +371 -0
- package/src/JsonTree.tsx +850 -0
- package/src/LinkIcon.tsx +118 -0
- package/src/Menu.module.css +443 -0
- package/src/Menu.tsx +553 -0
- package/src/MenuGroup.module.css +37 -0
- package/src/MenuGroup.tsx +103 -0
- package/src/MenuRow.tsx +89 -0
- package/src/MenuSurface.module.css +65 -0
- package/src/MenuSurface.tsx +54 -0
- package/src/Modal.module.css +147 -0
- package/src/Modal.tsx +96 -0
- package/src/PathLabel.module.css +28 -0
- package/src/PathLabel.tsx +41 -0
- package/src/PermissionIcon.tsx +119 -0
- package/src/Pill.module.css +30 -0
- package/src/Pill.tsx +36 -0
- package/src/QuotePreview.module.css +138 -0
- package/src/QuotePreview.tsx +133 -0
- package/src/ReadBlock.module.css +45 -0
- package/src/ReadBlock.tsx +151 -0
- package/src/ReferenceIcon.tsx +49 -0
- package/src/RiskConfirmation.module.css +47 -0
- package/src/RiskConfirmation.tsx +79 -0
- package/src/RowDragPreview.module.css +12 -0
- package/src/SearchBlock.module.css +114 -0
- package/src/SearchBlock.tsx +281 -0
- package/src/SegmentedControl.module.css +81 -0
- package/src/SegmentedControl.tsx +140 -0
- package/src/SegmentedTabs.module.css +77 -0
- package/src/SegmentedTabs.tsx +84 -0
- package/src/ShortcutKeys.module.css +33 -0
- package/src/ShortcutKeys.tsx +12 -0
- package/src/SiteGlyph.tsx +149 -0
- package/src/StateDot.module.css +100 -0
- package/src/StateDot.tsx +77 -0
- package/src/SubmenuPanel.tsx +51 -0
- package/src/Switch.module.css +49 -0
- package/src/Switch.tsx +42 -0
- package/src/Tag.module.css +57 -0
- package/src/Tag.tsx +42 -0
- package/src/TerminalBlock.module.css +207 -0
- package/src/TerminalBlock.tsx +294 -0
- package/src/TextShimmer.module.css +88 -0
- package/src/TextShimmer.tsx +58 -0
- package/src/Toast.module.css +102 -0
- package/src/Toast.tsx +101 -0
- package/src/Tooltip.module.css +68 -0
- package/src/Tooltip.tsx +273 -0
- package/src/UserMessageBubble.module.css +11 -0
- package/src/UserMessageBubble.tsx +16 -0
- package/src/WebBlock.module.css +153 -0
- package/src/WebBlock.tsx +196 -0
- package/src/ZoomControls.module.css +54 -0
- package/src/ZoomControls.tsx +92 -0
- package/src/ansi.ts +447 -0
- package/src/clipboard.ts +47 -0
- package/src/code-file-icon-artwork.manifest.json +71 -0
- package/src/code-file-icon-artwork.ts +60 -0
- package/src/code-file-types.ts +340 -0
- package/src/code-highlighting.ts +25 -0
- package/src/css-modules.d.ts +6 -0
- package/src/darwin-desktop.ts +11 -0
- package/src/document-zoom.ts +12 -0
- package/src/file-drag-actions.ts +29 -0
- package/src/file-size.ts +16 -0
- package/src/focus.ts +44 -0
- package/src/guide-artwork.tsx +32 -0
- package/src/head-tail-cap.ts +27 -0
- package/src/icons/artwork.ts +156 -0
- package/src/icons/index.tsx +634 -0
- package/src/icons/props.ts +8 -0
- package/src/icons/shared-artwork.tsx +71 -0
- package/src/image-drag.ts +84 -0
- package/src/index.ts +142 -0
- package/src/input-modality.ts +59 -0
- package/src/keyboard-composition.ts +36 -0
- package/src/markdown/CodeBlock.module.css +209 -0
- package/src/markdown/CodeBlock.tsx +268 -0
- package/src/markdown/FenceBlocks.tsx +51 -0
- package/src/markdown/JsonBlock.module.css +34 -0
- package/src/markdown/JsonBlock.tsx +36 -0
- package/src/markdown/MarkdownDelegate.tsx +102 -0
- package/src/markdown/MarkdownText.module.css +511 -0
- package/src/markdown/MarkdownText.tsx +218 -0
- package/src/markdown/cjkFriendlyStrong.ts +83 -0
- package/src/markdown/copy-tex.ts +53 -0
- package/src/markdown/diff-fence.ts +212 -0
- package/src/markdown/file-link.ts +45 -0
- package/src/markdown/highlight.ts +689 -0
- package/src/markdown/incremental.ts +360 -0
- package/src/markdown/katex.tsx +90 -0
- package/src/markdown/local-image-syntax.ts +29 -0
- package/src/markdown/mathCompatibility.ts +349 -0
- package/src/markdown/parse.ts +49 -0
- package/src/markdown/plain-text.ts +121 -0
- package/src/markdown/quote-text.ts +10 -0
- package/src/markdown/render.tsx +834 -0
- package/src/markdown/useViewportHighlighting.ts +72 -0
- package/src/markdown/windows-path-destinations.ts +34 -0
- package/src/menu-submenu-position.ts +20 -0
- package/src/overlay-top-margin.ts +18 -0
- package/src/plugin-artwork.tsx +110 -0
- package/src/pointer-grace.ts +47 -0
- package/src/rank-by-name.ts +93 -0
- package/src/relative-time.ts +36 -0
- package/src/settings-form/SettingsForm.module.css +53 -0
- package/src/settings-form/SettingsForm.tsx +74 -0
- package/src/settings-form/fields.module.css +153 -0
- package/src/settings-form/fields.tsx +151 -0
- package/src/settings-form/form-model.ts +393 -0
- package/src/tooltip-placement.ts +60 -0
- package/src/use-clipboard-copy.ts +44 -0
- package/src/useAnchoredMaxHeight.ts +45 -0
- package/src/useDismissOnOutsidePointer.ts +34 -0
- package/src/useMenuSubmenu.ts +182 -0
- package/src/useModalLayer.ts +108 -0
- package/src/useRowDragPreview.ts +76 -0
- package/src/user-text.module.css +46 -0
- package/src/user-text.tsx +67 -0
- package/lib/types/useAnchoredPosition.d.ts +0 -36
package/src/JsonTree.tsx
ADDED
|
@@ -0,0 +1,850 @@
|
|
|
1
|
+
import clsx from 'clsx'
|
|
2
|
+
import { useCallback, useEffect, useId, useLayoutEffect, useRef, useState, useSyncExternalStore } from 'react'
|
|
3
|
+
import type {
|
|
4
|
+
CSSProperties,
|
|
5
|
+
KeyboardEvent as ReactKeyboardEvent,
|
|
6
|
+
MouseEvent as ReactMouseEvent,
|
|
7
|
+
ReactNode,
|
|
8
|
+
} from 'react'
|
|
9
|
+
import { IconCheckOutlineRegular, IconCopyOutlineRegular, IconWrapLinesOutlineRegular } from './icons/index.tsx'
|
|
10
|
+
import { Menu } from './Menu.tsx'
|
|
11
|
+
import type { MenuEntry } from './Menu.tsx'
|
|
12
|
+
import css from './JsonTree.module.css'
|
|
13
|
+
|
|
14
|
+
const OBJECT_PREVIEW_LIMIT = 4
|
|
15
|
+
const ARRAY_PREVIEW_LIMIT = 5
|
|
16
|
+
const PREVIEW_DEPTH_LIMIT = 2
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Display copy for the tree's copy affordance; the owner passes localized
|
|
20
|
+
* labels (this package is cordis-free, so copy arrives via props).
|
|
21
|
+
*/
|
|
22
|
+
export interface JsonTreeLabels {
|
|
23
|
+
/** Menu item: copy the raw primitive value. */
|
|
24
|
+
copyValue: string
|
|
25
|
+
/** Menu item: copy the value as compact JSON (primitive rows). */
|
|
26
|
+
copyJson: string
|
|
27
|
+
/** Menu item: copy the property path. */
|
|
28
|
+
copyPath: string
|
|
29
|
+
/** Menu item: copy the value as pretty-printed JSON. */
|
|
30
|
+
copyPrettyJson: string
|
|
31
|
+
/** Menu item: copy the value as compact JSON (object rows). */
|
|
32
|
+
copyCompactJson: string
|
|
33
|
+
/** Copy-button state label after a successful copy. */
|
|
34
|
+
copied: string
|
|
35
|
+
/** Copy-button state label after a failed copy. */
|
|
36
|
+
copyFailed: string
|
|
37
|
+
/** Expander aria label while expanded. */
|
|
38
|
+
collapseNode: string
|
|
39
|
+
/** Expander aria label while collapsed. */
|
|
40
|
+
expandNode: string
|
|
41
|
+
/** Copy-button tooltip, given the current action label. */
|
|
42
|
+
copyButtonTitle: (action: string) => string
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function valueCopyMenuItems(labels: JsonTreeLabels): readonly MenuEntry[] {
|
|
46
|
+
return [
|
|
47
|
+
{ id: 'value', label: labels.copyValue },
|
|
48
|
+
{ id: 'json', label: labels.copyJson },
|
|
49
|
+
{ id: 'path', label: labels.copyPath },
|
|
50
|
+
]
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function objectCopyMenuItems(labels: JsonTreeLabels): readonly MenuEntry[] {
|
|
54
|
+
return [
|
|
55
|
+
{ id: 'prettyJson', label: labels.copyPrettyJson },
|
|
56
|
+
{ id: 'json', label: labels.copyCompactJson },
|
|
57
|
+
{ id: 'path', label: labels.copyPath },
|
|
58
|
+
]
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
type JsonPath = readonly (number | string)[]
|
|
62
|
+
|
|
63
|
+
interface RowTarget {
|
|
64
|
+
path: JsonPath
|
|
65
|
+
value: unknown
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
type CopyMode = 'json' | 'path' | 'prettyJson' | 'value'
|
|
69
|
+
|
|
70
|
+
interface CopySnapshot {
|
|
71
|
+
id: string
|
|
72
|
+
target: RowTarget
|
|
73
|
+
state: 'idle' | 'copied' | 'failed'
|
|
74
|
+
menuOpen: boolean
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Notify only the old and new row actions; JSON values do not subscribe to hover state. */
|
|
78
|
+
function createCopyStore() {
|
|
79
|
+
let current: CopySnapshot | undefined
|
|
80
|
+
const listeners = new Map<string, Set<() => void>>()
|
|
81
|
+
return {
|
|
82
|
+
get: () => current,
|
|
83
|
+
set(next: CopySnapshot | undefined) {
|
|
84
|
+
const previous = current?.id
|
|
85
|
+
current = next
|
|
86
|
+
for (const id of new Set([previous, next?.id])) {
|
|
87
|
+
if (id === undefined) continue
|
|
88
|
+
for (const listener of listeners.get(id) ?? []) listener()
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
subscribe(id: string, listener: () => void) {
|
|
92
|
+
let row = listeners.get(id)
|
|
93
|
+
if (row === undefined) listeners.set(id, row = new Set())
|
|
94
|
+
row.add(listener)
|
|
95
|
+
return () => {
|
|
96
|
+
row.delete(listener)
|
|
97
|
+
if (row.size === 0) listeners.delete(id)
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function JsonCopyAction({ store, target, persistent, labels, onCopy, onClose }: {
|
|
104
|
+
store: ReturnType<typeof createCopyStore>
|
|
105
|
+
target: RowTarget
|
|
106
|
+
persistent: boolean
|
|
107
|
+
labels: JsonTreeLabels
|
|
108
|
+
onCopy: (target: RowTarget, mode: CopyMode) => Promise<void>
|
|
109
|
+
onClose: () => void
|
|
110
|
+
}) {
|
|
111
|
+
const id = pathId(target.path)
|
|
112
|
+
const subscribe = useCallback((listener: () => void) => store.subscribe(id, listener), [id, store])
|
|
113
|
+
const getSnapshot = () => {
|
|
114
|
+
const current = store.get()
|
|
115
|
+
return current?.id === id ? current : undefined
|
|
116
|
+
}
|
|
117
|
+
const snapshot = useSyncExternalStore(subscribe, getSnapshot, getSnapshot)
|
|
118
|
+
const buttonRef = useRef<HTMLButtonElement>(null)
|
|
119
|
+
const state = snapshot?.state ?? 'idle'
|
|
120
|
+
const object = typeof target.value === 'object' && target.value !== null
|
|
121
|
+
const copyTitle = state === 'copied'
|
|
122
|
+
? labels.copied
|
|
123
|
+
: state === 'failed'
|
|
124
|
+
? labels.copyFailed
|
|
125
|
+
: object ? labels.copyPrettyJson : labels.copyValue
|
|
126
|
+
return (
|
|
127
|
+
<span className={css.copySlot}>
|
|
128
|
+
{(persistent || snapshot !== undefined) && (
|
|
129
|
+
<Menu
|
|
130
|
+
open={snapshot?.menuOpen === true}
|
|
131
|
+
compact
|
|
132
|
+
portal
|
|
133
|
+
align="end"
|
|
134
|
+
anchor={(
|
|
135
|
+
<button
|
|
136
|
+
ref={buttonRef}
|
|
137
|
+
type="button"
|
|
138
|
+
className={css.actionButton}
|
|
139
|
+
data-json-copy-button
|
|
140
|
+
data-state={state}
|
|
141
|
+
aria-label={copyTitle}
|
|
142
|
+
title={labels.copyButtonTitle(copyTitle)}
|
|
143
|
+
onClick={() => void onCopy(target, object ? 'prettyJson' : 'value')}
|
|
144
|
+
onContextMenu={(event) => {
|
|
145
|
+
event.preventDefault()
|
|
146
|
+
event.stopPropagation()
|
|
147
|
+
store.set({ id, target, state, menuOpen: true })
|
|
148
|
+
}}
|
|
149
|
+
>
|
|
150
|
+
{state === 'copied'
|
|
151
|
+
? <IconCheckOutlineRegular size={12} />
|
|
152
|
+
: <IconCopyOutlineRegular size={12} />}
|
|
153
|
+
</button>
|
|
154
|
+
)}
|
|
155
|
+
items={object ? objectCopyMenuItems(labels) : valueCopyMenuItems(labels)}
|
|
156
|
+
onSelect={(mode) => {
|
|
157
|
+
void onCopy(target, mode as CopyMode)
|
|
158
|
+
}}
|
|
159
|
+
onClose={onClose}
|
|
160
|
+
anchorRef={buttonRef}
|
|
161
|
+
/>
|
|
162
|
+
)}
|
|
163
|
+
</span>
|
|
164
|
+
)
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function isExpandableValue(value: unknown): value is object | unknown[] {
|
|
168
|
+
return typeof value === 'object' && value !== null && !(value instanceof Date)
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function entriesOf(value: object | unknown[]): readonly (readonly [string, unknown])[] {
|
|
172
|
+
if (Array.isArray(value)) {
|
|
173
|
+
return value.map((item, index) => [String(index), item] as const)
|
|
174
|
+
}
|
|
175
|
+
return Object.keys(value).map(key => [
|
|
176
|
+
key,
|
|
177
|
+
(value as Record<string, unknown>)[key],
|
|
178
|
+
] as const)
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function bracketOf(value: object | unknown[]): readonly [string, string] {
|
|
182
|
+
return Array.isArray(value) ? ['[', ']'] : ['{', '}']
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function previewPrimitive(value: unknown): ReactNode {
|
|
186
|
+
if (value === null) return <span className={css.keywordValue}>null</span>
|
|
187
|
+
if (typeof value === 'string') {
|
|
188
|
+
return <span className={css.stringValue}>{JSON.stringify(value)}</span>
|
|
189
|
+
}
|
|
190
|
+
if (typeof value === 'number') {
|
|
191
|
+
return <span className={css.numberValue}>{String(value)}</span>
|
|
192
|
+
}
|
|
193
|
+
if (typeof value === 'boolean') {
|
|
194
|
+
return <span className={css.keywordValue}>{String(value)}</span>
|
|
195
|
+
}
|
|
196
|
+
if (typeof value === 'bigint') {
|
|
197
|
+
return <span className={css.otherValue}>{value.toString()}</span>
|
|
198
|
+
}
|
|
199
|
+
if (typeof value === 'undefined') {
|
|
200
|
+
return <span className={css.otherValue}>undefined</span>
|
|
201
|
+
}
|
|
202
|
+
if (typeof value === 'symbol') {
|
|
203
|
+
return <span className={css.otherValue}>{value.description ?? 'Symbol'}</span>
|
|
204
|
+
}
|
|
205
|
+
if (typeof value === 'function') {
|
|
206
|
+
return <span className={css.otherValue}>{value.name || 'Function'}</span>
|
|
207
|
+
}
|
|
208
|
+
return null
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function previewValue(value: unknown, depth: number): ReactNode {
|
|
212
|
+
if (!isExpandableValue(value)) return previewPrimitive(value)
|
|
213
|
+
|
|
214
|
+
const array = Array.isArray(value)
|
|
215
|
+
const entries = entriesOf(value)
|
|
216
|
+
const limit = array ? ARRAY_PREVIEW_LIMIT : OBJECT_PREVIEW_LIMIT
|
|
217
|
+
const visible = entries.slice(0, limit)
|
|
218
|
+
const [open, close] = bracketOf(value)
|
|
219
|
+
|
|
220
|
+
return (
|
|
221
|
+
<>
|
|
222
|
+
<span className={css.punctuation}>{open}</span>
|
|
223
|
+
{depth >= PREVIEW_DEPTH_LIMIT
|
|
224
|
+
? <span className={css.previewEllipsis}>…</span>
|
|
225
|
+
: visible.map(([key, item], index) => (
|
|
226
|
+
<span key={key}>
|
|
227
|
+
{index > 0 && <span className={css.punctuation}>, </span>}
|
|
228
|
+
{!array && (
|
|
229
|
+
<>
|
|
230
|
+
<span className={css.previewProperty}>{key}</span>
|
|
231
|
+
<span className={css.punctuation}>: </span>
|
|
232
|
+
</>
|
|
233
|
+
)}
|
|
234
|
+
{previewValue(item, depth + 1)}
|
|
235
|
+
</span>
|
|
236
|
+
))}
|
|
237
|
+
{depth < PREVIEW_DEPTH_LIMIT && entries.length > limit && (
|
|
238
|
+
<span className={css.previewEllipsis}>, …</span>
|
|
239
|
+
)}
|
|
240
|
+
<span className={css.punctuation}>{close}</span>
|
|
241
|
+
</>
|
|
242
|
+
)
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function primitiveValue(value: unknown): ReactNode {
|
|
246
|
+
if (value === null) return <span className={css.keywordValue}>null</span>
|
|
247
|
+
if (typeof value === 'string') {
|
|
248
|
+
return <span className={css.stringValue}>{JSON.stringify(value)}</span>
|
|
249
|
+
}
|
|
250
|
+
if (typeof value === 'boolean') {
|
|
251
|
+
return <span className={css.keywordValue}>{String(value)}</span>
|
|
252
|
+
}
|
|
253
|
+
if (typeof value === 'number') {
|
|
254
|
+
return <span className={css.numberValue}>{String(value)}</span>
|
|
255
|
+
}
|
|
256
|
+
if (typeof value === 'bigint') {
|
|
257
|
+
return <span className={css.numberValue}>{`${value.toString()}n`}</span>
|
|
258
|
+
}
|
|
259
|
+
if (value instanceof Date) {
|
|
260
|
+
return <span className={css.otherValue}>{value.toISOString()}</span>
|
|
261
|
+
}
|
|
262
|
+
if (typeof value === 'function') {
|
|
263
|
+
return <span className={css.otherValue}>function() {'{ }'}</span>
|
|
264
|
+
}
|
|
265
|
+
if (typeof value === 'undefined') {
|
|
266
|
+
return <span className={css.otherValue}>undefined</span>
|
|
267
|
+
}
|
|
268
|
+
return <span className={css.otherValue}>{(value as symbol).toString()}</span>
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function fieldText(field: string): string {
|
|
272
|
+
return field === '' ? '""' : field
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
function pathId(path: JsonPath): string {
|
|
276
|
+
return path.map(part => (
|
|
277
|
+
typeof part === 'number' ? `n${String(part)}` : `s${String(part.length)}:${part}`
|
|
278
|
+
)).join('/')
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function claimFocus(button: HTMLElement): void {
|
|
282
|
+
button.focus()
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function moveFocus(button: HTMLElement, direction: -1 | 1): void {
|
|
286
|
+
const tree = button.closest<HTMLElement>('[role="tree"]')
|
|
287
|
+
/* v8 ignore next -- JsonTree attaches expander handlers only beneath its owning role=tree. */
|
|
288
|
+
if (tree === null) return
|
|
289
|
+
const expanders = Array.from(tree.querySelectorAll<HTMLElement>('[data-json-expander]'))
|
|
290
|
+
const current = expanders.indexOf(button)
|
|
291
|
+
/* v8 ignore next -- the current expander is a member of the queried non-empty set. */
|
|
292
|
+
if (current < 0 || expanders.length === 0) return
|
|
293
|
+
const next = (current + direction + expanders.length) % expanders.length
|
|
294
|
+
const nextExpander = expanders[next]
|
|
295
|
+
/* v8 ignore next -- modulo over the non-empty expander set always resolves a member. */
|
|
296
|
+
if (nextExpander !== undefined) claimFocus(nextExpander)
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
function NodeField({
|
|
300
|
+
field,
|
|
301
|
+
expandable,
|
|
302
|
+
onToggle,
|
|
303
|
+
}: {
|
|
304
|
+
field: string | undefined
|
|
305
|
+
expandable: boolean
|
|
306
|
+
onToggle: () => void
|
|
307
|
+
}) {
|
|
308
|
+
if (field === undefined) return null
|
|
309
|
+
return (
|
|
310
|
+
<span
|
|
311
|
+
className={clsx(css.label, expandable && css.clickableLabel)}
|
|
312
|
+
onClick={expandable ? onToggle : undefined}
|
|
313
|
+
>
|
|
314
|
+
{fieldText(field)}:
|
|
315
|
+
</span>
|
|
316
|
+
)
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
interface JsonTreeNodeProps {
|
|
320
|
+
collapsedStringLines: number
|
|
321
|
+
stringWrapping: JsonTreeProps['stringWrapping']
|
|
322
|
+
field?: string
|
|
323
|
+
initialExpanded: boolean
|
|
324
|
+
labels: JsonTreeLabels
|
|
325
|
+
lastElement: boolean
|
|
326
|
+
onClaimTabStop: (id: string) => void
|
|
327
|
+
onRowHover: (row: HTMLElement, target: RowTarget) => void
|
|
328
|
+
path: JsonPath
|
|
329
|
+
renderCopy: ((target: RowTarget, persistent?: boolean) => ReactNode) | undefined
|
|
330
|
+
tabStopId: string | null
|
|
331
|
+
value: unknown
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
function JsonString({
|
|
335
|
+
collapsedStringLines,
|
|
336
|
+
stringWrapping,
|
|
337
|
+
field,
|
|
338
|
+
labels,
|
|
339
|
+
lastElement,
|
|
340
|
+
renderCopy,
|
|
341
|
+
value,
|
|
342
|
+
}: {
|
|
343
|
+
collapsedStringLines: number
|
|
344
|
+
stringWrapping: JsonTreeProps['stringWrapping']
|
|
345
|
+
field: string | undefined
|
|
346
|
+
labels: JsonTreeLabels
|
|
347
|
+
lastElement: boolean
|
|
348
|
+
renderCopy: ((persistent?: boolean) => ReactNode) | undefined
|
|
349
|
+
value: string
|
|
350
|
+
}) {
|
|
351
|
+
const contentsId = useId()
|
|
352
|
+
const contentRef = useRef<HTMLSpanElement>(null)
|
|
353
|
+
const rawRef = useRef<HTMLPreElement>(null)
|
|
354
|
+
const [expanded, setExpanded] = useState(false)
|
|
355
|
+
const [wrapped, setWrapped] = useState(false)
|
|
356
|
+
const [truncated, setTruncated] = useState(false)
|
|
357
|
+
|
|
358
|
+
useLayoutEffect(() => {
|
|
359
|
+
if (expanded) return
|
|
360
|
+
const content = contentRef.current as HTMLSpanElement
|
|
361
|
+
const measure = () => {
|
|
362
|
+
const lineHeight = Number.parseFloat(getComputedStyle(content).lineHeight)
|
|
363
|
+
setTruncated(content.scrollHeight > lineHeight * collapsedStringLines)
|
|
364
|
+
}
|
|
365
|
+
measure()
|
|
366
|
+
if (typeof ResizeObserver === 'undefined') return
|
|
367
|
+
const observer = new ResizeObserver(measure)
|
|
368
|
+
observer.observe(content)
|
|
369
|
+
return () => { observer.disconnect() }
|
|
370
|
+
}, [collapsedStringLines, expanded, field, lastElement, value])
|
|
371
|
+
|
|
372
|
+
useLayoutEffect(() => {
|
|
373
|
+
if (!expanded) return
|
|
374
|
+
const raw = rawRef.current as HTMLPreElement
|
|
375
|
+
// Keep raw text within the window and clipping ancestors outside the tree.
|
|
376
|
+
// Capture scrolling because an ancestor can move the string without resizing it.
|
|
377
|
+
const clips: HTMLElement[] = []
|
|
378
|
+
const tree = raw.closest<HTMLElement>(`.${css.root}`) as HTMLElement
|
|
379
|
+
for (let parent = tree.parentElement; parent !== null; parent = parent.parentElement) {
|
|
380
|
+
if (/auto|scroll|hidden|clip/.test(getComputedStyle(parent).overflowY)) clips.push(parent)
|
|
381
|
+
}
|
|
382
|
+
const measure = () => {
|
|
383
|
+
let top = 0
|
|
384
|
+
let bottom = window.innerHeight
|
|
385
|
+
for (const clip of clips) {
|
|
386
|
+
const rect = clip.getBoundingClientRect()
|
|
387
|
+
const style = getComputedStyle(clip)
|
|
388
|
+
top = Math.max(top, rect.top + clip.clientTop)
|
|
389
|
+
bottom = Math.min(bottom, rect.top + clip.clientTop + clip.clientHeight
|
|
390
|
+
- Number.parseFloat(style.paddingBottom))
|
|
391
|
+
}
|
|
392
|
+
const available = bottom - Math.max(top, raw.getBoundingClientRect().top)
|
|
393
|
+
raw.style.maxHeight = `${Math.max(16, available - 4)}px`
|
|
394
|
+
}
|
|
395
|
+
measure()
|
|
396
|
+
const observer = typeof ResizeObserver === 'undefined' ? undefined : new ResizeObserver(measure)
|
|
397
|
+
observer?.observe(raw)
|
|
398
|
+
for (const clip of clips) observer?.observe(clip)
|
|
399
|
+
window.addEventListener('resize', measure)
|
|
400
|
+
window.addEventListener('scroll', measure, true)
|
|
401
|
+
return () => {
|
|
402
|
+
observer?.disconnect()
|
|
403
|
+
window.removeEventListener('resize', measure)
|
|
404
|
+
window.removeEventListener('scroll', measure, true)
|
|
405
|
+
}
|
|
406
|
+
}, [expanded, value])
|
|
407
|
+
|
|
408
|
+
if (expanded) {
|
|
409
|
+
const fieldId = `${contentsId}-field`
|
|
410
|
+
return (
|
|
411
|
+
<div className={css.stringField} data-expanded>
|
|
412
|
+
{field !== undefined && <span id={fieldId} className={css.label}>{fieldText(field)}:</span>}
|
|
413
|
+
<pre
|
|
414
|
+
ref={rawRef}
|
|
415
|
+
id={contentsId}
|
|
416
|
+
className={css.stringRaw}
|
|
417
|
+
data-wrap={wrapped}
|
|
418
|
+
tabIndex={0}
|
|
419
|
+
aria-labelledby={field === undefined ? undefined : fieldId}
|
|
420
|
+
>
|
|
421
|
+
{value}
|
|
422
|
+
</pre>
|
|
423
|
+
{!lastElement && <span className={css.punctuation}>,</span>}
|
|
424
|
+
<div className={css.stringActions}>
|
|
425
|
+
{stringWrapping !== undefined && (
|
|
426
|
+
<button
|
|
427
|
+
type="button"
|
|
428
|
+
className={css.actionButton}
|
|
429
|
+
aria-label={stringWrapping.label}
|
|
430
|
+
title={stringWrapping.label}
|
|
431
|
+
aria-pressed={wrapped}
|
|
432
|
+
aria-controls={contentsId}
|
|
433
|
+
onClick={() => {
|
|
434
|
+
const next = !wrapped
|
|
435
|
+
setWrapped(next)
|
|
436
|
+
stringWrapping.setDefault(next)
|
|
437
|
+
}}
|
|
438
|
+
>
|
|
439
|
+
<IconWrapLinesOutlineRegular size={12} />
|
|
440
|
+
</button>
|
|
441
|
+
)}
|
|
442
|
+
<button
|
|
443
|
+
type="button"
|
|
444
|
+
className={css.actionButton}
|
|
445
|
+
aria-label={labels.collapseNode}
|
|
446
|
+
title={labels.collapseNode}
|
|
447
|
+
aria-expanded
|
|
448
|
+
aria-controls={contentsId}
|
|
449
|
+
onClick={() => { setExpanded(false) }}
|
|
450
|
+
>
|
|
451
|
+
<svg
|
|
452
|
+
width="12"
|
|
453
|
+
height="12"
|
|
454
|
+
viewBox="0 0 16 16"
|
|
455
|
+
fill="none"
|
|
456
|
+
stroke="currentColor"
|
|
457
|
+
aria-hidden="true"
|
|
458
|
+
>
|
|
459
|
+
<path d="M9.5 1.5V6.5H14.5M1.5 9.5H6.5V14.5" />
|
|
460
|
+
</svg>
|
|
461
|
+
</button>
|
|
462
|
+
{renderCopy?.(true)}
|
|
463
|
+
</div>
|
|
464
|
+
</div>
|
|
465
|
+
)
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
return (
|
|
469
|
+
<>
|
|
470
|
+
{renderCopy?.()}
|
|
471
|
+
<span className={css.stringField} data-expanded={expanded}>
|
|
472
|
+
<span ref={contentRef} id={contentsId} className={css.stringText}>
|
|
473
|
+
{truncated && (
|
|
474
|
+
<span className={css.stringToggleSlot}>
|
|
475
|
+
<button
|
|
476
|
+
type="button"
|
|
477
|
+
className={css.stringToggle}
|
|
478
|
+
aria-label={labels.expandNode}
|
|
479
|
+
aria-expanded={false}
|
|
480
|
+
aria-controls={contentsId}
|
|
481
|
+
onClick={() => {
|
|
482
|
+
setWrapped(stringWrapping?.getDefault() ?? false)
|
|
483
|
+
setExpanded(true)
|
|
484
|
+
}}
|
|
485
|
+
>
|
|
486
|
+
<span aria-hidden="true">…</span>{labels.expandNode}
|
|
487
|
+
</button>
|
|
488
|
+
</span>
|
|
489
|
+
)}
|
|
490
|
+
{field !== undefined && <span className={css.label}>{fieldText(field)}:</span>}
|
|
491
|
+
{primitiveValue(value)}
|
|
492
|
+
{!lastElement && <span className={css.punctuation}>,</span>}
|
|
493
|
+
</span>
|
|
494
|
+
</span>
|
|
495
|
+
</>
|
|
496
|
+
)
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
function JsonTreeNode({
|
|
500
|
+
collapsedStringLines,
|
|
501
|
+
stringWrapping,
|
|
502
|
+
field,
|
|
503
|
+
initialExpanded,
|
|
504
|
+
labels,
|
|
505
|
+
lastElement,
|
|
506
|
+
onClaimTabStop,
|
|
507
|
+
onRowHover,
|
|
508
|
+
path,
|
|
509
|
+
renderCopy,
|
|
510
|
+
tabStopId,
|
|
511
|
+
value,
|
|
512
|
+
}: JsonTreeNodeProps) {
|
|
513
|
+
const contentsId = useId()
|
|
514
|
+
const expanderRef = useRef<HTMLSpanElement>(null)
|
|
515
|
+
const [expanded, setExpanded] = useState(initialExpanded)
|
|
516
|
+
const nodeId = pathId(path)
|
|
517
|
+
const container = isExpandableValue(value)
|
|
518
|
+
const entries = container ? entriesOf(value) : []
|
|
519
|
+
const expandable = entries.length > 0
|
|
520
|
+
|
|
521
|
+
const toggle = () => {
|
|
522
|
+
setExpanded(current => !current)
|
|
523
|
+
claimFocus(expanderRef.current as HTMLSpanElement)
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
const onExpanderKeyDown = (event: ReactKeyboardEvent<HTMLSpanElement>) => {
|
|
527
|
+
if (event.key === 'ArrowRight' || event.key === 'ArrowLeft') {
|
|
528
|
+
event.preventDefault()
|
|
529
|
+
setExpanded(event.key === 'ArrowRight')
|
|
530
|
+
return
|
|
531
|
+
}
|
|
532
|
+
if (event.key === 'ArrowUp' || event.key === 'ArrowDown') {
|
|
533
|
+
event.preventDefault()
|
|
534
|
+
moveFocus(event.currentTarget, event.key === 'ArrowUp' ? -1 : 1)
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
const row = (children: ReactNode, ariaExpanded?: boolean) => (
|
|
539
|
+
<div
|
|
540
|
+
className={css.row}
|
|
541
|
+
role="treeitem"
|
|
542
|
+
aria-expanded={ariaExpanded}
|
|
543
|
+
onMouseOver={(event) => {
|
|
544
|
+
event.stopPropagation()
|
|
545
|
+
onRowHover(event.currentTarget, { path, value })
|
|
546
|
+
}}
|
|
547
|
+
>
|
|
548
|
+
{typeof value !== 'string' && renderCopy?.({ path, value })}
|
|
549
|
+
{children}
|
|
550
|
+
</div>
|
|
551
|
+
)
|
|
552
|
+
|
|
553
|
+
if (typeof value === 'string') {
|
|
554
|
+
return row(
|
|
555
|
+
<JsonString
|
|
556
|
+
collapsedStringLines={collapsedStringLines}
|
|
557
|
+
stringWrapping={stringWrapping}
|
|
558
|
+
field={field}
|
|
559
|
+
value={value}
|
|
560
|
+
labels={labels}
|
|
561
|
+
lastElement={lastElement}
|
|
562
|
+
renderCopy={renderCopy === undefined ? undefined : persistent => renderCopy({ path, value }, persistent)}
|
|
563
|
+
/>,
|
|
564
|
+
)
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
if (!container) {
|
|
568
|
+
return row((
|
|
569
|
+
<>
|
|
570
|
+
<NodeField field={field} expandable={false} onToggle={toggle} />
|
|
571
|
+
{primitiveValue(value)}
|
|
572
|
+
{!lastElement && <span className={css.punctuation}>,</span>}
|
|
573
|
+
</>
|
|
574
|
+
))
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
const [open, close] = bracketOf(value)
|
|
578
|
+
if (!expandable) {
|
|
579
|
+
return row((
|
|
580
|
+
<>
|
|
581
|
+
<NodeField field={field} expandable={false} onToggle={toggle} />
|
|
582
|
+
<span className={css.punctuation}>{open}</span>
|
|
583
|
+
<span className={css.punctuation}>{close}</span>
|
|
584
|
+
{!lastElement && <span className={css.punctuation}>,</span>}
|
|
585
|
+
</>
|
|
586
|
+
))
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
return row((
|
|
590
|
+
<>
|
|
591
|
+
<span
|
|
592
|
+
ref={expanderRef}
|
|
593
|
+
className={clsx(css.expander, expanded ? css.collapseIcon : css.expandIcon)}
|
|
594
|
+
data-json-expander
|
|
595
|
+
role="button"
|
|
596
|
+
aria-label={expanded ? labels.collapseNode : labels.expandNode}
|
|
597
|
+
aria-expanded={expanded}
|
|
598
|
+
aria-controls={expanded ? contentsId : undefined}
|
|
599
|
+
tabIndex={tabStopId === nodeId ? 0 : -1}
|
|
600
|
+
onFocus={() => { onClaimTabStop(nodeId) }}
|
|
601
|
+
onClick={toggle}
|
|
602
|
+
onKeyDown={onExpanderKeyDown}
|
|
603
|
+
/>
|
|
604
|
+
<span className={css.summary}>
|
|
605
|
+
<NodeField field={field} expandable onToggle={toggle} />
|
|
606
|
+
<span className={css.preview}>{previewValue(value, 0)}</span>
|
|
607
|
+
{!lastElement && <span className={css.punctuation}>,</span>}
|
|
608
|
+
</span>
|
|
609
|
+
{expanded && (
|
|
610
|
+
<ul id={contentsId} role="group" className={css.children}>
|
|
611
|
+
{entries.map(([key, item], index) => (
|
|
612
|
+
<JsonTreeNode
|
|
613
|
+
key={key}
|
|
614
|
+
collapsedStringLines={collapsedStringLines}
|
|
615
|
+
stringWrapping={stringWrapping}
|
|
616
|
+
field={key}
|
|
617
|
+
value={item}
|
|
618
|
+
path={[...path, Array.isArray(value) ? index : key]}
|
|
619
|
+
labels={labels}
|
|
620
|
+
lastElement={index === entries.length - 1}
|
|
621
|
+
initialExpanded={false}
|
|
622
|
+
tabStopId={tabStopId}
|
|
623
|
+
onClaimTabStop={onClaimTabStop}
|
|
624
|
+
onRowHover={onRowHover}
|
|
625
|
+
renderCopy={renderCopy}
|
|
626
|
+
/>
|
|
627
|
+
))}
|
|
628
|
+
</ul>
|
|
629
|
+
)}
|
|
630
|
+
</>
|
|
631
|
+
), expanded)
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
function formattedPath(path: JsonPath): string {
|
|
635
|
+
return path.reduce<string>((result, part) => {
|
|
636
|
+
if (typeof part === 'number') return `${result}[${String(part)}]`
|
|
637
|
+
return /^[A-Za-z_$][\w$]*$/.test(part)
|
|
638
|
+
? `${result}.${part}`
|
|
639
|
+
: `${result}[${JSON.stringify(part)}]`
|
|
640
|
+
}, '$')
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
function copyText(target: RowTarget, mode: CopyMode): string {
|
|
644
|
+
if (mode === 'path') return formattedPath(target.path)
|
|
645
|
+
if (mode === 'prettyJson') return JSON.stringify(target.value, null, 2)
|
|
646
|
+
if (mode === 'json') return JSON.stringify(target.value)
|
|
647
|
+
if (typeof target.value === 'string') return target.value
|
|
648
|
+
if (typeof target.value === 'undefined') return 'undefined'
|
|
649
|
+
if (typeof target.value === 'bigint') return target.value.toString()
|
|
650
|
+
if (typeof target.value === 'symbol') return target.value.description ?? 'Symbol'
|
|
651
|
+
if (typeof target.value === 'function') return target.value.name || 'Function'
|
|
652
|
+
return JSON.stringify(target.value)
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
/** Props for the read-only, token-themed JSON tree. */
|
|
656
|
+
export interface JsonTreeProps {
|
|
657
|
+
/** Parsed JSON object or array. */
|
|
658
|
+
data: object | unknown[]
|
|
659
|
+
/** Accessible label for the tree. */
|
|
660
|
+
label: string
|
|
661
|
+
/** Optional positioning class owned by the caller. */
|
|
662
|
+
className?: string | undefined
|
|
663
|
+
/** Maximum visible lines per collapsed string; defaults to 3. */
|
|
664
|
+
collapsedStringLines?: number
|
|
665
|
+
/** Optional wrap toggle; each expansion reads the shared default without changing other open strings. */
|
|
666
|
+
stringWrapping?: {
|
|
667
|
+
/** Localized label for the wrapping toggle. */
|
|
668
|
+
label: string
|
|
669
|
+
/** Read the wrapping preference when a string is expanded. @returns Whether to wrap long lines. */
|
|
670
|
+
getDefault: () => boolean
|
|
671
|
+
/** Remember a user toggle for future expansions. @param wrapped - Whether to wrap long lines. */
|
|
672
|
+
setDefault: (wrapped: boolean) => void
|
|
673
|
+
} | undefined
|
|
674
|
+
/** Whether JSON rows expose copy actions. */
|
|
675
|
+
copyable?: boolean
|
|
676
|
+
/** Whether the top-level object or array is always expanded. */
|
|
677
|
+
expandTopLevel?: boolean
|
|
678
|
+
/** Localized display copy supplied by the owning render site. */
|
|
679
|
+
labels: JsonTreeLabels
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
/**
|
|
683
|
+
* Render parsed JSON as a compact, keyboard-accessible inspector tree.
|
|
684
|
+
* @param props - Parsed data, accessible label, and display options.
|
|
685
|
+
* @returns A read-only JSON tree with an optionally fixed-open top level.
|
|
686
|
+
*/
|
|
687
|
+
export function JsonTree({
|
|
688
|
+
data,
|
|
689
|
+
label,
|
|
690
|
+
className,
|
|
691
|
+
collapsedStringLines = 3,
|
|
692
|
+
stringWrapping,
|
|
693
|
+
copyable = true,
|
|
694
|
+
expandTopLevel = true,
|
|
695
|
+
labels,
|
|
696
|
+
}: JsonTreeProps) {
|
|
697
|
+
const rootEntries = entriesOf(data)
|
|
698
|
+
const firstExpandableIndex = rootEntries.findIndex(([, value]) => (
|
|
699
|
+
isExpandableValue(value) && entriesOf(value).length > 0
|
|
700
|
+
))
|
|
701
|
+
const firstExpandableEntry = rootEntries[firstExpandableIndex]
|
|
702
|
+
const initialTabStopId = expandTopLevel
|
|
703
|
+
? firstExpandableEntry === undefined
|
|
704
|
+
? null
|
|
705
|
+
: pathId([Array.isArray(data) ? firstExpandableIndex : firstExpandableEntry[0]])
|
|
706
|
+
: isExpandableValue(data) && rootEntries.length > 0 ? pathId([]) : null
|
|
707
|
+
const activeRowRef = useRef<HTMLElement>()
|
|
708
|
+
const resetTimer = useRef<ReturnType<typeof setTimeout>>()
|
|
709
|
+
const copySequence = useRef(0)
|
|
710
|
+
const [copyStore] = useState(createCopyStore)
|
|
711
|
+
const [tabStopId, setTabStopId] = useState<string | null>(initialTabStopId)
|
|
712
|
+
|
|
713
|
+
const setActiveRow = (row: HTMLElement | undefined) => {
|
|
714
|
+
activeRowRef.current?.removeAttribute('data-json-copy-active')
|
|
715
|
+
activeRowRef.current = row
|
|
716
|
+
row?.setAttribute('data-json-copy-active', '')
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
const clearCopyTarget = () => {
|
|
720
|
+
copySequence.current += 1
|
|
721
|
+
if (resetTimer.current !== undefined) clearTimeout(resetTimer.current)
|
|
722
|
+
setActiveRow(undefined)
|
|
723
|
+
copyStore.set(undefined)
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
useEffect(() => () => {
|
|
727
|
+
copySequence.current += 1
|
|
728
|
+
if (resetTimer.current !== undefined) clearTimeout(resetTimer.current)
|
|
729
|
+
activeRowRef.current?.removeAttribute('data-json-copy-active')
|
|
730
|
+
}, [])
|
|
731
|
+
|
|
732
|
+
useEffect(() => {
|
|
733
|
+
clearCopyTarget()
|
|
734
|
+
setTabStopId(initialTabStopId)
|
|
735
|
+
}, [data, expandTopLevel, initialTabStopId])
|
|
736
|
+
|
|
737
|
+
const handleRowHover = (row: HTMLElement, target: RowTarget) => {
|
|
738
|
+
if (!copyable || copyStore.get()?.menuOpen) return
|
|
739
|
+
if (activeRowRef.current === row) return
|
|
740
|
+
setActiveRow(row)
|
|
741
|
+
copyStore.set({ id: pathId(target.path), target, state: 'idle', menuOpen: false })
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
const handleRootMouseOver = (event: ReactMouseEvent<HTMLDivElement>) => {
|
|
745
|
+
if (!copyable || copyStore.get()?.menuOpen) return
|
|
746
|
+
/* v8 ignore next -- browser mouse events delivered through React target an Element. */
|
|
747
|
+
if (!(event.target instanceof Element)) return
|
|
748
|
+
if (event.target.closest('[data-json-copy-button]') === null) clearCopyTarget()
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
const copy = async (target: RowTarget, mode: CopyMode) => {
|
|
752
|
+
const sequence = ++copySequence.current
|
|
753
|
+
const snapshot: CopySnapshot = {
|
|
754
|
+
id: pathId(target.path), target, state: 'idle', menuOpen: false,
|
|
755
|
+
}
|
|
756
|
+
copyStore.set(snapshot)
|
|
757
|
+
let state: CopySnapshot['state']
|
|
758
|
+
try {
|
|
759
|
+
await navigator.clipboard.writeText(copyText(target, mode))
|
|
760
|
+
state = 'copied'
|
|
761
|
+
} catch {
|
|
762
|
+
state = 'failed'
|
|
763
|
+
}
|
|
764
|
+
const current = copyStore.get()
|
|
765
|
+
if (sequence !== copySequence.current || current?.target !== target) return
|
|
766
|
+
copyStore.set({ ...current, state })
|
|
767
|
+
if (resetTimer.current !== undefined) clearTimeout(resetTimer.current)
|
|
768
|
+
resetTimer.current = setTimeout(() => {
|
|
769
|
+
const current = copyStore.get()
|
|
770
|
+
if (current?.target === target) copyStore.set({ ...current, state: 'idle' })
|
|
771
|
+
}, 1_500)
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
const [rootOpen, rootClose] = bracketOf(data)
|
|
775
|
+
const renderCopy = copyable ? (target: RowTarget, persistent = false) => (
|
|
776
|
+
<JsonCopyAction store={copyStore} target={target} persistent={persistent} labels={labels}
|
|
777
|
+
onCopy={copy} onClose={clearCopyTarget} />
|
|
778
|
+
) : undefined
|
|
779
|
+
|
|
780
|
+
return (
|
|
781
|
+
<div
|
|
782
|
+
className={clsx(css.root, className)}
|
|
783
|
+
style={{ '--json-tree-collapsed-lines': collapsedStringLines } as CSSProperties}
|
|
784
|
+
onMouseOver={handleRootMouseOver}
|
|
785
|
+
onMouseLeave={() => {
|
|
786
|
+
if (!copyStore.get()?.menuOpen) clearCopyTarget()
|
|
787
|
+
}}
|
|
788
|
+
>
|
|
789
|
+
{expandTopLevel
|
|
790
|
+
? (
|
|
791
|
+
<div className={css.expandedTopLevel}>
|
|
792
|
+
<div
|
|
793
|
+
className={clsx(css.row, css.topLevelBracket)}
|
|
794
|
+
data-json-root-row
|
|
795
|
+
onMouseOver={(event) => {
|
|
796
|
+
event.stopPropagation()
|
|
797
|
+
handleRowHover(event.currentTarget, { path: [], value: data })
|
|
798
|
+
}}
|
|
799
|
+
>
|
|
800
|
+
{renderCopy?.({ path: [], value: data })}
|
|
801
|
+
<span className={css.punctuation}>{rootOpen}</span>
|
|
802
|
+
</div>
|
|
803
|
+
<div
|
|
804
|
+
aria-label={label}
|
|
805
|
+
className={clsx(css.container, css.expandedTopLevelContainer)}
|
|
806
|
+
role="tree"
|
|
807
|
+
>
|
|
808
|
+
{rootEntries.map(([key, value], index) => (
|
|
809
|
+
<JsonTreeNode
|
|
810
|
+
key={key}
|
|
811
|
+
collapsedStringLines={collapsedStringLines}
|
|
812
|
+
stringWrapping={stringWrapping}
|
|
813
|
+
field={key}
|
|
814
|
+
value={value}
|
|
815
|
+
path={[Array.isArray(data) ? index : key]}
|
|
816
|
+
labels={labels}
|
|
817
|
+
lastElement={index === rootEntries.length - 1}
|
|
818
|
+
initialExpanded={false}
|
|
819
|
+
tabStopId={tabStopId}
|
|
820
|
+
onClaimTabStop={setTabStopId}
|
|
821
|
+
onRowHover={handleRowHover}
|
|
822
|
+
renderCopy={renderCopy}
|
|
823
|
+
/>
|
|
824
|
+
))}
|
|
825
|
+
</div>
|
|
826
|
+
<div className={clsx(css.row, css.topLevelBracket)}>
|
|
827
|
+
<span className={css.punctuation}>{rootClose}</span>
|
|
828
|
+
</div>
|
|
829
|
+
</div>
|
|
830
|
+
)
|
|
831
|
+
: (
|
|
832
|
+
<div aria-label={label} className={css.container} role="tree">
|
|
833
|
+
<JsonTreeNode
|
|
834
|
+
collapsedStringLines={collapsedStringLines}
|
|
835
|
+
stringWrapping={stringWrapping}
|
|
836
|
+
value={data}
|
|
837
|
+
path={[]}
|
|
838
|
+
labels={labels}
|
|
839
|
+
lastElement
|
|
840
|
+
initialExpanded
|
|
841
|
+
tabStopId={tabStopId}
|
|
842
|
+
onClaimTabStop={setTabStopId}
|
|
843
|
+
onRowHover={handleRowHover}
|
|
844
|
+
renderCopy={renderCopy}
|
|
845
|
+
/>
|
|
846
|
+
</div>
|
|
847
|
+
)}
|
|
848
|
+
</div>
|
|
849
|
+
)
|
|
850
|
+
}
|