@invana/editor 0.0.25 → 0.0.27
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.
- package/dist/index.cjs +10 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +10 -2
- package/dist/index.d.ts +10 -2
- package/dist/index.js +11 -3
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
package/dist/index.cjs
CHANGED
|
@@ -85,6 +85,7 @@ var MODES = {
|
|
|
85
85
|
python: () => language.StreamLanguage.define(python.python),
|
|
86
86
|
shell: () => language.StreamLanguage.define(shell.shell),
|
|
87
87
|
javascript: () => language.StreamLanguage.define(javascript.javascript),
|
|
88
|
+
json: () => language.StreamLanguage.define(javascript.json),
|
|
88
89
|
cypher: () => language.StreamLanguage.define(cypher.cypher),
|
|
89
90
|
plain: () => null
|
|
90
91
|
};
|
|
@@ -92,7 +93,9 @@ function CodeBlock({
|
|
|
92
93
|
value,
|
|
93
94
|
language: language$1 = "plain",
|
|
94
95
|
showLineNumbers,
|
|
96
|
+
maxHeight,
|
|
95
97
|
className,
|
|
98
|
+
style,
|
|
96
99
|
...props
|
|
97
100
|
}) {
|
|
98
101
|
const extensions = React__namespace.useMemo(() => {
|
|
@@ -105,9 +108,13 @@ function CodeBlock({
|
|
|
105
108
|
state.EditorState.readOnly.of(true),
|
|
106
109
|
view.EditorView.lineWrapping,
|
|
107
110
|
...showLineNumbers ? [view.lineNumbers()] : [],
|
|
108
|
-
...mode ? [mode] : []
|
|
111
|
+
...mode ? [mode] : [],
|
|
112
|
+
// The scroller lives inside CodeMirror rather than on the wrapper, so the
|
|
113
|
+
// editor knows its own viewport and does not render every line of a long
|
|
114
|
+
// document into a box that then clips them.
|
|
115
|
+
...maxHeight != null ? [view.EditorView.theme({ "&": { maxHeight: `${maxHeight}px` } })] : []
|
|
109
116
|
];
|
|
110
|
-
}, [language$1, showLineNumbers]);
|
|
117
|
+
}, [language$1, showLineNumbers, maxHeight]);
|
|
111
118
|
const { ref } = useCodeMirror({ value, extensions });
|
|
112
119
|
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
113
120
|
"div",
|
|
@@ -119,6 +126,7 @@ function CodeBlock({
|
|
|
119
126
|
"[&_.cm-gutters]:border-none [&_.cm-gutters]:bg-transparent [&_.cm-gutters]:text-muted-foreground",
|
|
120
127
|
className
|
|
121
128
|
),
|
|
129
|
+
style,
|
|
122
130
|
...props
|
|
123
131
|
}
|
|
124
132
|
);
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/use-code-mirror.ts","../src/code-block.tsx","../src/markdown-editor-block.tsx"],"names":["React","view","Compartment","EditorView","EditorState","StreamLanguage","python","shell","javascript","cypher","language","React2","syntaxHighlighting","defaultHighlightStyle","lineNumbers","jsx","cn","React3","history","keymap","defaultKeymap","historyKeymap","jsxs"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkBO,SAAS,aAAA,CAAc;AAAA,EAC5B,KAAA;AAAA,EACA,UAAA;AAAA,EACA;AACF,CAAA,EAAyB;AACvB,EAAA,MAAM,IAAA,GAAaA,wBAA8B,IAAI,CAAA;AACrD,EAAA,MAAMC,MAAA,GAAaD,wBAA0B,IAAI,CAAA;AAKjD,EAAA,MAAM,WAAA,GAAoBA,wBAAO,QAAQ,CAAA;AACzC,EAAMA,2BAAU,MAAM;AACpB,IAAA,WAAA,CAAY,OAAA,GAAU,QAAA;AAAA,EACxB,CAAA,EAAG,CAAC,QAAQ,CAAC,CAAA;AAOb,EAAA,MAAM,aAAA,GAAsBA,wBAAO,UAAU,CAAA;AAC7C,EAAA,aAAA,CAAc,OAAA,GAAU,UAAA;AAExB,EAAA,MAAM,WAAA,GAAoBA,gBAAA,CAAA,MAAA,CAAO,IAAIE,iBAAA,EAAa,CAAA;AAElD,EAAMF,2BAAU,MAAM;AACpB,IAAA,IAAI,CAAC,KAAK,OAAA,EAAS;AACnB,IAAA,MA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AACF;AAAA;AACF,GAAA,EACF,CAAA;AAEJ","file":"index.cjs","sourcesContent":["import * as React from \"react\"\nimport { Compartment, EditorState, type Extension } from \"@codemirror/state\"\nimport { EditorView } from \"@codemirror/view\"\n\nexport interface UseCodeMirrorOptions {\n value: string\n extensions: Extension[]\n onChange?: (value: string) => void\n}\n\n/**\n * Mounts a CodeMirror view into a ref'd element and keeps it in sync.\n *\n * The whole reason this is a hook rather than a component: CodeMirror owns its\n * own DOM, so React must not re-render into it. Value changes are pushed as\n * transactions, and only when they actually differ — writing the view's own\n * output back into it would move the cursor to the end on every keystroke.\n */\nexport function useCodeMirror({\n value,\n extensions,\n onChange,\n}: UseCodeMirrorOptions) {\n const host = React.useRef<HTMLDivElement | null>(null)\n const view = React.useRef<EditorView | null>(null)\n\n // Keep the latest onChange without tearing down the view when it changes\n // identity — a caller passing an inline arrow would otherwise remount the\n // editor on every render.\n const onChangeRef = React.useRef(onChange)\n React.useEffect(() => {\n onChangeRef.current = onChange\n }, [onChange])\n\n // The view is created once and reconfigured in place. Keying the effect on\n // `extensions` identity looked simpler, but a caller passing an inline array\n // would then rebuild the editor on every render — losing the cursor, the\n // selection and focus mid-keystroke. \"Callers are expected to memoise\" is a\n // trap, not a contract, so the hook does not depend on it.\n const extensionsRef = React.useRef(extensions)\n extensionsRef.current = extensions\n\n const compartment = React.useRef(new Compartment())\n\n React.useEffect(() => {\n if (!host.current) return\n const v = new EditorView({\n state: EditorState.create({\n doc: value,\n extensions: [\n compartment.current.of(extensionsRef.current),\n EditorView.updateListener.of((u) => {\n if (u.docChanged) onChangeRef.current?.(u.state.doc.toString())\n }),\n ],\n }),\n parent: host.current,\n })\n view.current = v\n return () => {\n v.destroy()\n view.current = null\n }\n // Mount once. Configuration changes go through the compartment below.\n }, []) // eslint-disable-line react-hooks/exhaustive-deps\n\n // Swap configuration without touching the document — this is what a\n // Compartment is for, and it is why toggling `readOnly` or a language keeps\n // the caret where it was.\n React.useEffect(() => {\n const v = view.current\n if (!v) return\n v.dispatch({ effects: compartment.current.reconfigure(extensions) })\n }, [extensions])\n\n React.useEffect(() => {\n const v = view.current\n if (!v) return\n const current = v.state.doc.toString()\n if (current === value) return\n v.dispatch({ changes: { from: 0, to: current.length, insert: value } })\n }, [value])\n\n return { ref: host, view }\n}\n","import * as React from \"react\"\nimport { EditorView, lineNumbers } from \"@codemirror/view\"\nimport { EditorState } from \"@codemirror/state\"\nimport {\n StreamLanguage,\n syntaxHighlighting,\n defaultHighlightStyle,\n} from \"@codemirror/language\"\nimport { python } from \"@codemirror/legacy-modes/mode/python\"\nimport { shell } from \"@codemirror/legacy-modes/mode/shell\"\nimport { javascript } from \"@codemirror/legacy-modes/mode/javascript\"\nimport { cypher } from \"@codemirror/legacy-modes/mode/cypher\"\nimport { cn } from \"@invana/ui\"\n\nimport { useCodeMirror } from \"./use-code-mirror\"\n\nexport type CodeLanguage = \"python\" | \"shell\" | \"javascript\" | \"cypher\" | \"plain\"\n\nexport interface CodeBlockProps\n extends Omit<React.HTMLAttributes<HTMLDivElement>, \"onChange\"> {\n value: string\n language?: CodeLanguage\n /** Line numbers are off by default — a five-line snippet does not need a gutter. */\n showLineNumbers?: boolean\n}\n\nconst MODES = {\n python: () => StreamLanguage.define(python),\n shell: () => StreamLanguage.define(shell),\n javascript: () => StreamLanguage.define(javascript),\n cypher: () => StreamLanguage.define(cypher),\n plain: () => null,\n}\n\n/**\n * Code, shown as it is — read-only.\n *\n * The DAG in a hand-off, a query in a diagnosis, an exported model. It is not a\n * disabled editor: `EditorState.readOnly` plus a non-editable view means there\n * is no cursor to place and no illusion that typing would do something.\n */\nexport function CodeBlock({\n value,\n language = \"plain\",\n showLineNumbers,\n className,\n ...props\n}: CodeBlockProps) {\n const extensions = React.useMemo(() => {\n const mode = MODES[language]()\n return [\n // StreamLanguage only produces tokens; without a highlight style nothing\n // paints them and the block renders as flat text.\n syntaxHighlighting(defaultHighlightStyle, { fallback: true }),\n EditorView.editable.of(false),\n EditorState.readOnly.of(true),\n EditorView.lineWrapping,\n ...(showLineNumbers ? [lineNumbers()] : []),\n ...(mode ? [mode] : []),\n ]\n }, [language, showLineNumbers])\n\n const { ref } = useCodeMirror({ value, extensions })\n\n return (\n <div\n ref={ref}\n className={cn(\n \"overflow-hidden border border-border bg-muted/40 [&_.cm-editor]:bg-transparent\",\n \"[&_.cm-content]:font-mono [&_.cm-content]:text-meta [&_.cm-focused]:outline-none\",\n \"[&_.cm-gutters]:border-none [&_.cm-gutters]:bg-transparent [&_.cm-gutters]:text-muted-foreground\",\n className,\n )}\n {...props}\n />\n )\n}\n","import * as React from \"react\"\nimport { EditorView } from \"@codemirror/view\"\nimport { history, defaultKeymap, historyKeymap } from \"@codemirror/commands\"\nimport { keymap } from \"@codemirror/view\"\nimport { cn } from \"@invana/ui\"\n\nimport { useCodeMirror } from \"./use-code-mirror\"\n\nexport interface MarkdownEditorBlockProps\n extends Omit<React.HTMLAttributes<HTMLDivElement>, \"onChange\" | \"children\"> {\n value: string\n onChange?: (value: string) => void\n readOnly?: boolean\n /** A version marker shown in the corner — `v5`, `v7 · editing`. */\n version?: React.ReactNode\n placeholder?: string\n}\n\n/**\n * The prose a person writes for an agent to read — graph rules, a skill's\n * content, an acceptance note.\n *\n * Plain markdown in a monospace field, not a rich-text editor. What is typed\n * here is offered verbatim in a model's prompt, so what you see has to be\n * exactly what the agent gets; a WYSIWYG layer would put a translation step\n * between the author and the thing that runs.\n *\n * `version` sits in the corner because these documents are versioned and a\n * running thinking finishes on the version it started with — editing without\n * seeing which version you are on is how two people overwrite each other.\n */\nexport function MarkdownEditorBlock({\n value,\n onChange,\n readOnly,\n version,\n className,\n ...props\n}: MarkdownEditorBlockProps) {\n const extensions = React.useMemo(\n () => [\n // No markdown syntax mode, deliberately. What is typed here is offered\n // verbatim in a model's prompt, and highlighting it would be decoration\n // on text whose whole contract is that it is plain. It also keeps this\n // package's dependencies to the CodeMirror set the product already ships.\n EditorView.lineWrapping,\n history(),\n keymap.of([...defaultKeymap, ...historyKeymap]),\n EditorView.editable.of(!readOnly),\n ],\n [readOnly],\n )\n\n const { ref } = useCodeMirror({ value, extensions, onChange })\n\n return (\n <div className={cn(\"relative border border-border bg-muted/40\", className)} {...props}>\n {version != null ? (\n <span className=\"absolute right-1 top-1 z-10 border border-border bg-card px-1 text-meta text-muted-foreground\">\n {version}\n </span>\n ) : null}\n <div\n ref={ref}\n className={cn(\n \"[&_.cm-editor]:bg-transparent [&_.cm-content]:font-mono [&_.cm-content]:text-meta\",\n \"[&_.cm-focused]:outline-none [&_.cm-cursor]:border-foreground\",\n )}\n />\n </div>\n )\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/use-code-mirror.ts","../src/code-block.tsx","../src/markdown-editor-block.tsx"],"names":["React","view","Compartment","EditorView","EditorState","StreamLanguage","python","shell","javascript","json","cypher","language","React2","syntaxHighlighting","defaultHighlightStyle","lineNumbers","jsx","cn","React3","history","keymap","defaultKeymap","historyKeymap","jsxs"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkBO,SAAS,aAAA,CAAc;AAAA,EAC5B,KAAA;AAAA,EACA,UAAA;AAAA,EACA;AACF,CAAA,EAAyB;AACvB,EAAA,MAAM,IAAA,GAAaA,wBAA8B,IAAI,CAAA;AACrD,EAAA,MAAMC,MAAA,GAAaD,wBAA0B,IAAI,CAAA;AAKjD,EAAA,MAAM,WAAA,GAAoBA,wBAAO,QAAQ,CAAA;AACzC,EAAMA,2BAAU,MAAM;AACpB,IAAA,WAAA,CAAY,OAAA,GAAU,QAAA;AAAA,EACxB,CAAA,EAAG,CAAC,QAAQ,CAAC,CAAA;AAOb,EAAA,MAAM,aAAA,GAAsBA,wBAAO,UAAU,CAAA;AAC7C,EAAA,aAAA,CAAc,OAAA,GAAU,UAAA;AAExB,EAAA,MAAM,WAAA,GAAoBA,gBAAA,CAAA,MAAA,CAAO,IAAIE,iBAAA,EAAa,CAAA;AAElD,EAAMF,2BAAU,MAAM;AACpB,IAAA,IAAI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AE7D,EAAA,uBACEoB,eAAA,CAAC,SAAI,SAAA,EAAWN,KAAAA,CAAG,6CAA6C,SAAS,CAAA,EAAI,GAAG,KAAA,EAC7E,QAAA,EAAA;AAAA,IAAA,OAAA,IAAW,uBACVD,cAAAA,CAAC,UAAK,SAAA,EAAU,+FAAA,EACb,mBACH,CAAA,GACE,IAAA;AAAA,oBACJA,cAAAA;AAAA,MAAC,KAAA;AAAA,MAAA;AAAA,QACC,GAAA;AAAA,QACA,SAAA,EAAWC,KAAAA;AAAA,UACT,mFAAA;AAAA,UACA;AAAA;AACF;AAAA;AACF,GAAA,EACF,CAAA;AAEJ","file":"index.cjs","sourcesContent":["import * as React from \"react\"\nimport { Compartment, EditorState, type Extension } from \"@codemirror/state\"\nimport { EditorView } from \"@codemirror/view\"\n\nexport interface UseCodeMirrorOptions {\n value: string\n extensions: Extension[]\n onChange?: (value: string) => void\n}\n\n/**\n * Mounts a CodeMirror view into a ref'd element and keeps it in sync.\n *\n * The whole reason this is a hook rather than a component: CodeMirror owns its\n * own DOM, so React must not re-render into it. Value changes are pushed as\n * transactions, and only when they actually differ — writing the view's own\n * output back into it would move the cursor to the end on every keystroke.\n */\nexport function useCodeMirror({\n value,\n extensions,\n onChange,\n}: UseCodeMirrorOptions) {\n const host = React.useRef<HTMLDivElement | null>(null)\n const view = React.useRef<EditorView | null>(null)\n\n // Keep the latest onChange without tearing down the view when it changes\n // identity — a caller passing an inline arrow would otherwise remount the\n // editor on every render.\n const onChangeRef = React.useRef(onChange)\n React.useEffect(() => {\n onChangeRef.current = onChange\n }, [onChange])\n\n // The view is created once and reconfigured in place. Keying the effect on\n // `extensions` identity looked simpler, but a caller passing an inline array\n // would then rebuild the editor on every render — losing the cursor, the\n // selection and focus mid-keystroke. \"Callers are expected to memoise\" is a\n // trap, not a contract, so the hook does not depend on it.\n const extensionsRef = React.useRef(extensions)\n extensionsRef.current = extensions\n\n const compartment = React.useRef(new Compartment())\n\n React.useEffect(() => {\n if (!host.current) return\n const v = new EditorView({\n state: EditorState.create({\n doc: value,\n extensions: [\n compartment.current.of(extensionsRef.current),\n EditorView.updateListener.of((u) => {\n if (u.docChanged) onChangeRef.current?.(u.state.doc.toString())\n }),\n ],\n }),\n parent: host.current,\n })\n view.current = v\n return () => {\n v.destroy()\n view.current = null\n }\n // Mount once. Configuration changes go through the compartment below.\n }, []) // eslint-disable-line react-hooks/exhaustive-deps\n\n // Swap configuration without touching the document — this is what a\n // Compartment is for, and it is why toggling `readOnly` or a language keeps\n // the caret where it was.\n React.useEffect(() => {\n const v = view.current\n if (!v) return\n v.dispatch({ effects: compartment.current.reconfigure(extensions) })\n }, [extensions])\n\n React.useEffect(() => {\n const v = view.current\n if (!v) return\n const current = v.state.doc.toString()\n if (current === value) return\n v.dispatch({ changes: { from: 0, to: current.length, insert: value } })\n }, [value])\n\n return { ref: host, view }\n}\n","import * as React from \"react\"\nimport { EditorView, lineNumbers } from \"@codemirror/view\"\nimport { EditorState } from \"@codemirror/state\"\nimport {\n StreamLanguage,\n syntaxHighlighting,\n defaultHighlightStyle,\n} from \"@codemirror/language\"\nimport { python } from \"@codemirror/legacy-modes/mode/python\"\nimport { shell } from \"@codemirror/legacy-modes/mode/shell\"\nimport { javascript, json } from \"@codemirror/legacy-modes/mode/javascript\"\nimport { cypher } from \"@codemirror/legacy-modes/mode/cypher\"\nimport { cn } from \"@invana/ui\"\n\nimport { useCodeMirror } from \"./use-code-mirror\"\n\nexport type CodeLanguage =\n | \"python\"\n | \"shell\"\n | \"javascript\"\n | \"json\"\n | \"cypher\"\n | \"plain\"\n\nexport interface CodeBlockProps\n extends Omit<React.HTMLAttributes<HTMLDivElement>, \"onChange\"> {\n value: string\n language?: CodeLanguage\n /** Line numbers are off by default — a five-line snippet does not need a gutter. */\n showLineNumbers?: boolean\n /**\n * Cap the block's height in px and let it scroll inside that, for a document\n * whose length is not the point — a run's `result.json`, an exported model.\n *\n * Unset, the block is as tall as its content, which is right for a snippet\n * and wrong for a record that grows as a run proceeds.\n */\n maxHeight?: number\n}\n\nconst MODES = {\n python: () => StreamLanguage.define(python),\n shell: () => StreamLanguage.define(shell),\n javascript: () => StreamLanguage.define(javascript),\n json: () => StreamLanguage.define(json),\n cypher: () => StreamLanguage.define(cypher),\n plain: () => null,\n}\n\n/**\n * Code, shown as it is — read-only.\n *\n * The DAG in a hand-off, a query in a diagnosis, an exported model. It is not a\n * disabled editor: `EditorState.readOnly` plus a non-editable view means there\n * is no cursor to place and no illusion that typing would do something.\n */\nexport function CodeBlock({\n value,\n language = \"plain\",\n showLineNumbers,\n maxHeight,\n className,\n style,\n ...props\n}: CodeBlockProps) {\n const extensions = React.useMemo(() => {\n const mode = MODES[language]()\n return [\n // StreamLanguage only produces tokens; without a highlight style nothing\n // paints them and the block renders as flat text.\n syntaxHighlighting(defaultHighlightStyle, { fallback: true }),\n EditorView.editable.of(false),\n EditorState.readOnly.of(true),\n EditorView.lineWrapping,\n ...(showLineNumbers ? [lineNumbers()] : []),\n ...(mode ? [mode] : []),\n // The scroller lives inside CodeMirror rather than on the wrapper, so the\n // editor knows its own viewport and does not render every line of a long\n // document into a box that then clips them.\n ...(maxHeight != null\n ? [EditorView.theme({ \"&\": { maxHeight: `${maxHeight}px` } })]\n : []),\n ]\n }, [language, showLineNumbers, maxHeight])\n\n const { ref } = useCodeMirror({ value, extensions })\n\n return (\n <div\n ref={ref}\n className={cn(\n \"overflow-hidden border border-border bg-muted/40 [&_.cm-editor]:bg-transparent\",\n \"[&_.cm-content]:font-mono [&_.cm-content]:text-meta [&_.cm-focused]:outline-none\",\n \"[&_.cm-gutters]:border-none [&_.cm-gutters]:bg-transparent [&_.cm-gutters]:text-muted-foreground\",\n className,\n )}\n style={style}\n {...props}\n />\n )\n}\n","import * as React from \"react\"\nimport { EditorView } from \"@codemirror/view\"\nimport { history, defaultKeymap, historyKeymap } from \"@codemirror/commands\"\nimport { keymap } from \"@codemirror/view\"\nimport { cn } from \"@invana/ui\"\n\nimport { useCodeMirror } from \"./use-code-mirror\"\n\nexport interface MarkdownEditorBlockProps\n extends Omit<React.HTMLAttributes<HTMLDivElement>, \"onChange\" | \"children\"> {\n value: string\n onChange?: (value: string) => void\n readOnly?: boolean\n /** A version marker shown in the corner — `v5`, `v7 · editing`. */\n version?: React.ReactNode\n placeholder?: string\n}\n\n/**\n * The prose a person writes for an agent to read — graph rules, a skill's\n * content, an acceptance note.\n *\n * Plain markdown in a monospace field, not a rich-text editor. What is typed\n * here is offered verbatim in a model's prompt, so what you see has to be\n * exactly what the agent gets; a WYSIWYG layer would put a translation step\n * between the author and the thing that runs.\n *\n * `version` sits in the corner because these documents are versioned and a\n * running thinking finishes on the version it started with — editing without\n * seeing which version you are on is how two people overwrite each other.\n */\nexport function MarkdownEditorBlock({\n value,\n onChange,\n readOnly,\n version,\n className,\n ...props\n}: MarkdownEditorBlockProps) {\n const extensions = React.useMemo(\n () => [\n // No markdown syntax mode, deliberately. What is typed here is offered\n // verbatim in a model's prompt, and highlighting it would be decoration\n // on text whose whole contract is that it is plain. It also keeps this\n // package's dependencies to the CodeMirror set the product already ships.\n EditorView.lineWrapping,\n history(),\n keymap.of([...defaultKeymap, ...historyKeymap]),\n EditorView.editable.of(!readOnly),\n ],\n [readOnly],\n )\n\n const { ref } = useCodeMirror({ value, extensions, onChange })\n\n return (\n <div className={cn(\"relative border border-border bg-muted/40\", className)} {...props}>\n {version != null ? (\n <span className=\"absolute right-1 top-1 z-10 border border-border bg-card px-1 text-meta text-muted-foreground\">\n {version}\n </span>\n ) : null}\n <div\n ref={ref}\n className={cn(\n \"[&_.cm-editor]:bg-transparent [&_.cm-content]:font-mono [&_.cm-content]:text-meta\",\n \"[&_.cm-focused]:outline-none [&_.cm-cursor]:border-foreground\",\n )}\n />\n </div>\n )\n}\n"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -2,12 +2,20 @@ import * as React from 'react';
|
|
|
2
2
|
import { Extension } from '@codemirror/state';
|
|
3
3
|
import { EditorView } from '@codemirror/view';
|
|
4
4
|
|
|
5
|
-
type CodeLanguage = "python" | "shell" | "javascript" | "cypher" | "plain";
|
|
5
|
+
type CodeLanguage = "python" | "shell" | "javascript" | "json" | "cypher" | "plain";
|
|
6
6
|
interface CodeBlockProps extends Omit<React.HTMLAttributes<HTMLDivElement>, "onChange"> {
|
|
7
7
|
value: string;
|
|
8
8
|
language?: CodeLanguage;
|
|
9
9
|
/** Line numbers are off by default — a five-line snippet does not need a gutter. */
|
|
10
10
|
showLineNumbers?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Cap the block's height in px and let it scroll inside that, for a document
|
|
13
|
+
* whose length is not the point — a run's `result.json`, an exported model.
|
|
14
|
+
*
|
|
15
|
+
* Unset, the block is as tall as its content, which is right for a snippet
|
|
16
|
+
* and wrong for a record that grows as a run proceeds.
|
|
17
|
+
*/
|
|
18
|
+
maxHeight?: number;
|
|
11
19
|
}
|
|
12
20
|
/**
|
|
13
21
|
* Code, shown as it is — read-only.
|
|
@@ -16,7 +24,7 @@ interface CodeBlockProps extends Omit<React.HTMLAttributes<HTMLDivElement>, "onC
|
|
|
16
24
|
* disabled editor: `EditorState.readOnly` plus a non-editable view means there
|
|
17
25
|
* is no cursor to place and no illusion that typing would do something.
|
|
18
26
|
*/
|
|
19
|
-
declare function CodeBlock({ value, language, showLineNumbers, className, ...props }: CodeBlockProps): React.JSX.Element;
|
|
27
|
+
declare function CodeBlock({ value, language, showLineNumbers, maxHeight, className, style, ...props }: CodeBlockProps): React.JSX.Element;
|
|
20
28
|
|
|
21
29
|
interface MarkdownEditorBlockProps extends Omit<React.HTMLAttributes<HTMLDivElement>, "onChange" | "children"> {
|
|
22
30
|
value: string;
|
package/dist/index.d.ts
CHANGED
|
@@ -2,12 +2,20 @@ import * as React from 'react';
|
|
|
2
2
|
import { Extension } from '@codemirror/state';
|
|
3
3
|
import { EditorView } from '@codemirror/view';
|
|
4
4
|
|
|
5
|
-
type CodeLanguage = "python" | "shell" | "javascript" | "cypher" | "plain";
|
|
5
|
+
type CodeLanguage = "python" | "shell" | "javascript" | "json" | "cypher" | "plain";
|
|
6
6
|
interface CodeBlockProps extends Omit<React.HTMLAttributes<HTMLDivElement>, "onChange"> {
|
|
7
7
|
value: string;
|
|
8
8
|
language?: CodeLanguage;
|
|
9
9
|
/** Line numbers are off by default — a five-line snippet does not need a gutter. */
|
|
10
10
|
showLineNumbers?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Cap the block's height in px and let it scroll inside that, for a document
|
|
13
|
+
* whose length is not the point — a run's `result.json`, an exported model.
|
|
14
|
+
*
|
|
15
|
+
* Unset, the block is as tall as its content, which is right for a snippet
|
|
16
|
+
* and wrong for a record that grows as a run proceeds.
|
|
17
|
+
*/
|
|
18
|
+
maxHeight?: number;
|
|
11
19
|
}
|
|
12
20
|
/**
|
|
13
21
|
* Code, shown as it is — read-only.
|
|
@@ -16,7 +24,7 @@ interface CodeBlockProps extends Omit<React.HTMLAttributes<HTMLDivElement>, "onC
|
|
|
16
24
|
* disabled editor: `EditorState.readOnly` plus a non-editable view means there
|
|
17
25
|
* is no cursor to place and no illusion that typing would do something.
|
|
18
26
|
*/
|
|
19
|
-
declare function CodeBlock({ value, language, showLineNumbers, className, ...props }: CodeBlockProps): React.JSX.Element;
|
|
27
|
+
declare function CodeBlock({ value, language, showLineNumbers, maxHeight, className, style, ...props }: CodeBlockProps): React.JSX.Element;
|
|
20
28
|
|
|
21
29
|
interface MarkdownEditorBlockProps extends Omit<React.HTMLAttributes<HTMLDivElement>, "onChange" | "children"> {
|
|
22
30
|
value: string;
|
package/dist/index.js
CHANGED
|
@@ -4,7 +4,7 @@ import { Compartment, EditorState } from '@codemirror/state';
|
|
|
4
4
|
import { syntaxHighlighting, defaultHighlightStyle, StreamLanguage } from '@codemirror/language';
|
|
5
5
|
import { python } from '@codemirror/legacy-modes/mode/python';
|
|
6
6
|
import { shell } from '@codemirror/legacy-modes/mode/shell';
|
|
7
|
-
import { javascript } from '@codemirror/legacy-modes/mode/javascript';
|
|
7
|
+
import { json, javascript } from '@codemirror/legacy-modes/mode/javascript';
|
|
8
8
|
import { cypher } from '@codemirror/legacy-modes/mode/cypher';
|
|
9
9
|
import { cn } from '@invana/ui';
|
|
10
10
|
import { jsx, jsxs } from 'react/jsx-runtime';
|
|
@@ -63,6 +63,7 @@ var MODES = {
|
|
|
63
63
|
python: () => StreamLanguage.define(python),
|
|
64
64
|
shell: () => StreamLanguage.define(shell),
|
|
65
65
|
javascript: () => StreamLanguage.define(javascript),
|
|
66
|
+
json: () => StreamLanguage.define(json),
|
|
66
67
|
cypher: () => StreamLanguage.define(cypher),
|
|
67
68
|
plain: () => null
|
|
68
69
|
};
|
|
@@ -70,7 +71,9 @@ function CodeBlock({
|
|
|
70
71
|
value,
|
|
71
72
|
language = "plain",
|
|
72
73
|
showLineNumbers,
|
|
74
|
+
maxHeight,
|
|
73
75
|
className,
|
|
76
|
+
style,
|
|
74
77
|
...props
|
|
75
78
|
}) {
|
|
76
79
|
const extensions = React.useMemo(() => {
|
|
@@ -83,9 +86,13 @@ function CodeBlock({
|
|
|
83
86
|
EditorState.readOnly.of(true),
|
|
84
87
|
EditorView.lineWrapping,
|
|
85
88
|
...showLineNumbers ? [lineNumbers()] : [],
|
|
86
|
-
...mode ? [mode] : []
|
|
89
|
+
...mode ? [mode] : [],
|
|
90
|
+
// The scroller lives inside CodeMirror rather than on the wrapper, so the
|
|
91
|
+
// editor knows its own viewport and does not render every line of a long
|
|
92
|
+
// document into a box that then clips them.
|
|
93
|
+
...maxHeight != null ? [EditorView.theme({ "&": { maxHeight: `${maxHeight}px` } })] : []
|
|
87
94
|
];
|
|
88
|
-
}, [language, showLineNumbers]);
|
|
95
|
+
}, [language, showLineNumbers, maxHeight]);
|
|
89
96
|
const { ref } = useCodeMirror({ value, extensions });
|
|
90
97
|
return /* @__PURE__ */ jsx(
|
|
91
98
|
"div",
|
|
@@ -97,6 +104,7 @@ function CodeBlock({
|
|
|
97
104
|
"[&_.cm-gutters]:border-none [&_.cm-gutters]:bg-transparent [&_.cm-gutters]:text-muted-foreground",
|
|
98
105
|
className
|
|
99
106
|
),
|
|
107
|
+
style,
|
|
100
108
|
...props
|
|
101
109
|
}
|
|
102
110
|
);
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/use-code-mirror.ts","../src/code-block.tsx","../src/markdown-editor-block.tsx"],"names":["React2","EditorView","EditorState","React3","cn","jsx"],"mappings":";;;;;;;;;;;;;AAkBO,SAAS,aAAA,CAAc;AAAA,EAC5B,KAAA;AAAA,EACA,UAAA;AAAA,EACA;AACF,CAAA,EAAyB;AACvB,EAAA,MAAM,IAAA,GAAa,aAA8B,IAAI,CAAA;AACrD,EAAA,MAAM,IAAA,GAAa,aAA0B,IAAI,CAAA;AAKjD,EAAA,MAAM,WAAA,GAAoB,aAAO,QAAQ,CAAA;AACzC,EAAM,gBAAU,MAAM;AACpB,IAAA,WAAA,CAAY,OAAA,GAAU,QAAA;AAAA,EACxB,CAAA,EAAG,CAAC,QAAQ,CAAC,CAAA;AAOb,EAAA,MAAM,aAAA,GAAsB,aAAO,UAAU,CAAA;AAC7C,EAAA,aAAA,CAAc,OAAA,GAAU,UAAA;AAExB,EAAA,MAAM,WAAA,GAAoB,KAAA,CAAA,MAAA,CAAO,IAAI,WAAA,EAAa,CAAA;AAElD,EAAM,gBAAU,MAAM;AACpB,IAAA,IAAI,CAAC,KAAK,OAAA,EAAS;AACnB,IAAA,MAAM,CAAA,GAAI,IAAI,UAAA,CAAW;AAAA,MACvB,KAAA,EAAO,YAAY,MAAA,CAAO;AAAA,QACxB,GAAA,EAAK,KAAA;AAAA,QACL,UAAA,EAAY;AAAA,UACV,WAAA,CAAY,OAAA,CAAQ,EAAA,CAAG,aAAA,CAAc,OAAO,CAAA;AAAA,UAC5C,UAAA,CAAW,cAAA,CAAe,EAAA,CAAG,CAAC,CAAA,KAAM;AAClC,YAAA,IAAI,CAAA,CAAE,YAAY,WAAA,CAAY,OAAA,GAAU,E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AA,EACA,OAAA;AAAA,EACA,SAAA;AAAA,EACA,GAAG;AACL,CAAA,EAA6B;AAC3B,EAAA,MAAM,UAAA,GAAmBE,KAAA,CAAA,OAAA;AAAA,IACvB,MAAM;AAAA;AAAA;AAAA;AAAA;AAAA,MAKJF,UAAAA,CAAW,YAAA;AAAA,MACX,OAAA,EAAQ;AAAA,MACR,OAAO,EAAA,CAAG,CAAC,GAAG,aAAA,EAAe,GAAG,aAAa,CAAC,CAAA;AAAA,MAC9CA,UAAAA,CAAW,QAAA,CAAS,EAAA,CAAG,CAAC,QAAQ;AAAA,KAClC;AAAA,IACA,CAAC,QAAQ;AAAA,GACX;AAEA,EAAA,MAAM,EAAE,KAAI,GAAI,aAAA,CAAc,EAAE,KAAA,EAAO,UAAA,EAAY,UAAU,CAAA;AAE7D,EAAA,uBACE,IAAA,CAAC,SAAI,SAAA,EAAWG,EAAAA,CAAG,6CAA6C,SAAS,CAAA,EAAI,GAAG,KAAA,EAC7E,QAAA,EAAA;AAAA,IAAA,OAAA,IAAW,uBACVC,GAAAA,CAAC,UAAK,SAAA,EAAU,+FAAA,EACb,mBACH,CAAA,GACE,IAAA;AAAA,oBACJA,GAAAA;AAAA,MAAC,KAAA;AAAA,MAAA;AAAA,QACC,GAAA;AAAA,QACA,SAAA,EAAWD,EAAAA;AAAA,UACT,mFAAA;AAAA,UACA;AAAA;AACF;AAAA;AACF,GAAA,EACF,CAAA;AAEJ","file":"index.js","sourcesContent":["import * as React from \"react\"\nimport { Compartment, EditorState, type Extension } from \"@codemirror/state\"\nimport { EditorView } from \"@codemirror/view\"\n\nexport interface UseCodeMirrorOptions {\n value: string\n extensions: Extension[]\n onChange?: (value: string) => void\n}\n\n/**\n * Mounts a CodeMirror view into a ref'd element and keeps it in sync.\n *\n * The whole reason this is a hook rather than a component: CodeMirror owns its\n * own DOM, so React must not re-render into it. Value changes are pushed as\n * transactions, and only when they actually differ — writing the view's own\n * output back into it would move the cursor to the end on every keystroke.\n */\nexport function useCodeMirror({\n value,\n extensions,\n onChange,\n}: UseCodeMirrorOptions) {\n const host = React.useRef<HTMLDivElement | null>(null)\n const view = React.useRef<EditorView | null>(null)\n\n // Keep the latest onChange without tearing down the view when it changes\n // identity — a caller passing an inline arrow would otherwise remount the\n // editor on every render.\n const onChangeRef = React.useRef(onChange)\n React.useEffect(() => {\n onChangeRef.current = onChange\n }, [onChange])\n\n // The view is created once and reconfigured in place. Keying the effect on\n // `extensions` identity looked simpler, but a caller passing an inline array\n // would then rebuild the editor on every render — losing the cursor, the\n // selection and focus mid-keystroke. \"Callers are expected to memoise\" is a\n // trap, not a contract, so the hook does not depend on it.\n const extensionsRef = React.useRef(extensions)\n extensionsRef.current = extensions\n\n const compartment = React.useRef(new Compartment())\n\n React.useEffect(() => {\n if (!host.current) return\n const v = new EditorView({\n state: EditorState.create({\n doc: value,\n extensions: [\n compartment.current.of(extensionsRef.current),\n EditorView.updateListener.of((u) => {\n if (u.docChanged) onChangeRef.current?.(u.state.doc.toString())\n }),\n ],\n }),\n parent: host.current,\n })\n view.current = v\n return () => {\n v.destroy()\n view.current = null\n }\n // Mount once. Configuration changes go through the compartment below.\n }, []) // eslint-disable-line react-hooks/exhaustive-deps\n\n // Swap configuration without touching the document — this is what a\n // Compartment is for, and it is why toggling `readOnly` or a language keeps\n // the caret where it was.\n React.useEffect(() => {\n const v = view.current\n if (!v) return\n v.dispatch({ effects: compartment.current.reconfigure(extensions) })\n }, [extensions])\n\n React.useEffect(() => {\n const v = view.current\n if (!v) return\n const current = v.state.doc.toString()\n if (current === value) return\n v.dispatch({ changes: { from: 0, to: current.length, insert: value } })\n }, [value])\n\n return { ref: host, view }\n}\n","import * as React from \"react\"\nimport { EditorView, lineNumbers } from \"@codemirror/view\"\nimport { EditorState } from \"@codemirror/state\"\nimport {\n StreamLanguage,\n syntaxHighlighting,\n defaultHighlightStyle,\n} from \"@codemirror/language\"\nimport { python } from \"@codemirror/legacy-modes/mode/python\"\nimport { shell } from \"@codemirror/legacy-modes/mode/shell\"\nimport { javascript } from \"@codemirror/legacy-modes/mode/javascript\"\nimport { cypher } from \"@codemirror/legacy-modes/mode/cypher\"\nimport { cn } from \"@invana/ui\"\n\nimport { useCodeMirror } from \"./use-code-mirror\"\n\nexport type CodeLanguage = \"python\" | \"shell\" | \"javascript\" | \"cypher\" | \"plain\"\n\nexport interface CodeBlockProps\n extends Omit<React.HTMLAttributes<HTMLDivElement>, \"onChange\"> {\n value: string\n language?: CodeLanguage\n /** Line numbers are off by default — a five-line snippet does not need a gutter. */\n showLineNumbers?: boolean\n}\n\nconst MODES = {\n python: () => StreamLanguage.define(python),\n shell: () => StreamLanguage.define(shell),\n javascript: () => StreamLanguage.define(javascript),\n cypher: () => StreamLanguage.define(cypher),\n plain: () => null,\n}\n\n/**\n * Code, shown as it is — read-only.\n *\n * The DAG in a hand-off, a query in a diagnosis, an exported model. It is not a\n * disabled editor: `EditorState.readOnly` plus a non-editable view means there\n * is no cursor to place and no illusion that typing would do something.\n */\nexport function CodeBlock({\n value,\n language = \"plain\",\n showLineNumbers,\n className,\n ...props\n}: CodeBlockProps) {\n const extensions = React.useMemo(() => {\n const mode = MODES[language]()\n return [\n // StreamLanguage only produces tokens; without a highlight style nothing\n // paints them and the block renders as flat text.\n syntaxHighlighting(defaultHighlightStyle, { fallback: true }),\n EditorView.editable.of(false),\n EditorState.readOnly.of(true),\n EditorView.lineWrapping,\n ...(showLineNumbers ? [lineNumbers()] : []),\n ...(mode ? [mode] : []),\n ]\n }, [language, showLineNumbers])\n\n const { ref } = useCodeMirror({ value, extensions })\n\n return (\n <div\n ref={ref}\n className={cn(\n \"overflow-hidden border border-border bg-muted/40 [&_.cm-editor]:bg-transparent\",\n \"[&_.cm-content]:font-mono [&_.cm-content]:text-meta [&_.cm-focused]:outline-none\",\n \"[&_.cm-gutters]:border-none [&_.cm-gutters]:bg-transparent [&_.cm-gutters]:text-muted-foreground\",\n className,\n )}\n {...props}\n />\n )\n}\n","import * as React from \"react\"\nimport { EditorView } from \"@codemirror/view\"\nimport { history, defaultKeymap, historyKeymap } from \"@codemirror/commands\"\nimport { keymap } from \"@codemirror/view\"\nimport { cn } from \"@invana/ui\"\n\nimport { useCodeMirror } from \"./use-code-mirror\"\n\nexport interface MarkdownEditorBlockProps\n extends Omit<React.HTMLAttributes<HTMLDivElement>, \"onChange\" | \"children\"> {\n value: string\n onChange?: (value: string) => void\n readOnly?: boolean\n /** A version marker shown in the corner — `v5`, `v7 · editing`. */\n version?: React.ReactNode\n placeholder?: string\n}\n\n/**\n * The prose a person writes for an agent to read — graph rules, a skill's\n * content, an acceptance note.\n *\n * Plain markdown in a monospace field, not a rich-text editor. What is typed\n * here is offered verbatim in a model's prompt, so what you see has to be\n * exactly what the agent gets; a WYSIWYG layer would put a translation step\n * between the author and the thing that runs.\n *\n * `version` sits in the corner because these documents are versioned and a\n * running thinking finishes on the version it started with — editing without\n * seeing which version you are on is how two people overwrite each other.\n */\nexport function MarkdownEditorBlock({\n value,\n onChange,\n readOnly,\n version,\n className,\n ...props\n}: MarkdownEditorBlockProps) {\n const extensions = React.useMemo(\n () => [\n // No markdown syntax mode, deliberately. What is typed here is offered\n // verbatim in a model's prompt, and highlighting it would be decoration\n // on text whose whole contract is that it is plain. It also keeps this\n // package's dependencies to the CodeMirror set the product already ships.\n EditorView.lineWrapping,\n history(),\n keymap.of([...defaultKeymap, ...historyKeymap]),\n EditorView.editable.of(!readOnly),\n ],\n [readOnly],\n )\n\n const { ref } = useCodeMirror({ value, extensions, onChange })\n\n return (\n <div className={cn(\"relative border border-border bg-muted/40\", className)} {...props}>\n {version != null ? (\n <span className=\"absolute right-1 top-1 z-10 border border-border bg-card px-1 text-meta text-muted-foreground\">\n {version}\n </span>\n ) : null}\n <div\n ref={ref}\n className={cn(\n \"[&_.cm-editor]:bg-transparent [&_.cm-content]:font-mono [&_.cm-content]:text-meta\",\n \"[&_.cm-focused]:outline-none [&_.cm-cursor]:border-foreground\",\n )}\n />\n </div>\n )\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/use-code-mirror.ts","../src/code-block.tsx","../src/markdown-editor-block.tsx"],"names":["React2","EditorView","EditorState","React3","cn","jsx"],"mappings":";;;;;;;;;;;;;AAkBO,SAAS,aAAA,CAAc;AAAA,EAC5B,KAAA;AAAA,EACA,UAAA;AAAA,EACA;AACF,CAAA,EAAyB;AACvB,EAAA,MAAM,IAAA,GAAa,aAA8B,IAAI,CAAA;AACrD,EAAA,MAAM,IAAA,GAAa,aAA0B,IAAI,CAAA;AAKjD,EAAA,MAAM,WAAA,GAAoB,aAAO,QAAQ,CAAA;AACzC,EAAM,gBAAU,MAAM;AACpB,IAAA,WAAA,CAAY,OAAA,GAAU,QAAA;AAAA,EACxB,CAAA,EAAG,CAAC,QAAQ,CAAC,CAAA;AAOb,EAAA,MAAM,aAAA,GAAsB,aAAO,UAAU,CAAA;AAC7C,EAAA,aAAA,CAAc,OAAA,GAAU,UAAA;AAExB,EAAA,MAAM,WAAA,GAAoB,KAAA,CAAA,MAAA,CAAO,IAAI,WAAA,EAAa,CAAA;AAElD,EAAM,gBAAU,MAAM;AACpB,IAAA,IAAI,CAAC,KAAK,OAAA,EAAS;AACnB,IAAA,MAAM,CAAA,GAAI,IAAI,UAAA,CAAW;AAAA,MACvB,KAAA,EAAO,YAAY,MAAA,CAAO;AAAA,QACxB,GAAA,EAAK,KAAA;AAAA,QACL,UAAA,EAAY;AAAA,UACV,WAAA,CAAY,OAAA,CAAQ,EAAA,CAAG,aAAA,CAAc,OAAO,CAAA;AAAA,UAC5C,UAAA,CAAW,cAAA,CAAe,EAAA,CAAG,CAAC,CAAA,KAAM;AAClC,YAAA,IAAI,CAAA,CAAE,YAAY,WAAA,CAAY,OAAA,GAAU,EAAE,KAAA,CAAM,GAAA,CAAI,UAAU,CAAA;AAAA,UAChE,CAAC;AAAA;AACH,OACD,CAAA;AAAA,MACD,QAAQ,IAAA,CAAK;AAAA,KACd,CAAA;AACD,IAAA,IAAA,CAAK,OAAA,GAAU,CAAA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AAAA,MACA,KAAA;AAAA,MACC,GAAG;AAAA;AAAA,GACN;AAEJ;ACrEO,SAAS,mBAAA,CAAoB;AAAA,EAClC,KAAA;AAAA,EACA,QAAA;AAAA,EACA,QAAA;AAAA,EACA,OAAA;AAAA,EACA,SAAA;AAAA,EACA,GAAG;AACL,CAAA,EAA6B;AAC3B,EAAA,MAAM,UAAA,GAAmBE,KAAA,CAAA,OAAA;AAAA,IACvB,MAAM;AAAA;AAAA;AAAA;AAAA;AAAA,MAKJF,UAAAA,CAAW,YAAA;AAAA,MACX,OAAA,EAAQ;AAAA,MACR,OAAO,EAAA,CAAG,CAAC,GAAG,aAAA,EAAe,GAAG,aAAa,CAAC,CAAA;AAAA,MAC9CA,UAAAA,CAAW,QAAA,CAAS,EAAA,CAAG,CAAC,QAAQ;AAAA,KAClC;AAAA,IACA,CAAC,QAAQ;AAAA,GACX;AAEA,EAAA,MAAM,EAAE,KAAI,GAAI,aAAA,CAAc,EAAE,KAAA,EAAO,UAAA,EAAY,UAAU,CAAA;AAE7D,EAAA,uBACE,IAAA,CAAC,SAAI,SAAA,EAAWG,EAAAA,CAAG,6CAA6C,SAAS,CAAA,EAAI,GAAG,KAAA,EAC7E,QAAA,EAAA;AAAA,IAAA,OAAA,IAAW,uBACVC,GAAAA,CAAC,UAAK,SAAA,EAAU,+FAAA,EACb,mBACH,CAAA,GACE,IAAA;AAAA,oBACJA,GAAAA;AAAA,MAAC,KAAA;AAAA,MAAA;AAAA,QACC,GAAA;AAAA,QACA,SAAA,EAAWD,EAAAA;AAAA,UACT,mFAAA;AAAA,UACA;AAAA;AACF;AAAA;AACF,GAAA,EACF,CAAA;AAEJ","file":"index.js","sourcesContent":["import * as React from \"react\"\nimport { Compartment, EditorState, type Extension } from \"@codemirror/state\"\nimport { EditorView } from \"@codemirror/view\"\n\nexport interface UseCodeMirrorOptions {\n value: string\n extensions: Extension[]\n onChange?: (value: string) => void\n}\n\n/**\n * Mounts a CodeMirror view into a ref'd element and keeps it in sync.\n *\n * The whole reason this is a hook rather than a component: CodeMirror owns its\n * own DOM, so React must not re-render into it. Value changes are pushed as\n * transactions, and only when they actually differ — writing the view's own\n * output back into it would move the cursor to the end on every keystroke.\n */\nexport function useCodeMirror({\n value,\n extensions,\n onChange,\n}: UseCodeMirrorOptions) {\n const host = React.useRef<HTMLDivElement | null>(null)\n const view = React.useRef<EditorView | null>(null)\n\n // Keep the latest onChange without tearing down the view when it changes\n // identity — a caller passing an inline arrow would otherwise remount the\n // editor on every render.\n const onChangeRef = React.useRef(onChange)\n React.useEffect(() => {\n onChangeRef.current = onChange\n }, [onChange])\n\n // The view is created once and reconfigured in place. Keying the effect on\n // `extensions` identity looked simpler, but a caller passing an inline array\n // would then rebuild the editor on every render — losing the cursor, the\n // selection and focus mid-keystroke. \"Callers are expected to memoise\" is a\n // trap, not a contract, so the hook does not depend on it.\n const extensionsRef = React.useRef(extensions)\n extensionsRef.current = extensions\n\n const compartment = React.useRef(new Compartment())\n\n React.useEffect(() => {\n if (!host.current) return\n const v = new EditorView({\n state: EditorState.create({\n doc: value,\n extensions: [\n compartment.current.of(extensionsRef.current),\n EditorView.updateListener.of((u) => {\n if (u.docChanged) onChangeRef.current?.(u.state.doc.toString())\n }),\n ],\n }),\n parent: host.current,\n })\n view.current = v\n return () => {\n v.destroy()\n view.current = null\n }\n // Mount once. Configuration changes go through the compartment below.\n }, []) // eslint-disable-line react-hooks/exhaustive-deps\n\n // Swap configuration without touching the document — this is what a\n // Compartment is for, and it is why toggling `readOnly` or a language keeps\n // the caret where it was.\n React.useEffect(() => {\n const v = view.current\n if (!v) return\n v.dispatch({ effects: compartment.current.reconfigure(extensions) })\n }, [extensions])\n\n React.useEffect(() => {\n const v = view.current\n if (!v) return\n const current = v.state.doc.toString()\n if (current === value) return\n v.dispatch({ changes: { from: 0, to: current.length, insert: value } })\n }, [value])\n\n return { ref: host, view }\n}\n","import * as React from \"react\"\nimport { EditorView, lineNumbers } from \"@codemirror/view\"\nimport { EditorState } from \"@codemirror/state\"\nimport {\n StreamLanguage,\n syntaxHighlighting,\n defaultHighlightStyle,\n} from \"@codemirror/language\"\nimport { python } from \"@codemirror/legacy-modes/mode/python\"\nimport { shell } from \"@codemirror/legacy-modes/mode/shell\"\nimport { javascript, json } from \"@codemirror/legacy-modes/mode/javascript\"\nimport { cypher } from \"@codemirror/legacy-modes/mode/cypher\"\nimport { cn } from \"@invana/ui\"\n\nimport { useCodeMirror } from \"./use-code-mirror\"\n\nexport type CodeLanguage =\n | \"python\"\n | \"shell\"\n | \"javascript\"\n | \"json\"\n | \"cypher\"\n | \"plain\"\n\nexport interface CodeBlockProps\n extends Omit<React.HTMLAttributes<HTMLDivElement>, \"onChange\"> {\n value: string\n language?: CodeLanguage\n /** Line numbers are off by default — a five-line snippet does not need a gutter. */\n showLineNumbers?: boolean\n /**\n * Cap the block's height in px and let it scroll inside that, for a document\n * whose length is not the point — a run's `result.json`, an exported model.\n *\n * Unset, the block is as tall as its content, which is right for a snippet\n * and wrong for a record that grows as a run proceeds.\n */\n maxHeight?: number\n}\n\nconst MODES = {\n python: () => StreamLanguage.define(python),\n shell: () => StreamLanguage.define(shell),\n javascript: () => StreamLanguage.define(javascript),\n json: () => StreamLanguage.define(json),\n cypher: () => StreamLanguage.define(cypher),\n plain: () => null,\n}\n\n/**\n * Code, shown as it is — read-only.\n *\n * The DAG in a hand-off, a query in a diagnosis, an exported model. It is not a\n * disabled editor: `EditorState.readOnly` plus a non-editable view means there\n * is no cursor to place and no illusion that typing would do something.\n */\nexport function CodeBlock({\n value,\n language = \"plain\",\n showLineNumbers,\n maxHeight,\n className,\n style,\n ...props\n}: CodeBlockProps) {\n const extensions = React.useMemo(() => {\n const mode = MODES[language]()\n return [\n // StreamLanguage only produces tokens; without a highlight style nothing\n // paints them and the block renders as flat text.\n syntaxHighlighting(defaultHighlightStyle, { fallback: true }),\n EditorView.editable.of(false),\n EditorState.readOnly.of(true),\n EditorView.lineWrapping,\n ...(showLineNumbers ? [lineNumbers()] : []),\n ...(mode ? [mode] : []),\n // The scroller lives inside CodeMirror rather than on the wrapper, so the\n // editor knows its own viewport and does not render every line of a long\n // document into a box that then clips them.\n ...(maxHeight != null\n ? [EditorView.theme({ \"&\": { maxHeight: `${maxHeight}px` } })]\n : []),\n ]\n }, [language, showLineNumbers, maxHeight])\n\n const { ref } = useCodeMirror({ value, extensions })\n\n return (\n <div\n ref={ref}\n className={cn(\n \"overflow-hidden border border-border bg-muted/40 [&_.cm-editor]:bg-transparent\",\n \"[&_.cm-content]:font-mono [&_.cm-content]:text-meta [&_.cm-focused]:outline-none\",\n \"[&_.cm-gutters]:border-none [&_.cm-gutters]:bg-transparent [&_.cm-gutters]:text-muted-foreground\",\n className,\n )}\n style={style}\n {...props}\n />\n )\n}\n","import * as React from \"react\"\nimport { EditorView } from \"@codemirror/view\"\nimport { history, defaultKeymap, historyKeymap } from \"@codemirror/commands\"\nimport { keymap } from \"@codemirror/view\"\nimport { cn } from \"@invana/ui\"\n\nimport { useCodeMirror } from \"./use-code-mirror\"\n\nexport interface MarkdownEditorBlockProps\n extends Omit<React.HTMLAttributes<HTMLDivElement>, \"onChange\" | \"children\"> {\n value: string\n onChange?: (value: string) => void\n readOnly?: boolean\n /** A version marker shown in the corner — `v5`, `v7 · editing`. */\n version?: React.ReactNode\n placeholder?: string\n}\n\n/**\n * The prose a person writes for an agent to read — graph rules, a skill's\n * content, an acceptance note.\n *\n * Plain markdown in a monospace field, not a rich-text editor. What is typed\n * here is offered verbatim in a model's prompt, so what you see has to be\n * exactly what the agent gets; a WYSIWYG layer would put a translation step\n * between the author and the thing that runs.\n *\n * `version` sits in the corner because these documents are versioned and a\n * running thinking finishes on the version it started with — editing without\n * seeing which version you are on is how two people overwrite each other.\n */\nexport function MarkdownEditorBlock({\n value,\n onChange,\n readOnly,\n version,\n className,\n ...props\n}: MarkdownEditorBlockProps) {\n const extensions = React.useMemo(\n () => [\n // No markdown syntax mode, deliberately. What is typed here is offered\n // verbatim in a model's prompt, and highlighting it would be decoration\n // on text whose whole contract is that it is plain. It also keeps this\n // package's dependencies to the CodeMirror set the product already ships.\n EditorView.lineWrapping,\n history(),\n keymap.of([...defaultKeymap, ...historyKeymap]),\n EditorView.editable.of(!readOnly),\n ],\n [readOnly],\n )\n\n const { ref } = useCodeMirror({ value, extensions, onChange })\n\n return (\n <div className={cn(\"relative border border-border bg-muted/40\", className)} {...props}>\n {version != null ? (\n <span className=\"absolute right-1 top-1 z-10 border border-border bg-card px-1 text-meta text-muted-foreground\">\n {version}\n </span>\n ) : null}\n <div\n ref={ref}\n className={cn(\n \"[&_.cm-editor]:bg-transparent [&_.cm-content]:font-mono [&_.cm-content]:text-meta\",\n \"[&_.cm-focused]:outline-none [&_.cm-cursor]:border-foreground\",\n )}\n />\n </div>\n )\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@invana/editor",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.27",
|
|
4
4
|
"description": "Invana Design System - Code and markdown editing surfaces (CodeMirror 6)",
|
|
5
5
|
"homepage": "https://github.com/invana/design-kit#readme",
|
|
6
6
|
"type": "module",
|
|
@@ -40,8 +40,8 @@
|
|
|
40
40
|
"peerDependencies": {
|
|
41
41
|
"react": "^18 || ^19",
|
|
42
42
|
"react-dom": "^18 || ^19",
|
|
43
|
-
"@invana/
|
|
44
|
-
"@invana/
|
|
43
|
+
"@invana/styling": "0.0.27",
|
|
44
|
+
"@invana/ui": "0.0.27"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
47
|
"@codemirror/commands": "^6.10.3",
|
|
@@ -57,8 +57,8 @@
|
|
|
57
57
|
"react-dom": "^19.2.7",
|
|
58
58
|
"tsup": "^8.5.1",
|
|
59
59
|
"typescript": "~5.9.3",
|
|
60
|
-
"@invana/styling": "0.0.
|
|
61
|
-
"@invana/ui": "0.0.
|
|
60
|
+
"@invana/styling": "0.0.27",
|
|
61
|
+
"@invana/ui": "0.0.27"
|
|
62
62
|
},
|
|
63
63
|
"scripts": {
|
|
64
64
|
"build": "tsup",
|