@vmz/plugin-monaco 0.3.5 → 0.3.7

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 (3) hide show
  1. package/package.json +23 -8
  2. package/runtime.ts +0 -280
  3. package/vmz.plugin.ts +0 -46
package/package.json CHANGED
@@ -1,18 +1,30 @@
1
1
  {
2
2
  "dependencies": {
3
- "@vmz/plugin": "0.3.5"
3
+ "@vmz/plugin": "0.3.7"
4
4
  },
5
5
  "description": "VMZ Monaco editor adapter - <Monaco>",
6
+ "devDependencies": {
7
+ "@types/node": "^22.15.0",
8
+ "typescript": "^5.8.3"
9
+ },
10
+ "engines": {
11
+ "node": ">=24.12.0"
12
+ },
6
13
  "exports": {
7
14
  ".": {
8
- "default": "./vmz.plugin.ts",
9
- "types": "./vmz.plugin.ts"
15
+ "default": "./dist/vmz.plugin.js",
16
+ "types": "./dist/vmz.plugin.d.ts"
10
17
  },
11
18
  "./runtime": {
12
- "default": "./runtime.ts",
13
- "types": "./runtime.ts"
19
+ "default": "./dist/runtime.js",
20
+ "types": "./dist/runtime.d.ts"
14
21
  }
15
22
  },
23
+ "files": [
24
+ "dist",
25
+ "components",
26
+ "README.md"
27
+ ],
16
28
  "keywords": [
17
29
  "vmz",
18
30
  "plugin",
@@ -20,7 +32,7 @@
20
32
  "editor"
21
33
  ],
22
34
  "license": "MIT",
23
- "main": "./vmz.plugin.ts",
35
+ "main": "./dist/vmz.plugin.js",
24
36
  "name": "@vmz/plugin-monaco",
25
37
  "peerDependencies": {
26
38
  "monaco-editor": ">=0.52"
@@ -33,7 +45,10 @@
33
45
  "type": "git",
34
46
  "url": "git+https://github.com/doki-land/vmz-framework.git"
35
47
  },
48
+ "scripts": {
49
+ "build": "tsc -p tsconfig.json"
50
+ },
36
51
  "type": "module",
37
- "types": "./vmz.plugin.ts",
38
- "version": "0.3.5"
52
+ "types": "./dist/vmz.plugin.d.ts",
53
+ "version": "0.3.7"
39
54
  }
package/runtime.ts DELETED
@@ -1,280 +0,0 @@
1
- /**
2
- * Monaco mount helper (browser). Peer: monaco-editor.
3
- *
4
- * Dynamic-imports monaco so Node/SSR route collection never evaluates the
5
- * browser bundle (`window is not defined`).
6
- *
7
- * Embedded VMZ hosts desync selection overlays unless we pin fonts, disable
8
- * EditContext, remasure fonts, and drive layout via ResizeObserver.
9
- */
10
-
11
- export type JsonSchemaRegistration = {
12
- uri: string;
13
- fileMatch?: string[];
14
- schema: object;
15
- };
16
-
17
- export type MountMonacoOptions = {
18
- value?: string;
19
- language?: string;
20
- theme?: string;
21
- readOnly?: boolean;
22
- onChange?: (v: string) => void;
23
- jsonSchemaUrl?: string;
24
- jsonSchemas?: JsonSchemaRegistration[];
25
- };
26
-
27
- type MonacoEnv = {
28
- getWorkerUrl?: (moduleId: string, label: string) => string;
29
- getWorker?: (moduleId: string, label: string) => Worker;
30
- };
31
-
32
- let mountSeq = 0;
33
-
34
- function ensureMonacoEnvironment(): void {
35
- const g = globalThis as typeof globalThis & { MonacoEnvironment?: MonacoEnv };
36
- if (g.MonacoEnvironment?.getWorker || g.MonacoEnvironment?.getWorkerUrl) return;
37
- g.MonacoEnvironment = {
38
- getWorkerUrl(_moduleId: string, label: string) {
39
- if (label === 'json') return '/vendor/monaco-json.worker.js';
40
- return '/vendor/monaco-editor.worker.js';
41
- },
42
- };
43
- }
44
-
45
- function ensureMonacoCss(): void {
46
- if (typeof document === 'undefined') return;
47
- if (document.querySelector('link[data-vmz-monaco-css]')) return;
48
- const link = document.createElement('link');
49
- link.rel = 'stylesheet';
50
- link.href = '/vendor/plugin-monaco-runtime.css';
51
- link.setAttribute('data-vmz-monaco-css', '');
52
- document.head.appendChild(link);
53
- }
54
-
55
- async function loadJsonSchema(url: string): Promise<JsonSchemaRegistration | null> {
56
- try {
57
- const res = await fetch(url);
58
- if (!res.ok) return null;
59
- const schema = (await res.json()) as object;
60
- const id = typeof (schema as { $id?: unknown }).$id === 'string' ? String((schema as { $id: string }).$id) : url;
61
- return {
62
- uri: id,
63
- fileMatch: ['*'],
64
- schema,
65
- };
66
- } catch {
67
- return null;
68
- }
69
- }
70
-
71
- function disposeHostEditor(el: HTMLElement): void {
72
- const prior = (el as HTMLElement & { __vmzMonaco?: { dispose: () => void } }).__vmzMonaco;
73
- if (!prior) return;
74
- try {
75
- prior.dispose();
76
- } catch {
77
- /* ignore */
78
- }
79
- delete (el as HTMLElement & { __vmzMonaco?: unknown }).__vmzMonaco;
80
- el.replaceChildren();
81
- }
82
-
83
- function deadApi() {
84
- return {
85
- editor: null as unknown as never,
86
- monaco: null as unknown as never,
87
- getValue: () => '',
88
- setValue: (_v: string) => {},
89
- dispose: () => {},
90
- };
91
- }
92
-
93
- export async function mountMonaco(el: HTMLElement, opts: MountMonacoOptions = {}) {
94
- if (typeof window === 'undefined' || typeof document === 'undefined') {
95
- return deadApi();
96
- }
97
-
98
- const seq = ++mountSeq;
99
- ensureMonacoEnvironment();
100
- ensureMonacoCss();
101
-
102
- // Prefer vendor-injected monaco (single ESM chunk); otherwise dynamic-import.
103
- const injected = (globalThis as { __VMZ_MONACO__?: unknown }).__VMZ_MONACO__;
104
- let MonacoNS: unknown = injected;
105
- if (!MonacoNS) {
106
- MonacoNS = await import('monaco-editor');
107
- await import('monaco-editor/language/json/monaco.contribution.js');
108
- }
109
- const monaco = (MonacoNS as { languages?: unknown; default?: unknown }).languages
110
- ? MonacoNS
111
- : ((MonacoNS as { default: typeof MonacoNS }).default ?? MonacoNS);
112
-
113
- const language = opts.language ?? 'typescript';
114
- disposeHostEditor(el);
115
-
116
- if (language === 'json') {
117
- const jsonDefaults =
118
- (monaco as { json?: { jsonDefaults?: { setDiagnosticsOptions: (o: unknown) => void } } }).json?.jsonDefaults ??
119
- (
120
- monaco as {
121
- languages?: {
122
- json?: { jsonDefaults?: { setDiagnosticsOptions: (o: unknown) => void } };
123
- };
124
- }
125
- ).languages?.json?.jsonDefaults;
126
- if (jsonDefaults) {
127
- const schemas: JsonSchemaRegistration[] = [...(opts.jsonSchemas ?? [])];
128
- if (opts.jsonSchemaUrl) {
129
- const loaded = await loadJsonSchema(opts.jsonSchemaUrl);
130
- if (loaded) schemas.push(loaded);
131
- }
132
- if (seq !== mountSeq) return deadApi();
133
- if (schemas.length) {
134
- jsonDefaults.setDiagnosticsOptions({
135
- validate: true,
136
- allowComments: true,
137
- schemas: schemas.map((s) => ({
138
- uri: s.uri,
139
- fileMatch: s.fileMatch ?? ['*'],
140
- schema: s.schema,
141
- })),
142
- });
143
- }
144
- }
145
- }
146
-
147
- if (seq !== mountSeq) return deadApi();
148
-
149
- const uri = monaco.Uri.parse(`inmemory://vmz-monaco/${seq}/${language}.json`);
150
- const model = monaco.editor.createModel(opts.value ?? '', language, uri);
151
-
152
- const editor = monaco.editor.create(el, {
153
- model,
154
- theme: opts.theme ?? 'vs',
155
- readOnly: !!opts.readOnly,
156
- automaticLayout: false,
157
- minimap: { enabled: false },
158
- scrollBeyondLastLine: false,
159
- tabSize: 2,
160
- editContext: false,
161
- fontFamily: 'Consolas, "Courier New", Menlo, Monaco, monospace',
162
- fontSize: 14,
163
- lineHeight: 21,
164
- letterSpacing: 0,
165
- fontLigatures: false,
166
- fixedOverflowWidgets: true,
167
- renderLineHighlight: 'none',
168
- renderValidationDecorations: 'on',
169
- occurrencesHighlight: 'off',
170
- selectionHighlight: false,
171
- matchBrackets: 'near',
172
- guides: { indentation: true, highlightActiveIndentation: false },
173
- padding: { top: 4, bottom: 4 },
174
- scrollbar: {
175
- verticalScrollbarSize: 10,
176
- horizontalScrollbarSize: 10,
177
- useShadows: false,
178
- },
179
- });
180
-
181
- const layout = () => {
182
- if (seq !== mountSeq) return;
183
- try {
184
- const rect = el.getBoundingClientRect();
185
- editor.layout({
186
- width: Math.max(0, Math.floor(rect.width)),
187
- height: Math.max(0, Math.floor(rect.height)),
188
- });
189
- } catch {
190
- /* ignore */
191
- }
192
- };
193
-
194
- try {
195
- monaco.editor.remeasureFonts();
196
- } catch {
197
- /* ignore */
198
- }
199
- layout();
200
- requestAnimationFrame(() => {
201
- try {
202
- monaco.editor.remeasureFonts();
203
- } catch {
204
- /* ignore */
205
- }
206
- layout();
207
- });
208
- if (document.fonts?.ready) {
209
- void document.fonts.ready.then(() => {
210
- if (seq !== mountSeq) return;
211
- try {
212
- monaco.editor.remeasureFonts();
213
- } catch {
214
- /* ignore */
215
- }
216
- layout();
217
- });
218
- }
219
-
220
- let ro: ResizeObserver | null = null;
221
- if (typeof ResizeObserver !== 'undefined') {
222
- ro = new ResizeObserver(() => layout());
223
- ro.observe(el);
224
- }
225
-
226
- if (typeof opts.onChange === 'function') {
227
- editor.onDidChangeModelContent(() => {
228
- opts.onChange?.(editor.getValue());
229
- });
230
- }
231
-
232
- const api = {
233
- editor,
234
- monaco,
235
- getValue: () => editor.getValue(),
236
- setValue: (v: string) => {
237
- const next = v ?? '';
238
- if (editor.getValue() === next) return;
239
- // Full replace must NOT restore the previous selection — stale ranges
240
- // paint ghost overlays (yellow bars / black bands) after content swaps.
241
- editor.pushUndoStop();
242
- editor.executeEdits(
243
- 'vmz-setValue',
244
- [
245
- {
246
- range: model.getFullModelRange(),
247
- text: next,
248
- forceMoveMarkers: true,
249
- },
250
- ],
251
- [new monaco.Selection(1, 1, 1, 1)],
252
- );
253
- editor.pushUndoStop();
254
- editor.revealPositionInCenterIfOutsideViewport({
255
- lineNumber: 1,
256
- column: 1,
257
- });
258
- },
259
- dispose: () => {
260
- try {
261
- ro?.disconnect();
262
- } catch {
263
- /* ignore */
264
- }
265
- ro = null;
266
- try {
267
- editor.dispose();
268
- } catch {
269
- /* ignore */
270
- }
271
- try {
272
- model.dispose();
273
- } catch {
274
- /* ignore */
275
- }
276
- },
277
- };
278
- (el as HTMLElement & { __vmzMonaco?: typeof api }).__vmzMonaco = api;
279
- return api;
280
- }
package/vmz.plugin.ts DELETED
@@ -1,46 +0,0 @@
1
- import { definePlugin, loadPluginSource } from '@vmz/plugin';
2
-
3
- const source = loadPluginSource(import.meta.url, 'components/Monaco.vmz');
4
-
5
- export default definePlugin({
6
- name: '@vmz/plugin-monaco',
7
- version: '0.1.0',
8
- protocol: '0.1.0',
9
- stages: ['workspace_resolve', 'analyzer'],
10
- deterministic: true,
11
- async contribute(ctx) {
12
- if (ctx.stage === 'workspace_resolve') {
13
- return {
14
- stage: 'workspace_resolve',
15
- cacheKey: `@vmz/plugin-monaco:Monaco.vmz:${source.contentHash.slice(0, 12)}`,
16
- items: [
17
- {
18
- id: 'component-monaco',
19
- kind: 'source',
20
- path: 'src/components/Monaco.vmz',
21
- content: source.content,
22
- contentHash: source.contentHash,
23
- materialize: true,
24
- },
25
- ],
26
- };
27
- }
28
- if (ctx.stage === 'analyzer') {
29
- return {
30
- stage: 'analyzer',
31
- cacheKey: '@vmz/plugin-monaco:analyzer',
32
- items: [
33
- {
34
- id: 'component-monaco-online',
35
- kind: 'analyzer',
36
- path: 'src/components/Monaco.vmz',
37
- severity: 'advice',
38
- message: 'Monaco component online',
39
- code: 'vmz.plugin.monaco',
40
- },
41
- ],
42
- };
43
- }
44
- return { stage: ctx.stage, items: [] };
45
- },
46
- });