code/editor/useCodeDocument
import { … } from 'zidane-ui/code/editor'ui/src/code/editor/useCodeDocument.ts
Functions#
useCodeDocumentFunction#
Keeps one document's draft, dirty state and disk conflicts consistent; see the module comment.
function useCodeDocument<D extends CodeDoc>(
options: CodeDocumentOptions<D>,
): CodeDocument<D>Type parameters
| Name | Type | Description |
|---|---|---|
| D | extends CodeDoc |
Parameters
| Name | Type | Description |
|---|---|---|
| options | CodeDocumentOptions<D> | The on-disk file and how to save and reload it. |
Returns
CodeDocument<D>Interfaces#
CodeDocInterface#
A file as read from disk.
interface CodeDocProperties
| Name | Type | Description |
|---|---|---|
| contents | string | The file's text. |
| mtimeMs | number | Its modification time (or any version stamp); a save checks it to spot a file that moved on. |
CodeDocumentInterface#
The document's live state and actions; pass it to CodeEditor.
Type parameters
| Name | Type | Default | Description |
|---|---|---|---|
| D | extends CodeDoc | CodeDoc |
Properties
| Name | Type | Description |
|---|---|---|
| base | D | The on-disk text the editor works against. |
| conflict | boolean | The file changed on disk while dirty; the editor shows Reload / Overwrite. |
| contents | string | The text shown now: the draft while dirty, else |
| dirty | boolean | The text differs from |
| discard | () => void | Drops the draft and reseeds from the file on disk. |
| onEditorChange | (text: string, changes: readonly EditChange[]) => void | Feed it the editor's changes ( |
| resets | number | Bumps each time the edit session restarts (adopted from disk, discarded): a key for the editor's state. |
| save | (force?: boolean) => Promise<void> | Writes the draft (a no-op when clean or already saving); |
| seed | string | What the editor is seeded with. |
CodeDocumentOptionsInterface#
What useCodeDocument takes; D can carry extra fields alongside the text and version.
interface CodeDocumentOptions<D extends CodeDoc>Type parameters
| Name | Type | Description |
|---|---|---|
| D | extends CodeDoc |
Properties
| Name | Type | Description |
|---|---|---|
| doc | D | The latest on-disk truth. |
| initialBase? | D | The file |
| initialDraft? | string | null | A draft left by an earlier view, read once at mount, and the file it was typed against. |
| onAdopt? | (doc: D, reset: boolean) => void |
|
| onChange? | (text: string, changes: readonly EditChange[]) => void | Every editor change, unthrottled (a language server's sync). |
| onDraft? | (text: string | null, base: D) => void | The live draft, throttled; |
| reload | () => Promise<D | null> | The file as it is on disk now; |
| save | (text: string, base: D, force: boolean) => Promise<SaveOutcome<D>> | Write |
| throttleMs? | number | Least time between |