@seanyeh77/ui 0.41.0 → 0.42.0

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.
Files changed (361) hide show
  1. package/README.md +32 -0
  2. package/dist/announce.d.ts +3 -0
  3. package/dist/announce.js +35 -0
  4. package/dist/announce.js.map +1 -0
  5. package/dist/bundle.js +1 -1
  6. package/dist/bundle.js.map +1 -1
  7. package/dist/components/AccentPicker/AccentPicker.d.ts +12 -0
  8. package/dist/components/AccentPicker/AccentPicker.js +96 -0
  9. package/dist/components/AccentPicker/AccentPicker.js.map +1 -0
  10. package/dist/components/ActionBar/ActionBar.d.ts +27 -0
  11. package/dist/components/ActionBar/ActionBar.js +62 -0
  12. package/dist/components/ActionBar/ActionBar.js.map +1 -0
  13. package/dist/components/AiMark/AiMark.d.ts +10 -0
  14. package/dist/components/AiMark/AiMark.js +21 -0
  15. package/dist/components/AiMark/AiMark.js.map +1 -0
  16. package/dist/components/Alert/Alert.d.ts +17 -0
  17. package/dist/components/Alert/Alert.js +56 -0
  18. package/dist/components/Alert/Alert.js.map +1 -0
  19. package/dist/components/AppShell/AppShell.d.ts +36 -0
  20. package/dist/components/AppShell/AppShell.js +195 -0
  21. package/dist/components/AppShell/AppShell.js.map +1 -0
  22. package/dist/components/Badge/Badge.d.ts +12 -0
  23. package/dist/components/Badge/Badge.js +58 -0
  24. package/dist/components/Badge/Badge.js.map +1 -0
  25. package/dist/components/BarChart/BarChart.d.ts +19 -0
  26. package/dist/components/BarChart/BarChart.js +78 -0
  27. package/dist/components/BarChart/BarChart.js.map +1 -0
  28. package/dist/components/Breadcrumb/Breadcrumb.d.ts +11 -0
  29. package/dist/components/Breadcrumb/Breadcrumb.js +29 -0
  30. package/dist/components/Breadcrumb/Breadcrumb.js.map +1 -0
  31. package/dist/components/Button/Button.d.ts +39 -0
  32. package/dist/components/Button/Button.js +54 -0
  33. package/dist/components/Button/Button.js.map +1 -0
  34. package/dist/components/Calendar/Calendar.d.ts +18 -0
  35. package/dist/components/Calendar/Calendar.js +125 -0
  36. package/dist/components/Calendar/Calendar.js.map +1 -0
  37. package/dist/components/Card/Card.d.ts +14 -0
  38. package/dist/components/Card/Card.js +33 -0
  39. package/dist/components/Card/Card.js.map +1 -0
  40. package/dist/components/Checkbox/Checkbox.d.ts +14 -0
  41. package/dist/components/Checkbox/Checkbox.js +27 -0
  42. package/dist/components/Checkbox/Checkbox.js.map +1 -0
  43. package/dist/components/CodeView/CodeView.d.ts +111 -0
  44. package/dist/components/CodeView/CodeView.js +790 -0
  45. package/dist/components/CodeView/CodeView.js.map +1 -0
  46. package/dist/components/CodeView/tokens.d.ts +5 -0
  47. package/dist/components/CodeView/tokens.js +70 -0
  48. package/dist/components/CodeView/tokens.js.map +1 -0
  49. package/dist/components/CommandPalette/CommandPalette.d.ts +52 -0
  50. package/dist/components/CommandPalette/CommandPalette.js +242 -0
  51. package/dist/components/CommandPalette/CommandPalette.js.map +1 -0
  52. package/dist/components/ConsoleLayout/ConsoleLayout.d.ts +44 -0
  53. package/dist/components/ConsoleLayout/ConsoleLayout.js +119 -0
  54. package/dist/components/ConsoleLayout/ConsoleLayout.js.map +1 -0
  55. package/dist/components/DangerZone/DangerZone.d.ts +16 -0
  56. package/dist/components/DangerZone/DangerZone.js +28 -0
  57. package/dist/components/DangerZone/DangerZone.js.map +1 -0
  58. package/dist/components/DataTable/DataTable.d.ts +99 -0
  59. package/dist/components/DataTable/DataTable.js +1267 -0
  60. package/dist/components/DataTable/DataTable.js.map +1 -0
  61. package/dist/components/DataTable/SortButton.js +222 -0
  62. package/dist/components/DataTable/SortButton.js.map +1 -0
  63. package/dist/components/DataTable/filters.js +147 -0
  64. package/dist/components/DataTable/filters.js.map +1 -0
  65. package/dist/components/DataTable/parts.js +373 -0
  66. package/dist/components/DataTable/parts.js.map +1 -0
  67. package/dist/components/DataTable/types.d.ts +85 -0
  68. package/dist/components/DatePicker/DatePicker.d.ts +15 -0
  69. package/dist/components/DatePicker/DatePicker.js +104 -0
  70. package/dist/components/DatePicker/DatePicker.js.map +1 -0
  71. package/dist/components/DateRangePicker/DateRangePicker.d.ts +11 -0
  72. package/dist/components/DateRangePicker/DateRangePicker.js +88 -0
  73. package/dist/components/DateRangePicker/DateRangePicker.js.map +1 -0
  74. package/dist/components/DetailList/DetailList.d.ts +17 -0
  75. package/dist/components/DetailList/DetailList.js +18 -0
  76. package/dist/components/DetailList/DetailList.js.map +1 -0
  77. package/dist/components/Dialog/Dialog.d.ts +30 -0
  78. package/dist/components/Dialog/Dialog.js +87 -0
  79. package/dist/components/Dialog/Dialog.js.map +1 -0
  80. package/dist/components/Drawer/Drawer.d.ts +20 -0
  81. package/dist/components/Drawer/Drawer.js +64 -0
  82. package/dist/components/Drawer/Drawer.js.map +1 -0
  83. package/dist/components/EmptyState/EmptyState.d.ts +15 -0
  84. package/dist/components/EmptyState/EmptyState.js +34 -0
  85. package/dist/components/EmptyState/EmptyState.js.map +1 -0
  86. package/dist/components/ErrorPage/ErrorPage.d.ts +14 -0
  87. package/dist/components/ErrorPage/ErrorPage.js +47 -0
  88. package/dist/components/ErrorPage/ErrorPage.js.map +1 -0
  89. package/dist/components/EventTimeline/EventTimeline.d.ts +19 -0
  90. package/dist/components/EventTimeline/EventTimeline.js +63 -0
  91. package/dist/components/EventTimeline/EventTimeline.js.map +1 -0
  92. package/dist/components/FileUpload/FileUpload.d.ts +27 -0
  93. package/dist/components/FileUpload/FileUpload.js +121 -0
  94. package/dist/components/FileUpload/FileUpload.js.map +1 -0
  95. package/dist/components/FormGrid/FormGrid.d.ts +7 -0
  96. package/dist/components/FormGrid/FormGrid.js +14 -0
  97. package/dist/components/FormGrid/FormGrid.js.map +1 -0
  98. package/dist/components/Graph/Graph.d.ts +18 -0
  99. package/dist/components/Graph/Graph.js +97 -0
  100. package/dist/components/Graph/Graph.js.map +1 -0
  101. package/dist/components/Grid/Grid.d.ts +16 -0
  102. package/dist/components/Grid/Grid.js +21 -0
  103. package/dist/components/Grid/Grid.js.map +1 -0
  104. package/dist/components/IconButton/IconButton.d.ts +32 -0
  105. package/dist/components/IconButton/IconButton.js +104 -0
  106. package/dist/components/IconButton/IconButton.js.map +1 -0
  107. package/dist/components/InlineCode/InlineCode.d.ts +46 -0
  108. package/dist/components/InlineCode/InlineCode.js +71 -0
  109. package/dist/components/InlineCode/InlineCode.js.map +1 -0
  110. package/dist/components/KpiCard/KpiCard.d.ts +38 -0
  111. package/dist/components/KpiCard/KpiCard.js +116 -0
  112. package/dist/components/KpiCard/KpiCard.js.map +1 -0
  113. package/dist/components/LayerGraph/LayerGraph.d.ts +117 -0
  114. package/dist/components/LayerGraph/LayerGraph.js +1644 -0
  115. package/dist/components/LayerGraph/LayerGraph.js.map +1 -0
  116. package/dist/components/LineChart/LineChart.d.ts +73 -0
  117. package/dist/components/LineChart/LineChart.js +337 -0
  118. package/dist/components/LineChart/LineChart.js.map +1 -0
  119. package/dist/components/LoadingBar/LoadingBar.d.ts +7 -0
  120. package/dist/components/LoadingBar/LoadingBar.js +18 -0
  121. package/dist/components/LoadingBar/LoadingBar.js.map +1 -0
  122. package/dist/components/LoginCard/LoginCard.d.ts +23 -0
  123. package/dist/components/LoginCard/LoginCard.js +113 -0
  124. package/dist/components/LoginCard/LoginCard.js.map +1 -0
  125. package/dist/components/Markdown/Markdown.d.ts +38 -0
  126. package/dist/components/Markdown/Markdown.js +716 -0
  127. package/dist/components/Markdown/Markdown.js.map +1 -0
  128. package/dist/components/Menu/Menu.d.ts +33 -0
  129. package/dist/components/Menu/Menu.js +75 -0
  130. package/dist/components/Menu/Menu.js.map +1 -0
  131. package/dist/components/Meter/Meter.d.ts +20 -0
  132. package/dist/components/Meter/Meter.js +120 -0
  133. package/dist/components/Meter/Meter.js.map +1 -0
  134. package/dist/components/MobileTabBar/MobileTabBar.d.ts +21 -0
  135. package/dist/components/MobileTabBar/MobileTabBar.js +47 -0
  136. package/dist/components/MobileTabBar/MobileTabBar.js.map +1 -0
  137. package/dist/components/MultiSelect/MultiSelect.d.ts +25 -0
  138. package/dist/components/MultiSelect/MultiSelect.js +116 -0
  139. package/dist/components/MultiSelect/MultiSelect.js.map +1 -0
  140. package/dist/components/NotificationBell/NotificationBell.d.ts +20 -0
  141. package/dist/components/NotificationBell/NotificationBell.js +92 -0
  142. package/dist/components/NotificationBell/NotificationBell.js.map +1 -0
  143. package/dist/components/Popover/Popover.d.ts +20 -0
  144. package/dist/components/Popover/Popover.js +48 -0
  145. package/dist/components/Popover/Popover.js.map +1 -0
  146. package/dist/components/ProgressBar/ProgressBar.d.ts +10 -0
  147. package/dist/components/ProgressBar/ProgressBar.js +38 -0
  148. package/dist/components/ProgressBar/ProgressBar.js.map +1 -0
  149. package/dist/components/ProgressRing/ProgressRing.d.ts +11 -0
  150. package/dist/components/ProgressRing/ProgressRing.js +53 -0
  151. package/dist/components/ProgressRing/ProgressRing.js.map +1 -0
  152. package/dist/components/ProjectSwitcher/ProjectSwitcher.d.ts +25 -0
  153. package/dist/components/ProjectSwitcher/ProjectSwitcher.js +97 -0
  154. package/dist/components/ProjectSwitcher/ProjectSwitcher.js.map +1 -0
  155. package/dist/components/RadioGroup/RadioGroup.d.ts +18 -0
  156. package/dist/components/RadioGroup/RadioGroup.js +63 -0
  157. package/dist/components/RadioGroup/RadioGroup.js.map +1 -0
  158. package/dist/components/SearchField/SearchField.d.ts +9 -0
  159. package/dist/components/SearchField/SearchField.js +46 -0
  160. package/dist/components/SearchField/SearchField.js.map +1 -0
  161. package/dist/components/Section/Section.d.ts +50 -0
  162. package/dist/components/Section/Section.js +220 -0
  163. package/dist/components/Section/Section.js.map +1 -0
  164. package/dist/components/SegmentedControl/SegmentedControl.d.ts +23 -0
  165. package/dist/components/SegmentedControl/SegmentedControl.js +86 -0
  166. package/dist/components/SegmentedControl/SegmentedControl.js.map +1 -0
  167. package/dist/components/Select/Select.d.ts +31 -0
  168. package/dist/components/Select/Select.js +138 -0
  169. package/dist/components/Select/Select.js.map +1 -0
  170. package/dist/components/SettingsLayout/SettingsLayout.d.ts +27 -0
  171. package/dist/components/SettingsLayout/SettingsLayout.js +69 -0
  172. package/dist/components/SettingsLayout/SettingsLayout.js.map +1 -0
  173. package/dist/components/SideNav/SideNav.d.ts +48 -0
  174. package/dist/components/SideNav/SideNav.js +220 -0
  175. package/dist/components/SideNav/SideNav.js.map +1 -0
  176. package/dist/components/Skeleton/Skeleton.d.ts +8 -0
  177. package/dist/components/Skeleton/Skeleton.js +17 -0
  178. package/dist/components/Skeleton/Skeleton.js.map +1 -0
  179. package/dist/components/SplitPane/SplitPane.d.ts +23 -0
  180. package/dist/components/SplitPane/SplitPane.js +140 -0
  181. package/dist/components/SplitPane/SplitPane.js.map +1 -0
  182. package/dist/components/StatusBand/StatusBand.d.ts +31 -0
  183. package/dist/components/StatusBand/StatusBand.js +43 -0
  184. package/dist/components/StatusBand/StatusBand.js.map +1 -0
  185. package/dist/components/Suggestion/Suggestion.d.ts +22 -0
  186. package/dist/components/Suggestion/Suggestion.js +72 -0
  187. package/dist/components/Suggestion/Suggestion.js.map +1 -0
  188. package/dist/components/Switch/Switch.d.ts +11 -0
  189. package/dist/components/Switch/Switch.js +56 -0
  190. package/dist/components/Switch/Switch.js.map +1 -0
  191. package/dist/components/Tabs/Tabs.d.ts +23 -0
  192. package/dist/components/Tabs/Tabs.js +117 -0
  193. package/dist/components/Tabs/Tabs.js.map +1 -0
  194. package/dist/components/Tag/Tag.d.ts +13 -0
  195. package/dist/components/Tag/Tag.js +22 -0
  196. package/dist/components/Tag/Tag.js.map +1 -0
  197. package/dist/components/Term/Term.d.ts +34 -0
  198. package/dist/components/Term/Term.js +138 -0
  199. package/dist/components/Term/Term.js.map +1 -0
  200. package/dist/components/TextArea/TextArea.d.ts +15 -0
  201. package/dist/components/TextArea/TextArea.js +75 -0
  202. package/dist/components/TextArea/TextArea.js.map +1 -0
  203. package/dist/components/TextField/TextField.d.ts +15 -0
  204. package/dist/components/TextField/TextField.js +43 -0
  205. package/dist/components/TextField/TextField.js.map +1 -0
  206. package/dist/components/ThemePicker/ThemePicker.d.ts +13 -0
  207. package/dist/components/ThemePicker/ThemePicker.js +66 -0
  208. package/dist/components/ThemePicker/ThemePicker.js.map +1 -0
  209. package/dist/components/TimeControl/TimeControl.d.ts +19 -0
  210. package/dist/components/TimeControl/TimeControl.js +47 -0
  211. package/dist/components/TimeControl/TimeControl.js.map +1 -0
  212. package/dist/components/Toast/Toast.d.ts +10 -0
  213. package/dist/components/Toast/Toast.js +27 -0
  214. package/dist/components/Toast/Toast.js.map +1 -0
  215. package/dist/components/Tooltip/Tooltip.d.ts +17 -0
  216. package/dist/components/Tooltip/Tooltip.js +69 -0
  217. package/dist/components/Tooltip/Tooltip.js.map +1 -0
  218. package/dist/components/TopBar/TopBar.d.ts +18 -0
  219. package/dist/components/TopBar/TopBar.js +55 -0
  220. package/dist/components/TopBar/TopBar.js.map +1 -0
  221. package/dist/components/Tour/Tour.d.ts +44 -0
  222. package/dist/components/Tour/Tour.js +440 -0
  223. package/dist/components/Tour/Tour.js.map +1 -0
  224. package/dist/components/TreeList/TreeList.d.ts +90 -0
  225. package/dist/components/TreeList/TreeList.js +588 -0
  226. package/dist/components/TreeList/TreeList.js.map +1 -0
  227. package/dist/components/TreeList/helpers.d.ts +2 -0
  228. package/dist/components/TreeList/helpers.js +104 -0
  229. package/dist/components/TreeList/helpers.js.map +1 -0
  230. package/dist/components/Workbench/Workbench.d.ts +100 -0
  231. package/dist/components/Workbench/Workbench.js +1137 -0
  232. package/dist/components/Workbench/Workbench.js.map +1 -0
  233. package/dist/components/ZoneMap/ZoneMap.d.ts +49 -0
  234. package/dist/components/ZoneMap/ZoneMap.js +89 -0
  235. package/dist/components/ZoneMap/ZoneMap.js.map +1 -0
  236. package/dist/css/AccentPicker.css +49 -0
  237. package/dist/css/ActionBar.css +36 -0
  238. package/dist/css/AiMark.css +14 -0
  239. package/dist/css/Alert.css +80 -0
  240. package/dist/css/AppShell.css +81 -0
  241. package/dist/css/Badge.css +68 -0
  242. package/dist/css/BarChart.css +66 -0
  243. package/dist/css/Breadcrumb.css +33 -0
  244. package/dist/css/Button.css +190 -0
  245. package/dist/css/Calendar.css +96 -0
  246. package/dist/css/Card.css +70 -0
  247. package/dist/css/Checkbox.css +38 -0
  248. package/dist/css/CodeView.css +302 -0
  249. package/dist/css/CommandPalette.css +232 -0
  250. package/dist/css/ConsoleLayout.css +131 -0
  251. package/dist/css/DangerZone.css +32 -0
  252. package/dist/css/DataTable.css +1195 -0
  253. package/dist/css/DatePicker.css +38 -0
  254. package/dist/css/DateRangePicker.css +70 -0
  255. package/dist/css/DetailList.css +69 -0
  256. package/dist/css/Dialog.css +3 -0
  257. package/dist/css/Drawer.css +90 -0
  258. package/dist/css/EmptyState.css +61 -0
  259. package/dist/css/ErrorPage.css +40 -0
  260. package/dist/css/EventTimeline.css +43 -0
  261. package/dist/css/FileUpload.css +92 -0
  262. package/dist/css/FormGrid.css +17 -0
  263. package/dist/css/Graph.css +99 -0
  264. package/dist/css/Grid.css +48 -0
  265. package/dist/css/IconButton.css +149 -0
  266. package/dist/css/InlineCode.css +92 -0
  267. package/dist/css/KpiCard.css +121 -0
  268. package/dist/css/LayerGraph.css +575 -0
  269. package/dist/css/LineChart.css +3 -0
  270. package/dist/css/LoadingBar.css +45 -0
  271. package/dist/css/LoginCard.css +65 -0
  272. package/dist/css/Markdown.css +266 -0
  273. package/dist/css/Menu.css +88 -0
  274. package/dist/css/Meter.css +110 -0
  275. package/dist/css/MobileTabBar.css +63 -0
  276. package/dist/css/MultiSelect.css +78 -0
  277. package/dist/css/NotificationBell.css +129 -0
  278. package/dist/css/Popover.css +57 -0
  279. package/dist/css/ProgressBar.css +42 -0
  280. package/dist/css/ProgressRing.css +19 -0
  281. package/dist/css/ProjectSwitcher.css +153 -0
  282. package/dist/css/RadioGroup.css +52 -0
  283. package/dist/css/SearchField.css +79 -0
  284. package/dist/css/Section.css +115 -0
  285. package/dist/css/SegmentedControl.css +67 -0
  286. package/dist/css/Select.css +136 -0
  287. package/dist/css/SettingsLayout.css +110 -0
  288. package/dist/css/SideNav.css +281 -0
  289. package/dist/css/Skeleton.css +19 -0
  290. package/dist/css/SplitPane.css +108 -0
  291. package/dist/css/StatusBand.css +23 -0
  292. package/dist/css/Suggestion.css +82 -0
  293. package/dist/css/Switch.css +46 -0
  294. package/dist/css/Tabs.css +91 -0
  295. package/dist/css/Tag.css +36 -0
  296. package/dist/css/Term.css +160 -0
  297. package/dist/css/TextArea.css +22 -0
  298. package/dist/css/TextField.css +2 -0
  299. package/dist/css/ThemePicker.css +70 -0
  300. package/dist/css/TimeControl.css +33 -0
  301. package/dist/css/Toast.css +47 -0
  302. package/dist/css/Tooltip.css +58 -0
  303. package/dist/css/TopBar.css +86 -0
  304. package/dist/css/Tour.css +90 -0
  305. package/dist/css/TreeList.css +532 -0
  306. package/dist/css/Workbench.css +652 -0
  307. package/dist/css/ZoneMap.css +203 -0
  308. package/dist/css/base.css +556 -0
  309. package/dist/css/end.css +273 -0
  310. package/dist/format-entry.d.ts +3 -0
  311. package/dist/format-entry.js +3 -0
  312. package/dist/format.d.ts +68 -2
  313. package/dist/format.js +237 -1
  314. package/dist/format.js.map +1 -0
  315. package/dist/i18n.d.ts +11 -0
  316. package/dist/{format-entry-CyChe8bk.js → i18n.js} +2 -235
  317. package/dist/i18n.js.map +1 -0
  318. package/dist/icons/Icon.d.ts +23 -0
  319. package/dist/icons/Icon.js +90 -0
  320. package/dist/icons/Icon.js.map +1 -0
  321. package/dist/icons/data.d.ts +150 -0
  322. package/dist/icons/data.js +256 -0
  323. package/dist/icons/data.js.map +1 -0
  324. package/dist/index.d.ts +84 -2293
  325. package/dist/index.js +79 -13348
  326. package/dist/internal/Field.js +71 -0
  327. package/dist/internal/Field.js.map +1 -0
  328. package/dist/internal/FloatingPanel.js +41 -0
  329. package/dist/internal/FloatingPanel.js.map +1 -0
  330. package/dist/internal/chart.js +131 -0
  331. package/dist/internal/chart.js.map +1 -0
  332. package/dist/internal/dev.js +21 -0
  333. package/dist/internal/dev.js.map +1 -0
  334. package/dist/internal/focus.js +94 -0
  335. package/dist/internal/focus.js.map +1 -0
  336. package/dist/internal/focusMain.d.ts +4 -0
  337. package/dist/internal/focusMain.js +22 -0
  338. package/dist/internal/focusMain.js.map +1 -0
  339. package/dist/internal/scrollHint.js +75 -0
  340. package/dist/internal/scrollHint.js.map +1 -0
  341. package/dist/internal/tableTools.js +109 -0
  342. package/dist/internal/tableTools.js.map +1 -0
  343. package/dist/internal/terms.d.ts +18 -0
  344. package/dist/internal/terms.js +64 -0
  345. package/dist/internal/terms.js.map +1 -0
  346. package/dist/internal/util.js +107 -0
  347. package/dist/internal/util.js.map +1 -0
  348. package/dist/motion.d.ts +5 -0
  349. package/dist/motion.js +29 -0
  350. package/dist/motion.js.map +1 -0
  351. package/dist/styles.css +4280 -4265
  352. package/dist/theme.d.ts +20 -0
  353. package/dist/theme.js +210 -0
  354. package/dist/theme.js.map +1 -0
  355. package/dist/tokens.css +4 -0
  356. package/docs/guidelines.md +1 -0
  357. package/docs/theme.md +49 -0
  358. package/package.json +5 -4
  359. package/dist/format-entry-CgMBtkni.d.ts +0 -79
  360. package/dist/format-entry-CyChe8bk.js.map +0 -1
  361. package/dist/index.js.map +0 -1
@@ -0,0 +1,790 @@
1
+ "use client";
2
+ import { InlineCode } from "../InlineCode/InlineCode.js";
3
+ import { announce } from "../../announce.js";
4
+ import { tr } from "../../i18n.js";
5
+ import { copyText, cx, monoNums, useNarrow } from "../../internal/util.js";
6
+ import { devWarn } from "../../internal/dev.js";
7
+ import { CHECK } from "../../icons/Icon.js";
8
+ import { IconButton } from "../IconButton/IconButton.js";
9
+ import { FloatingPanel } from "../../internal/FloatingPanel.js";
10
+ import { findTerms, termPattern, withTerms } from "../../internal/terms.js";
11
+ import { LANGUAGE_NAMES, languageOf, tokenize } from "./tokens.js";
12
+ import * as React from "react";
13
+ import { jsx, jsxs } from "react/jsx-runtime";
14
+ import { PreviewCard } from "@base-ui/react/preview-card";
15
+ // src/components/CodeView/CodeView.tsx
16
+ /** A line's indentation, said after its number: a screen reader drops leading spaces, and in Python or YAML
17
+ * the indentation is the structure (which `if` a line belongs to). Tabs and spaces are said apart. */
18
+ function indentText(line) {
19
+ const lead = /^[ \t]*/.exec(line)?.[0] ?? "";
20
+ if (!lead || lead.length === line.length) return "";
21
+ const tabs = lead.split(" ").length - 1;
22
+ const spaces = lead.length - tabs;
23
+ return tr(",") + (tabs && spaces ? tr("縮排 {a} 個 tab、{b} 格", {
24
+ a: tabs,
25
+ b: spaces
26
+ }) : tabs ? tr("縮排 {n} 個 tab", { n: tabs }) : tr("縮排 {n} 格", { n: spaces }));
27
+ }
28
+ const OPS = /(->|::|==|!=|<=|>=|&&|\|\||\+\+|--|<<|>>|=>|\+=|-=|\*=|\/=)/;
29
+ const AFTER = /([,.([{;])/;
30
+ /** A piece of code with soft break points after commas, dots and opening brackets, and operators kept together. */
31
+ const PATH_IN_CODE = /(?<![\w./-])((?:\.{1,2}\/)?(?:[\w.-]+\/)+[\w.-]+\.[A-Za-z0-9]+(?::\d+(?:-\d+)?)?|[\w.-]+\.[A-Za-z0-9]+:\d+(?:-\d+)?)(?![\w/])/g;
32
+ /** The paths in a comment or string as links (refHref, onRef); the rest stays text. */
33
+ function withPaths(text, refHref, onRef, tab) {
34
+ const out = [];
35
+ let at = 0;
36
+ for (const m of text.matchAll(PATH_IN_CODE)) {
37
+ const v = m[1];
38
+ const href = refHref ? refHref(v) || void 0 : void 0;
39
+ if (refHref ? !href : !onRef) continue;
40
+ if (m.index > at) out.push(breakable(text.slice(at, m.index)));
41
+ const click = onRef ? (e) => {
42
+ if (href && (e.metaKey || e.ctrlKey || e.shiftKey || e.button === 1)) return;
43
+ e.preventDefault();
44
+ onRef(v, e);
45
+ } : void 0;
46
+ out.push(href ? /* @__PURE__ */ jsx("a", {
47
+ className: "ui-cv-ref",
48
+ href,
49
+ onClick: click,
50
+ tabIndex: tab,
51
+ children: v
52
+ }, m.index) : /* @__PURE__ */ jsx("button", {
53
+ type: "button",
54
+ className: "ui-cv-ref",
55
+ onClick: click,
56
+ tabIndex: tab,
57
+ children: v
58
+ }, m.index));
59
+ at = m.index + v.length;
60
+ }
61
+ if (!out.length) return breakable(text);
62
+ if (at < text.length) out.push(breakable(text.slice(at)));
63
+ return out;
64
+ }
65
+ function breakable(text) {
66
+ if (!OPS.test(text) && !AFTER.test(text)) return text;
67
+ const out = [];
68
+ text.split(OPS).forEach((part, i) => {
69
+ if (!part) return;
70
+ if (i % 2) {
71
+ out.push(/* @__PURE__ */ jsx("span", {
72
+ className: "ui-cv-op",
73
+ children: part
74
+ }, i), /* @__PURE__ */ jsx("wbr", {}, i + "w"));
75
+ return;
76
+ }
77
+ part.split(AFTER).forEach((bit, k) => {
78
+ if (!bit) return;
79
+ out.push(bit);
80
+ if (k % 2) out.push(/* @__PURE__ */ jsx("wbr", {}, i + "." + k));
81
+ });
82
+ });
83
+ return out;
84
+ }
85
+ const NAME_CHAR = /[\p{L}\p{N}_$]/u;
86
+ /** UTF-16 index of a 1-based column counted in code points; -1 past the end. */
87
+ function colIndex(s, col) {
88
+ let i = 0;
89
+ for (let c = 1; c < col; c++) {
90
+ if (i >= s.length) return -1;
91
+ i += (s.codePointAt(i) ?? 0) > 65535 ? 2 : 1;
92
+ }
93
+ return i;
94
+ }
95
+ /** The first whole `text` on the line: not inside a longer name, and not the tail of a dotted one (x.runtime.deploy). */
96
+ function findName(s, text) {
97
+ for (let i = s.indexOf(text); i >= 0; i = s.indexOf(text, i + 1)) {
98
+ const before = s[i - 1] ?? "";
99
+ const after = s[i + text.length] ?? "";
100
+ if (!NAME_CHAR.test(before) && before !== "." && !NAME_CHAR.test(after)) return i;
101
+ }
102
+ return -1;
103
+ }
104
+ /** Each line's links, sorted, without overlaps; what does not match is left out with a development warning. */
105
+ function matchLinks(lines, start, links) {
106
+ const out = /* @__PURE__ */ new Map();
107
+ links?.forEach((l, i) => {
108
+ const s = lines[l.line - start];
109
+ if (s == null) {
110
+ devWarn(true, "CodeView", `第 ${l.line} 行不在程式碼裡,連結「${l.text}」沒有加上。line 和 target 一樣從 start(${start})算起。`);
111
+ return;
112
+ }
113
+ let from = -1;
114
+ if (l.col != null) {
115
+ const at = colIndex(s, l.col);
116
+ if (at >= 0 && s.startsWith(l.text, at)) from = at;
117
+ else devWarn(true, "CodeView", `第 ${l.line} 行第 ${l.col} 欄不是「${l.text}」,連結沒有加上。col 從 1 開始、以字元計;位元組或 UTF-16 位置要先換算。`);
118
+ } else {
119
+ from = findName(s, l.text);
120
+ devWarn(from < 0, "CodeView", `第 ${l.line} 行找不到完整的「${l.text}」,連結沒有加上。text 要和程式碼一字不差(整個帶點的名稱),或給 col。`);
121
+ }
122
+ if (from < 0) return;
123
+ const hit = {
124
+ from,
125
+ to: from + l.text.length,
126
+ i
127
+ };
128
+ const list = out.get(l.line) ?? [];
129
+ if (list.some((h) => h.from < hit.to && hit.from < h.to)) {
130
+ devWarn(true, "CodeView", `第 ${l.line} 行的「${l.text}」和另一個連結重疊,沒有加上。同一行出現兩次的名稱,第二個要給 col。`);
131
+ return;
132
+ }
133
+ list.push(hit);
134
+ out.set(l.line, list);
135
+ });
136
+ for (const list of out.values()) list.sort((a, b) => a.from - b.from);
137
+ return out;
138
+ }
139
+ /** True when the reader has text selected inside el. */
140
+ function selectedIn(el) {
141
+ const sel = typeof window === "undefined" ? null : window.getSelection();
142
+ if (!el || !sel || sel.isCollapsed || !sel.rangeCount) return false;
143
+ const r = sel.getRangeAt(0);
144
+ return el.contains(r.startContainer) || el.contains(r.endContainer);
145
+ }
146
+ /** The selected code as plain text, one line per line; null when the selection is not in pre. */
147
+ function selectedCode(pre) {
148
+ const sel = window.getSelection();
149
+ if (!sel || sel.isCollapsed || !sel.rangeCount) return null;
150
+ const r = sel.getRangeAt(0);
151
+ if (!pre.contains(r.commonAncestorContainer)) return null;
152
+ const frag = r.cloneContents();
153
+ frag.querySelectorAll(".ui-cv-num, .ui-visually-hidden").forEach((x) => x.remove());
154
+ const rows = [...frag.querySelectorAll("[role=\"listitem\"]")];
155
+ return rows.length ? rows.map((x) => x.textContent ?? "").join("\n") : frag.textContent ?? "";
156
+ }
157
+ /** The nearest box that scrolls el's content: el itself, a parent with overflow auto or scroll, or the page (null). */
158
+ function scroller(el) {
159
+ for (let n = el; n && n !== document.body && n !== document.documentElement; n = n.parentElement) {
160
+ const o = getComputedStyle(n).overflowY;
161
+ if ((o === "auto" || o === "scroll" || o === "overlay") && n.scrollHeight > n.clientHeight) return n;
162
+ }
163
+ return null;
164
+ }
165
+ /** Scrolls the lines first…last into view with the least movement; when they are taller than the view, the first
166
+ * line goes to the top. Nothing moves when they are already in view. */
167
+ function showRange(first, last, pre) {
168
+ const box = scroller(pre);
169
+ const view = box ? (() => {
170
+ const top = box.getBoundingClientRect().top + box.clientTop;
171
+ return {
172
+ top,
173
+ bottom: top + box.clientHeight
174
+ };
175
+ })() : {
176
+ top: 0,
177
+ bottom: window.innerHeight
178
+ };
179
+ const top = first.getBoundingClientRect().top;
180
+ const bottom = last.getBoundingClientRect().bottom;
181
+ let by = 0;
182
+ if (bottom - top > view.bottom - view.top) by = top - view.top;
183
+ else if (top < view.top) by = top - view.top;
184
+ else if (bottom > view.bottom) by = bottom - view.bottom;
185
+ by = Math.round(by);
186
+ if (!by) return;
187
+ if (box) box.scrollTop += by;
188
+ else window.scrollBy(0, by);
189
+ }
190
+ /** Each line's words from `terms`: only in comments and strings, never where a name link or a path is. With 'first',
191
+ * each word once per file, at its first place. */
192
+ function matchTerms(toks, start, links, terms, mark) {
193
+ const out = /* @__PURE__ */ new Map();
194
+ const re = termPattern(terms);
195
+ if (!re || !terms) return out;
196
+ const seen = /* @__PURE__ */ new Set();
197
+ toks.forEach((line, i) => {
198
+ const n = start + i;
199
+ const taken = links.get(n) ?? [];
200
+ let at = 0;
201
+ for (const [cls, text] of line) {
202
+ if (cls === "is-comment" || cls === "is-string") {
203
+ const paths = [...text.matchAll(PATH_IN_CODE)].map((m) => ({
204
+ from: at + m.index,
205
+ to: at + m.index + m[0].length
206
+ }));
207
+ const free = (from, to) => ![...taken, ...paths].some((h) => h.from < to && from < h.to);
208
+ const found = findTerms(text, terms, re, "all", /* @__PURE__ */ new Set()).map((w) => ({
209
+ ...w,
210
+ from: w.from + at,
211
+ to: w.to + at
212
+ })).filter((w) => free(w.from, w.to)).filter((w) => {
213
+ if (mark === "all") return true;
214
+ const t = terms[w.i].term;
215
+ if (seen.has(t)) return false;
216
+ seen.add(t);
217
+ return true;
218
+ });
219
+ if (found.length) out.set(n, [...out.get(n) ?? [], ...found]);
220
+ }
221
+ at += text.length;
222
+ }
223
+ });
224
+ return out;
225
+ }
226
+ /** What the keys do: a small panel opened from the keyboard button or with ?; nothing on screen until asked. */
227
+ function KeysPanel(p) {
228
+ return /* @__PURE__ */ jsx(FloatingPanel, {
229
+ open: p.open,
230
+ onOpenChange: p.onOpenChange,
231
+ trigger: p.trigger,
232
+ side: "bottom",
233
+ align: "end",
234
+ className: "ui-popover ui-cv-keys",
235
+ "aria-label": tr("鍵盤操作"),
236
+ children: /* @__PURE__ */ jsxs("dl", {
237
+ className: "ui-cv-keylist",
238
+ children: [p.keys.map((k) => /* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsx("dt", { children: /* @__PURE__ */ jsx("kbd", { children: k.label }) }), /* @__PURE__ */ jsx("dd", { children: k.what })] }, k.k)), /* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsx("dt", { children: /* @__PURE__ */ jsx("kbd", { children: "?" }) }), /* @__PURE__ */ jsx("dd", { children: tr("顯示或關閉這份說明") })] })]
239
+ })
240
+ });
241
+ }
242
+ /** Source with line numbers and an optional target line: soft accent background, long lines wrap, light syntax colors. */
243
+ function CodeView({ code, start = 1, target, context = 2, file, fileHref, onFile, title, language, languageLabel, highlight = true, tokenizer = tokenize, refHref, onRef, links, onLink, preview, terms, termsMark = "first", maxHeight, sections, copy, header = true, lineNumbers = true, onCopy, onCopyError, onLine, peek, className, ref }) {
244
+ const lines = React.useMemo(() => String(code || "").replace(/\n$/, "").split("\n"), [code]);
245
+ const lang = language || languageOf(file);
246
+ const toks = React.useMemo(() => highlight ? tokenizer(lines, lang) : lines.map((l) => [["", l]]), [
247
+ lines,
248
+ lang,
249
+ highlight,
250
+ tokenizer
251
+ ]);
252
+ const hits = React.useMemo(() => matchLinks(lines, start, links), [
253
+ lines,
254
+ start,
255
+ links
256
+ ]);
257
+ const termHits = React.useMemo(() => matchTerms(toks, start, hits, terms, termsMark), [
258
+ toks,
259
+ start,
260
+ hits,
261
+ terms,
262
+ termsMark
263
+ ]);
264
+ const nav = hits.size > 0 || termHits.size > 0;
265
+ const [armed, setArmed] = React.useState(false);
266
+ const inside = React.useRef(false);
267
+ React.useEffect(() => {
268
+ if (!nav) return;
269
+ const key = (e) => {
270
+ if (e.key === "Control" || e.key === "Meta") setArmed(e.type === "keydown" && inside.current);
271
+ };
272
+ const off = () => setArmed(false);
273
+ window.addEventListener("keydown", key);
274
+ window.addEventListener("keyup", key);
275
+ window.addEventListener("blur", off);
276
+ return () => {
277
+ window.removeEventListener("keydown", key);
278
+ window.removeEventListener("keyup", key);
279
+ window.removeEventListener("blur", off);
280
+ };
281
+ }, [nav]);
282
+ const [pv, setPv] = React.useState(null);
283
+ const pvId = React.useId();
284
+ const showTimer = React.useRef(void 0);
285
+ const hideTimer = React.useRef(void 0);
286
+ React.useEffect(() => () => {
287
+ clearTimeout(showTimer.current);
288
+ clearTimeout(hideTimer.current);
289
+ }, []);
290
+ const pvSoon = (el) => {
291
+ if (!preview) return;
292
+ clearTimeout(hideTimer.current);
293
+ clearTimeout(showTimer.current);
294
+ if (pv?.el === el) return;
295
+ showTimer.current = setTimeout(() => setPv({
296
+ i: Number(el.dataset.link),
297
+ el
298
+ }), 500);
299
+ };
300
+ const pvLater = () => {
301
+ clearTimeout(showTimer.current);
302
+ clearTimeout(hideTimer.current);
303
+ hideTimer.current = setTimeout(() => setPv(null), 200);
304
+ };
305
+ const pvKeep = () => clearTimeout(hideTimer.current);
306
+ const linkEl = (t) => t instanceof Element ? t.closest(".ui-cv-name") : null;
307
+ const open = (el, e) => {
308
+ const link = links?.[Number(el.dataset.link)];
309
+ if (!link) return;
310
+ clearTimeout(showTimer.current);
311
+ setPv(null);
312
+ if (onLink) onLink(link, e);
313
+ else if (link.href) window.location.assign(link.href);
314
+ };
315
+ const onLinkClick = (e) => {
316
+ const el = linkEl(e.target);
317
+ if (!el) return;
318
+ const href = links?.[Number(el.dataset.link)]?.href;
319
+ if (e.shiftKey && href) return;
320
+ e.preventDefault();
321
+ const type = e.nativeEvent.pointerType;
322
+ if (e.detail === 0 || type === "touch" || type === "pen" || e.ctrlKey || e.metaKey) open(el, e);
323
+ };
324
+ const [ta, tb] = target == null ? [null, null] : Array.isArray(target) ? target : [target, target];
325
+ const narrow = useNarrow();
326
+ const [all, setAll] = React.useState(false);
327
+ const [shownFor, setShownFor] = React.useState({
328
+ code,
329
+ ta,
330
+ tb
331
+ });
332
+ if (shownFor.code !== code || shownFor.ta !== ta || shownFor.tb !== tb) {
333
+ setShownFor({
334
+ code,
335
+ ta,
336
+ tb
337
+ });
338
+ setAll(false);
339
+ }
340
+ const canWindow = ta != null && tb != null && lines.length > tb - ta + 1 + context * 2;
341
+ const windowed = narrow && canWindow && !all;
342
+ const from = windowed ? Math.max(0, ta - start - context) : 0;
343
+ const to = windowed ? Math.min(lines.length, tb - start + context + 1) : lines.length;
344
+ const body = React.useRef(null);
345
+ React.useImperativeHandle(ref, () => ({ focusTarget: () => {
346
+ if (selectedIn(body.current)) return;
347
+ body.current?.querySelector(".is-target")?.focus();
348
+ } }));
349
+ React.useEffect(() => {
350
+ const b = body.current;
351
+ const el = b?.querySelector(".is-target");
352
+ if (b && el && b.scrollHeight > b.clientHeight && !selectedIn(b)) b.scrollTop += el.getBoundingClientRect().top - b.getBoundingClientRect().top - b.clientHeight / 2 + el.offsetHeight / 2;
353
+ }, [ta, code]);
354
+ const [pFrom, pTo] = peek == null ? [NaN, NaN] : typeof peek === "number" ? [peek, peek] : [Math.min(peek.from, peek.to), Math.max(peek.from, peek.to)];
355
+ React.useEffect(() => {
356
+ if (Number.isNaN(pFrom)) return;
357
+ const pre = body.current;
358
+ if (!pre) return;
359
+ const rows = [...pre.querySelectorAll("[data-n]")].filter((el) => {
360
+ const n = Number(el.dataset.n);
361
+ return n >= pFrom && n <= pTo;
362
+ });
363
+ if (rows.length) showRange(rows[0], rows[rows.length - 1], pre);
364
+ }, [pFrom, pTo]);
365
+ const digits = String(start + lines.length - 1).length;
366
+ const prose = (cls, text, inLink) => !inLink && (refHref || onRef) && (cls === "is-comment" || cls === "is-string") ? withPaths(text, refHref, onRef, nav ? -1 : void 0) : breakable(text);
367
+ const piece = (cls, text, key, inLink, at = 0, words) => {
368
+ const mine = !inLink && terms && words ? words.filter((w) => w.from >= at && w.to <= at + text.length) : [];
369
+ let k = 0;
370
+ const kids = mine.length && terms ? withTerms(text, mine.map((w) => ({
371
+ ...w,
372
+ from: w.from - at,
373
+ to: w.to - at
374
+ })), terms, () => key + "t" + k++, (t) => prose(cls, t, false), {
375
+ className: "ui-cv-term",
376
+ tabIndex: -1,
377
+ openOnFocus: false
378
+ }) : prose(cls, text, inLink);
379
+ return cls ? /* @__PURE__ */ jsx("span", {
380
+ className: cls,
381
+ children: kids
382
+ }, key) : /* @__PURE__ */ jsx(React.Fragment, { children: kids }, key);
383
+ };
384
+ const nameLink = (hit, kids) => {
385
+ const link = links?.[hit.i];
386
+ if (!link) return null;
387
+ return /* @__PURE__ */ jsx("a", {
388
+ className: "ui-cv-name",
389
+ href: link.href,
390
+ role: link.href ? void 0 : "link",
391
+ tabIndex: -1,
392
+ draggable: false,
393
+ "data-link": hit.i,
394
+ "aria-label": link.label,
395
+ "aria-describedby": pv?.i === hit.i ? pvId : void 0,
396
+ title: preview ? void 0 : link.label,
397
+ children: kids
398
+ }, "n" + hit.i);
399
+ };
400
+ const lineContent = (line, lineHits, words) => {
401
+ if (!lineHits) {
402
+ let at = 0;
403
+ return line.map(([cls, text], j) => {
404
+ const el = piece(cls, text, String(j), false, at, words);
405
+ at += text.length;
406
+ return el;
407
+ });
408
+ }
409
+ const out = [];
410
+ let group = [];
411
+ let open = null;
412
+ const flush = () => {
413
+ if (open) out.push(nameLink(open, group));
414
+ open = null;
415
+ group = [];
416
+ };
417
+ let pos = 0;
418
+ let h = 0;
419
+ line.forEach(([cls, text], j) => {
420
+ let k = 0;
421
+ while (k < text.length) {
422
+ const at = pos + k;
423
+ while (h < lineHits.length && lineHits[h].to <= at) h++;
424
+ const next = lineHits[h];
425
+ const hit = next && next.from <= at ? next : null;
426
+ const end = Math.min(pos + text.length, hit ? hit.to : next ? next.from : Infinity);
427
+ const key = j + "." + k;
428
+ if (hit) {
429
+ if (open !== hit) {
430
+ flush();
431
+ open = hit;
432
+ }
433
+ group.push(piece(cls, text.slice(k, end - pos), key, true));
434
+ } else {
435
+ flush();
436
+ out.push(piece(cls, text.slice(k, end - pos), key, false, at, words));
437
+ }
438
+ k = end - pos;
439
+ }
440
+ pos += text.length;
441
+ });
442
+ flush();
443
+ return out;
444
+ };
445
+ const rows = [];
446
+ for (let i = from; i < to; i++) {
447
+ const n = start + i;
448
+ const on = ta != null && tb != null && n >= ta && n <= tb;
449
+ rows.push(/* @__PURE__ */ jsxs("div", {
450
+ role: "listitem",
451
+ className: cx("ui-cv-line", on && "is-target", n >= pFrom && n <= pTo && "is-peek"),
452
+ "data-n": n,
453
+ "aria-current": on && n === ta ? "location" : void 0,
454
+ tabIndex: on && n === ta ? -1 : void 0,
455
+ children: [lineNumbers ? /* @__PURE__ */ jsx("span", {
456
+ className: "ui-cv-num",
457
+ style: { width: `calc(${digits}ch + 28px)` },
458
+ "aria-hidden": "true",
459
+ children: n
460
+ }) : null, /* @__PURE__ */ jsxs("span", {
461
+ className: "ui-cv-text",
462
+ children: [/* @__PURE__ */ jsx("span", {
463
+ className: "ui-visually-hidden ui-cv-sr",
464
+ children: tr("第 {n} 行", { n }) + indentText(lines[i]) + tr(":")
465
+ }), lineContent(toks[i], hits.get(n), termHits.get(n))]
466
+ })]
467
+ }, i));
468
+ }
469
+ const keys = [
470
+ ...nav ? [
471
+ {
472
+ k: "ArrowUp ArrowDown",
473
+ label: "↑↓",
474
+ what: tr("上一行/下一行")
475
+ },
476
+ {
477
+ k: "ArrowLeft ArrowRight",
478
+ label: "←→",
479
+ what: tr(!termHits.size ? "上一個/下一個連結" : hits.size ? "上一個/下一個連結或名詞" : "上一個/下一個名詞")
480
+ },
481
+ {
482
+ k: "Enter",
483
+ label: "Enter",
484
+ what: tr("開啟")
485
+ },
486
+ {
487
+ k: "Escape",
488
+ label: "Esc",
489
+ what: tr("回到這一行")
490
+ }
491
+ ] : [],
492
+ {
493
+ k: "Home",
494
+ label: "Home",
495
+ what: tr("第一行")
496
+ },
497
+ {
498
+ k: "End",
499
+ label: "End",
500
+ what: tr("最後一行")
501
+ },
502
+ {
503
+ k: "PageUp",
504
+ label: "PgUp",
505
+ what: tr("上 20 行")
506
+ },
507
+ {
508
+ k: "PageDown",
509
+ label: "PgDn",
510
+ what: tr("下 20 行")
511
+ },
512
+ ...sections?.length ? [{
513
+ k: "[",
514
+ label: "[",
515
+ what: tr("上一段")
516
+ }, {
517
+ k: "]",
518
+ label: "]",
519
+ what: tr("下一段")
520
+ }] : []
521
+ ];
522
+ const onMove = (e) => {
523
+ if (e.altKey || e.ctrlKey || e.metaKey) return;
524
+ if (e.key === "?") {
525
+ e.preventDefault();
526
+ byKey.current = true;
527
+ setKeysOpen((o) => !o);
528
+ return;
529
+ }
530
+ const pre = e.currentTarget;
531
+ const items = [...pre.querySelectorAll("[role=\"listitem\"]")];
532
+ if (!items.length) return;
533
+ const nOf = (el) => Number(el.dataset.n);
534
+ const act = document.activeElement instanceof HTMLElement ? document.activeElement : null;
535
+ const curLine = act && act !== pre ? act.closest("[role=\"listitem\"]") : null;
536
+ const at = curLine ? items.indexOf(curLine) : -1;
537
+ const now = at < 0 ? -1 : at;
538
+ const focusLine = (el) => {
539
+ if (!el) return;
540
+ el.tabIndex = -1;
541
+ el.focus();
542
+ onLine?.(nOf(el), "key");
543
+ };
544
+ if (onLine && !nav && (e.key === "ArrowUp" || e.key === "ArrowDown")) {
545
+ e.preventDefault();
546
+ if (now < 0) focusLine(items.find((el) => el.classList.contains("is-target")) ?? items[0]);
547
+ else focusLine(items[Math.max(0, Math.min(items.length - 1, now + (e.key === "ArrowDown" ? 1 : -1)))]);
548
+ return;
549
+ }
550
+ if (nav) {
551
+ const stops = [...pre.querySelectorAll(".ui-cv-name, .ui-cv-ref, .ui-cv-term")];
552
+ const onStop = act ? stops.indexOf(act) : -1;
553
+ if (e.key === "Escape") {
554
+ if (act?.classList.contains("ui-cv-term") && act.getAttribute("aria-expanded") === "true") return;
555
+ if (pv) setPv(null);
556
+ else if (onStop >= 0) focusLine(curLine ?? void 0);
557
+ else if (curLine) pre.focus();
558
+ else return;
559
+ e.preventDefault();
560
+ return;
561
+ }
562
+ if (e.key === "Enter" && act && onStop >= 0 && act.classList.contains("ui-cv-name") && !act.hasAttribute("href")) {
563
+ e.preventDefault();
564
+ open(act, e);
565
+ return;
566
+ }
567
+ if (e.key === "ArrowUp" || e.key === "ArrowDown") {
568
+ e.preventDefault();
569
+ if (now < 0) focusLine(items.find((el) => el.classList.contains("is-target")) ?? items[0]);
570
+ else focusLine(items[Math.max(0, Math.min(items.length - 1, now + (e.key === "ArrowDown" ? 1 : -1)))]);
571
+ return;
572
+ }
573
+ if (e.key === "ArrowLeft" || e.key === "ArrowRight") {
574
+ e.preventDefault();
575
+ const fwd = e.key === "ArrowRight";
576
+ let to;
577
+ if (onStop >= 0) to = stops[onStop + (fwd ? 1 : -1)];
578
+ else {
579
+ const n = curLine ? nOf(curLine) : ta ?? nOf(items[0]);
580
+ const lineOf = (el) => Number(el.closest("[role=\"listitem\"]")?.dataset.n);
581
+ to = fwd ? stops.find((el) => lineOf(el) >= n) : stops.filter((el) => lineOf(el) <= n).pop();
582
+ }
583
+ to?.focus();
584
+ return;
585
+ }
586
+ }
587
+ let to = -1;
588
+ let said = "";
589
+ if (e.key === "Home") to = 0;
590
+ else if (e.key === "End") to = items.length - 1;
591
+ else if (e.key === "PageDown") to = Math.min(items.length - 1, Math.max(0, now) + 20);
592
+ else if (e.key === "PageUp") to = Math.max(0, now - 20);
593
+ else if ((e.key === "[" || e.key === "]") && sections?.length) {
594
+ const cur = now < 0 ? e.key === "]" ? -Infinity : Infinity : nOf(items[now]);
595
+ const sorted = [...sections].sort((a, b) => a.line - b.line);
596
+ const s = e.key === "]" ? sorted.find((x) => x.line > cur) : [...sorted].reverse().find((x) => x.line < cur);
597
+ if (s) {
598
+ to = items.findIndex((el) => nOf(el) >= s.line);
599
+ said = s.label;
600
+ }
601
+ } else return;
602
+ e.preventDefault();
603
+ const el = items[to];
604
+ if (!el) return;
605
+ el.tabIndex = -1;
606
+ el.focus();
607
+ onLine?.(nOf(el), "key");
608
+ if (said) announce(tr("{name},第 {n} 行", {
609
+ name: said,
610
+ n: nOf(el)
611
+ }));
612
+ };
613
+ const onLineClick = (e) => {
614
+ if (!onLine) return;
615
+ const t = e.target instanceof Element ? e.target : null;
616
+ if (!t) return;
617
+ const type = e.nativeEvent.pointerType;
618
+ if (t.closest(".ui-cv-name") && (e.detail === 0 || type === "touch" || type === "pen" || e.ctrlKey || e.metaKey)) return;
619
+ if (t.closest("a:not(.ui-cv-name), button:not(.ui-cv-name)")) return;
620
+ if (selectedIn(e.currentTarget)) return;
621
+ const row = t.closest("[role=\"listitem\"]");
622
+ if (row) onLine(Number(row.dataset.n), "click");
623
+ };
624
+ const onCopyText = (e) => {
625
+ const text = selectedCode(e.currentTarget);
626
+ if (text == null) return;
627
+ e.preventDefault();
628
+ e.clipboardData.setData("text/plain", text);
629
+ };
630
+ const [copied, setCopied] = React.useState(false);
631
+ const [keysOpen, setKeysOpen] = React.useState(false);
632
+ const byKey = React.useRef(false);
633
+ const copyTimer = React.useRef(void 0);
634
+ React.useEffect(() => () => clearTimeout(copyTimer.current), []);
635
+ const doCopy = () => {
636
+ copyText(code).then(() => {
637
+ setCopied(true);
638
+ announce(tr("已複製"));
639
+ clearTimeout(copyTimer.current);
640
+ copyTimer.current = setTimeout(() => setCopied(false), 2e3);
641
+ onCopy?.(code);
642
+ }, () => onCopyError?.());
643
+ };
644
+ const langText = languageLabel !== false && lang !== "plain" ? /* @__PURE__ */ jsxs("span", {
645
+ className: "ui-cv-lang",
646
+ children: [/* @__PURE__ */ jsx("span", {
647
+ className: "ui-visually-hidden",
648
+ children: tr("程式語言") + tr(":")
649
+ }), tr(languageLabel || LANGUAGE_NAMES[lang] || lang)]
650
+ }) : null;
651
+ const copyBtn = copy ? /* @__PURE__ */ jsx(IconButton, {
652
+ className: cx("ui-cv-copy", copied && "is-copied"),
653
+ variant: header === "float" ? "quiet" : void 0,
654
+ label: copied ? tr("已複製") : tr("複製程式碼"),
655
+ icon: copied ? CHECK : "copy",
656
+ onClick: doCopy
657
+ }) : null;
658
+ const onKeysChange = (o) => {
659
+ setKeysOpen(o);
660
+ if (!o && byKey.current) {
661
+ byKey.current = false;
662
+ requestAnimationFrame(() => body.current?.focus());
663
+ }
664
+ };
665
+ const keysPanel = (trigger) => /* @__PURE__ */ jsx(KeysPanel, {
666
+ open: keysOpen,
667
+ onOpenChange: onKeysChange,
668
+ trigger,
669
+ keys
670
+ });
671
+ const keysBtn = keysPanel(/* @__PURE__ */ jsx(IconButton, {
672
+ variant: "quiet",
673
+ icon: "keyboard",
674
+ label: tr("鍵盤操作"),
675
+ onClick: () => byKey.current = false
676
+ }));
677
+ const head = header === "float" ? /* @__PURE__ */ jsxs("div", {
678
+ className: "ui-cv-tools",
679
+ children: [
680
+ langText,
681
+ copyBtn,
682
+ keysBtn
683
+ ]
684
+ }) : header === false ? keysPanel(/* @__PURE__ */ jsx("span", {
685
+ className: "ui-cv-keyanchor",
686
+ "aria-hidden": "true"
687
+ })) : file || title || copy ? /* @__PURE__ */ jsxs("div", {
688
+ className: "ui-cv-head",
689
+ children: [
690
+ file ? fileHref || onFile ? /* @__PURE__ */ jsx(InlineCode, {
691
+ href: fileHref,
692
+ onClick: onFile,
693
+ className: "ui-cv-file",
694
+ children: file
695
+ }) : /* @__PURE__ */ jsx("span", {
696
+ className: "ui-cv-file",
697
+ children: file
698
+ }) : title ? /* @__PURE__ */ jsx("span", {
699
+ className: "ui-cv-title",
700
+ children: title
701
+ }) : null,
702
+ langText,
703
+ copyBtn,
704
+ keysBtn
705
+ ]
706
+ }) : keysPanel(/* @__PURE__ */ jsx("span", {
707
+ className: "ui-cv-keyanchor",
708
+ "aria-hidden": "true"
709
+ }));
710
+ return /* @__PURE__ */ jsxs("div", {
711
+ className: cx("ui-cv", !lineNumbers && "no-nums", header === "float" && "is-float", className),
712
+ role: "region",
713
+ "aria-label": file || title || tr("原始碼"),
714
+ children: [
715
+ head,
716
+ /* @__PURE__ */ jsx("pre", {
717
+ ref: body,
718
+ style: maxHeight ? { maxHeight } : void 0,
719
+ tabIndex: 0,
720
+ "aria-label": tr("程式碼"),
721
+ "aria-keyshortcuts": [...keys.map((k) => k.k), "?"].join(" "),
722
+ onKeyDown: onMove,
723
+ onCopy: onCopyText,
724
+ onClick: nav ? void 0 : onLineClick,
725
+ ...nav ? {
726
+ onClick: (e) => {
727
+ onLinkClick(e);
728
+ onLineClick(e);
729
+ },
730
+ onPointerEnter: () => inside.current = true,
731
+ onPointerLeave: () => {
732
+ inside.current = false;
733
+ setArmed(false);
734
+ },
735
+ onPointerMove: (e) => {
736
+ const held = e.ctrlKey || e.metaKey;
737
+ if (held !== armed) setArmed(held);
738
+ },
739
+ onPointerOver: (e) => {
740
+ const el = linkEl(e.target);
741
+ if (el && e.pointerType === "mouse") pvSoon(el);
742
+ },
743
+ onPointerOut: (e) => {
744
+ const el = linkEl(e.target);
745
+ if (el && !el.contains(e.relatedTarget)) pvLater();
746
+ },
747
+ onFocus: (e) => {
748
+ const el = linkEl(e.target);
749
+ if (el && el.matches(":focus-visible")) pvSoon(el);
750
+ },
751
+ onBlur: (e) => {
752
+ if (linkEl(e.target)) pvLater();
753
+ }
754
+ } : null,
755
+ className: cx("ui-cv-body", armed && "is-armed"),
756
+ children: /* @__PURE__ */ jsx("code", {
757
+ role: "list",
758
+ children: rows
759
+ })
760
+ }),
761
+ preview && pv && links?.[pv.i] ? /* @__PURE__ */ jsx(PreviewCard.Root, {
762
+ open: true,
763
+ onOpenChange: (o) => o ? void 0 : setPv(null),
764
+ children: /* @__PURE__ */ jsx(PreviewCard.Portal, { children: /* @__PURE__ */ jsx(PreviewCard.Positioner, {
765
+ anchor: pv.el,
766
+ side: "bottom",
767
+ align: "start",
768
+ sideOffset: 6,
769
+ className: "ui-float",
770
+ children: /* @__PURE__ */ jsx(PreviewCard.Popup, {
771
+ id: pvId,
772
+ className: "ui-popover ui-cv-preview",
773
+ onPointerEnter: pvKeep,
774
+ onPointerLeave: pvLater,
775
+ children: preview(links[pv.i])
776
+ })
777
+ }) })
778
+ }) : null,
779
+ narrow && canWindow ? /* @__PURE__ */ jsx("button", {
780
+ type: "button",
781
+ className: "ui-link-btn ui-cv-more",
782
+ onClick: () => setAll(!all),
783
+ children: /* @__PURE__ */ jsx("span", { children: all ? tr("只顯示第 {n} 行前後", { n: ta }) : monoNums(tr("顯示全部 {n} 行", { n: "" }), [lines.length]) })
784
+ }) : null
785
+ ]
786
+ });
787
+ }
788
+ export { CodeView };
789
+
790
+ //# sourceMappingURL=CodeView.js.map