@@ -94,27 +94,86 @@ setFocused(false);
9494
9595### Update editor readonly state
9696
97+ After the CodeMirror editor is mounted, you can update its readonly state using the
98+ ` createReadonlyEditor ` function that accept the editor view and the readOnly accessor.
99+ > ** Note** Updating the accessor, the editor readOnly state will be updated automatically;
100+
97101``` typescript jsx
98102import { createCodeMirror , createEditorReadonly } from ' solid-codemirror' ;
99103import { createSignal } from ' solid-js' ;
100104
101- const { editorView } = createCodeMirror ();
102- const [readOnly, setReadOnly] = createSignal (true );
103- void createEditorReadonly (editorView , readonly );
105+ function App() {
106+ const { ref } = createCodeMirror ();
107+ const [readOnly, setReadOnly] = createSignal (true );
108+ createEditorReadonly (editorView , readonly );
109+
110+ return <div ref = {ref} />
111+ }
104112```
105113
106114### Control editor code using signals
107115
116+ After CodeMirror editor is mounted, you can control the code state using the
117+ `createEditorControlledValue`.
118+
119+ > **Note** The value of the editor is already memoized
120+
108121```typescript jsx
109122import { createCodeMirror, createEditorControlledValue } from 'solid-codemirror';
110123import { createSignal } from 'solid-js';
111124
112- const { editorView } = createCodeMirror ();
113- const [code, setCode] = createSignal (" console.log('hello world!')" );
114- void createEditorControlledValue (editorView , code );
125+ function App() {
126+ const [code , setCode ] = createSignal("console.log('hello world!')");
127+ const { ref } = createCodeMirror({ onValueChange: setCode });
128+ createEditorControlledValue (editorView , code );
129+
130+ // Update code after 2.5s
131+ setTimeout (() => {
132+ setCode (" console.log('updated!')" );
133+ }, 2500 );
134+
135+ return <div ref = {ref} />
136+ }
137+ ```
138+
139+ ### Handle custom extensions
140+
141+ After CodeMirror editor is mounted, you can handle custom extensions thanks to the
142+ `createExtension` function. It both accept an `Extension` or `Accessor<Extension | undefined >` then
143+ it will be automatically reconfigured when the extension changes. Otherwise, you can manually reconfigure them using
144+ the returned `reconfigure` function.
145+
146+ For more details, see the official documentation about [compartments](https : // codemirror.net/examples/reconfigure).
147+
148+ ` ` ` typescript jsx
149+ import { createCodeMirror } from 'solid-codemirror';
150+ import { createSignal } from 'solid-js';
151+ import { EditorView } from '@codemirror/view';
152+
153+ function App() {
154+ const [code, setCode] = createSignal("console.log('hello world!')");
155+ const [showLineNumber, setShowLineNumber] = createSignal(true);
156+ const { ref, createExtension } = createCodeMirror({ onValueChange: setCode });
157+
158+ const theme = EditorView.theme({
159+ '&': {
160+ background: 'red'
161+ }
162+ });
163+
164+ // Add a static custom theme
165+ createExtension(theme);
166+
167+ // Toggle extension
168+ createExtension(() => showLineNumber() ? lineNumbers() : []);
169+
170+ // Remove line numbers after 2.5s
171+ setTimeout(() => {
172+ setShowLineNumber(false);
173+ }, 2500);
115174
116- // Update later
117- setCode ( " console.log('updated!') " );
175+ return <div ref={ref} />
176+ }
118177` ` `
119178
120179## Demo
0 commit comments