@fieldia/code 0.4.0
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/LICENSE +21 -0
- package/README.md +27 -0
- package/package.json +45 -0
- package/src/index.d.mts +2 -0
- package/src/index.d.ts +2 -0
- package/src/index.js +6 -0
- package/src/index.js.map +1 -0
- package/src/index.mjs +3 -0
- package/src/index.mjs.map +1 -0
- package/src/lib/code.d.mts +4 -0
- package/src/lib/code.d.ts +4 -0
- package/src/lib/code.js +98 -0
- package/src/lib/code.js.map +1 -0
- package/src/lib/code.mjs +94 -0
- package/src/lib/code.mjs.map +1 -0
- package/src/lib/styles.d.mts +3 -0
- package/src/lib/styles.d.ts +3 -0
- package/src/lib/styles.js +25 -0
- package/src/lib/styles.js.map +1 -0
- package/src/lib/styles.mjs +21 -0
- package/src/lib/styles.mjs.map +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 fieldia-dev
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# @fieldia/code
|
|
2
|
+
|
|
3
|
+
A [Fieldia](https://fieldia.dev) JSON field in a code editor: CodeMirror 6,
|
|
4
|
+
bundled with the package, so it works offline and loads only on pages that
|
|
5
|
+
use it.
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @fieldia/code
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { mountViewer } from '@fieldia/viewer';
|
|
13
|
+
import { codeWidgets } from '@fieldia/code';
|
|
14
|
+
|
|
15
|
+
mountViewer(host, { page, dataSource, widgets: codeWidgets });
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
A json node opts in with `"widget": "code"`; others keep the plain JSON box.
|
|
19
|
+
In React pass `widgets={codeWidgets}`, in Vue `:widgets="codeWidgets"`, in
|
|
20
|
+
Angular `[widgets]="codeWidgets"` (spread it beside `gridWidgets` to use both).
|
|
21
|
+
|
|
22
|
+
- JSON highlighting, line numbers, bracket matching, undo.
|
|
23
|
+
- Valid JSON reaches the form as it is typed; text that is not valid is said
|
|
24
|
+
so, and the last good value stays.
|
|
25
|
+
- Tab still moves to the next field.
|
|
26
|
+
|
|
27
|
+
MIT. CodeMirror is MIT too.
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@fieldia/code",
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "Fieldia's JSON fields in a code editor: CodeMirror 6, bundled, so it works offline.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"homepage": "https://fieldia.dev",
|
|
7
|
+
"type": "commonjs",
|
|
8
|
+
"main": "./src/index.js",
|
|
9
|
+
"module": "./src/index.mjs",
|
|
10
|
+
"types": "./src/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": {
|
|
14
|
+
"import": "./src/index.d.mts",
|
|
15
|
+
"require": "./src/index.d.ts"
|
|
16
|
+
},
|
|
17
|
+
"import": "./src/index.mjs",
|
|
18
|
+
"require": "./src/index.js",
|
|
19
|
+
"default": "./src/index.js"
|
|
20
|
+
},
|
|
21
|
+
"./package.json": "./package.json"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@codemirror/commands": "^6.11.1",
|
|
25
|
+
"@codemirror/lang-json": "^6.0.2",
|
|
26
|
+
"@codemirror/language": "^6.12.4",
|
|
27
|
+
"@codemirror/lint": "^6.9.7",
|
|
28
|
+
"@codemirror/state": "^6.7.6",
|
|
29
|
+
"@codemirror/view": "^6.43.13",
|
|
30
|
+
"@fieldia/core": "0.4.0",
|
|
31
|
+
"@fieldia/widgets": "0.4.0",
|
|
32
|
+
"tslib": "^2.8.1"
|
|
33
|
+
},
|
|
34
|
+
"sideEffects": false,
|
|
35
|
+
"repository": {
|
|
36
|
+
"type": "git",
|
|
37
|
+
"url": "https://github.com/fieldia-dev/fieldia",
|
|
38
|
+
"directory": "libs/code"
|
|
39
|
+
},
|
|
40
|
+
"files": [
|
|
41
|
+
"src",
|
|
42
|
+
"README.md",
|
|
43
|
+
"LICENSE"
|
|
44
|
+
]
|
|
45
|
+
}
|
package/src/index.d.mts
ADDED
package/src/index.d.ts
ADDED
package/src/index.js
ADDED
package/src/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../libs/code/src/index.ts"],"names":[],"mappings":";;;AAAA,qDAA2B;AAC3B,uDAA6B"}
|
package/src/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../../../libs/code/src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { type WidgetFactory } from '@fieldia/widgets';
|
|
2
|
+
export declare const codeWidget: WidgetFactory;
|
|
3
|
+
/** Pass these to the viewer (`widgets`) to show json nodes marked `"widget": "code"` in the editor. */
|
|
4
|
+
export declare const codeWidgets: Record<string, WidgetFactory>;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { type WidgetFactory } from '@fieldia/widgets';
|
|
2
|
+
export declare const codeWidget: WidgetFactory;
|
|
3
|
+
/** Pass these to the viewer (`widgets`) to show json nodes marked `"widget": "code"` in the editor. */
|
|
4
|
+
export declare const codeWidgets: Record<string, WidgetFactory>;
|
package/src/lib/code.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.codeWidgets = exports.codeWidget = void 0;
|
|
4
|
+
const commands_1 = require("@codemirror/commands");
|
|
5
|
+
const lang_json_1 = require("@codemirror/lang-json");
|
|
6
|
+
const language_1 = require("@codemirror/language");
|
|
7
|
+
const state_1 = require("@codemirror/state");
|
|
8
|
+
const view_1 = require("@codemirror/view");
|
|
9
|
+
const widgets_1 = require("@fieldia/widgets");
|
|
10
|
+
const styles_1 = require("./styles");
|
|
11
|
+
/**
|
|
12
|
+
* A JSON field in a code editor: CodeMirror 6, bundled with the package, so it
|
|
13
|
+
* works offline and loads only on pages that ask for it (`"widget": "code"` on
|
|
14
|
+
* a json node, with `codeWidgets` passed to the viewer). Valid JSON reaches
|
|
15
|
+
* the form as it is typed; text that is not valid is said so and kept, and the
|
|
16
|
+
* last good value stays. Tab is left to move between fields.
|
|
17
|
+
*/
|
|
18
|
+
const show = (value) => (value === null || value === undefined ? '' : JSON.stringify(value, null, 2));
|
|
19
|
+
/** The text as JSON, `null` for nothing, or `undefined` when it is not valid. */
|
|
20
|
+
function read(text) {
|
|
21
|
+
if (text.trim() === '')
|
|
22
|
+
return null;
|
|
23
|
+
try {
|
|
24
|
+
return JSON.parse(text);
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
const codeWidget = ({ form, name, id, document, labels = widgets_1.WIDGET_LABELS.en }) => {
|
|
31
|
+
(0, styles_1.installCodeStyles)(document);
|
|
32
|
+
const element = document.createElement('div');
|
|
33
|
+
element.className = 'fd-code';
|
|
34
|
+
const error = document.createElement('div');
|
|
35
|
+
error.className = 'fd-error fd-code-error';
|
|
36
|
+
error.setAttribute('role', 'alert');
|
|
37
|
+
error.textContent = labels.invalidJson;
|
|
38
|
+
error.hidden = true;
|
|
39
|
+
const editable = new state_1.Compartment();
|
|
40
|
+
/** What the form holds, as last written or shown: an echo of it is not a change. */
|
|
41
|
+
let known = JSON.stringify(form.getState().values[name] ?? null);
|
|
42
|
+
let writing = false;
|
|
43
|
+
const view = new view_1.EditorView({
|
|
44
|
+
parent: element,
|
|
45
|
+
state: state_1.EditorState.create({
|
|
46
|
+
doc: show(form.getState().values[name]),
|
|
47
|
+
extensions: [
|
|
48
|
+
(0, view_1.lineNumbers)(),
|
|
49
|
+
(0, commands_1.history)(),
|
|
50
|
+
(0, language_1.bracketMatching)(),
|
|
51
|
+
(0, language_1.indentOnInput)(),
|
|
52
|
+
(0, language_1.syntaxHighlighting)(language_1.defaultHighlightStyle, { fallback: true }),
|
|
53
|
+
(0, lang_json_1.json)(),
|
|
54
|
+
view_1.keymap.of([...commands_1.defaultKeymap, ...commands_1.historyKeymap]),
|
|
55
|
+
editable.of([view_1.EditorView.editable.of(true), state_1.EditorState.readOnly.of(false)]),
|
|
56
|
+
// Named by the field's label, like every other field.
|
|
57
|
+
view_1.EditorView.contentAttributes.of({ id, 'aria-labelledby': `${id}-label` }),
|
|
58
|
+
view_1.EditorView.updateListener.of((update) => {
|
|
59
|
+
if (!update.docChanged || writing)
|
|
60
|
+
return;
|
|
61
|
+
const parsed = read(update.state.doc.toString());
|
|
62
|
+
const valid = parsed !== undefined;
|
|
63
|
+
error.hidden = valid;
|
|
64
|
+
view.contentDOM.setAttribute('aria-invalid', String(!valid));
|
|
65
|
+
if (!valid)
|
|
66
|
+
return;
|
|
67
|
+
known = JSON.stringify(parsed);
|
|
68
|
+
form.setValue(name, parsed);
|
|
69
|
+
}),
|
|
70
|
+
],
|
|
71
|
+
}),
|
|
72
|
+
});
|
|
73
|
+
element.append(error);
|
|
74
|
+
return {
|
|
75
|
+
element,
|
|
76
|
+
focus: () => view.focus(),
|
|
77
|
+
destroy: () => view.destroy(),
|
|
78
|
+
update(state) {
|
|
79
|
+
view.dispatch({
|
|
80
|
+
effects: editable.reconfigure([view_1.EditorView.editable.of(!state.readonly), state_1.EditorState.readOnly.of(state.readonly)]),
|
|
81
|
+
});
|
|
82
|
+
// A value from elsewhere (loaded, discarded) replaces the text; the echo of what was typed does not.
|
|
83
|
+
const incoming = JSON.stringify(state.value ?? null);
|
|
84
|
+
if (incoming === known)
|
|
85
|
+
return;
|
|
86
|
+
known = incoming;
|
|
87
|
+
writing = true;
|
|
88
|
+
view.dispatch({ changes: { from: 0, to: view.state.doc.length, insert: show(state.value) } });
|
|
89
|
+
writing = false;
|
|
90
|
+
error.hidden = true;
|
|
91
|
+
view.contentDOM.setAttribute('aria-invalid', 'false');
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
};
|
|
95
|
+
exports.codeWidget = codeWidget;
|
|
96
|
+
/** Pass these to the viewer (`widgets`) to show json nodes marked `"widget": "code"` in the editor. */
|
|
97
|
+
exports.codeWidgets = { 'json.code': exports.codeWidget };
|
|
98
|
+
//# sourceMappingURL=code.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"code.js","sourceRoot":"","sources":["../../../../../libs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E,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,EAAE,CAAC,CAAC;YAC9F,OAAO,GAAG,KAAK,CAAC;YAChB,KAAK,CAAC,MAAM,GAAG,IAAI,CAAC;YACpB,IAAI,CAAC,UAAU,CAAC,YAAY,CAAC,cAAc,EAAE,OAAO,CAAC,CAAC;QACxD,CAAC;KACF,CAAC;AACJ,CAAC,CAAC;AAhEW,QAAA,UAAU,cAgErB;AAEF,uGAAuG;AAC1F,QAAA,WAAW,GAAkC,EAAE,WAAW,EAAE,kBAAU,EAAE,CAAC"}
|
package/src/lib/code.mjs
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { defaultKeymap, history, historyKeymap } from '@codemirror/commands';
|
|
2
|
+
import { json } from '@codemirror/lang-json';
|
|
3
|
+
import { bracketMatching, defaultHighlightStyle, indentOnInput, syntaxHighlighting } from '@codemirror/language';
|
|
4
|
+
import { Compartment, EditorState } from '@codemirror/state';
|
|
5
|
+
import { EditorView, keymap, lineNumbers } from '@codemirror/view';
|
|
6
|
+
import { WIDGET_LABELS } from '@fieldia/widgets';
|
|
7
|
+
import { installCodeStyles } from './styles.mjs';
|
|
8
|
+
/**
|
|
9
|
+
* A JSON field in a code editor: CodeMirror 6, bundled with the package, so it
|
|
10
|
+
* works offline and loads only on pages that ask for it (`"widget": "code"` on
|
|
11
|
+
* a json node, with `codeWidgets` passed to the viewer). Valid JSON reaches
|
|
12
|
+
* the form as it is typed; text that is not valid is said so and kept, and the
|
|
13
|
+
* last good value stays. Tab is left to move between fields.
|
|
14
|
+
*/
|
|
15
|
+
const show = (value) => (value === null || value === undefined ? '' : JSON.stringify(value, null, 2));
|
|
16
|
+
/** The text as JSON, `null` for nothing, or `undefined` when it is not valid. */
|
|
17
|
+
function read(text) {
|
|
18
|
+
if (text.trim() === '')
|
|
19
|
+
return null;
|
|
20
|
+
try {
|
|
21
|
+
return JSON.parse(text);
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export const codeWidget = ({ form, name, id, document, labels = WIDGET_LABELS.en }) => {
|
|
28
|
+
installCodeStyles(document);
|
|
29
|
+
const element = document.createElement('div');
|
|
30
|
+
element.className = 'fd-code';
|
|
31
|
+
const error = document.createElement('div');
|
|
32
|
+
error.className = 'fd-error fd-code-error';
|
|
33
|
+
error.setAttribute('role', 'alert');
|
|
34
|
+
error.textContent = labels.invalidJson;
|
|
35
|
+
error.hidden = true;
|
|
36
|
+
const editable = new Compartment();
|
|
37
|
+
/** What the form holds, as last written or shown: an echo of it is not a change. */
|
|
38
|
+
let known = JSON.stringify(form.getState().values[name] ?? null);
|
|
39
|
+
let writing = false;
|
|
40
|
+
const view = new EditorView({
|
|
41
|
+
parent: element,
|
|
42
|
+
state: EditorState.create({
|
|
43
|
+
doc: show(form.getState().values[name]),
|
|
44
|
+
extensions: [
|
|
45
|
+
lineNumbers(),
|
|
46
|
+
history(),
|
|
47
|
+
bracketMatching(),
|
|
48
|
+
indentOnInput(),
|
|
49
|
+
syntaxHighlighting(defaultHighlightStyle, { fallback: true }),
|
|
50
|
+
json(),
|
|
51
|
+
keymap.of([...defaultKeymap, ...historyKeymap]),
|
|
52
|
+
editable.of([EditorView.editable.of(true), EditorState.readOnly.of(false)]),
|
|
53
|
+
// Named by the field's label, like every other field.
|
|
54
|
+
EditorView.contentAttributes.of({ id, 'aria-labelledby': `${id}-label` }),
|
|
55
|
+
EditorView.updateListener.of((update) => {
|
|
56
|
+
if (!update.docChanged || writing)
|
|
57
|
+
return;
|
|
58
|
+
const parsed = read(update.state.doc.toString());
|
|
59
|
+
const valid = parsed !== undefined;
|
|
60
|
+
error.hidden = valid;
|
|
61
|
+
view.contentDOM.setAttribute('aria-invalid', String(!valid));
|
|
62
|
+
if (!valid)
|
|
63
|
+
return;
|
|
64
|
+
known = JSON.stringify(parsed);
|
|
65
|
+
form.setValue(name, parsed);
|
|
66
|
+
}),
|
|
67
|
+
],
|
|
68
|
+
}),
|
|
69
|
+
});
|
|
70
|
+
element.append(error);
|
|
71
|
+
return {
|
|
72
|
+
element,
|
|
73
|
+
focus: () => view.focus(),
|
|
74
|
+
destroy: () => view.destroy(),
|
|
75
|
+
update(state) {
|
|
76
|
+
view.dispatch({
|
|
77
|
+
effects: editable.reconfigure([EditorView.editable.of(!state.readonly), EditorState.readOnly.of(state.readonly)]),
|
|
78
|
+
});
|
|
79
|
+
// A value from elsewhere (loaded, discarded) replaces the text; the echo of what was typed does not.
|
|
80
|
+
const incoming = JSON.stringify(state.value ?? null);
|
|
81
|
+
if (incoming === known)
|
|
82
|
+
return;
|
|
83
|
+
known = incoming;
|
|
84
|
+
writing = true;
|
|
85
|
+
view.dispatch({ changes: { from: 0, to: view.state.doc.length, insert: show(state.value) } });
|
|
86
|
+
writing = false;
|
|
87
|
+
error.hidden = true;
|
|
88
|
+
view.contentDOM.setAttribute('aria-invalid', 'false');
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
/** Pass these to the viewer (`widgets`) to show json nodes marked `"widget": "code"` in the editor. */
|
|
93
|
+
export const codeWidgets = { 'json.code': codeWidget };
|
|
94
|
+
//# sourceMappingURL=code.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"code.mjs","sourceRoot":"","sources":["../../../../../libs/code/src/lib/code.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAC7E,OAAO,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAC;AAC7C,OAAO,EAAE,eAAe,EAAE,qBAAqB,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AACjH,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAC7D,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAA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AK,CAAC;YAChB,KAAK,CAAC,MAAM,GAAG,IAAI,CAAC;YACpB,IAAI,CAAC,UAAU,CAAC,YAAY,CAAC,cAAc,EAAE,OAAO,CAAC,CAAC;QACxD,CAAC;KACF,CAAC;AACJ,CAAC,CAAC;AAEF,uGAAuG;AACvG,MAAM,CAAC,MAAM,WAAW,GAAkC,EAAE,WAAW,EAAE,UAAU,EAAE,CAAC"}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
/** The editor in Fieldia's colours, so it sits among the other fields. Installed once per document. */
|
|
2
|
+
export declare const CODE_CSS = "\n.fd-code { display: grid; gap: 4px; min-width: 0; }\n.fd-code .cm-editor {\n border: 1px solid var(--fd-border); border-radius: var(--fd-control-radius); background: var(--fd-surface);\n color: var(--fd-text); font-size: 13px; min-height: 96px; max-height: 360px;\n}\n.fd-code .cm-editor.cm-focused { outline: 2px solid var(--fd-focus); outline-offset: 1px; }\n.fd-code .cm-scroller { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; line-height: 1.5; }\n.fd-code .cm-gutters { background: var(--fd-page); color: var(--fd-muted); border-inline-end: 1px solid var(--fd-border); }\n.fd-code .cm-content[aria-invalid=\"true\"] { background: var(--fd-error-soft); }\n";
|
|
3
|
+
export declare function installCodeStyles(doc: Document): void;
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
/** The editor in Fieldia's colours, so it sits among the other fields. Installed once per document. */
|
|
2
|
+
export declare const CODE_CSS = "\n.fd-code { display: grid; gap: 4px; min-width: 0; }\n.fd-code .cm-editor {\n border: 1px solid var(--fd-border); border-radius: var(--fd-control-radius); background: var(--fd-surface);\n color: var(--fd-text); font-size: 13px; min-height: 96px; max-height: 360px;\n}\n.fd-code .cm-editor.cm-focused { outline: 2px solid var(--fd-focus); outline-offset: 1px; }\n.fd-code .cm-scroller { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; line-height: 1.5; }\n.fd-code .cm-gutters { background: var(--fd-page); color: var(--fd-muted); border-inline-end: 1px solid var(--fd-border); }\n.fd-code .cm-content[aria-invalid=\"true\"] { background: var(--fd-error-soft); }\n";
|
|
3
|
+
export declare function installCodeStyles(doc: Document): void;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CODE_CSS = void 0;
|
|
4
|
+
exports.installCodeStyles = installCodeStyles;
|
|
5
|
+
/** The editor in Fieldia's colours, so it sits among the other fields. Installed once per document. */
|
|
6
|
+
exports.CODE_CSS = `
|
|
7
|
+
.fd-code { display: grid; gap: 4px; min-width: 0; }
|
|
8
|
+
.fd-code .cm-editor {
|
|
9
|
+
border: 1px solid var(--fd-border); border-radius: var(--fd-control-radius); background: var(--fd-surface);
|
|
10
|
+
color: var(--fd-text); font-size: 13px; min-height: 96px; max-height: 360px;
|
|
11
|
+
}
|
|
12
|
+
.fd-code .cm-editor.cm-focused { outline: 2px solid var(--fd-focus); outline-offset: 1px; }
|
|
13
|
+
.fd-code .cm-scroller { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; line-height: 1.5; }
|
|
14
|
+
.fd-code .cm-gutters { background: var(--fd-page); color: var(--fd-muted); border-inline-end: 1px solid var(--fd-border); }
|
|
15
|
+
.fd-code .cm-content[aria-invalid="true"] { background: var(--fd-error-soft); }
|
|
16
|
+
`;
|
|
17
|
+
function installCodeStyles(doc) {
|
|
18
|
+
if (doc.getElementById('fieldia-code-styles'))
|
|
19
|
+
return;
|
|
20
|
+
const style = doc.createElement('style');
|
|
21
|
+
style.id = 'fieldia-code-styles';
|
|
22
|
+
style.textContent = exports.CODE_CSS;
|
|
23
|
+
doc.head.append(style);
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=styles.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"styles.js","sourceRoot":"","sources":["../../../../../libs/code/src/lib/styles.ts"],"names":[],"mappings":";;;AAaA,8CAMC;AAnBD,uGAAuG;AAC1F,QAAA,QAAQ,GAAG;;;;;;;;;;CAUvB,CAAC;AAEF,SAAgB,iBAAiB,CAAC,GAAa;IAC7C,IAAI,GAAG,CAAC,cAAc,CAAC,qBAAqB,CAAC;QAAE,OAAO;IACtD,MAAM,KAAK,GAAG,GAAG,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC;IACzC,KAAK,CAAC,EAAE,GAAG,qBAAqB,CAAC;IACjC,KAAK,CAAC,WAAW,GAAG,gBAAQ,CAAC;IAC7B,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AACzB,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** The editor in Fieldia's colours, so it sits among the other fields. Installed once per document. */
|
|
2
|
+
export const CODE_CSS = `
|
|
3
|
+
.fd-code { display: grid; gap: 4px; min-width: 0; }
|
|
4
|
+
.fd-code .cm-editor {
|
|
5
|
+
border: 1px solid var(--fd-border); border-radius: var(--fd-control-radius); background: var(--fd-surface);
|
|
6
|
+
color: var(--fd-text); font-size: 13px; min-height: 96px; max-height: 360px;
|
|
7
|
+
}
|
|
8
|
+
.fd-code .cm-editor.cm-focused { outline: 2px solid var(--fd-focus); outline-offset: 1px; }
|
|
9
|
+
.fd-code .cm-scroller { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; line-height: 1.5; }
|
|
10
|
+
.fd-code .cm-gutters { background: var(--fd-page); color: var(--fd-muted); border-inline-end: 1px solid var(--fd-border); }
|
|
11
|
+
.fd-code .cm-content[aria-invalid="true"] { background: var(--fd-error-soft); }
|
|
12
|
+
`;
|
|
13
|
+
export function installCodeStyles(doc) {
|
|
14
|
+
if (doc.getElementById('fieldia-code-styles'))
|
|
15
|
+
return;
|
|
16
|
+
const style = doc.createElement('style');
|
|
17
|
+
style.id = 'fieldia-code-styles';
|
|
18
|
+
style.textContent = CODE_CSS;
|
|
19
|
+
doc.head.append(style);
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=styles.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"styles.mjs","sourceRoot":"","sources":["../../../../../libs/code/src/lib/styles.ts"],"names":[],"mappings":"AAAA,uGAAuG;AACvG,MAAM,CAAC,MAAM,QAAQ,GAAG;;;;;;;;;;CAUvB,CAAC;AAEF,MAAM,UAAU,iBAAiB,CAAC,GAAa;IAC7C,IAAI,GAAG,CAAC,cAAc,CAAC,qBAAqB,CAAC;QAAE,OAAO;IACtD,MAAM,KAAK,GAAG,GAAG,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC;IACzC,KAAK,CAAC,EAAE,GAAG,qBAAqB,CAAC;IACjC,KAAK,CAAC,WAAW,GAAG,QAAQ,CAAC;IAC7B,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AACzB,CAAC"}
|