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.
- package/AnsiOutput.svelte +33 -6
- package/AnsiOutput.svelte.d.ts +8 -0
- package/CodeToolbar.svelte +144 -0
- package/CodeToolbar.svelte.d.ts +125 -0
- package/CodeWindow.svelte +19 -0
- package/CodeWindow.svelte.d.ts +10 -0
- package/CopyButton.svelte +107 -3
- package/CopyButton.svelte.d.ts +17 -1
- package/DiffFileList.svelte +523 -0
- package/DiffFileList.svelte.d.ts +94 -0
- package/DiffFileListItem.svelte +44 -0
- package/DiffMinimap.svelte +99 -0
- package/DiffMinimap.svelte.d.ts +17 -0
- package/DiffStats.svelte +43 -0
- package/DiffStats.svelte.d.ts +17 -0
- package/DiffView.svelte +1213 -0
- package/DiffView.svelte.d.ts +76 -0
- package/FileTabs.svelte +2 -5
- package/Highlight.svelte +158 -2
- package/Highlight.svelte.d.ts +50 -2
- package/HighlightAuto.svelte.d.ts +3 -2
- package/HighlightAutoLazy.svelte +112 -0
- package/HighlightAutoLazy.svelte.d.ts +66 -0
- package/HighlightDiff.svelte +162 -0
- package/HighlightDiff.svelte.d.ts +102 -0
- package/HighlightEditable.svelte +65 -766
- package/HighlightEditable.svelte.d.ts +15 -5
- package/HighlightStream.svelte +117 -218
- package/HighlightStream.svelte.d.ts +8 -0
- package/HighlightSvelte.svelte.d.ts +3 -2
- package/HighlightVirtual.svelte +41 -8
- package/HighlightVirtual.svelte.d.ts +8 -0
- package/LangTag.svelte +33 -4
- package/LangTag.svelte.d.ts +6 -2
- package/LineNumbers.svelte +25 -4
- package/LineNumbers.svelte.d.ts +8 -0
- package/MarkdownStream.svelte +1 -0
- package/README.md +736 -19
- package/ToolbarButton.svelte +90 -0
- package/ToolbarButton.svelte.d.ts +106 -0
- package/Typewriter.svelte +59 -10
- package/Typewriter.svelte.d.ts +7 -0
- package/WrapToggle.svelte +34 -0
- package/WrapToggle.svelte.d.ts +39 -0
- package/action.js +7 -3
- package/ansi.d.ts +1 -3
- package/ansi.js +44 -12
- package/auto-detect.d.ts +52 -0
- package/auto-detect.js +208 -0
- package/code-region.d.ts +7 -0
- package/code-region.js +20 -0
- package/css-highlights.js +115 -0
- package/css-walk.js +50 -0
- package/diff-controller.d.ts +120 -0
- package/diff-controller.js +721 -0
- package/diff-edits.d.ts +101 -0
- package/diff-edits.js +890 -0
- package/diff-html.d.ts +8 -0
- package/diff-html.js +60 -0
- package/diff-languages.d.ts +4 -0
- package/diff-languages.js +73 -0
- package/diff.d.ts +161 -0
- package/diff.js +1111 -0
- package/editable-commands.js +81 -0
- package/editable-css-paint.js +5 -3
- package/editable-dom-paint.js +1 -1
- package/editable-history.js +199 -0
- package/editable-line-dom.js +176 -1
- package/editable-painter.js +356 -0
- package/editable.d.ts +118 -0
- package/editable.js +513 -0
- package/engine.d.ts +1 -1
- package/fence.js +1 -1
- package/highlight-cascade.js +679 -0
- package/highlight-theme.d.ts +7 -0
- package/highlight-theme.js +23 -46
- package/incremental-tokenize.js +1 -1
- package/index.d.ts +39 -6
- package/index.js +30 -0
- package/languages/detect-index.d.ts +16 -0
- package/languages/detect-index.js +6 -0
- package/languages/loaders.d.ts +8 -0
- package/languages/loaders.js +370 -0
- package/package.json +50 -2
- package/preprocessor.js +20 -5
- package/search.js +2 -2
- package/stream-highlighter.js +198 -0
- package/stream.css +16 -0
- package/stream.d.ts +60 -0
- package/stream.js +283 -0
- package/style-registry.js +34 -3
- package/styles/3024.css +1 -1
- package/styles/3024.js +1 -1
- package/styles/apathy.css +1 -1
- package/styles/apathy.js +1 -1
- package/styles/apprentice.css +1 -1
- package/styles/apprentice.js +1 -1
- package/styles/ashes.css +1 -1
- package/styles/ashes.js +1 -1
- package/styles/atelier-cave-light.css +1 -1
- package/styles/atelier-cave-light.js +1 -1
- package/styles/atelier-cave.css +1 -1
- package/styles/atelier-cave.js +1 -1
- package/styles/atelier-dune-light.css +1 -1
- package/styles/atelier-dune-light.js +1 -1
- package/styles/atelier-dune.css +1 -1
- package/styles/atelier-dune.js +1 -1
- package/styles/atelier-estuary-light.css +1 -1
- package/styles/atelier-estuary-light.js +1 -1
- package/styles/atelier-estuary.css +1 -1
- package/styles/atelier-estuary.js +1 -1
- package/styles/atelier-forest-light.css +1 -1
- package/styles/atelier-forest-light.js +1 -1
- package/styles/atelier-forest.css +1 -1
- package/styles/atelier-forest.js +1 -1
- package/styles/atelier-heath-light.css +1 -1
- package/styles/atelier-heath-light.js +1 -1
- package/styles/atelier-heath.css +1 -1
- package/styles/atelier-heath.js +1 -1
- package/styles/atelier-lakeside-light.css +1 -1
- package/styles/atelier-lakeside-light.js +1 -1
- package/styles/atelier-lakeside.css +1 -1
- package/styles/atelier-lakeside.js +1 -1
- package/styles/atelier-plateau-light.css +1 -1
- package/styles/atelier-plateau-light.js +1 -1
- package/styles/atelier-plateau.css +1 -1
- package/styles/atelier-plateau.js +1 -1
- package/styles/atelier-savanna-light.css +1 -1
- package/styles/atelier-savanna-light.js +1 -1
- package/styles/atelier-savanna.css +1 -1
- package/styles/atelier-savanna.js +1 -1
- package/styles/atelier-seaside-light.css +1 -1
- package/styles/atelier-seaside-light.js +1 -1
- package/styles/atelier-seaside.css +1 -1
- package/styles/atelier-seaside.js +1 -1
- package/styles/atelier-sulphurpool-light.css +1 -1
- package/styles/atelier-sulphurpool-light.js +1 -1
- package/styles/atelier-sulphurpool.css +1 -1
- package/styles/atelier-sulphurpool.js +1 -1
- package/styles/atlas.css +1 -1
- package/styles/atlas.js +1 -1
- package/styles/base16-github.css +1 -1
- package/styles/base16-github.js +1 -1
- package/styles/base16-ir-black.css +1 -1
- package/styles/base16-ir-black.js +1 -1
- package/styles/base16-monokai.css +1 -1
- package/styles/base16-monokai.js +1 -1
- package/styles/base16-nord.css +1 -1
- package/styles/base16-nord.js +1 -1
- package/styles/bespin.css +1 -1
- package/styles/bespin.js +1 -1
- package/styles/black-metal-bathory.css +1 -1
- package/styles/black-metal-bathory.js +1 -1
- package/styles/black-metal-burzum.css +1 -1
- package/styles/black-metal-burzum.js +1 -1
- package/styles/black-metal-dark-funeral.css +1 -1
- package/styles/black-metal-dark-funeral.js +1 -1
- package/styles/black-metal-gorgoroth.css +1 -1
- package/styles/black-metal-gorgoroth.js +1 -1
- package/styles/black-metal-immortal.css +1 -1
- package/styles/black-metal-immortal.js +1 -1
- package/styles/black-metal-khold.css +1 -1
- package/styles/black-metal-khold.js +1 -1
- package/styles/black-metal-marduk.css +1 -1
- package/styles/black-metal-marduk.js +1 -1
- package/styles/black-metal-mayhem.css +1 -1
- package/styles/black-metal-mayhem.js +1 -1
- package/styles/black-metal-nile.css +1 -1
- package/styles/black-metal-nile.js +1 -1
- package/styles/black-metal-venom.css +1 -1
- package/styles/black-metal-venom.js +1 -1
- package/styles/black-metal.css +1 -1
- package/styles/black-metal.js +1 -1
- package/styles/brewer.css +1 -1
- package/styles/brewer.js +1 -1
- package/styles/bright.css +1 -1
- package/styles/bright.js +1 -1
- package/styles/brogrammer.css +1 -1
- package/styles/brogrammer.js +1 -1
- package/styles/brush-trees-dark.css +1 -1
- package/styles/brush-trees-dark.js +1 -1
- package/styles/brush-trees.css +1 -1
- package/styles/brush-trees.js +1 -1
- package/styles/chalk.css +1 -1
- package/styles/chalk.js +1 -1
- package/styles/circus.css +1 -1
- package/styles/circus.js +1 -1
- package/styles/classic-dark.css +1 -1
- package/styles/classic-dark.js +1 -1
- package/styles/classic-light.css +1 -1
- package/styles/classic-light.js +1 -1
- package/styles/codeschool.css +1 -1
- package/styles/codeschool.js +1 -1
- package/styles/colors.css +1 -1
- package/styles/colors.js +1 -1
- package/styles/cupcake.css +1 -1
- package/styles/cupcake.js +1 -1
- package/styles/cupertino.css +1 -1
- package/styles/cupertino.js +1 -1
- package/styles/cybertopia-cherry.css +2 -1
- package/styles/cybertopia-cherry.js +2 -1
- package/styles/cybertopia-dimmer.css +2 -1
- package/styles/cybertopia-dimmer.js +2 -1
- package/styles/cybertopia-icecap.css +2 -1
- package/styles/cybertopia-icecap.js +2 -1
- package/styles/cybertopia-saturated.css +2 -1
- package/styles/cybertopia-saturated.js +2 -1
- package/styles/danqing.css +1 -1
- package/styles/danqing.js +1 -1
- package/styles/darcula.css +1 -1
- package/styles/darcula.js +1 -1
- package/styles/dark-violet.css +1 -1
- package/styles/dark-violet.js +1 -1
- package/styles/darkmoss.css +1 -1
- package/styles/darkmoss.js +1 -1
- package/styles/darktooth.css +1 -1
- package/styles/darktooth.js +1 -1
- package/styles/decaf.css +1 -1
- package/styles/decaf.js +1 -1
- package/styles/default-dark.css +1 -1
- package/styles/default-dark.js +1 -1
- package/styles/default-light.css +1 -1
- package/styles/default-light.js +1 -1
- package/styles/dirtysea.css +1 -1
- package/styles/dirtysea.js +1 -1
- package/styles/docco.css +1 -1
- package/styles/docco.js +1 -1
- package/styles/dracula.css +1 -1
- package/styles/dracula.js +1 -1
- package/styles/edge-dark.css +1 -1
- package/styles/edge-dark.js +1 -1
- package/styles/edge-light.css +1 -1
- package/styles/edge-light.js +1 -1
- package/styles/eighties.css +1 -1
- package/styles/eighties.js +1 -1
- package/styles/embers.css +1 -1
- package/styles/embers.js +1 -1
- package/styles/equilibrium-dark.css +1 -1
- package/styles/equilibrium-dark.js +1 -1
- package/styles/equilibrium-gray-dark.css +1 -1
- package/styles/equilibrium-gray-dark.js +1 -1
- package/styles/equilibrium-gray-light.css +1 -1
- package/styles/equilibrium-gray-light.js +1 -1
- package/styles/equilibrium-light.css +1 -1
- package/styles/equilibrium-light.js +1 -1
- package/styles/espresso.css +1 -1
- package/styles/espresso.js +1 -1
- package/styles/eva-dim.css +1 -1
- package/styles/eva-dim.js +1 -1
- package/styles/eva.css +1 -1
- package/styles/eva.js +1 -1
- package/styles/flat.css +1 -1
- package/styles/flat.js +1 -1
- package/styles/framer.css +1 -1
- package/styles/framer.js +1 -1
- package/styles/fruit-soda.css +1 -1
- package/styles/fruit-soda.js +1 -1
- package/styles/gigavolt.css +1 -1
- package/styles/gigavolt.js +1 -1
- package/styles/gml.css +1 -1
- package/styles/gml.js +1 -1
- package/styles/google-dark.css +1 -1
- package/styles/google-dark.js +1 -1
- package/styles/google-light.css +1 -1
- package/styles/google-light.js +1 -1
- package/styles/googlecode.css +1 -1
- package/styles/googlecode.js +1 -1
- package/styles/grayscale-dark.css +1 -1
- package/styles/grayscale-dark.js +1 -1
- package/styles/grayscale-light.css +1 -1
- package/styles/grayscale-light.js +1 -1
- package/styles/grayscale.css +1 -1
- package/styles/grayscale.js +1 -1
- package/styles/green-screen.css +1 -1
- package/styles/green-screen.js +1 -1
- package/styles/gruvbox-dark-hard.css +1 -1
- package/styles/gruvbox-dark-hard.js +1 -1
- package/styles/gruvbox-dark-medium.css +1 -1
- package/styles/gruvbox-dark-medium.js +1 -1
- package/styles/gruvbox-dark-pale.css +1 -1
- package/styles/gruvbox-dark-pale.js +1 -1
- package/styles/gruvbox-dark-soft.css +1 -1
- package/styles/gruvbox-dark-soft.js +1 -1
- package/styles/gruvbox-light-hard.css +1 -1
- package/styles/gruvbox-light-hard.js +1 -1
- package/styles/gruvbox-light-medium.css +1 -1
- package/styles/gruvbox-light-medium.js +1 -1
- package/styles/gruvbox-light-soft.css +1 -1
- package/styles/gruvbox-light-soft.js +1 -1
- package/styles/hardcore.css +1 -1
- package/styles/hardcore.js +1 -1
- package/styles/harmonic16-dark.css +1 -1
- package/styles/harmonic16-dark.js +1 -1
- package/styles/harmonic16-light.css +1 -1
- package/styles/harmonic16-light.js +1 -1
- package/styles/heetch-dark.css +1 -1
- package/styles/heetch-dark.js +1 -1
- package/styles/heetch-light.css +1 -1
- package/styles/heetch-light.js +1 -1
- package/styles/helios.css +1 -1
- package/styles/helios.js +1 -1
- package/styles/hopscotch.css +1 -1
- package/styles/hopscotch.js +1 -1
- package/styles/horizon-dark.css +1 -1
- package/styles/horizon-dark.js +1 -1
- package/styles/horizon-light.css +1 -1
- package/styles/horizon-light.js +1 -1
- package/styles/humanoid-dark.css +1 -1
- package/styles/humanoid-dark.js +1 -1
- package/styles/humanoid-light.css +1 -1
- package/styles/humanoid-light.js +1 -1
- package/styles/ia-dark.css +1 -1
- package/styles/ia-dark.js +1 -1
- package/styles/ia-light.css +1 -1
- package/styles/ia-light.js +1 -1
- package/styles/icy-dark.css +1 -1
- package/styles/icy-dark.js +1 -1
- package/styles/idea.css +1 -1
- package/styles/idea.js +1 -1
- package/styles/intellij-light.css +1 -1
- package/styles/intellij-light.js +1 -1
- package/styles/ir-black.css +1 -1
- package/styles/ir-black.js +1 -1
- package/styles/isotope.css +1 -1
- package/styles/isotope.js +1 -1
- package/styles/kimber.css +1 -1
- package/styles/kimber.js +1 -1
- package/styles/kimbie-dark.css +1 -1
- package/styles/kimbie-dark.js +1 -1
- package/styles/kimbie-light.css +1 -1
- package/styles/kimbie-light.js +1 -1
- package/styles/london-tube.css +1 -1
- package/styles/london-tube.js +1 -1
- package/styles/macintosh.css +1 -1
- package/styles/macintosh.js +1 -1
- package/styles/marrakesh.css +1 -1
- package/styles/marrakesh.js +1 -1
- package/styles/materia.css +1 -1
- package/styles/materia.js +1 -1
- package/styles/material-darker.css +1 -1
- package/styles/material-darker.js +1 -1
- package/styles/material-lighter.css +1 -1
- package/styles/material-lighter.js +1 -1
- package/styles/material-palenight.css +1 -1
- package/styles/material-palenight.js +1 -1
- package/styles/material-vivid.css +1 -1
- package/styles/material-vivid.js +1 -1
- package/styles/material.css +1 -1
- package/styles/material.js +1 -1
- package/styles/mellow-purple.css +1 -1
- package/styles/mellow-purple.js +1 -1
- package/styles/mexico-light.css +1 -1
- package/styles/mexico-light.js +1 -1
- package/styles/mocha.css +1 -1
- package/styles/mocha.js +1 -1
- package/styles/nebula.css +1 -1
- package/styles/nebula.js +1 -1
- package/styles/nnfx-dark.css +1 -1
- package/styles/nnfx-dark.js +1 -1
- package/styles/nnfx-light.css +1 -1
- package/styles/nnfx-light.js +1 -1
- package/styles/nord.css +1 -1
- package/styles/nord.js +1 -1
- package/styles/nova.css +1 -1
- package/styles/nova.js +1 -1
- package/styles/ocean.css +1 -1
- package/styles/ocean.js +1 -1
- package/styles/oceanicnext.css +1 -1
- package/styles/oceanicnext.js +1 -1
- package/styles/one-light.css +1 -1
- package/styles/one-light.js +1 -1
- package/styles/onedark.css +1 -1
- package/styles/onedark.js +1 -1
- package/styles/outrun-dark.css +1 -1
- package/styles/outrun-dark.js +1 -1
- package/styles/papercolor-dark.css +1 -1
- package/styles/papercolor-dark.js +1 -1
- package/styles/papercolor-light.css +1 -1
- package/styles/papercolor-light.js +1 -1
- package/styles/paraiso.css +1 -1
- package/styles/paraiso.js +1 -1
- package/styles/pasque.css +1 -1
- package/styles/pasque.js +1 -1
- package/styles/phd.css +1 -1
- package/styles/phd.js +1 -1
- package/styles/pico.css +1 -1
- package/styles/pico.js +1 -1
- package/styles/pojoaque.css +1 -1
- package/styles/pojoaque.js +1 -1
- package/styles/pop.css +1 -1
- package/styles/pop.js +1 -1
- package/styles/porple.css +1 -1
- package/styles/porple.js +1 -1
- package/styles/qualia.css +1 -1
- package/styles/qualia.js +1 -1
- package/styles/railscasts.css +1 -1
- package/styles/railscasts.js +1 -1
- package/styles/rebecca.css +1 -1
- package/styles/rebecca.js +1 -1
- package/styles/ros-pine-dawn.css +1 -1
- package/styles/ros-pine-dawn.js +1 -1
- package/styles/ros-pine-moon.css +1 -1
- package/styles/ros-pine-moon.js +1 -1
- package/styles/ros-pine.css +1 -1
- package/styles/ros-pine.js +1 -1
- package/styles/sagelight.css +1 -1
- package/styles/sagelight.js +1 -1
- package/styles/sandcastle.css +1 -1
- package/styles/sandcastle.js +1 -1
- package/styles/school-book.css +1 -1
- package/styles/school-book.js +1 -1
- package/styles/seti-ui.css +1 -1
- package/styles/seti-ui.js +1 -1
- package/styles/shapeshifter.css +1 -1
- package/styles/shapeshifter.js +1 -1
- package/styles/silk-dark.css +1 -1
- package/styles/silk-dark.js +1 -1
- package/styles/silk-light.css +1 -1
- package/styles/silk-light.js +1 -1
- package/styles/snazzy.css +1 -1
- package/styles/snazzy.js +1 -1
- package/styles/solar-flare-light.css +1 -1
- package/styles/solar-flare-light.js +1 -1
- package/styles/solar-flare.css +1 -1
- package/styles/solar-flare.js +1 -1
- package/styles/solarized-dark.css +1 -1
- package/styles/solarized-dark.js +1 -1
- package/styles/solarized-light.css +1 -1
- package/styles/solarized-light.js +1 -1
- package/styles/spacemacs.css +1 -1
- package/styles/spacemacs.js +1 -1
- package/styles/stackoverflow-dark.css +1 -1
- package/styles/stackoverflow-dark.js +1 -1
- package/styles/stackoverflow-light.css +1 -1
- package/styles/stackoverflow-light.js +1 -1
- package/styles/summercamp.css +1 -1
- package/styles/summercamp.js +1 -1
- package/styles/summerfruit-dark.css +1 -1
- package/styles/summerfruit-dark.js +1 -1
- package/styles/summerfruit-light.css +1 -1
- package/styles/summerfruit-light.js +1 -1
- package/styles/synth-midnight-terminal-dark.css +1 -1
- package/styles/synth-midnight-terminal-dark.js +1 -1
- package/styles/synth-midnight-terminal-light.css +1 -1
- package/styles/synth-midnight-terminal-light.js +1 -1
- package/styles/tango.css +1 -1
- package/styles/tango.js +1 -1
- package/styles/tender.css +1 -1
- package/styles/tender.js +1 -1
- package/styles/tomorrow-night.css +1 -1
- package/styles/tomorrow-night.js +1 -1
- package/styles/tomorrow.css +1 -1
- package/styles/tomorrow.js +1 -1
- package/styles/twilight.css +1 -1
- package/styles/twilight.js +1 -1
- package/styles/unikitty-dark.css +1 -1
- package/styles/unikitty-dark.js +1 -1
- package/styles/unikitty-light.css +1 -1
- package/styles/unikitty-light.js +1 -1
- package/styles/vs2015.css +1 -1
- package/styles/vs2015.js +1 -1
- package/styles/vulcan.css +1 -1
- package/styles/vulcan.js +1 -1
- package/styles/windows-10-light.css +1 -1
- package/styles/windows-10-light.js +1 -1
- package/styles/windows-10.css +1 -1
- package/styles/windows-10.js +1 -1
- package/styles/windows-95-light.css +1 -1
- package/styles/windows-95-light.js +1 -1
- package/styles/windows-95.css +1 -1
- package/styles/windows-95.js +1 -1
- package/styles/windows-high-contrast-light.css +1 -1
- package/styles/windows-high-contrast-light.js +1 -1
- package/styles/windows-high-contrast.css +1 -1
- package/styles/windows-high-contrast.js +1 -1
- package/styles/windows-nt-light.css +1 -1
- package/styles/windows-nt-light.js +1 -1
- package/styles/windows-nt.css +1 -1
- package/styles/windows-nt.js +1 -1
- package/styles/woodland.css +1 -1
- package/styles/woodland.js +1 -1
- package/styles/xcode-dusk.css +1 -1
- package/styles/xcode-dusk.js +1 -1
- package/styles/xcode.css +1 -1
- package/styles/xcode.js +1 -1
- package/styles/xt256.css +1 -1
- package/styles/xt256.js +1 -1
- package/styles/zenburn.css +1 -1
- package/styles/zenburn.js +1 -1
- package/themes/_base-css.d.ts +1 -0
- package/themes/_base-css.js +1 -0
- package/themes/cybertopia-cherry.css +5 -1
- package/themes/cybertopia-cherry.js +1 -1
- package/themes/cybertopia-dimmer.css +5 -1
- package/themes/cybertopia-dimmer.js +1 -1
- package/themes/cybertopia-icecap.css +5 -1
- package/themes/cybertopia-icecap.js +1 -1
- package/themes/cybertopia-saturated.css +5 -1
- package/themes/cybertopia-saturated.js +1 -1
- package/typewriter-units.d.ts +3 -0
- package/typewriter-units.js +43 -0
- package/virtual-window.js +9 -4
package/HighlightEditable.svelte
CHANGED
|
@@ -33,645 +33,80 @@
|
|
|
33
33
|
export let engine = "dom";
|
|
34
34
|
|
|
35
35
|
/**
|
|
36
|
-
* Theme CSS or `ThemePalette`
|
|
37
|
-
*
|
|
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 {
|
|
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
|
|
55
|
+
let element;
|
|
70
56
|
|
|
71
|
-
|
|
72
|
-
let
|
|
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
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
85
|
+
editor?.focus();
|
|
636
86
|
}
|
|
637
87
|
|
|
638
88
|
export function selectAll() {
|
|
639
|
-
editor
|
|
640
|
-
setSelection(0, code.length);
|
|
89
|
+
editor?.selectAll();
|
|
641
90
|
}
|
|
642
91
|
|
|
643
92
|
export function insert(text) {
|
|
644
|
-
|
|
645
|
-
insertText(text);
|
|
93
|
+
editor?.insert(text);
|
|
646
94
|
}
|
|
647
95
|
|
|
648
96
|
export function indent() {
|
|
649
|
-
|
|
650
|
-
indentCommand();
|
|
97
|
+
editor?.indent();
|
|
651
98
|
}
|
|
652
99
|
|
|
653
100
|
export function outdent() {
|
|
654
|
-
|
|
655
|
-
shiftIndent(true);
|
|
101
|
+
editor?.outdent();
|
|
656
102
|
}
|
|
657
103
|
|
|
658
104
|
export function setCode(value) {
|
|
659
|
-
|
|
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
|
-
|
|
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
|
|
117
|
+
return editor?.canUndo() ?? false;
|
|
683
118
|
}
|
|
684
119
|
|
|
685
120
|
export function canRedo() {
|
|
686
|
-
return
|
|
121
|
+
return editor?.canRedo() ?? false;
|
|
687
122
|
}
|
|
688
123
|
|
|
689
|
-
/**
|
|
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
|
|
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
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
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
|
-
|
|
821
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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 {
|