### Initialize Basic CodeMirror Editor Source: https://github.com/codemirror/website/blob/main/site/examples/basic/index.md Instantiate an EditorView with basic setup. This is the simplest way to get a functional editor. ```javascript import {basicSetup} from "codemirror" import {EditorView} from "@codemirror/view" const view = new EditorView({ doc: "Start document", parent: document.body, extensions: [basicSetup] }) ``` -------------------------------- ### Minimal CodeMirror Editor Setup Source: https://github.com/codemirror/website/blob/main/site/docs/ref/index.html Demonstrates the basic setup for a CodeMirror editor using essential view and command modules. Requires importing EditorView and keymap from '@codemirror/view' and defaultKeymap from '@codemirror/commands'. ```javascript import {EditorView, keymap} from "@codemirror/view" import {defaultKeymap} from "@codemirror/commands" let myView = new EditorView({ doc: "hello", extensions: [keymap.of(defaultKeymap)], parent: document.body }) ``` -------------------------------- ### CodeMirror Editor Setup with Basic Extensions Source: https://github.com/codemirror/website/blob/main/site/docs/guide/index.md Configures a CodeMirror editor with common features included in the 'basicSetup' extension and language support for JavaScript. This is a more convenient way to start. ```javascript import {EditorView, basicSetup} from "codemirror" import {javascript} from "@codemirror/lang-javascript" let view = new EditorView({ extensions: [basicSetup, javascript()], parent: document.body }) ``` -------------------------------- ### Build CodeMirror Website Source: https://github.com/codemirror/website/blob/main/README.md Standard npm commands to install dependencies, build the website, and list the output directory. ```bash npm i npm run build ls output # ← the website is in there ``` -------------------------------- ### Main Editor State Setup Source: https://github.com/codemirror/website/blob/main/site/examples/split/index.md Sets up the state for the main editor, including the history extension. ```typescript let startState = EditorState.create({ doc: "The first editor content.\n", extensions: [ history(), EditorView.updateListener.of(update => { if (update.docChanged) console.log("Main editor changed") }) ] }); ``` -------------------------------- ### Install CodeMirror and Rollup Dependencies Source: https://github.com/codemirror/website/blob/main/site/examples/bundle/index.md Install the required CodeMirror packages, Rollup, and the node-resolve plugin using npm. This sets up your project for bundling. ```shell # The CodeMirror packages used in our script npm i codemirror @codemirror/lang-javascript # Rollup and its plugin npm i rollup @rollup/plugin-node-resolve ``` -------------------------------- ### Minimal CodeMirror Editor Setup Source: https://github.com/codemirror/website/blob/main/site/examples/bundle/index.md This is the most basic CodeMirror editor setup. It uses `minimalSetup` which includes essential extensions for a functional editor, resulting in a smaller bundle size compared to `basicSetup`. ```javascript import {EditorView, minimalSetup} from "codemirror" let editor = new EditorView({ extensions: minimalSetup, parent: document.body }) ``` -------------------------------- ### Bundle CodeMirror with Rollup Source: https://context7.com/codemirror/website/llms.txt This example demonstrates how to bundle a CodeMirror editor using Rollup for browser compatibility. Ensure you have installed the necessary npm packages. ```javascript import { EditorView, basicSetup } from "codemirror" import { javascript } from "@codemirror/lang-javascript" new EditorView({ extensions: [basicSetup, javascript()], parent: document.body }) ``` ```shell # Install packages npm install codemirror @codemirror/lang-javascript npm install rollup @rollup/plugin-node-resolve # Bundle npx rollup editor.mjs -f iife -o editor.bundle.js -p @rollup/plugin-node-resolve ``` ```javascript // rollup.config.mjs import { nodeResolve } from "@rollup/plugin-node-resolve" export default { input: "./editor.mjs", output: { file: "./editor.bundle.js", format: "iife" }, plugins: [nodeResolve()] } ``` ```html ``` -------------------------------- ### Example Usage of ToggleWith Source: https://github.com/codemirror/website/blob/main/site/examples/config/index.md Demonstrates how to use the `toggleWith` function to conditionally apply editor attributes. This example applies a yellow background when the extension is active. ```javascript toggleWith("Mod-o", EditorView.editorAttributes.of({ style: "background: yellow" })) ``` -------------------------------- ### Extension Precedence Example Source: https://github.com/codemirror/website/blob/main/site/examples/config/index.md Demonstrates how extensions are ordered and how precedence affects their resolution. Higher precedence extensions are applied before lower ones. ```javascript [..., [keymap.of(A)], ..., [[], ..., [keymap.of(B)]]] ``` ```javascript [Prec.high(A), B, Prec.high([C, D])] ``` -------------------------------- ### Minimal Viable CodeMirror Editor Setup Source: https://github.com/codemirror/website/blob/main/site/docs/guide/index.md Sets up a basic CodeMirror editor using core packages. Requires explicit import of state, view, and command modules. ```javascript import {EditorState} from "@codemirror/state" import {EditorView, keymap} from "@codemirror/view" import {defaultKeymap} from "@codemirror/commands" let startState = EditorState.create({ doc: "Hello World", extensions: [keymap.of(defaultKeymap)] }) let view = new EditorView({ state: startState, parent: document.body }) ``` -------------------------------- ### Basic Editor Setup with basicSetup Source: https://context7.com/codemirror/website/llms.txt Sets up a minimal CodeMirror editor with common extensions like syntax highlighting and autocompletion. Requires importing EditorView, basicSetup, and language extensions. ```javascript import { EditorView, basicSetup } from "codemirror" import { javascript } from "@codemirror/lang-javascript" const view = new EditorView({ doc: "console.log('hello world')", extensions: [basicSetup, javascript()], parent: document.getElementById("editor") }) // Read current document console.log(view.state.doc.toString()) // => "console.log('hello world')" ``` -------------------------------- ### Integrate Language Support in CodeMirror Source: https://github.com/codemirror/website/blob/main/site/examples/basic/index.md Add language-specific support, such as JavaScript syntax highlighting and autocompletion, to the CodeMirror editor. This example includes the basic setup and the JavaScript language extension. ```javascript import {javascript} from "@codemirror/lang-javascript" import {EditorView, basicSetup} from "codemirror" const view = new EditorView({ doc: "Start document", parent: document.body, extensions: [ basicSetup, javascript({typescript: true}) ] }) ``` -------------------------------- ### Minimal Editor Setup (Low Bundle Size) Source: https://context7.com/codemirror/website/llms.txt Constructs a CodeMirror editor with only essential extensions for a smaller bundle size. This approach requires manually selecting and importing each needed extension. ```javascript import { EditorView, keymap, lineNumbers, highlightActiveLine, highlightSpecialChars, drawSelection, dropCursor } from "@codemirror/view" import { EditorState, EditorStateConfig } from "@codemirror/state" import { defaultKeymap, history, historyKeymap } from "@codemirror/commands" import { syntaxHighlighting, defaultHighlightStyle } from "@codemirror/language" import { javascript } from "@codemirror/lang-javascript" const view = new EditorView({ doc: "// type here", parent: document.body, extensions: [ lineNumbers(), highlightSpecialChars(), history(), drawSelection(), dropCursor(), highlightActiveLine(), syntaxHighlighting(defaultHighlightStyle), javascript(), keymap.of([...defaultKeymap, ...historyKeymap]) ] }) ``` -------------------------------- ### Second Editor State Setup Source: https://github.com/codemirror/website/blob/main/site/examples/split/index.md Sets up the state for the second editor, which does not track its own history but binds history keys to the main editor. ```typescript let otherState = EditorState.create({ doc: "The second editor content.\n", extensions: [ EditorView.updateListener.of(update => { if (update.docChanged) console.log("Second editor changed") }) ] }); ``` -------------------------------- ### Extension Bundles Source: https://github.com/codemirror/website/blob/main/site/docs/extensions/index.html Pre-defined bundles of extensions for common setups. ```APIDOC ## basicSetup ### Description An array of extensions that enables many of the features listed on this page. ### Type Extension Array ### Reference `[basicSetup](../ref/#codemirror.basicSetup)` ``` ```APIDOC ## minimalSetup ### Description A much more minimal collection of extensions, containing just the ones that are recommended for every editor. ### Type Extension Array ### Reference `[minimalSetup](../ref/#codemirror.minimalSetup)` ``` -------------------------------- ### Setup Collaborative Editing Environment Source: https://github.com/codemirror/website/blob/main/site/examples/collab/index.md Includes HTML structure and script tags to load CodeMirror and the collaborative editing module. This sets up the server controls and editor areas. ```html
Hello World
", extensions: [ basicSetup, autocompletion(), html() ] }), parent: document.getElementById("editor-html") }) ``` -------------------------------- ### Custom Completion Source with Override Source: https://github.com/codemirror/website/blob/main/site/examples/autocompletion/index.md Provide a custom completion source by overriding the default behavior. This example uses `matchBefore` to find completable text and returns a list of options. ```typescript const editor = new EditorView({ state: EditorState.create({ doc: "", extensions: [ basicSetup, autocompletion({ override: [ (context: CompletionContext) => { const word = context.matchBefore(/\w*/) if (!word || (word.from == word.to && !context.explicit)) return null return { from: word.from, options: [ {label: "function", type: "keyword"}, {label: "variable", type: "variable"}, {label: "class", type: "keyword"}, {label: "interface", type: "keyword"}, {label: "enum", type: "keyword"} ] } } ] }) ] }), parent: document.getElementById("editor-override") }) ``` -------------------------------- ### Move Cursor to Document Start Source: https://github.com/codemirror/website/blob/main/site/examples/selection/index.md Dispatches a transaction to move the cursor to the beginning of the document. The `selection` property accepts a shorthand object. ```javascript view.dispatch({selection: {anchor: 0}}) ``` -------------------------------- ### Create Wired-Up Editor View Source: https://github.com/codemirror/website/blob/main/site/examples/collab/index.md Creates an EditorView instance with the necessary extensions for collaborative editing, including basic setup and the peer extension. ```typescript async function createPeer(connection: Connection) { let {version, doc} = await getDocument(connection) let state = EditorState.create({ doc, extensions: [basicSetup, peerExtension(version, connection)] }) return new EditorView({state}) } ``` -------------------------------- ### Enable Syntax Highlighting for JavaScript Source: https://github.com/codemirror/website/blob/main/site/docs/migration/index.md Add extensions for language support and syntax highlighting to the editor configuration. This example enables JavaScript highlighting and indentation using default styles. ```javascript import {syntaxHighlighting, defaultHighlightStyle} from "@codemirror/language" import {javascript} from "@codemirror/lang-javascript" // Add these extensions javascript(), syntaxHighlighting(defaultHighlightStyle), ``` -------------------------------- ### Basic RTL Editor Setup Source: https://github.com/codemirror/website/blob/main/site/examples/bidi/index.md To create a basic editor for Arabic or Hebrew text, style the editor or a parent document with `direction: rtl`. This sets the base direction for text rendering. ```html ``` -------------------------------- ### Insert Text at Document Start Source: https://github.com/codemirror/website/blob/main/site/examples/change/index.md Use the `dispatch` method with a `changes` object to insert text at a specific position, like the beginning of the document. ```javascript view.dispatch({ changes: {from: 0, insert: "#!/usr/bin/env node\n"} }) ``` -------------------------------- ### Keymaps and Commands in CodeMirror Source: https://context7.com/codemirror/website/llms.txt Define custom keybindings and commands using the `keymap` facet. This example includes a high-precedence binding for saving and a default binding for inserting a character. ```javascript import { EditorView, keymap } from "@codemirror/view" import { EditorState, Prec } from "@codemirror/state" import { defaultKeymap, history, historyKeymap, undo, redo } from "@codemirror/commands" const view = new EditorView({ extensions: [ history(), // High-precedence custom binding runs before the defaults Prec.high(keymap.of([{ key: "Mod-s", run(view) { console.log("Saved:", view.state.doc.toString()) return true } }])), keymap.of([ ...defaultKeymap, ...historyKeymap, { key: "Alt-c", run(view) { view.dispatch(view.state.replaceSelection("✓")) return true } } ]) ], parent: document.body }) // Invoke commands programmatically undo(view) redo(view) ``` -------------------------------- ### Linting with @codemirror/lint Source: https://context7.com/codemirror/website/llms.txt Implement inline diagnostics by providing a LintSource function to the linter. This example forbids the use of the `var` keyword and offers a 'Fix' action. ```javascript import { EditorView, basicSetup } from "codemirror" import { javascript } from "@codemirror/lang-javascript" import { linter, lintGutter, Diagnostic } from "@codemirror/lint" import { syntaxTree } from "@codemirror/language" // Lint source: forbid use of `var` const noVarLinter = linter(view => { const diagnostics: Diagnostic[] = [] syntaxTree(view.state).cursor().iterate(node => { if (node.name === "var") { diagnostics.push({ from: node.from, to: node.to, severity: "warning", message: "Use `let` or `const` instead of `var`.", actions: [{ name: "Fix", apply(view, from, to) { view.dispatch({ changes: { from, to, insert: "let" } }) } }] }) } }) return diagnostics }) const view = new EditorView({ doc: "var x = 1;\nlet y = 2;", extensions: [ basicSetup, javascript(), lintGutter(), noVarLinter ], parent: document.body }) // Press Ctrl-Shift-m / Cmd-Shift-m to open the diagnostic panel ``` -------------------------------- ### Decorations for Styling and Widgets Source: https://context7.com/codemirror/website/llms.txt Apply visual styling or insert DOM elements without altering the document content using Decorations. This example demonstrates underline marks and a custom widget. ```javascript import { EditorState, StateField, StateEffect, RangeSetBuilder } from "@codemirror/state" import { EditorView, Decoration, WidgetType } from "@codemirror/view" import { basicSetup } from "codemirror" import { javascript } from "@codemirror/lang-javascript" // Mark decoration: underline selected ranges const addUnderline = StateEffect.define() const clearUnderline = StateEffect.define() const underlineMark = Decoration.mark({ attributes: { style: "text-decoration: underline" } }) const underlineField = StateField.define({ create: () => Decoration.none, update(deco, tr) { deco = deco.map(tr.changes) for (const e of tr.effects) { if (e.is(addUnderline)) deco = deco.update({ add: e.value, sort: true }) else if (e.is(clearUnderline)) deco = deco.update({ filter: e.value }) } return deco }, provide: f => EditorView.decorations.from(f) }) const view = new EditorView({ doc: "Hello, CodeMirror!", extensions: [basicSetup, javascript(), underlineField], parent: document.body }) // Underline "Hello" view.dispatch({ effects: addUnderline.of([underlineMark.range(0, 5)]) }) // Widget decoration: insert a DOM element at position 5 class BannerWidget extends WidgetType { toDOM() { const el = document.createElement("span") el.textContent = "★" el.style.color = "gold" return el } eq(other) { return true } } const banner = Decoration.widget({ widget: new BannerWidget(), side: 1 }) // Provide it via a ViewPlugin or StateField in the same way ``` -------------------------------- ### Custom Autocompletion with @codemirror/autocomplete Source: https://context7.com/codemirror/website/llms.txt Provide custom suggestions to the autocompletion popup by supplying CompletionSource functions. This example offers a predefined list of array methods and built-in classes. ```javascript import { EditorView, basicSetup } from "codemirror" import { autocompletion, CompletionContext, CompletionResult } from "@codemirror/autocomplete" const myCompletions = [ { label: "forEach", type: "function", detail: "Array method", info: "Calls a function for each element" }, { label: "filter", type: "function", detail: "Array method", info: "Returns a filtered array" }, { label: "map", type: "function", detail: "Array method", info: "Maps array elements" }, { label: "Promise", type: "class", detail: "Built-in", info: "Asynchronous value holder" }, ] function myCompletionSource(context: CompletionContext): CompletionResult | null { const word = context.matchBefore(/\w+/) if (!word || (word.from === word.to && !context.explicit)) return null return { from: word.from, options: myCompletions, validFor: /^\w*$/ // reuse list while the user keeps typing word chars } } const view = new EditorView({ doc: "// Type 'fo' then Ctrl-Space", extensions: [ basicSetup, autocompletion({ override: [myCompletionSource] }) ], parent: document.body }) ``` -------------------------------- ### Configure Tab Indentation in CodeMirror Source: https://github.com/codemirror/website/blob/main/site/examples/tab/index.md This example configures CodeMirror to use the Tab key for indentation. It includes a script to load CodeMirror and the specific tab handling functionality. Ensure the 'tab.js' file contains the necessary keymap configuration. ```html ``` -------------------------------- ### Configure Indentation with indentNodeProp Source: https://github.com/codemirror/website/blob/main/site/examples/lang-package/index.md Associate indentation functions with node types using indentNodeProp. The function receives a context object and should return the desired indentation column. This example indents application nodes one unit beyond their start. ```typescript language.indentNodeProp.add({ "Application": (context) => { let base = context.base for ( let above ; above ; ) { let aboveType = context.nodeAt(above)?.type if ( aboveType && !aboveType.prop(language.indentNodeProp) && aboveType.name !== "SoftLineBreak" ) break above = context.nodeBefore(above) } let pos = context.pos if (above) pos = context.nodeStart(above) + context.line return context.column(pos) + context.unit } }) ``` -------------------------------- ### Initialize Editor with Compartments Source: https://github.com/codemirror/website/blob/main/site/examples/config/index.md Sets up an editor state with basic extensions and defines compartments for language and tab size. Use this to create a configurable editor instance. ```javascript import {basicSetup, EditorView} from "codemirror" import {EditorState, Compartment} from "@codemirror/state" import {python} from "@codemirror/lang-python" let language = new Compartment, tabSize = new Compartment let state = EditorState.create({ extensions: [ basicSetup, language.of(python()), tabSize.of(EditorState.tabSize.of(8)) ] }) let view = new EditorView({ state, parent: document.body }) ``` -------------------------------- ### Dynamic Configuration with Compartments Source: https://github.com/codemirror/website/blob/main/site/docs/migration/index.md Demonstrates how to dynamically change editor configuration, such as tab size, using Compartments and dispatching transactions. ```javascript let tabSize = new Compartment let view = new EditorView({ extensions: [ // ... tabSize.of(EditorState.tabSize.of(2)) ], // ... }) function setTabSize(size) { view.dispatch({ effects: tabSize.reconfigure(EditorState.tabSize.of(size)) }) } ``` -------------------------------- ### Word Counting Function Source: https://github.com/codemirror/website/blob/main/site/examples/panel/index.md A basic function to count words in a given string. This is a utility for the word-counting panel example. ```typescript function countWords(text: string): number { let count = 0 let inWord = false for (let i = 0; i < text.length; i++) { const isSpace = /\s/.test(text[i]) if (!isSpace && !inWord) { count++ inWord = true } else if (isSpace && inWord) { inWord = false } } return count } ``` -------------------------------- ### Underline Decoration Example Source: https://github.com/codemirror/website/blob/main/site/examples/decoration/index.md Applies an underline decoration to specific text ranges. This is a common use case for mark decorations. ```typescript import {Decoration, EditorView, RangeSet} from "@codemirror/view" import {StateField, EditorState} from "@codemirror/state" const underlineTheme = EditorView.baseTheme({ "& .cm-underline": { textDecoration: "underline" } }) const underlinePlugin = StateField.define