Display Tooltips near document positions
main@codemirror/view package provides mechanisms to manage, position, and show these elements using Tooltip and showTooltip.repository·main·Indexed 41 days ago
https://github.com/codemirror/viewThe DOM view component for the CodeMirror code editor (v6.41.0). It handles the visual representation of the editor, including the EditorView class, ViewPlugins, decorations, gutters, tooltips, panels, and layers. It provides utilities for mapping document positions to DOM coordinates and managing the editor's user interface and interaction.
@codemirror/view package provides mechanisms to manage, position, and show these elements using Tooltip and showTooltip.@EditorView is the DOM component responsible for displaying the editor state and handling text input. It is the visual representation of the editor that the user interacts with directly.Decoration and DecorationSet to influence how content is drawn. Decorations allow you to add styling (like colors or backgrounds) or replace content with an alternative representation (like a WidgetType).ViewPlugin (which implements PluginSpec) to react to ViewUpdate events. This allows you to manage state that is specific to the visual representation of the document rather than the document content itself.gutter and gutters to define these areas and GutterMarker to specify what is drawn within them.rectangularSelection and crosshairCursor.Layers are sets of DOM elements drawn over or below the document text. They are useful for UI elements that should not take up space or influence line wrapping, such as additional cursors.
Accessibility Note: Because layers are outside the regular DOM order, they are invisible to screen readers. You must provide any important information they convey in an accessible way (e.g., using EditorView.announce).
To create a CodeMirror editor instance, import EditorView from @codemirror/view and instantiate it with a configuration object. You must provide a parent element (the DOM node where the editor will be rendered) and can optionally provide initial doc content and an array of extensions to configure behavior and appearance.
To update the editor (e.g., to change text or move the cursor), use the view.dispatch() method.
import {EditorView} from "@codemirror/view"
import {basicSetup} from "codemirror"
const view = new EditorView({
parent: document.querySelector("#some-node"),
doc: "Content text",
extensions: [basicSetup /* ... */]
})Gutters are vertical areas in the editor used to display metadata (like line numbers, error icons, or git blame info). They work through a combination of configuration and markers:
gutter(config). This registers a GutterConfig via a facet.GutterMarker instances associated with specific positions in the document. They are typically provided via a RangeSet<GutterMarker> in the markers config option.markers provided by the gutter config.lineMarker function (for markers applied to every line).widgetMarker function (for markers associated with block widgets).gutterLineClass and gutterWidgetClass facets (for adding CSS classes).The MatchDecorator class is a helper designed to be used within a ViewPlugin. It simplifies the process of maintaining decorations (like colors or styles) on text segments that match a specific regular expression. It handles viewport management, re-matching on document changes, and optimizes performance by only re-calculating decorations for visible ranges.
To use it, you typically instantiate it in your plugin's create function and use its updateDeco method in the plugin's update lifecycle hook.
import {EditorView, ViewPlugin} from "@codemirror/view";
import {MatchDecorator} from "@codemirror/view";
import {Decoration} from "@codemirror/view";
const myPlugin = ViewPlugin.fromClass(class {
decorations: DecorationSet
constructor(view: EditorView) {
const matcher = new MatchDecorator({
regexp: /your-regex/g,
decoration: Decoration.mark({class: "my-highlight"})
});
this.decorations = matcher.createDeco(view);
}
update(update: ViewUpdate) {
// Note: You must keep track of the MatchDecorator instance
// to call updateDeco correctly here.
this.decorations = matcher.updateDeco(update, this.decorations);
}
}, {
decorations: v => v.decorations
});A LayerMarker is an object that represents a visual element in a layer. Markers must be created during a measuring phase and must contain all necessary positioning information so they can be drawn without triggering further DOM layout reads.
Required methods:
eq(other: LayerMarker): boolean: Used to compare markers of the same type to avoid unnecessary redraws.draw(): HTMLElement: Returns the DOM element for the marker.Optional method:
update?(dom: HTMLElement, oldMarker: LayerMarker): boolean: Allows updating an existing DOM element instead of recreating it. Return true if the update was successful.