// effectified/packages · Effect UI · ▲ alpha

@effectified/codemirror-view

@codemirror/view for Effect UI: an editor whose document is the Model, its cursor kept. Generated from its own types by the engine, and tested against the real library.

1
mount
1
test in Chrome
$ npm install --allow-remote=all https://effectified.dev/codemirror-view.tgz effect https://pkg.pr.new/Effect-TS/ui/@effect/ui@2fd1a57

Always the newest release. To pin one, install its tarball from the releases. Effect UI is in public preview: its snapshot build installs with it.

// Examples

Code from its tests. Running, beside it.

An editor whose document is the Model

test/fixtures/editor.ts
import { Mount, UiSchema } from "@effect/ui";
import { BrowserHost, Html } from "@effect/ui/html";
import { EditorState } from "@codemirror/state";
import { EditorView } from "@codemirror/view";
import { Effect, Layer, Schema } from "effect";
import { EditorMount } from "@effectified/codemirror-view";

// Module constants: the mount reconfigures the editor only when the view gives it another value.
const editable = EditorView.lineWrapping;
const locked = [EditorView.lineWrapping, EditorState.readOnly.of(true)];

const Message = UiSchema.TaggedEnum({
	ChangedDoc: { doc: Schema.String },
	MovedSelection: { anchor: Schema.Number, head: Schema.Number },
	ClickedSet: {},
	ClickedLock: {},
	ClickedHide: {},
});

const Notes = Html.component({
	name: "Notes",
	model: Schema.Struct({ doc: Schema.String, anchor: Schema.Number, head: Schema.Number, locked: Schema.Boolean, shown: Schema.Boolean }),
	message: Message,
}).make({
	init: () => ({ doc: "hello", anchor: 0, head: 0, locked: false, shown: true }),
	update: {
		ChangedDoc: ($, { doc }) => $.evolve({ doc: () => doc }),
		MovedSelection: ($, { anchor, head }) => $.evolve({ anchor: () => anchor, head: () => head }),
		ClickedSet: ($) => $.evolve({ doc: () => "set from the model" }),
		ClickedLock: ($) => $.evolve({ locked: () => true }),
		ClickedHide: ($) => $.evolve({ shown: () => false }),
	},
	view: (h, model) =>
		h.div.children(
			h.button.id("set").on("click", Message.ClickedSet()).children("Set"),
			h.button.id("lock").on("click", Message.ClickedLock()).children("Lock"),
			h.button.id("hide").on("click", Message.ClickedHide()).children("Hide"),
			h.output.id("state").children(JSON.stringify(model)),
			model.shown
				? h.div.id("editor").onMount(
						EditorMount({ doc: model.doc, extensions: model.locked ? locked : editable }).pipe(
							Mount.map((change) =>
								change._tag === "DocChanged" ? Message.ChangedDoc({ doc: change.doc }) : Message.MovedSelection({ anchor: change.anchor, head: change.head }),
							),
						),
					)
				: h.p.id("hidden").children("hidden"),
		),
});

Layer.launch(BrowserHost.mount(Notes, {}, document.getElementById("root")!)).pipe(Effect.runFork);
Live: the fixture CI drives in Chrome, built from the newest release.
// API

What it offers. Read from upstream's types.

HandleEffectsStreams and eventsReleased by
EditorView––destroy

Mounts: EditorMount: one instance per element, updated from the view's args, released with the element.

ErrorWhen
ViewErrorany other failure
// Agent skill

For your coding agent. Ships in the package.

SKILL.md teaches an agent this facade: its rules, its API and these examples. It is in every release (node_modules/@effectified/codemirror-view/SKILL.md); copy it into your agent's skills, or download it.

---
name: effectified-codemirror-view
description: Use @codemirror/view in Effect UI apps through @effectified/codemirror-view. Use when an Effect UI view renders @codemirror/view: what the facade offers, how its bindings and mounts work, and tested examples.
---

# @codemirror/view through @effectified/codemirror-view

An editor whose document is the Model, its cursor kept.

@codemirror/view for Effect UI: typed bindings and mounts, its events as messages, its instances scoped to their elements. Generated from @codemirror/view's own types (this copy: 6.44.0+3, for @codemirror/view 6.44.0), so its types are upstream's and a call the facade does not change behaves as upstream documents.

## Install

```sh
pnpm add https://effectified.dev/codemirror-view.tgz effect https://pkg.pr.new/Effect-TS/ui/@effect/ui@2fd1a57
```

## Rules

- Import from `@effectified/codemirror-view`. It runs in [Effect UI](https://ui.effect.website) views (`Html.component`), installed from its snapshot build.
- Mounts: `EditorMount`. A mount keeps one @codemirror/view instance on the element it renders into: `h.<tag>.onMount(<Name>Mount({ ...args }).pipe(Mount.map(...)))`. New args update the live instance; leaving the view releases it.
- Never reach into the element or instance to change what the Model owns: render the Model, and let messages change it.

## Examples

Each is code from a test that passes in CI.

### An editor whose document is the Model

```ts
import { Mount, UiSchema } from "@effect/ui";
import { BrowserHost, Html } from "@effect/ui/html";
import { EditorState } from "@codemirror/state";
import { EditorView } from "@codemirror/view";
import { Effect, Layer, Schema } from "effect";
import { EditorMount } from "@effectified/codemirror-view";

// Module constants: the mount reconfigures the editor only when the view gives it another value.
const editable = EditorView.lineWrapping;
const locked = [EditorView.lineWrapping, EditorState.readOnly.of(true)];

const Message = UiSchema.TaggedEnum({
	ChangedDoc: { doc: Schema.String },
	MovedSelection: { anchor: Schema.Number, head: Schema.Number },
	ClickedSet: {},
	ClickedLock: {},
	ClickedHide: {},
});

const Notes = Html.component({
	name: "Notes",
	model: Schema.Struct({ doc: Schema.String, anchor: Schema.Number, head: Schema.Number, locked: Schema.Boolean, shown: Schema.Boolean }),
	message: Message,
}).make({
	init: () => ({ doc: "hello", anchor: 0, head: 0, locked: false, shown: true }),
	update: {
		ChangedDoc: ($, { doc }) => $.evolve({ doc: () => doc }),
		MovedSelection: ($, { anchor, head }) => $.evolve({ anchor: () => anchor, head: () => head }),
		ClickedSet: ($) => $.evolve({ doc: () => "set from the model" }),
		ClickedLock: ($) => $.evolve({ locked: () => true }),
		ClickedHide: ($) => $.evolve({ shown: () => false }),
	},
	view: (h, model) =>
		h.div.children(
			h.button.id("set").on("click", Message.ClickedSet()).children("Set"),
			h.button.id("lock").on("click", Message.ClickedLock()).children("Lock"),
			h.button.id("hide").on("click", Message.ClickedHide()).children("Hide"),
			h.output.id("state").children(JSON.stringify(model)),
			model.shown
				? h.div.id("editor").onMount(
						EditorMount({ doc: model.doc, extensions: model.locked ? locked : editable }).pipe(
							Mount.map((change) =>
								change._tag === "DocChanged" ? Message.ChangedDoc({ doc: change.doc }) : Message.MovedSelection({ anchor: change.anchor, head: change.head }),
							),
						),
					)
				: h.p.id("hidden").children("hidden"),
		),
});

Layer.launch(BrowserHost.mount(Notes, {}, document.getElementById("root")!)).pipe(Effect.runFork);
```

From `test/fixtures/editor.ts`.

## API

Services: `ViewEditorView` (a `EditorView`, from `EditorView`).

| Error | From |
|---|---|
| `ViewError` | any other failure |

| Handle | Effects | Streams | Events | Released by |
|---|---|---|---|---|
| `EditorView` | – | – | – | `destroy` |

Mounts: `EditorMount`.

More: https://effectified.dev/codemirror-view