svelte-highlight 7.23.1 → 7.24.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 (502) hide show
  1. package/AnsiOutput.svelte +33 -6
  2. package/AnsiOutput.svelte.d.ts +8 -0
  3. package/CodeToolbar.svelte +144 -0
  4. package/CodeToolbar.svelte.d.ts +125 -0
  5. package/CodeWindow.svelte +19 -0
  6. package/CodeWindow.svelte.d.ts +10 -0
  7. package/CopyButton.svelte +107 -3
  8. package/CopyButton.svelte.d.ts +17 -1
  9. package/DiffFileList.svelte +523 -0
  10. package/DiffFileList.svelte.d.ts +94 -0
  11. package/DiffFileListItem.svelte +44 -0
  12. package/DiffMinimap.svelte +99 -0
  13. package/DiffMinimap.svelte.d.ts +17 -0
  14. package/DiffStats.svelte +43 -0
  15. package/DiffStats.svelte.d.ts +17 -0
  16. package/DiffView.svelte +1213 -0
  17. package/DiffView.svelte.d.ts +76 -0
  18. package/FileTabs.svelte +2 -5
  19. package/Highlight.svelte +158 -2
  20. package/Highlight.svelte.d.ts +50 -2
  21. package/HighlightAuto.svelte.d.ts +3 -2
  22. package/HighlightAutoLazy.svelte +112 -0
  23. package/HighlightAutoLazy.svelte.d.ts +66 -0
  24. package/HighlightDiff.svelte +162 -0
  25. package/HighlightDiff.svelte.d.ts +102 -0
  26. package/HighlightEditable.svelte +65 -766
  27. package/HighlightEditable.svelte.d.ts +15 -5
  28. package/HighlightStream.svelte +117 -218
  29. package/HighlightStream.svelte.d.ts +8 -0
  30. package/HighlightSvelte.svelte.d.ts +3 -2
  31. package/HighlightVirtual.svelte +41 -8
  32. package/HighlightVirtual.svelte.d.ts +8 -0
  33. package/LangTag.svelte +33 -4
  34. package/LangTag.svelte.d.ts +6 -2
  35. package/LineNumbers.svelte +25 -4
  36. package/LineNumbers.svelte.d.ts +8 -0
  37. package/MarkdownStream.svelte +1 -0
  38. package/README.md +736 -19
  39. package/ToolbarButton.svelte +90 -0
  40. package/ToolbarButton.svelte.d.ts +106 -0
  41. package/Typewriter.svelte +59 -10
  42. package/Typewriter.svelte.d.ts +7 -0
  43. package/WrapToggle.svelte +34 -0
  44. package/WrapToggle.svelte.d.ts +39 -0
  45. package/action.js +7 -3
  46. package/ansi.d.ts +1 -3
  47. package/ansi.js +44 -12
  48. package/auto-detect.d.ts +52 -0
  49. package/auto-detect.js +208 -0
  50. package/code-region.d.ts +7 -0
  51. package/code-region.js +20 -0
  52. package/css-highlights.js +115 -0
  53. package/css-walk.js +50 -0
  54. package/diff-controller.d.ts +120 -0
  55. package/diff-controller.js +721 -0
  56. package/diff-edits.d.ts +101 -0
  57. package/diff-edits.js +890 -0
  58. package/diff-html.d.ts +8 -0
  59. package/diff-html.js +60 -0
  60. package/diff-languages.d.ts +4 -0
  61. package/diff-languages.js +73 -0
  62. package/diff.d.ts +161 -0
  63. package/diff.js +1111 -0
  64. package/editable-commands.js +81 -0
  65. package/editable-css-paint.js +5 -3
  66. package/editable-dom-paint.js +1 -1
  67. package/editable-history.js +199 -0
  68. package/editable-line-dom.js +176 -1
  69. package/editable-painter.js +356 -0
  70. package/editable.d.ts +118 -0
  71. package/editable.js +513 -0
  72. package/engine.d.ts +1 -1
  73. package/fence.js +1 -1
  74. package/highlight-cascade.js +679 -0
  75. package/highlight-theme.d.ts +7 -0
  76. package/highlight-theme.js +23 -46
  77. package/incremental-tokenize.js +1 -1
  78. package/index.d.ts +39 -6
  79. package/index.js +30 -0
  80. package/languages/detect-index.d.ts +16 -0
  81. package/languages/detect-index.js +6 -0
  82. package/languages/loaders.d.ts +8 -0
  83. package/languages/loaders.js +370 -0
  84. package/package.json +50 -2
  85. package/preprocessor.js +20 -5
  86. package/search.js +2 -2
  87. package/stream-highlighter.js +198 -0
  88. package/stream.css +16 -0
  89. package/stream.d.ts +60 -0
  90. package/stream.js +283 -0
  91. package/style-registry.js +34 -3
  92. package/styles/3024.css +1 -1
  93. package/styles/3024.js +1 -1
  94. package/styles/apathy.css +1 -1
  95. package/styles/apathy.js +1 -1
  96. package/styles/apprentice.css +1 -1
  97. package/styles/apprentice.js +1 -1
  98. package/styles/ashes.css +1 -1
  99. package/styles/ashes.js +1 -1
  100. package/styles/atelier-cave-light.css +1 -1
  101. package/styles/atelier-cave-light.js +1 -1
  102. package/styles/atelier-cave.css +1 -1
  103. package/styles/atelier-cave.js +1 -1
  104. package/styles/atelier-dune-light.css +1 -1
  105. package/styles/atelier-dune-light.js +1 -1
  106. package/styles/atelier-dune.css +1 -1
  107. package/styles/atelier-dune.js +1 -1
  108. package/styles/atelier-estuary-light.css +1 -1
  109. package/styles/atelier-estuary-light.js +1 -1
  110. package/styles/atelier-estuary.css +1 -1
  111. package/styles/atelier-estuary.js +1 -1
  112. package/styles/atelier-forest-light.css +1 -1
  113. package/styles/atelier-forest-light.js +1 -1
  114. package/styles/atelier-forest.css +1 -1
  115. package/styles/atelier-forest.js +1 -1
  116. package/styles/atelier-heath-light.css +1 -1
  117. package/styles/atelier-heath-light.js +1 -1
  118. package/styles/atelier-heath.css +1 -1
  119. package/styles/atelier-heath.js +1 -1
  120. package/styles/atelier-lakeside-light.css +1 -1
  121. package/styles/atelier-lakeside-light.js +1 -1
  122. package/styles/atelier-lakeside.css +1 -1
  123. package/styles/atelier-lakeside.js +1 -1
  124. package/styles/atelier-plateau-light.css +1 -1
  125. package/styles/atelier-plateau-light.js +1 -1
  126. package/styles/atelier-plateau.css +1 -1
  127. package/styles/atelier-plateau.js +1 -1
  128. package/styles/atelier-savanna-light.css +1 -1
  129. package/styles/atelier-savanna-light.js +1 -1
  130. package/styles/atelier-savanna.css +1 -1
  131. package/styles/atelier-savanna.js +1 -1
  132. package/styles/atelier-seaside-light.css +1 -1
  133. package/styles/atelier-seaside-light.js +1 -1
  134. package/styles/atelier-seaside.css +1 -1
  135. package/styles/atelier-seaside.js +1 -1
  136. package/styles/atelier-sulphurpool-light.css +1 -1
  137. package/styles/atelier-sulphurpool-light.js +1 -1
  138. package/styles/atelier-sulphurpool.css +1 -1
  139. package/styles/atelier-sulphurpool.js +1 -1
  140. package/styles/atlas.css +1 -1
  141. package/styles/atlas.js +1 -1
  142. package/styles/base16-github.css +1 -1
  143. package/styles/base16-github.js +1 -1
  144. package/styles/base16-ir-black.css +1 -1
  145. package/styles/base16-ir-black.js +1 -1
  146. package/styles/base16-monokai.css +1 -1
  147. package/styles/base16-monokai.js +1 -1
  148. package/styles/base16-nord.css +1 -1
  149. package/styles/base16-nord.js +1 -1
  150. package/styles/bespin.css +1 -1
  151. package/styles/bespin.js +1 -1
  152. package/styles/black-metal-bathory.css +1 -1
  153. package/styles/black-metal-bathory.js +1 -1
  154. package/styles/black-metal-burzum.css +1 -1
  155. package/styles/black-metal-burzum.js +1 -1
  156. package/styles/black-metal-dark-funeral.css +1 -1
  157. package/styles/black-metal-dark-funeral.js +1 -1
  158. package/styles/black-metal-gorgoroth.css +1 -1
  159. package/styles/black-metal-gorgoroth.js +1 -1
  160. package/styles/black-metal-immortal.css +1 -1
  161. package/styles/black-metal-immortal.js +1 -1
  162. package/styles/black-metal-khold.css +1 -1
  163. package/styles/black-metal-khold.js +1 -1
  164. package/styles/black-metal-marduk.css +1 -1
  165. package/styles/black-metal-marduk.js +1 -1
  166. package/styles/black-metal-mayhem.css +1 -1
  167. package/styles/black-metal-mayhem.js +1 -1
  168. package/styles/black-metal-nile.css +1 -1
  169. package/styles/black-metal-nile.js +1 -1
  170. package/styles/black-metal-venom.css +1 -1
  171. package/styles/black-metal-venom.js +1 -1
  172. package/styles/black-metal.css +1 -1
  173. package/styles/black-metal.js +1 -1
  174. package/styles/brewer.css +1 -1
  175. package/styles/brewer.js +1 -1
  176. package/styles/bright.css +1 -1
  177. package/styles/bright.js +1 -1
  178. package/styles/brogrammer.css +1 -1
  179. package/styles/brogrammer.js +1 -1
  180. package/styles/brush-trees-dark.css +1 -1
  181. package/styles/brush-trees-dark.js +1 -1
  182. package/styles/brush-trees.css +1 -1
  183. package/styles/brush-trees.js +1 -1
  184. package/styles/chalk.css +1 -1
  185. package/styles/chalk.js +1 -1
  186. package/styles/circus.css +1 -1
  187. package/styles/circus.js +1 -1
  188. package/styles/classic-dark.css +1 -1
  189. package/styles/classic-dark.js +1 -1
  190. package/styles/classic-light.css +1 -1
  191. package/styles/classic-light.js +1 -1
  192. package/styles/codeschool.css +1 -1
  193. package/styles/codeschool.js +1 -1
  194. package/styles/colors.css +1 -1
  195. package/styles/colors.js +1 -1
  196. package/styles/cupcake.css +1 -1
  197. package/styles/cupcake.js +1 -1
  198. package/styles/cupertino.css +1 -1
  199. package/styles/cupertino.js +1 -1
  200. package/styles/cybertopia-cherry.css +2 -1
  201. package/styles/cybertopia-cherry.js +2 -1
  202. package/styles/cybertopia-dimmer.css +2 -1
  203. package/styles/cybertopia-dimmer.js +2 -1
  204. package/styles/cybertopia-icecap.css +2 -1
  205. package/styles/cybertopia-icecap.js +2 -1
  206. package/styles/cybertopia-saturated.css +2 -1
  207. package/styles/cybertopia-saturated.js +2 -1
  208. package/styles/danqing.css +1 -1
  209. package/styles/danqing.js +1 -1
  210. package/styles/darcula.css +1 -1
  211. package/styles/darcula.js +1 -1
  212. package/styles/dark-violet.css +1 -1
  213. package/styles/dark-violet.js +1 -1
  214. package/styles/darkmoss.css +1 -1
  215. package/styles/darkmoss.js +1 -1
  216. package/styles/darktooth.css +1 -1
  217. package/styles/darktooth.js +1 -1
  218. package/styles/decaf.css +1 -1
  219. package/styles/decaf.js +1 -1
  220. package/styles/default-dark.css +1 -1
  221. package/styles/default-dark.js +1 -1
  222. package/styles/default-light.css +1 -1
  223. package/styles/default-light.js +1 -1
  224. package/styles/dirtysea.css +1 -1
  225. package/styles/dirtysea.js +1 -1
  226. package/styles/docco.css +1 -1
  227. package/styles/docco.js +1 -1
  228. package/styles/dracula.css +1 -1
  229. package/styles/dracula.js +1 -1
  230. package/styles/edge-dark.css +1 -1
  231. package/styles/edge-dark.js +1 -1
  232. package/styles/edge-light.css +1 -1
  233. package/styles/edge-light.js +1 -1
  234. package/styles/eighties.css +1 -1
  235. package/styles/eighties.js +1 -1
  236. package/styles/embers.css +1 -1
  237. package/styles/embers.js +1 -1
  238. package/styles/equilibrium-dark.css +1 -1
  239. package/styles/equilibrium-dark.js +1 -1
  240. package/styles/equilibrium-gray-dark.css +1 -1
  241. package/styles/equilibrium-gray-dark.js +1 -1
  242. package/styles/equilibrium-gray-light.css +1 -1
  243. package/styles/equilibrium-gray-light.js +1 -1
  244. package/styles/equilibrium-light.css +1 -1
  245. package/styles/equilibrium-light.js +1 -1
  246. package/styles/espresso.css +1 -1
  247. package/styles/espresso.js +1 -1
  248. package/styles/eva-dim.css +1 -1
  249. package/styles/eva-dim.js +1 -1
  250. package/styles/eva.css +1 -1
  251. package/styles/eva.js +1 -1
  252. package/styles/flat.css +1 -1
  253. package/styles/flat.js +1 -1
  254. package/styles/framer.css +1 -1
  255. package/styles/framer.js +1 -1
  256. package/styles/fruit-soda.css +1 -1
  257. package/styles/fruit-soda.js +1 -1
  258. package/styles/gigavolt.css +1 -1
  259. package/styles/gigavolt.js +1 -1
  260. package/styles/gml.css +1 -1
  261. package/styles/gml.js +1 -1
  262. package/styles/google-dark.css +1 -1
  263. package/styles/google-dark.js +1 -1
  264. package/styles/google-light.css +1 -1
  265. package/styles/google-light.js +1 -1
  266. package/styles/googlecode.css +1 -1
  267. package/styles/googlecode.js +1 -1
  268. package/styles/grayscale-dark.css +1 -1
  269. package/styles/grayscale-dark.js +1 -1
  270. package/styles/grayscale-light.css +1 -1
  271. package/styles/grayscale-light.js +1 -1
  272. package/styles/grayscale.css +1 -1
  273. package/styles/grayscale.js +1 -1
  274. package/styles/green-screen.css +1 -1
  275. package/styles/green-screen.js +1 -1
  276. package/styles/gruvbox-dark-hard.css +1 -1
  277. package/styles/gruvbox-dark-hard.js +1 -1
  278. package/styles/gruvbox-dark-medium.css +1 -1
  279. package/styles/gruvbox-dark-medium.js +1 -1
  280. package/styles/gruvbox-dark-pale.css +1 -1
  281. package/styles/gruvbox-dark-pale.js +1 -1
  282. package/styles/gruvbox-dark-soft.css +1 -1
  283. package/styles/gruvbox-dark-soft.js +1 -1
  284. package/styles/gruvbox-light-hard.css +1 -1
  285. package/styles/gruvbox-light-hard.js +1 -1
  286. package/styles/gruvbox-light-medium.css +1 -1
  287. package/styles/gruvbox-light-medium.js +1 -1
  288. package/styles/gruvbox-light-soft.css +1 -1
  289. package/styles/gruvbox-light-soft.js +1 -1
  290. package/styles/hardcore.css +1 -1
  291. package/styles/hardcore.js +1 -1
  292. package/styles/harmonic16-dark.css +1 -1
  293. package/styles/harmonic16-dark.js +1 -1
  294. package/styles/harmonic16-light.css +1 -1
  295. package/styles/harmonic16-light.js +1 -1
  296. package/styles/heetch-dark.css +1 -1
  297. package/styles/heetch-dark.js +1 -1
  298. package/styles/heetch-light.css +1 -1
  299. package/styles/heetch-light.js +1 -1
  300. package/styles/helios.css +1 -1
  301. package/styles/helios.js +1 -1
  302. package/styles/hopscotch.css +1 -1
  303. package/styles/hopscotch.js +1 -1
  304. package/styles/horizon-dark.css +1 -1
  305. package/styles/horizon-dark.js +1 -1
  306. package/styles/horizon-light.css +1 -1
  307. package/styles/horizon-light.js +1 -1
  308. package/styles/humanoid-dark.css +1 -1
  309. package/styles/humanoid-dark.js +1 -1
  310. package/styles/humanoid-light.css +1 -1
  311. package/styles/humanoid-light.js +1 -1
  312. package/styles/ia-dark.css +1 -1
  313. package/styles/ia-dark.js +1 -1
  314. package/styles/ia-light.css +1 -1
  315. package/styles/ia-light.js +1 -1
  316. package/styles/icy-dark.css +1 -1
  317. package/styles/icy-dark.js +1 -1
  318. package/styles/idea.css +1 -1
  319. package/styles/idea.js +1 -1
  320. package/styles/intellij-light.css +1 -1
  321. package/styles/intellij-light.js +1 -1
  322. package/styles/ir-black.css +1 -1
  323. package/styles/ir-black.js +1 -1
  324. package/styles/isotope.css +1 -1
  325. package/styles/isotope.js +1 -1
  326. package/styles/kimber.css +1 -1
  327. package/styles/kimber.js +1 -1
  328. package/styles/kimbie-dark.css +1 -1
  329. package/styles/kimbie-dark.js +1 -1
  330. package/styles/kimbie-light.css +1 -1
  331. package/styles/kimbie-light.js +1 -1
  332. package/styles/london-tube.css +1 -1
  333. package/styles/london-tube.js +1 -1
  334. package/styles/macintosh.css +1 -1
  335. package/styles/macintosh.js +1 -1
  336. package/styles/marrakesh.css +1 -1
  337. package/styles/marrakesh.js +1 -1
  338. package/styles/materia.css +1 -1
  339. package/styles/materia.js +1 -1
  340. package/styles/material-darker.css +1 -1
  341. package/styles/material-darker.js +1 -1
  342. package/styles/material-lighter.css +1 -1
  343. package/styles/material-lighter.js +1 -1
  344. package/styles/material-palenight.css +1 -1
  345. package/styles/material-palenight.js +1 -1
  346. package/styles/material-vivid.css +1 -1
  347. package/styles/material-vivid.js +1 -1
  348. package/styles/material.css +1 -1
  349. package/styles/material.js +1 -1
  350. package/styles/mellow-purple.css +1 -1
  351. package/styles/mellow-purple.js +1 -1
  352. package/styles/mexico-light.css +1 -1
  353. package/styles/mexico-light.js +1 -1
  354. package/styles/mocha.css +1 -1
  355. package/styles/mocha.js +1 -1
  356. package/styles/nebula.css +1 -1
  357. package/styles/nebula.js +1 -1
  358. package/styles/nnfx-dark.css +1 -1
  359. package/styles/nnfx-dark.js +1 -1
  360. package/styles/nnfx-light.css +1 -1
  361. package/styles/nnfx-light.js +1 -1
  362. package/styles/nord.css +1 -1
  363. package/styles/nord.js +1 -1
  364. package/styles/nova.css +1 -1
  365. package/styles/nova.js +1 -1
  366. package/styles/ocean.css +1 -1
  367. package/styles/ocean.js +1 -1
  368. package/styles/oceanicnext.css +1 -1
  369. package/styles/oceanicnext.js +1 -1
  370. package/styles/one-light.css +1 -1
  371. package/styles/one-light.js +1 -1
  372. package/styles/onedark.css +1 -1
  373. package/styles/onedark.js +1 -1
  374. package/styles/outrun-dark.css +1 -1
  375. package/styles/outrun-dark.js +1 -1
  376. package/styles/papercolor-dark.css +1 -1
  377. package/styles/papercolor-dark.js +1 -1
  378. package/styles/papercolor-light.css +1 -1
  379. package/styles/papercolor-light.js +1 -1
  380. package/styles/paraiso.css +1 -1
  381. package/styles/paraiso.js +1 -1
  382. package/styles/pasque.css +1 -1
  383. package/styles/pasque.js +1 -1
  384. package/styles/phd.css +1 -1
  385. package/styles/phd.js +1 -1
  386. package/styles/pico.css +1 -1
  387. package/styles/pico.js +1 -1
  388. package/styles/pojoaque.css +1 -1
  389. package/styles/pojoaque.js +1 -1
  390. package/styles/pop.css +1 -1
  391. package/styles/pop.js +1 -1
  392. package/styles/porple.css +1 -1
  393. package/styles/porple.js +1 -1
  394. package/styles/qualia.css +1 -1
  395. package/styles/qualia.js +1 -1
  396. package/styles/railscasts.css +1 -1
  397. package/styles/railscasts.js +1 -1
  398. package/styles/rebecca.css +1 -1
  399. package/styles/rebecca.js +1 -1
  400. package/styles/ros-pine-dawn.css +1 -1
  401. package/styles/ros-pine-dawn.js +1 -1
  402. package/styles/ros-pine-moon.css +1 -1
  403. package/styles/ros-pine-moon.js +1 -1
  404. package/styles/ros-pine.css +1 -1
  405. package/styles/ros-pine.js +1 -1
  406. package/styles/sagelight.css +1 -1
  407. package/styles/sagelight.js +1 -1
  408. package/styles/sandcastle.css +1 -1
  409. package/styles/sandcastle.js +1 -1
  410. package/styles/school-book.css +1 -1
  411. package/styles/school-book.js +1 -1
  412. package/styles/seti-ui.css +1 -1
  413. package/styles/seti-ui.js +1 -1
  414. package/styles/shapeshifter.css +1 -1
  415. package/styles/shapeshifter.js +1 -1
  416. package/styles/silk-dark.css +1 -1
  417. package/styles/silk-dark.js +1 -1
  418. package/styles/silk-light.css +1 -1
  419. package/styles/silk-light.js +1 -1
  420. package/styles/snazzy.css +1 -1
  421. package/styles/snazzy.js +1 -1
  422. package/styles/solar-flare-light.css +1 -1
  423. package/styles/solar-flare-light.js +1 -1
  424. package/styles/solar-flare.css +1 -1
  425. package/styles/solar-flare.js +1 -1
  426. package/styles/solarized-dark.css +1 -1
  427. package/styles/solarized-dark.js +1 -1
  428. package/styles/solarized-light.css +1 -1
  429. package/styles/solarized-light.js +1 -1
  430. package/styles/spacemacs.css +1 -1
  431. package/styles/spacemacs.js +1 -1
  432. package/styles/stackoverflow-dark.css +1 -1
  433. package/styles/stackoverflow-dark.js +1 -1
  434. package/styles/stackoverflow-light.css +1 -1
  435. package/styles/stackoverflow-light.js +1 -1
  436. package/styles/summercamp.css +1 -1
  437. package/styles/summercamp.js +1 -1
  438. package/styles/summerfruit-dark.css +1 -1
  439. package/styles/summerfruit-dark.js +1 -1
  440. package/styles/summerfruit-light.css +1 -1
  441. package/styles/summerfruit-light.js +1 -1
  442. package/styles/synth-midnight-terminal-dark.css +1 -1
  443. package/styles/synth-midnight-terminal-dark.js +1 -1
  444. package/styles/synth-midnight-terminal-light.css +1 -1
  445. package/styles/synth-midnight-terminal-light.js +1 -1
  446. package/styles/tango.css +1 -1
  447. package/styles/tango.js +1 -1
  448. package/styles/tender.css +1 -1
  449. package/styles/tender.js +1 -1
  450. package/styles/tomorrow-night.css +1 -1
  451. package/styles/tomorrow-night.js +1 -1
  452. package/styles/tomorrow.css +1 -1
  453. package/styles/tomorrow.js +1 -1
  454. package/styles/twilight.css +1 -1
  455. package/styles/twilight.js +1 -1
  456. package/styles/unikitty-dark.css +1 -1
  457. package/styles/unikitty-dark.js +1 -1
  458. package/styles/unikitty-light.css +1 -1
  459. package/styles/unikitty-light.js +1 -1
  460. package/styles/vs2015.css +1 -1
  461. package/styles/vs2015.js +1 -1
  462. package/styles/vulcan.css +1 -1
  463. package/styles/vulcan.js +1 -1
  464. package/styles/windows-10-light.css +1 -1
  465. package/styles/windows-10-light.js +1 -1
  466. package/styles/windows-10.css +1 -1
  467. package/styles/windows-10.js +1 -1
  468. package/styles/windows-95-light.css +1 -1
  469. package/styles/windows-95-light.js +1 -1
  470. package/styles/windows-95.css +1 -1
  471. package/styles/windows-95.js +1 -1
  472. package/styles/windows-high-contrast-light.css +1 -1
  473. package/styles/windows-high-contrast-light.js +1 -1
  474. package/styles/windows-high-contrast.css +1 -1
  475. package/styles/windows-high-contrast.js +1 -1
  476. package/styles/windows-nt-light.css +1 -1
  477. package/styles/windows-nt-light.js +1 -1
  478. package/styles/windows-nt.css +1 -1
  479. package/styles/windows-nt.js +1 -1
  480. package/styles/woodland.css +1 -1
  481. package/styles/woodland.js +1 -1
  482. package/styles/xcode-dusk.css +1 -1
  483. package/styles/xcode-dusk.js +1 -1
  484. package/styles/xcode.css +1 -1
  485. package/styles/xcode.js +1 -1
  486. package/styles/xt256.css +1 -1
  487. package/styles/xt256.js +1 -1
  488. package/styles/zenburn.css +1 -1
  489. package/styles/zenburn.js +1 -1
  490. package/themes/_base-css.d.ts +1 -0
  491. package/themes/_base-css.js +1 -0
  492. package/themes/cybertopia-cherry.css +5 -1
  493. package/themes/cybertopia-cherry.js +1 -1
  494. package/themes/cybertopia-dimmer.css +5 -1
  495. package/themes/cybertopia-dimmer.js +1 -1
  496. package/themes/cybertopia-icecap.css +5 -1
  497. package/themes/cybertopia-icecap.js +1 -1
  498. package/themes/cybertopia-saturated.css +5 -1
  499. package/themes/cybertopia-saturated.js +1 -1
  500. package/typewriter-units.d.ts +3 -0
  501. package/typewriter-units.js +43 -0
  502. package/virtual-window.js +9 -4
@@ -33,645 +33,80 @@
33
33
  export let engine = "dom";
34
34
 
35
35
  /**
36
- * Theme CSS or `ThemePalette` for `::highlight()` rules (`"css-highlights"`
37
- * only). Colors only.
36
+ * Theme CSS or `ThemePalette` that `"css-highlights"` resolves token colors
37
+ * from, as `Highlight` does. Colors only.
38
38
  * @type {string | import("./theme.d.ts").ThemePalette | undefined}
39
39
  */
40
40
  export let theme = undefined;
41
41
 
42
+ /**
43
+ * Accessible name of the editor (`aria-label`).
44
+ * @type {string}
45
+ */
46
+ export let label = "Code editor";
47
+
42
48
  import { createEventDispatcher, onMount } from "svelte";
43
- import { lineTokenRanges, retokenizedEnd } from "./editable-css-paint.js";
44
- import { createDomLinePainter } from "./editable-dom-paint.js";
45
- import {
46
- createLineView,
47
- editorNodeAtOffset,
48
- lineStartOffset as lineStartIn,
49
- nodeAtOffset,
50
- renderLines as renderLineDom,
51
- } from "./editable-line-dom.js";
52
- import { toRanges } from "./engine.js";
53
- import {
54
- highlightRules,
55
- highlightRulesFromPalette,
56
- } from "./highlight-theme.js";
57
- import {
58
- parseIncremental,
59
- reparseIncremental,
60
- } from "./incremental-tokenize.js";
61
- import { ensureRegistered, registry } from "./registry.js";
62
- import { diffText } from "./text-diff.js";
49
+ import { createEditor } from "./editable.js";
63
50
 
64
51
  const dispatch = createEventDispatcher();
65
- const TRAILING_NEWLINE = /\n$/;
66
52
  const instanceId = nextInstanceId();
67
53
 
68
54
  /** @type {HTMLElement} */
69
- let editor;
55
+ let element;
70
56
 
71
- // Don't re-highlight during IME composition.
72
- let composing = false;
73
- let mounted = false;
74
- let restoringSelection = false;
75
-
76
- // Set by Escape so the next Tab moves focus instead of indenting.
77
- let tabTrapReleased = false;
78
-
79
- const view = createLineView();
57
+ /** @type {ReturnType<typeof createEditor> | undefined} */
58
+ let editor;
80
59
 
81
60
  // Tracks `code` so parent updates vs local edits can be distinguished.
82
61
  let internalCode = code;
83
62
 
84
- /** @type {import("./incremental-tokenize.js").IncrementalParse | undefined} */
85
- let incrementalParse;
86
-
87
- const domLinePainter = createDomLinePainter();
88
-
89
- function getEvents() {
90
- incrementalParse = incrementalParse
91
- ? reparseIncremental(registry, language.name, incrementalParse, code)
92
- : parseIncremental(registry, language.name, code);
93
- return incrementalParse.events;
94
- }
95
-
96
- $: resolvedEngineValue =
97
- engine === "css-highlights" &&
98
- typeof CSS !== "undefined" &&
99
- typeof CSS.highlights !== "undefined"
100
- ? "css-highlights"
101
- : "dom";
102
-
103
- let previousLanguageName;
104
- let previousEngine;
105
- $: {
106
- ensureRegistered(language);
107
- if (
108
- mounted &&
109
- (language.name !== previousLanguageName ||
110
- resolvedEngineValue !== previousEngine)
111
- ) {
112
- if (previousEngine === "css-highlights") clearCssHighlights();
113
- repaintForLanguageChange();
114
- }
115
- previousLanguageName = language.name;
116
- previousEngine = resolvedEngineValue;
117
- }
118
- $: indentUnit = " ".repeat(tabSize);
119
- $: if (mounted && code !== internalCode) syncExternal();
120
- $: cssHighlightStyle =
121
- resolvedEngineValue === "css-highlights" && theme !== undefined
122
- ? `<style>${(
123
- typeof theme === "object"
124
- ? highlightRulesFromPalette(theme)
125
- : highlightRules(theme)
126
- ).replaceAll(
127
- "::highlight(hljs-",
128
- `::highlight(hljs-${instanceId}-`,
129
- )}</style>`
130
- : "";
131
-
132
- function sumChildLengths(el, count) {
133
- let total = 0;
134
- for (let i = 0; i < count && i < el.childNodes.length; i++) {
135
- total += el.childNodes[i].textContent.length;
136
- }
137
- return total;
138
- }
139
-
140
- // Character offset of a Selection boundary, derived from the line
141
- // structure instead of serializing the document. Null when the DOM has
142
- // drifted from `view`, so the caller falls back to cloneRange/toString.
143
- function offsetOfBoundary(container, offsetInContainer) {
144
- const expectedChildren =
145
- view.lineEls.length === 0 ? 0 : view.lineEls.length * 2 - 1;
146
- if (editor.childNodes.length !== expectedChildren) return null;
147
-
148
- if (container === editor) {
149
- if (view.lineEls.length === 0) return offsetInContainer === 0 ? 0 : null;
150
- const lineIndex = offsetInContainer >> 1;
151
- if (lineIndex >= view.lineEls.length) return null;
152
- return offsetInContainer % 2 === 0
153
- ? lineStartOffset(lineIndex)
154
- : lineStartOffset(lineIndex) + view.lineLengths[lineIndex];
155
- }
156
-
157
- let node = container;
158
- while (node.parentNode !== editor) {
159
- node = node.parentNode;
160
- if (!node) return null;
161
- }
162
-
163
- let childIndex = 0;
164
- for (let sib = node.previousSibling; sib; sib = sib.previousSibling) {
165
- childIndex++;
166
- }
167
- const lineIndex = childIndex >> 1;
168
-
169
- if (childIndex % 2 === 1) {
170
- // A "\n" separator has no children, so it must be `container` itself.
171
- if (container !== node || lineIndex >= view.lineLengths.length)
172
- return null;
173
- return (
174
- lineStartOffset(lineIndex) +
175
- view.lineLengths[lineIndex] +
176
- offsetInContainer
177
- );
178
- }
179
-
180
- const span = node;
181
- if (lineIndex >= view.lineEls.length || view.lineEls[lineIndex] !== span)
182
- return null;
183
-
184
- let charOffset =
185
- container.nodeType === Node.TEXT_NODE
186
- ? offsetInContainer
187
- : sumChildLengths(container, offsetInContainer);
188
- let n = container;
189
- while (n !== span) {
190
- for (let sib = n.previousSibling; sib; sib = sib.previousSibling) {
191
- charOffset += sib.textContent.length;
192
- }
193
- n = n.parentNode;
194
- if (!n) return null;
195
- }
196
- return lineStartOffset(lineIndex) + charOffset;
197
- }
198
-
199
- function getSelectionRange() {
200
- const selection = window.getSelection();
201
- if (!selection || selection.rangeCount === 0) return null;
202
- const range = selection.getRangeAt(0);
203
- if (!editor.contains(range.commonAncestorContainer)) return null;
204
- const start = offsetOfBoundary(range.startContainer, range.startOffset);
205
- const end = offsetOfBoundary(range.endContainer, range.endOffset);
206
- if (start != null && end != null) return { start, end };
207
- const pre = range.cloneRange();
208
- pre.selectNodeContents(editor);
209
- pre.setEnd(range.startContainer, range.startOffset);
210
- const fallbackStart = pre.toString().length;
211
- return {
212
- start: fallbackStart,
213
- end: fallbackStart + range.toString().length,
214
- };
215
- }
216
-
217
- function getCaretOffset() {
218
- const sel = getSelectionRange();
219
- return sel ? sel.end : null;
220
- }
221
-
222
- // Firefox: a boundary at a text node's edge breaks its native undo after
223
- // a repaint, so anchor edges via setStartBefore/After. A boundary at a
224
- // separator's end goes inside the next line's <span>, else Firefox types
225
- // into a stray text node (forcing a full rebuild).
226
- function setRangeBoundary(range, side, node, offset) {
227
- const nextLine = node.nextSibling;
228
- if (
229
- node.parentNode === editor &&
230
- offset === node.textContent.length &&
231
- nextLine?.nodeType === Node.ELEMENT_NODE
232
- ) {
233
- range[side === "Start" ? "setStart" : "setEnd"](nextLine, 0);
234
- } else if (offset === 0) range[`set${side}Before`](node);
235
- else if (offset === node.textContent.length) range[`set${side}After`](node);
236
- else range[side === "Start" ? "setStart" : "setEnd"](node, offset);
237
- }
238
-
239
- function setSelection(start, end, root = editor) {
240
- const locate = (offset) =>
241
- root === editor
242
- ? editorNodeAtOffset(editor, view, offset)
243
- : nodeAtOffset(root, offset);
244
- const from = locate(start);
245
- if (!from) return;
246
- const range = document.createRange();
247
- setRangeBoundary(range, "Start", from.node, from.offset);
248
- const to = end != null && end !== start ? locate(end) : null;
249
- if (to) setRangeBoundary(range, "End", to.node, to.offset);
250
- else range.collapse(true);
251
- const selection = window.getSelection();
252
- selection.removeAllRanges();
253
- selection.addRange(range);
254
- }
255
-
256
- function lineStartOffset(index) {
257
- return lineStartIn(view, index);
258
- }
259
-
260
- const setHtml = (el, line) => {
261
- el.innerHTML = line;
262
- };
263
- const setText = (el, line) => {
264
- el.textContent = line;
265
- };
266
-
267
- function renderLines(lines, setContent) {
268
- return renderLineDom(editor, view, lines, setContent, lineHooks);
269
- }
270
-
271
- const lineHooks = {
272
- onReset: () => clearCssHighlights(),
273
- onSplice: (index, removed, inserted) => {
274
- if (index >= lineHighlightRanges.length) return;
275
- for (let i = index; i < index + removed; i++) clearLineHighlights(i);
276
- lineHighlightRanges = lineHighlightRanges.slice(0, index).concat(
277
- Array.from({ length: inserted }, () => []),
278
- lineHighlightRanges.slice(index + removed),
279
- );
280
- },
281
- };
282
-
283
- /** @type {Map<string, InstanceType<typeof Highlight>>} */
284
- let cssHighlights = new Map();
285
- /** @type {{ scope: string; range: Range }[][]} */
286
- let lineHighlightRanges = [];
287
- /** @type {import("./engine.d.ts").ScopeEvent[] | undefined} */
288
- let cssPaintedEvents;
289
-
290
- function cssHighlightFor(scope) {
291
- let highlight = cssHighlights.get(scope);
292
- if (!highlight) {
293
- highlight = new Highlight();
294
- cssHighlights.set(scope, highlight);
295
- CSS.highlights.set(`hljs-${instanceId}-${scope}`, highlight);
296
- }
297
- return highlight;
298
- }
299
-
300
- function clearLineHighlights(index) {
301
- const previous = lineHighlightRanges[index];
302
- if (!previous) return;
303
- for (const { scope, range } of previous) {
304
- cssHighlights.get(scope)?.delete(range);
305
- }
306
- }
307
-
308
- function clearCssHighlights() {
309
- for (const scope of cssHighlights.keys()) {
310
- CSS.highlights.delete(`hljs-${instanceId}-${scope}`);
311
- }
312
- cssHighlights = new Map();
313
- lineHighlightRanges = [];
314
- cssPaintedEvents = undefined;
315
- }
316
-
317
- // Re-registers ranges for lines from `first` (at offset `base`) through the
318
- // one ending at `end`; `tokenRanges` are relative to `base`. They're sorted
319
- // and disjoint, so `tokenIndex` only advances: O(lines + tokens).
320
- function paintLineHighlights(first, base, end, tokenRanges) {
321
- let tokenIndex = 0;
322
- let lineStart = 0;
323
- for (
324
- let i = first;
325
- i < view.lineEls.length && base + lineStart <= end;
326
- i++
327
- ) {
328
- clearLineHighlights(i);
329
- const lineEnd = lineStart + view.lineLengths[i];
330
- const textNode = view.lineEls[i].firstChild;
331
- const next = [];
332
- if (textNode) {
333
- while (
334
- tokenIndex < tokenRanges.length &&
335
- tokenRanges[tokenIndex].end <= lineStart
336
- ) {
337
- tokenIndex++;
338
- }
339
- for (
340
- let j = tokenIndex;
341
- j < tokenRanges.length && tokenRanges[j].start < lineEnd;
342
- j++
343
- ) {
344
- const token = tokenRanges[j];
345
- if (token.end <= lineStart) continue;
346
- const start = Math.max(token.start, lineStart) - lineStart;
347
- const end = Math.min(token.end, lineEnd) - lineStart;
348
- if (start === end) continue;
349
- const range = new Range();
350
- range.setStart(textNode, start);
351
- range.setEnd(textNode, end);
352
- cssHighlightFor(token.scope).add(range);
353
- next.push({ scope: token.scope, range });
354
- }
355
- }
356
- lineHighlightRanges[i] = next;
357
- lineStart = lineEnd + 1; // +1 for the "\n" separator
358
- }
359
- }
360
-
361
- function paintCssHighlights() {
362
- const changedIndex = renderLines(code.split("\n"), setText);
363
- const previousEvents = cssPaintedEvents;
364
- const events = getEvents();
365
- cssPaintedEvents = events;
366
-
367
- if (changedIndex == null) {
368
- paintLineHighlights(0, 0, code.length, toRanges(events));
369
- } else {
370
- // One line's text changed, but its tokens can affect later lines
371
- // (typing `/*`): repaint through where the re-parse converged.
372
- const base = lineStartOffset(changedIndex);
373
- const end = retokenizedEnd(
374
- code,
375
- events,
376
- previousEvents,
377
- incrementalParse?.reuse,
378
- base,
379
- );
380
- paintLineHighlights(
381
- changedIndex,
382
- base,
383
- end,
384
- lineTokenRanges(events, base, end),
385
- );
386
- }
387
- return changedIndex;
388
- }
389
-
390
- function paint() {
391
- if (resolvedEngineValue === "css-highlights") return paintCssHighlights();
392
- const events = getEvents();
393
- return renderLines(
394
- domLinePainter.paint(
395
- events,
396
- code,
397
- language.name,
398
- incrementalParse?.reuse,
399
- ),
400
- setHtml,
401
- );
402
- }
403
-
404
- function renderAt(start, end) {
405
- const changedIndex = paint();
406
- if (start == null) return;
407
- restoringSelection = true;
408
- if (changedIndex != null) {
409
- const lineStart = lineStartOffset(changedIndex);
410
- const lineEnd = lineStart + view.lineLengths[changedIndex];
411
- const inLine = (offset) =>
412
- offset == null || (offset >= lineStart && offset <= lineEnd);
413
- if (start >= lineStart && start <= lineEnd && inLine(end)) {
414
- setSelection(
415
- start - lineStart,
416
- end == null ? end : end - lineStart,
417
- view.lineEls[changedIndex],
418
- );
419
- restoringSelection = false;
420
- return;
421
- }
422
- }
423
- setSelection(start, end);
424
- restoringSelection = false;
425
- }
426
-
427
- function repaintForLanguageChange() {
428
- incrementalParse = undefined;
429
- domLinePainter.reset();
430
- const caret = document.activeElement === editor ? getCaretOffset() : null;
431
- if (caret == null) paint();
432
- else renderAt(caret, caret);
433
- }
434
-
435
- function syncCaretToHistory() {
436
- if (!editor.contains(document.activeElement)) return;
437
- if (restoringSelection || composing || undoStack.length === 0) return;
438
- const sel = getSelectionRange();
439
- if (!sel) return;
440
- const top = undoStack[undoStack.length - 1];
441
- if (top.resultLength !== code.length) return;
442
- undoStack[undoStack.length - 1] = {
443
- ...top,
444
- selectionAfter: { start: sel.start, end: sel.end },
445
- };
446
- }
447
-
448
- /**
449
- * @typedef {{ start: number; end: number }} Selection
450
- * @typedef {{
451
- * start: number;
452
- * removed: string;
453
- * inserted: string;
454
- * resultLength: number;
455
- * selectionBefore: Selection;
456
- * selectionAfter: Selection;
457
- * }} HistoryEntry
458
- */
459
-
460
- /** @type {HistoryEntry[]} */
461
- let undoStack = [];
462
- /** @type {HistoryEntry[]} */
463
- let redoStack = [];
464
- let lastRecord = 0;
465
-
466
- function emitHistory() {
467
- const future = redoStack.slice().reverse();
468
- dispatch("history", {
469
- entries: [...undoStack, ...future].map((snap) => ({
470
- size: snap.resultLength,
471
- })),
472
- index: undoStack.length - 1,
473
- canUndo: canUndo(),
474
- canRedo: canRedo(),
475
- });
476
- }
477
-
478
- /** @returns {HistoryEntry} */
479
- function makeEntry(before, after, selectionBefore, selectionAfter) {
480
- const { start, removed, inserted } = diffText(before, after);
481
- return {
482
- start,
483
- removed,
484
- inserted,
485
- resultLength: after.length,
486
- selectionBefore,
487
- selectionAfter,
488
- };
489
- }
490
-
491
- // The code an entry transformed *from*, given the code it produced.
492
- function beforeCodeOf(entry, resultCode) {
493
- return (
494
- resultCode.slice(0, entry.start) +
495
- entry.removed +
496
- resultCode.slice(entry.start + entry.inserted.length)
497
- );
498
- }
499
-
500
- // Coalesce typing within 250ms; Enter/Tab/paste pass coalesce=false.
501
- function pushHistory(previousCode, newCode, selectionAfter, coalesce) {
502
- redoStack = [];
503
- const now = Date.now();
504
- const top = undoStack[undoStack.length - 1];
505
-
506
- if (coalesce && undoStack.length > 1 && now - lastRecord < 250) {
507
- const burstStart = beforeCodeOf(top, previousCode);
508
- undoStack[undoStack.length - 1] = makeEntry(
509
- burstStart,
510
- newCode,
511
- top.selectionBefore,
512
- selectionAfter,
513
- );
514
- } else {
515
- undoStack.push(
516
- makeEntry(previousCode, newCode, top.selectionAfter, selectionAfter),
517
- );
518
- const limit = Math.max(1, historyLimit);
519
- if (undoStack.length > limit) {
520
- undoStack.splice(0, undoStack.length - limit);
521
- }
522
- }
523
- lastRecord = coalesce ? now : 0;
524
- emitHistory();
525
- }
526
-
527
- function applyHistoryMove(newCode, selection) {
528
- code = newCode;
529
- internalCode = newCode;
530
- dispatch("change", { code });
531
- emitHistory();
532
- renderAt(selection.start, selection.end);
533
- }
534
-
535
- function commit(value, start, end, coalesce) {
536
- if (readonly) return;
537
- const previousCode = code;
538
- code = value;
539
- internalCode = value;
540
- dispatch("change", { code });
541
- pushHistory(
542
- previousCode,
543
- value,
544
- { start, end: end == null ? start : end },
545
- coalesce,
546
- );
547
- renderAt(start, end);
548
- }
549
-
550
- function syncExternal() {
551
- const previousCode = internalCode;
63
+ $: editor?.update({
64
+ language,
65
+ tabSize,
66
+ historyLimit,
67
+ readonly,
68
+ engine,
69
+ theme,
70
+ });
71
+ $: if (editor && code !== internalCode) {
552
72
  internalCode = code;
553
- pushHistory(
554
- previousCode,
555
- code,
556
- { start: code.length, end: code.length },
557
- false,
558
- );
559
- paint();
560
- }
561
-
562
- function insertText(text) {
563
- const sel = getSelectionRange();
564
- if (!sel) return;
565
- const value = code.slice(0, sel.start) + text + code.slice(sel.end);
566
- commit(value, sel.start + text.length, undefined, false);
567
- }
568
-
569
- // Indent/dedent selected lines; blank lines skipped on indent.
570
- function shiftIndent(outdent) {
571
- const sel = getSelectionRange();
572
- if (!sel) return;
573
- const blockStart = code.lastIndexOf("\n", sel.start - 1) + 1;
574
- const lines = code.slice(blockStart, sel.end).split("\n");
575
-
576
- let firstDelta = 0;
577
- let totalDelta = 0;
578
- const next = lines
579
- .map((line, index) => {
580
- if (outdent) {
581
- const leading = line.length - line.trimStart().length;
582
- const remove = Math.min(tabSize, leading);
583
- if (index === 0) firstDelta = -remove;
584
- totalDelta -= remove;
585
- return line.slice(remove);
586
- }
587
- if (line.length === 0) return line;
588
- if (index === 0) firstDelta = tabSize;
589
- totalDelta += tabSize;
590
- return indentUnit + line;
591
- })
592
- .join("\n");
593
-
594
- if (totalDelta === 0) return;
595
- const value = code.slice(0, blockStart) + next + code.slice(sel.end);
596
- const start = Math.max(blockStart, sel.start + firstDelta);
597
- commit(value, start, sel.end + totalDelta, false);
598
- }
599
-
600
- function indentCommand() {
601
- const sel = getSelectionRange();
602
- if (sel && code.slice(sel.start, sel.end).includes("\n"))
603
- shiftIndent(false);
604
- else insertText(indentUnit);
605
- }
606
-
607
- function ensureFocus() {
608
- if (document.activeElement === editor) return;
609
- const prior = getSelectionRange();
610
- editor.focus();
611
- if (prior) setSelection(prior.start, prior.end);
612
- else setSelection(code.length);
613
- syncCaretToHistory();
73
+ editor.syncCode(code);
614
74
  }
615
75
 
616
76
  export function undo() {
617
- if (undoStack.length <= 1) return;
618
- const entry = undoStack.pop();
619
- redoStack.push(entry);
620
- applyHistoryMove(beforeCodeOf(entry, code), entry.selectionBefore);
77
+ editor?.undo();
621
78
  }
622
79
 
623
80
  export function redo() {
624
- if (redoStack.length === 0) return;
625
- const entry = redoStack.pop();
626
- const nextCode =
627
- code.slice(0, entry.start) +
628
- entry.inserted +
629
- code.slice(entry.start + entry.removed.length);
630
- undoStack.push(entry);
631
- applyHistoryMove(nextCode, entry.selectionAfter);
81
+ editor?.redo();
632
82
  }
633
83
 
634
84
  export function focus() {
635
- editor.focus();
85
+ editor?.focus();
636
86
  }
637
87
 
638
88
  export function selectAll() {
639
- editor.focus();
640
- setSelection(0, code.length);
89
+ editor?.selectAll();
641
90
  }
642
91
 
643
92
  export function insert(text) {
644
- ensureFocus();
645
- insertText(text);
93
+ editor?.insert(text);
646
94
  }
647
95
 
648
96
  export function indent() {
649
- ensureFocus();
650
- indentCommand();
97
+ editor?.indent();
651
98
  }
652
99
 
653
100
  export function outdent() {
654
- ensureFocus();
655
- shiftIndent(true);
101
+ editor?.outdent();
656
102
  }
657
103
 
658
104
  export function setCode(value) {
659
- if (readonly || value === code) return;
660
- const previousCode = code;
661
- internalCode = value;
662
- code = value;
663
- dispatch("change", { code });
664
- pushHistory(
665
- previousCode,
666
- value,
667
- { start: value.length, end: value.length },
668
- false,
669
- );
670
- paint();
105
+ editor?.setCode(value);
671
106
  }
672
107
 
673
108
  export function clear() {
674
- setCode("");
109
+ editor?.clear();
675
110
  }
676
111
 
677
112
  export function getCode() {
@@ -679,195 +114,59 @@
679
114
  }
680
115
 
681
116
  export function canUndo() {
682
- return undoStack.length > 1;
117
+ return editor?.canUndo() ?? false;
683
118
  }
684
119
 
685
120
  export function canRedo() {
686
- return redoStack.length > 0;
121
+ return editor?.canRedo() ?? false;
687
122
  }
688
123
 
689
- /** The engine actually in use: `engine`, or `"dom"` if it was requested but unsupported. */
124
+ /**
125
+ * The engine on screen. `"css-highlights"` once it paints. `"dom"`
126
+ * before mount, where `CSS.highlights` is missing, while the color
127
+ * resolver loads, and while that load has failed.
128
+ */
690
129
  export function resolvedEngine() {
691
- return resolvedEngineValue;
692
- }
693
-
694
- // WebKit fires historyUndo/historyRedo twice per execCommand; ignore the
695
- // repeat until the next task.
696
- let handlingNativeHistory = false;
697
- function runNativeHistory(action) {
698
- if (handlingNativeHistory) return;
699
- handlingNativeHistory = true;
700
- action();
701
- setTimeout(() => {
702
- handlingNativeHistory = false;
703
- }, 0);
704
- }
705
-
706
- function onInput(event) {
707
- if (composing) return;
708
- // Chromium's execCommand undo/redo skips cancelable beforeinput:
709
- // re-render from our history instead of recording the mutation.
710
- if (event?.inputType === "historyUndo") {
711
- runNativeHistory(undo);
712
- return;
713
- }
714
- if (event?.inputType === "historyRedo") {
715
- runNativeHistory(redo);
716
- return;
717
- }
718
- const previousCode = code;
719
- // Not innerText: it forces layout and Firefox can return a stale value
720
- // after editing a node written via textContent. `white-space: pre`
721
- // means native inserts use "\n", not <br>.
722
- code = editor.textContent.replace(TRAILING_NEWLINE, "");
723
- internalCode = code;
724
- const caret = getCaretOffset() ?? code.length;
725
- dispatch("change", { code });
726
- pushHistory(previousCode, code, { start: caret, end: caret }, true);
727
- renderAt(caret);
728
- }
729
-
730
- function onKeydown(event) {
731
- const mod = event.metaKey || event.ctrlKey;
732
-
733
- if (event.key === "Escape") {
734
- tabTrapReleased = true;
735
- return;
736
- }
737
- if (tabTrapReleased) {
738
- tabTrapReleased = false;
739
- if (event.key === "Tab") return;
740
- }
741
-
742
- if (mod && event.key === "z" && !event.shiftKey) {
743
- event.preventDefault();
744
- if (!readonly) undo();
745
- return;
746
- }
747
- if (mod && (event.key === "y" || (event.key === "z" && event.shiftKey))) {
748
- event.preventDefault();
749
- if (!readonly) redo();
750
- return;
751
- }
752
- if (event.key === "Enter") {
753
- event.preventDefault();
754
- insertText("\n");
755
- return;
756
- }
757
- if (event.key === "Tab") {
758
- event.preventDefault();
759
- if (event.shiftKey) shiftIndent(true);
760
- else indentCommand();
761
- }
762
- }
763
-
764
- function onPaste(event) {
765
- event.preventDefault();
766
- insertText(event.clipboardData?.getData("text/plain") ?? "");
767
- }
768
-
769
- function onDrop(event) {
770
- event.preventDefault();
771
- insertText(event.dataTransfer?.getData("text/plain") ?? "");
772
- }
773
-
774
- // Edit-menu / execCommand undo-redo bypass onKeydown.
775
- function onBeforeInput(event) {
776
- if (event.inputType === "historyUndo") {
777
- event.preventDefault();
778
- runNativeHistory(undo);
779
- } else if (event.inputType === "historyRedo") {
780
- event.preventDefault();
781
- runNativeHistory(redo);
782
- }
783
- }
784
-
785
- function onBlur() {
786
- tabTrapReleased = false;
787
- dispatch("blur", { code: getCode() });
788
- }
789
-
790
- // Only listen to document-wide selectionchange while focused.
791
- function onFocusIn() {
792
- document.addEventListener("selectionchange", syncCaretToHistory);
793
- }
794
-
795
- function onFocusOut() {
796
- document.removeEventListener("selectionchange", syncCaretToHistory);
797
- }
798
-
799
- function onCompositionStart() {
800
- composing = true;
801
- }
802
-
803
- function onCompositionEnd() {
804
- composing = false;
805
- onInput();
130
+ return editor?.resolvedEngine() ?? "dom";
806
131
  }
807
132
 
808
133
  onMount(() => {
809
- paint();
810
- undoStack = [
811
- {
812
- start: 0,
813
- removed: "",
814
- inserted: code,
815
- resultLength: code.length,
816
- selectionBefore: { start: 0, end: 0 },
817
- selectionAfter: { start: 0, end: 0 },
134
+ editor = createEditor(element, {
135
+ language,
136
+ code,
137
+ tabSize,
138
+ historyLimit,
139
+ readonly,
140
+ engine,
141
+ theme,
142
+ instanceId,
143
+ onChange: (value) => {
144
+ code = value;
145
+ internalCode = value;
146
+ dispatch("change", { code });
818
147
  },
819
- ];
820
- mounted = true;
821
- emitHistory();
822
-
823
- editor.addEventListener("input", onInput);
824
- editor.addEventListener("keydown", onKeydown);
825
- editor.addEventListener("beforeinput", onBeforeInput);
826
- editor.addEventListener("paste", onPaste);
827
- editor.addEventListener("drop", onDrop);
828
- editor.addEventListener("mouseup", syncCaretToHistory);
829
- editor.addEventListener("keyup", syncCaretToHistory);
830
- editor.addEventListener("focusin", onFocusIn);
831
- editor.addEventListener("focusout", onFocusOut);
832
- editor.addEventListener("blur", onBlur);
833
- editor.addEventListener("compositionstart", onCompositionStart);
834
- editor.addEventListener("compositionend", onCompositionEnd);
835
-
836
- return () => {
837
- editor.removeEventListener("input", onInput);
838
- editor.removeEventListener("keydown", onKeydown);
839
- editor.removeEventListener("beforeinput", onBeforeInput);
840
- editor.removeEventListener("paste", onPaste);
841
- editor.removeEventListener("drop", onDrop);
842
- editor.removeEventListener("mouseup", syncCaretToHistory);
843
- editor.removeEventListener("keyup", syncCaretToHistory);
844
- editor.removeEventListener("focusin", onFocusIn);
845
- editor.removeEventListener("focusout", onFocusOut);
846
- document.removeEventListener("selectionchange", syncCaretToHistory);
847
- editor.removeEventListener("blur", onBlur);
848
- editor.removeEventListener("compositionstart", onCompositionStart);
849
- editor.removeEventListener("compositionend", onCompositionEnd);
850
- clearCssHighlights();
851
- };
148
+ onHistory: (snapshot) => dispatch("history", snapshot),
149
+ onBlur: (value) => dispatch("blur", { code: value }),
150
+ });
151
+ return () => editor?.destroy();
852
152
  });
853
153
  </script>
854
154
 
855
- <svelte:head
856
- >{#if cssHighlightStyle}
857
- {@html cssHighlightStyle}
858
- {/if}</svelte:head
859
- >
860
-
155
+ <!-- svelte-ignore a11y-no-noninteractive-element-to-interactive-role -->
861
156
  <pre
862
157
  style:overflow-x="var(--overflow-x, auto)"
863
158
  style:border-radius="var(--border-radius, 0)"
864
- {...$$restProps}
865
- ><code
866
- bind:this={editor}
159
+ {...$$restProps}><code
160
+ bind:this={element}
867
161
  class:hljs={true}
868
162
  contenteditable={readonly ? "false" : "true"}
869
163
  spellcheck="false"
870
- >{code}</code></pre>
164
+ tabindex="0"
165
+ role="textbox"
166
+ aria-multiline="true"
167
+ aria-readonly={readonly ? "true" : undefined}
168
+ aria-label={label}>{code}</code
169
+ ></pre>
871
170
 
872
171
  <style>
873
172
  code {