1.1.1 • Published 8 months ago

sveltemirror v1.1.1

Weekly downloads
-
License
MIT
Repository
-
Last release
8 months ago

A svelte wrapper for CodeMirror 6

Installation

npm install sveltemirror

Usage

To use sveltemirror, you need to import the package and use it as any Svelte component. For more see docs.

<script lang="ts">
    import CodeMirror from "sveltemirror";
    import { javascript } from "@codemirror/lang-javascript";

    let value = "";
</script>

<CodeMirror bind:value lang={javascript()} />

Note: It debounces and checks for old vs new automatically. There is no need to repeat that logic in your code

Properties

PropertyTypeDescriptionDefault value
valuestringThe value that will be displayed and edited in the CodeMirror editor""
viewEditorViewCodemirror view instance""
basicbooleanWhether to use CodeMirror basicSetup extensions.true
langLanguageSupportThe language extension that will parse and highlight the value.undefined
themeExtensionThe theme extension to customize the appearence of the editor.undefined
extensionsExtension[]Additional extensions to inject in the editor.[]
useTabbooleanWhether to use the Tab shortcut to handle indentation.true
tabSizenumberThe number of space of an indentation level.2
editablebooleanWhether to make the editor editable or not.true
readonlybooleanWhether to make the editor readonly or not.false
placeholderstringA placeholder to include when value is empty.undefined
stylesThemeSpecIn-place theme configuration. See exemple below.undefined
delaynumberThe delay in ms before the change event is triggered. i.e the debounce duration.300
persistbooleanWhether to persist the editor state in the local storage.false
idstringThe id of the editor. It is recommended to use this property when using multiple editors on the same page. If you edit with multiple editors on localhost then it'll treat all of them as the same. So add an id anywayundefined

Events

EventTypeDescription
changestringTrigger when the code changes

Usage with vite/svelte-kit

If you try to use this component with vite or svelte-kit, you have to disable dependency optimization for codemirror and all its extensions.

// vite.config.js
const config = {
    //...
    optimizeDeps: {
        exclude: ["codemirror", "@codemirror/language-javascript" /* ... */],
    },
    //...
}

License

Most of this repository is still the original work by Touchify. Thanks to them for their work ❤️

MIT