reactive-md 0.5.1 → 0.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/CHANGELOG-ch.md +29 -0
  2. package/CHANGELOG.md +29 -0
  3. package/README-ch.md +7 -6
  4. package/README-en.md +8 -6
  5. package/README.md +8 -6
  6. package/cli/build.mjs +1 -1
  7. package/cli/package.mjs +1 -0
  8. package/dist-library/authoring-DJJjKYg9.js +842 -0
  9. package/dist-library/{runtime-CXBdLQ3A.js → compiler-UyPwGzJZ.js} +4374 -3730
  10. package/dist-library/react-fizgU3i8.js +12764 -0
  11. package/dist-library/reactive-md-authoring.js +1514 -0
  12. package/dist-library/reactive-md-editor.js +577 -571
  13. package/dist-library/reactive-md-print.css +284 -0
  14. package/dist-library/reactive-md-react-unstyled.js +1 -0
  15. package/dist-library/reactive-md-react.js +1 -13177
  16. package/dist-library/reactive-md.css +1 -1
  17. package/dist-library/reactive-md.js +260 -1062
  18. package/dist-library/runtime-DXtle_6c.js +498 -0
  19. package/dist-library/types/authoring/index.d.ts +24 -0
  20. package/dist-library/types/editor/command-types.d.ts +10 -0
  21. package/dist-library/types/editor/editor-commands.d.ts +2 -2
  22. package/dist-library/types/editor/editor-i18n.d.ts +1 -1
  23. package/dist-library/types/editor/native-components.d.ts +1 -1
  24. package/dist-library/types/editor/source-authoring.d.ts +5 -3
  25. package/dist-library/types/editor/types.d.ts +5 -10
  26. package/dist-library/types/local/LocalDocumentView.d.ts +14 -4
  27. package/dist-library/types/local/authoring.d.ts +9 -5
  28. package/dist-library/types/local/diagnostics.d.ts +80 -0
  29. package/dist-library/types/local/html-names.d.ts +6 -0
  30. package/dist-library/types/local/index.d.ts +2 -1
  31. package/dist-library/types/local/limits.d.ts +8 -0
  32. package/dist-library/types/local/math-display.d.ts +72 -0
  33. package/dist-library/types/local/math-view.d.ts +2 -0
  34. package/dist-library/types/local/options.d.ts +9 -2
  35. package/dist-library/types/local/render-types.d.ts +11 -0
  36. package/dist-library/types/local/types.d.ts +28 -0
  37. package/dist-library/types/local/worker.d.ts +5 -1
  38. package/dist-library/types/react/index.d.ts +1 -1
  39. package/dist-standalone/reactive-md-standalone.js +54 -50
  40. package/docs/author-tools-ch.md +11 -5
  41. package/docs/author-tools-en.md +11 -5
  42. package/docs/compatibility-ch.md +7 -5
  43. package/docs/compatibility-en.md +7 -5
  44. package/docs/component-development-ch.md +4 -4
  45. package/docs/component-development-en.md +3 -3
  46. package/docs/component-protocol-ch.md +48 -11
  47. package/docs/component-protocol-en.md +48 -11
  48. package/docs/editor-ch.md +237 -235
  49. package/docs/editor-en.md +237 -235
  50. package/docs/local-integration-ch.md +392 -366
  51. package/docs/local-integration-en.md +392 -366
  52. package/docs/release-0.5.2-ch.md +35 -0
  53. package/docs/release-0.5.2-en.md +35 -0
  54. package/docs/release-0.5.3-ch.md +25 -0
  55. package/docs/release-0.5.3-en.md +25 -0
  56. package/docs/releasing-ch.md +7 -7
  57. package/docs/releasing-en.md +8 -8
  58. package/examples/conformance/README.md +1 -1
  59. package/package.json +12 -2
package/docs/editor-en.md CHANGED
@@ -1,235 +1,237 @@
1
- # React source editor
2
-
3
- `reactive-md/editor` is a controlled React 19 editor for Reactive MD source. It comes from the Playground editor and provides source highlighting, a formatting and insertion toolbar, completion, binding navigation, rename and format commands, undo and redo, line numbers and scroll synchronization. Its built-in status bar provides a character count and Edit, Split and Preview views. The host keeps the source text and supplies preview content, persistence and export.
4
-
5
- The editor has its own stylesheet at `reactive-md/editor/styles.css`. It is separate from `reactive-md/styles.css`, so a page that only edits source does not load document rendering styles.
6
-
7
- The editor is part of the `0.5.0` preview, which targets the `next` channel. `latest` remains `0.4.0`. Existing `reactive-md`, `reactive-md/react` and `reactive-md/styles.css` imports are unchanged.
8
-
9
- ## Install
10
-
11
- ```sh
12
- npm install reactive-md@next
13
- npm install react@19 react-dom@19
14
- ```
15
-
16
- Pin the preview with `npm install reactive-md@0.5.0`. The package ships ESM only and needs Node.js 22.12 or newer for tooling and server rendering.
17
-
18
- ## Minimal controlled editor
19
-
20
- ```tsx
21
- import { useState } from 'react';
22
- import { ReactiveMarkdownEditor } from 'reactive-md/editor';
23
- import 'reactive-md/editor/styles.css';
24
-
25
- const initialSource = `# Cost estimate
26
-
27
- <slider bind="people" label="People" initial="2" min="1" max="20">
28
-
29
- Monthly cost: **{{ people * 100 }} USD**.`;
30
-
31
- export function SourceEditor() {
32
- const [source, setSource] = useState(initialSource);
33
-
34
- return (
35
- <ReactiveMarkdownEditor
36
- value={source}
37
- onChange={setSource}
38
- ariaLabel="Document source"
39
- />
40
- );
41
- }
42
- ```
43
-
44
- `value` and `onChange` are required. The component renders a textarea and keeps its own undo history. The source text stays in your state. Built-in labels default to English.
45
-
46
- ## Entries
47
-
48
- | Entry | Contents | Dependency |
49
- | --- | --- | --- |
50
- | `reactive-md/editor` | `ReactiveMarkdownEditor` and its types | React 19 |
51
- | `reactive-md/editor/styles.css` | Editor styles, scoped to the editor root | Imported explicitly by the host |
52
-
53
- Import the stylesheet once in the application or in the component module that renders the editor. A bundler or framework that handles CSS imports can place it with the editor module.
54
-
55
- ## Ref handle
56
-
57
- ```tsx
58
- import { useRef, useState } from 'react';
59
- import {
60
- ReactiveMarkdownEditor,
61
- type ReactiveMarkdownEditorHandle,
62
- } from 'reactive-md/editor';
63
-
64
- export function SourceEditor() {
65
- const [source, setSource] = useState('');
66
- const editor = useRef<ReactiveMarkdownEditorHandle>(null);
67
-
68
- return (
69
- <>
70
- <button type="button" onClick={() => editor.current?.focus()}>Focus</button>
71
- <button type="button" onClick={() => editor.current?.undo()}>Undo</button>
72
- <button type="button" onClick={() => editor.current?.selectRange({ start: 0, end: 5 })}>
73
- Select heading
74
- </button>
75
- <ReactiveMarkdownEditor ref={editor} value={source} onChange={setSource} />
76
- </>
77
- );
78
- }
79
- ```
80
-
81
- | Method | Behavior |
82
- | --- | --- |
83
- | `focus(selection?)` | Focuses the textarea. Applies the optional selection. |
84
- | `selectRange(range)` | Selects a source range. Ranges use the same UTF-16 half-open offsets as compile diagnostics. |
85
- | `getSelection()` | Returns the current selection. |
86
- | `getTextarea()` | Returns the underlying textarea, or `null` before mount. |
87
- | `undo()` / `redo()` | Moves through this instance's history and emits the resulting value through `onChange`. Both are no-ops at the ends of the history and while `readOnly` or `disabled` is set. |
88
-
89
- The entry also exports `ReactiveMarkdownEditorProps`, `ReactiveMarkdownEditorHandle`, `EditorLocale`, `EditorTheme` and `EditorSelection` types.
90
-
91
- ## Props
92
-
93
- | Prop | Type | Default | Notes |
94
- | --- | --- | --- | --- |
95
- | `value` | `string` | required | Controlled source text. |
96
- | `onChange` | `(value: string) => void` | required | Receives the next source for typing, toolbar commands and undo or redo. |
97
- | `locale` | `'en' \| 'zh'` | `'en'` | Built-in labels for this instance. Source text is unchanged. |
98
- | `theme` | `'light' \| 'dark' \| 'auto'` | `'auto'` | Sets `data-editor-theme` on the editor root. `auto` follows the system preference. |
99
- | `preview` | `ReactNode` | none | Preview content rendered inside the editor. Enables the three view buttons. |
100
- | `viewMode` | `EditorViewMode` | none | Controlled view: `'edit'`, `'split'` or `'preview'`. |
101
- | `defaultViewMode` | `EditorViewMode` | `'edit'` | Initial mode for an uncontrolled instance. |
102
- | `onViewModeChange` | `(mode: EditorViewMode) => void` | none | Called when a view button is clicked. |
103
- | `id` | `string` | generated | Textarea id. The instance uses it for its internal labels and gutter. |
104
- | `className` | `string` | none | Added to the `.reactive-md-editor` root. |
105
- | `style` | `CSSProperties` | none | Inline styles for the root. |
106
- | `height` | `number \| string` | `560` | A number is pixels. A string accepts any CSS length. |
107
- | `minHeight` | `number \| string` | `280` | Minimum editor height, in the same units as `height`. |
108
- | `placeholder` | `string` | none | Placeholder for an empty source. |
109
- | `ariaLabel` | `string` | localized label | Accessible name for the textarea. |
110
- | `readOnly` | `boolean` | `false` | Keeps text selectable and copyable while blocking changes. |
111
- | `disabled` | `boolean` | `false` | Blocks focus and editing. |
112
- | `documentKey` | `string \| number` | none | Change it when the editor switches to an independent source document. The change resets undo history and selection. |
113
- | `diagnostics` | `readonly Diagnostic[]` | none | Marks diagnostic lines in the gutter. It does not block typing or replace host validation. |
114
- | `document` | `LocalDocument \| null` | `null` | Compiled document used to show binding value types in the authoring tools. The editor does not compile it. |
115
- | `toolbar` | `boolean` | `true` | Shows the formatting and insertion toolbar, including its export action. |
116
- | `authoring` | `boolean` | `true` | Enables completion, binding navigation, rename and format tools. |
117
- | `onExport` | `() => void` | none | Called by the toolbar export action. Use it to open your export dialog or write the file yourself. |
118
- | `textareaRef` | `RefObject<HTMLTextAreaElement \| null>` | none | Receives the underlying textarea for advanced host code. |
119
-
120
- ## Built-in editor views
121
-
122
- The status bar sits inside the editor frame, directly below its editing area. It shows the Unicode character count and view buttons. Pass `preview` to enable Edit, Split and Preview. Containers up to 600px wide offer only Edit and Preview. Split temporarily displays Edit at that width and returns to Split when the container widens.
123
-
124
- ```tsx
125
- <ReactiveMarkdownEditor
126
- value={source}
127
- onChange={setSource}
128
- preview={<LocalDocumentView document={compiledDocument} />}
129
- defaultViewMode="split"
130
- />
131
- ```
132
-
133
- Switching views preserves the source selection, undo history and preview component state. Pass both `viewMode` and `onViewModeChange` to control the mode from the host. Source-only instances retain the count and keyboard hint. Press Esc, then Tab in the source to focus the toolbar.
134
-
135
- ## Controlled value and history
136
-
137
- Every edit calls `onChange` with the next source: typing, toolbar formatting, insertion templates, rename, format and undo or redo. Pass that value back into `value`. The editor treats the edits it emits as part of the current session, so a normal controlled round trip keeps undo steps.
138
-
139
- An external change to `value` updates the displayed text. When the host replaces the text with an independent document, update `documentKey` in the same render. A key change clears undo and redo, resets the selection and adopts the new value.
140
-
141
- ```tsx
142
- const [documentKey, setDocumentKey] = useState('estimate');
143
- const [source, setSource] = useState(loadSource('estimate'));
144
-
145
- function openDocument(key: string, text: string) {
146
- setDocumentKey(key);
147
- setSource(text);
148
- }
149
-
150
- <ReactiveMarkdownEditor
151
- value={source}
152
- onChange={setSource}
153
- documentKey={documentKey}
154
- />
155
- ```
156
-
157
- Undo history belongs to the mounted instance. It is not saved, not shared between instances and not derived from the whole `value` prop. Use `documentKey` for document switches rather than remounting on every keystroke.
158
-
159
- ## Locale and theme
160
-
161
- Each instance applies its own locale and theme. The editor does not change the document `data-theme` attribute, does not write browser storage and does not read the Playground language setting.
162
-
163
- ```tsx
164
- <ReactiveMarkdownEditor
165
- value={source}
166
- onChange={setSource}
167
- locale="zh"
168
- theme="dark"
169
- />
170
- ```
171
-
172
- `theme="auto"` follows the system color preference, including preference changes. Two editors on one page can use different values. The theme is set through the local `data-editor-theme` attribute on the `.reactive-md-editor` root.
173
-
174
- ## Read-only and disabled
175
-
176
- `readOnly` keeps the source visible, focusable and selectable. Typing, the toolbar, completion, rename, format and the ref `undo` and `redo` methods cannot change the value.
177
-
178
- `disabled` also blocks focus and pointer interaction. Both modes disable the toolbar export action. Neither mode changes the value or the history on its own. Use them when a host is not allowed to edit source, for example while a read-only preview is shown.
179
-
180
- Replacing `value` externally or changing `documentKey` resets history, selection and the editor's scroll position. Echoing a value received through `onChange` preserves them. The export button is hidden until `onExport` is provided.
181
-
182
- ## Diagnostics and compiled document
183
-
184
- The optional authoring tools analyze source locally, including syntax checks. The host manages compilation for its preview and can pass that result when it wants line markers or binding value types:
185
-
186
- ```tsx
187
- import { compileLocal } from 'reactive-md';
188
-
189
- const result = compileLocal(source);
190
-
191
- <ReactiveMarkdownEditor
192
- value={source}
193
- onChange={setSource}
194
- diagnostics={result.diagnostics}
195
- document={result.ok ? result.document : null}
196
- />
197
- ```
198
-
199
- `diagnostics` marks the reported lines in the gutter. Incomplete source remains editable, and a compile error does not lock the textarea. `document` supplies binding value types for the authoring inspection panel. Both props are optional; pass neither when the editor is used only to edit Markdown.
200
-
201
- ## Sizing and layout
202
-
203
- The editor fills the width of its container. `height` defaults to `560px` and `minHeight` to `280px`; numbers are pixels and strings can use `vh`, `rem` or other CSS lengths. The gutter follows line count, and soft-wrapped lines keep their line number and highlighting aligned during scrolling.
204
-
205
- Use `className` or `style` for margins and placement in a host layout. Internal element class names are implementation details.
206
-
207
- ## Toolbar and authoring tools
208
-
209
- `toolbar={false}` removes the formatting and insertion toolbar, including its export action. `authoring={false}` keeps the textarea and toolbar but disables syntax highlighting, completion, binding navigation, rename and format. Both default to enabled.
210
-
211
- The toolbar formats Markdown, inserts lists, tables, links, images and the built-in component templates, and exposes undo and redo. Formatting commands use the current selection and return the next source through `onChange`.
212
-
213
- ## Host responsibilities
214
-
215
- The editor edits source. The host handles the rest:
216
-
217
- - Preview: compile `value` and render it with `reactive-md/react` or your own runtime.
218
- - Persistence: store the source. The editor does not auto-save, write local storage or send the source anywhere.
219
- - Export: pass `onExport` to connect the toolbar action to your HTML export, download dialog or file service. The editor does not build a shareable HTML file.
220
-
221
- Compilation scheduling and debouncing also stay in the host. See [Local mode integration](local-integration-en.md) and [Shareable documents and static export](shareable-export-en.md). A complete editor, preview and document-switching example is in [react-editor.tsx](../examples/integration/react-editor.tsx).
222
-
223
- ## Server rendering
224
-
225
- The editor entry has no top-level `window` or `document` access. Importing it and rendering the initial textarea with `react-dom/server` is safe. Focus, selection, completion, layout measurement and toolbar menus run after the component mounts in a browser, so interactive checks and browser tests need a client.
226
-
227
- ## Styles and scope
228
-
229
- `reactive-md/editor/styles.css` contains the editor's own tokens and rules. All selectors are scoped to `.reactive-md-editor` or its descendants, so the stylesheet does not restyle host buttons, inputs or the page body. The theme comes from the instance attribute described above.
230
-
231
- If the same page also renders a document preview, import `reactive-md/styles.css` as well. The two stylesheets are independent and can be loaded in either order.
232
-
233
- ## Compatibility
234
-
235
- `0.5.0` adds the editor entry and keeps `local-draft-2`, `component-protocol-1` and `reactive-md-theme-1`. Projects that only render documents can upgrade without changing imports or source. The editor is new, so it has no older API to migrate from. See the [0.5.0 release notes](release-0.5.0-en.md) and [Versions and compatibility](compatibility-en.md).
1
+ # React source editor
2
+
3
+ `reactive-md/editor` is a controlled React 19 editor for Reactive MD source. It comes from the Playground editor and provides source highlighting, a formatting and insertion toolbar, completion, binding navigation, rename and format commands, undo and redo, line numbers and scroll synchronization. Its built-in status bar provides a character count and Edit, Split and Preview views. The host keeps the source text and supplies preview content, persistence and export.
4
+
5
+ The editor has its own stylesheet at `reactive-md/editor/styles.css`. It is separate from `reactive-md/styles.css`, so a page that only edits source does not load document rendering styles.
6
+
7
+ The editor is part of the `0.5.0` preview, which targets the `next` channel. `latest` remains `0.4.0`. Existing `reactive-md`, `reactive-md/react` and `reactive-md/styles.css` imports are unchanged.
8
+
9
+ ## Install
10
+
11
+ ```sh
12
+ npm install reactive-md@next
13
+ npm install react@19 react-dom@19
14
+ ```
15
+
16
+ Pin the preview with `npm install reactive-md@0.5.0`. The package ships ESM only and needs Node.js 22.12 or newer for tooling and server rendering.
17
+
18
+ ## Minimal controlled editor
19
+
20
+ ```tsx
21
+ import { useState } from 'react';
22
+ import { ReactiveMarkdownEditor } from 'reactive-md/editor';
23
+ import 'reactive-md/editor/styles.css';
24
+
25
+ const initialSource = `# Cost estimate
26
+
27
+ <slider bind="people" label="People" initial="2" min="1" max="20">
28
+
29
+ Monthly cost: **{{ people * 100 }} USD**.`;
30
+
31
+ export function SourceEditor() {
32
+ const [source, setSource] = useState(initialSource);
33
+
34
+ return (
35
+ <ReactiveMarkdownEditor
36
+ value={source}
37
+ onChange={setSource}
38
+ ariaLabel="Document source"
39
+ />
40
+ );
41
+ }
42
+ ```
43
+
44
+ `value` and `onChange` are required. The component renders a textarea and keeps its own undo history. The source text stays in your state. Built-in labels default to English.
45
+
46
+ ## Entries
47
+
48
+ | Entry | Contents | Dependency |
49
+ | --- | --- | --- |
50
+ | `reactive-md/editor` | `ReactiveMarkdownEditor` and its types | React 19 |
51
+ | `reactive-md/editor/styles.css` | Editor styles, scoped to the editor root | Imported explicitly by the host |
52
+
53
+ Import the stylesheet once in the application or in the component module that renders the editor. A bundler or framework that handles CSS imports can place it with the editor module.
54
+
55
+ ## Ref handle
56
+
57
+ ```tsx
58
+ import { useRef, useState } from 'react';
59
+ import {
60
+ ReactiveMarkdownEditor,
61
+ type ReactiveMarkdownEditorHandle,
62
+ } from 'reactive-md/editor';
63
+
64
+ export function SourceEditor() {
65
+ const [source, setSource] = useState('');
66
+ const editor = useRef<ReactiveMarkdownEditorHandle>(null);
67
+
68
+ return (
69
+ <>
70
+ <button type="button" onClick={() => editor.current?.focus()}>Focus</button>
71
+ <button type="button" onClick={() => editor.current?.undo()}>Undo</button>
72
+ <button type="button" onClick={() => editor.current?.selectRange({ start: 0, end: 5 })}>
73
+ Select heading
74
+ </button>
75
+ <ReactiveMarkdownEditor ref={editor} value={source} onChange={setSource} />
76
+ </>
77
+ );
78
+ }
79
+ ```
80
+
81
+ | Method | Behavior |
82
+ | --- | --- |
83
+ | `focus(selection?)` | Focuses the textarea. Applies the optional selection. |
84
+ | `selectRange(range)` | Selects a source range. Ranges use the same UTF-16 half-open offsets as compile diagnostics. |
85
+ | `getSelection()` | Returns the current selection. |
86
+ | `getTextarea()` | Returns the underlying textarea, or `null` before mount. |
87
+ | `undo()` / `redo()` | Moves through this instance's history and emits the resulting value through `onChange`. Both are no-ops at the ends of the history and while `readOnly` or `disabled` is set. |
88
+
89
+ The entry also exports `ReactiveMarkdownEditorProps`, `ReactiveMarkdownEditorHandle`, `EditorLocale`, `EditorTheme` and `EditorSelection` types.
90
+
91
+ ## Props
92
+
93
+ | Prop | Type | Default | Notes |
94
+ | --- | --- | --- | --- |
95
+ | `value` | `string` | required | Controlled source text. |
96
+ | `onChange` | `(value: string) => void` | required | Receives the next source for typing, toolbar commands and undo or redo. |
97
+ | `locale` | `'en' \| 'zh'` | `'en'` | Built-in labels for this instance. Source text is unchanged. |
98
+ | `theme` | `'light' \| 'dark' \| 'auto'` | `'auto'` | Sets `data-editor-theme` on the editor root. `auto` follows the system preference. |
99
+ | `preview` | `ReactNode` | none | Preview content rendered inside the editor. Enables the three view buttons. |
100
+ | `viewMode` | `EditorViewMode` | none | Controlled view: `'edit'`, `'split'` or `'preview'`. |
101
+ | `defaultViewMode` | `EditorViewMode` | `'edit'` | Initial mode for an uncontrolled instance. |
102
+ | `onViewModeChange` | `(mode: EditorViewMode) => void` | none | Called when a view button is clicked. |
103
+ | `id` | `string` | generated | Textarea id. The instance uses it for its internal labels and gutter. |
104
+ | `className` | `string` | none | Added to the `.reactive-md-editor` root. |
105
+ | `style` | `CSSProperties` | none | Inline styles for the root. |
106
+ | `height` | `number \| string` | `560` | A number is pixels. A string accepts any CSS length. |
107
+ | `minHeight` | `number \| string` | `280` | Minimum editor height, in the same units as `height`. |
108
+ | `placeholder` | `string` | none | Placeholder for an empty source. |
109
+ | `ariaLabel` | `string` | localized label | Accessible name for the textarea. |
110
+ | `readOnly` | `boolean` | `false` | Keeps text selectable and copyable while blocking changes. |
111
+ | `disabled` | `boolean` | `false` | Blocks focus and editing. |
112
+ | `documentKey` | `string \| number` | none | Change it when the editor switches to an independent source document. The change resets undo history and selection. |
113
+ | `diagnostics` | `readonly Diagnostic[]` | none | Marks diagnostic lines in the gutter. It does not block typing or replace host validation. |
114
+ | `document` | `LocalDocument \| null` | `null` | Compiled document used to show binding value types in the authoring tools. The editor does not compile it. |
115
+ | `toolbar` | `boolean` | `true` | Shows the formatting and insertion toolbar, including its export action. |
116
+ | `authoring` | `boolean` | `true` | Enables completion, binding navigation, rename and format tools. |
117
+ | `onExport` | `() => void` | none | Called by the toolbar export action. Use it to open your export dialog or write the file yourself. |
118
+ | `textareaRef` | `RefObject<HTMLTextAreaElement \| null>` | none | Receives the underlying textarea for advanced host code. |
119
+
120
+ ## Built-in editor views
121
+
122
+ The status bar sits inside the editor frame, directly below its editing area. It shows the Unicode character count and view buttons. Pass `preview` to enable Edit, Split and Preview. Containers up to 600px wide offer only Edit and Preview. Split temporarily displays Edit at that width and returns to Split when the container widens.
123
+
124
+ ```tsx
125
+ <ReactiveMarkdownEditor
126
+ value={source}
127
+ onChange={setSource}
128
+ preview={<LocalDocumentView document={compiledDocument} />}
129
+ defaultViewMode="split"
130
+ />
131
+ ```
132
+
133
+ Switching views preserves the source selection, undo history and preview component state. Pass both `viewMode` and `onViewModeChange` to control the mode from the host. Source-only instances retain the count and keyboard hint. Press Esc, then Tab in the source to focus the toolbar.
134
+
135
+ ## Controlled value and history
136
+
137
+ Every edit calls `onChange` with the next source: typing, toolbar formatting, insertion templates, rename, format and undo or redo. Pass that value back into `value`. The editor treats the edits it emits as part of the current session, so a normal controlled round trip keeps undo steps.
138
+
139
+ An external change to `value` updates the displayed text. When the host replaces the text with an independent document, update `documentKey` in the same render. A key change clears undo and redo, resets the selection and adopts the new value.
140
+
141
+ ```tsx
142
+ const [documentKey, setDocumentKey] = useState('estimate');
143
+ const [source, setSource] = useState(loadSource('estimate'));
144
+
145
+ function openDocument(key: string, text: string) {
146
+ setDocumentKey(key);
147
+ setSource(text);
148
+ }
149
+
150
+ <ReactiveMarkdownEditor
151
+ value={source}
152
+ onChange={setSource}
153
+ documentKey={documentKey}
154
+ />
155
+ ```
156
+
157
+ Undo history belongs to the mounted instance. It is not saved, not shared between instances and not derived from the whole `value` prop. Use `documentKey` for document switches rather than remounting on every keystroke.
158
+
159
+ ## Locale and theme
160
+
161
+ Each instance applies its own locale and theme. The editor does not change the document `data-theme` attribute, does not write browser storage and does not read the Playground language setting.
162
+
163
+ ```tsx
164
+ <ReactiveMarkdownEditor
165
+ value={source}
166
+ onChange={setSource}
167
+ locale="zh"
168
+ theme="dark"
169
+ />
170
+ ```
171
+
172
+ `theme="auto"` follows the system color preference, including preference changes. Two editors on one page can use different values. The theme is set through the local `data-editor-theme` attribute on the `.reactive-md-editor` root.
173
+
174
+ ## Read-only and disabled
175
+
176
+ `readOnly` keeps the source visible, focusable and selectable. Typing, the toolbar, completion, rename, format and the ref `undo` and `redo` methods cannot change the value.
177
+
178
+ `disabled` also blocks focus and pointer interaction. Both modes disable the toolbar export action. Neither mode changes the value or the history on its own. Use them when a host is not allowed to edit source, for example while a read-only preview is shown.
179
+
180
+ Replacing `value` externally or changing `documentKey` resets history, selection and the editor's scroll position. Echoing a value received through `onChange` preserves them. The export button is hidden until `onExport` is provided.
181
+
182
+ ## Diagnostics and compiled document
183
+
184
+ The optional authoring tools analyze source locally, including syntax checks. The host manages compilation for its preview and can pass that result when it wants line markers or binding value types:
185
+
186
+ ```tsx
187
+ import { compileLocal } from 'reactive-md';
188
+
189
+ const result = compileLocal(source);
190
+
191
+ <ReactiveMarkdownEditor
192
+ value={source}
193
+ onChange={setSource}
194
+ diagnostics={result.diagnostics}
195
+ document={result.ok ? result.document : null}
196
+ />
197
+ ```
198
+
199
+ `diagnostics` marks the reported lines in the gutter. Incomplete source remains editable, and a compile error does not lock the textarea. `document` supplies binding value types for the authoring inspection panel. Both props are optional; pass neither when the editor is used only to edit Markdown.
200
+
201
+ ## Sizing and layout
202
+
203
+ The editor fills the width of its container. `height` defaults to `560px` and `minHeight` to `280px`; numbers are pixels and strings can use `vh`, `rem` or other CSS lengths. The gutter follows line count, and soft-wrapped lines keep their line number and highlighting aligned during scrolling.
204
+
205
+ Use `className` or `style` for margins and placement in a host layout. Internal element class names are implementation details.
206
+
207
+ ## Toolbar and authoring tools
208
+
209
+ `toolbar={false}` removes the formatting and insertion toolbar, including its export action. `authoring={false}` keeps the textarea and toolbar but disables syntax highlighting, completion, binding navigation, rename and format. Both default to enabled.
210
+
211
+ The toolbar formats Markdown, inserts lists, tables, links, images and the built-in component templates, and exposes undo and redo. Formatting commands use the current selection and return the next source through `onChange`.
212
+
213
+ ## Host responsibilities
214
+
215
+ The editor edits source. The host handles the rest:
216
+
217
+ - Preview: compile `value` and render it with `reactive-md/react` or your own runtime.
218
+ - Persistence: store the source. The editor does not auto-save, write local storage or send the source anywhere.
219
+ - Export: pass `onExport` to connect the toolbar action to your HTML export, download dialog or file service. The editor does not build a shareable HTML file.
220
+
221
+ Compilation scheduling and debouncing also stay in the host. See [Local mode integration](local-integration-en.md) and [Shareable documents and static export](shareable-export-en.md). A complete editor, preview and document-switching example is in [react-editor.tsx](../examples/integration/react-editor.tsx).
222
+
223
+ ## Server rendering
224
+
225
+ The editor entry has no top-level `window` or `document` access. Importing it and rendering the initial textarea with `react-dom/server` is safe. Focus, selection, completion, layout measurement and toolbar menus run after the component mounts in a browser, so interactive checks and browser tests need a client.
226
+
227
+ ## Styles and scope
228
+
229
+ `reactive-md/editor/styles.css` contains the editor's own tokens and rules. All selectors are scoped to `.reactive-md-editor` or its descendants, so the stylesheet does not restyle host buttons, inputs or the page body. The theme comes from the instance attribute described above.
230
+
231
+ If the same page also renders a document preview, import `reactive-md/styles.css` as well. The two stylesheets are independent and can be loaded in either order.
232
+
233
+ ## Compatibility
234
+
235
+ `0.5.0` adds the editor entry and keeps `local-draft-2`, `component-protocol-1` and `reactive-md-theme-1`. Projects that only render documents can upgrade without changing imports or source. The editor is new, so it has no older API to migrate from. See the [0.5.0 release notes](release-0.5.0-en.md) and [Versions and compatibility](compatibility-en.md).
236
+
237
+ .5.3 adds compile-option-aware authoring and explicit screen/print styles. See [0.5.3 host integration](release-0.5.3-en.md).