@pngwasi/config-editor 0.0.0-stage → 0.1.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 +1985 -2
- package/binding.d.ts +216 -0
- package/binding.js +634 -0
- package/index.d.ts +41 -0
- package/index.js +14 -0
- package/load.d.ts +18 -0
- package/load.js +57 -0
- package/package.json +70 -4
- package/pkg/config_editor.d.ts +2500 -0
- package/pkg/config_editor.js +3281 -0
- package/pkg/config_editor_bg.wasm +0 -0
- package/pkg/config_editor_bg.wasm.d.ts +110 -0
- package/preset.css +2899 -0
- package/setting-types.js +24 -0
- package/tools.d.ts +23 -0
- package/tools.js +19 -0
package/binding.js
ADDED
|
@@ -0,0 +1,634 @@
|
|
|
1
|
+
// `@pngwasi/config-editor/binding`: what a framework component does with an
|
|
2
|
+
// editor, written once. The React, Vue and Svelte packages are thin wrappers
|
|
3
|
+
// around these bindings; a package for another framework builds on them too.
|
|
4
|
+
//
|
|
5
|
+
// A binding is fed the component's props (`attach` once its element exists,
|
|
6
|
+
// `update` after every change, `detach` on unmount) and decides what each
|
|
7
|
+
// change means: settings and most props apply live (the caret, undo history
|
|
8
|
+
// and folds stay), the few the editor is built from re-mount it (the
|
|
9
|
+
// document, its baseline, the caret and the echoes still on their way
|
|
10
|
+
// carried over), the bound value goes to the editor's `update({ value })`
|
|
11
|
+
// with `echoes` on (the editor tells an echo, late or out of order, from the
|
|
12
|
+
// parent's own value, so none rolls the document back), a mount that failed on a bad prop is retried when a prop
|
|
13
|
+
// changes, and the WebAssembly loads once for every editor on the page.
|
|
14
|
+
|
|
15
|
+
import { ConfigDocuments, ConfigEditor, unifiedDiff } from "./pkg/config_editor.js";
|
|
16
|
+
import { loadConfigEditor, whenLoaded } from "./load.js";
|
|
17
|
+
import { SETTING_TYPES } from "./setting-types.js";
|
|
18
|
+
|
|
19
|
+
export { SETTING_TYPES };
|
|
20
|
+
|
|
21
|
+
/** The settings among `props`: the keys of `EditorSettings` it sets, as plain data. */
|
|
22
|
+
function settingsOf(props) {
|
|
23
|
+
const settings = {};
|
|
24
|
+
for (const key of Object.keys(SETTING_TYPES)) {
|
|
25
|
+
if (props[key] !== undefined) settings[key] = props[key];
|
|
26
|
+
}
|
|
27
|
+
return JSON.stringify(settings);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* What `update` takes to go from the settings `before` to `after` (both
|
|
32
|
+
* from `settingsOf`): the keys that changed, `null` for one no longer set,
|
|
33
|
+
* `strings` text by text and `colors` token by token (`update` merges
|
|
34
|
+
* them).
|
|
35
|
+
*/
|
|
36
|
+
function settingsChange(before, after) {
|
|
37
|
+
const change = {};
|
|
38
|
+
const same = (left, right) => JSON.stringify(left) === JSON.stringify(right);
|
|
39
|
+
for (const key of Object.keys(SETTING_TYPES)) {
|
|
40
|
+
if (same(before[key], after[key])) continue;
|
|
41
|
+
if ((key === "strings" || key === "colors") && after[key]) {
|
|
42
|
+
const texts = {};
|
|
43
|
+
for (const text of new Set([...Object.keys(before[key] ?? {}), ...Object.keys(after[key])])) {
|
|
44
|
+
texts[text] = after[key][text] ?? null;
|
|
45
|
+
}
|
|
46
|
+
change[key] = texts;
|
|
47
|
+
} else {
|
|
48
|
+
change[key] = after[key] ?? null;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return change;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** What the editor and documents bindings share: mounting, retries, errors. */
|
|
55
|
+
class Binding {
|
|
56
|
+
#name;
|
|
57
|
+
#generation = 0;
|
|
58
|
+
#mountKey = null;
|
|
59
|
+
#retryKey = null;
|
|
60
|
+
#settings = null;
|
|
61
|
+
#onLoad = null;
|
|
62
|
+
|
|
63
|
+
constructor(name, props) {
|
|
64
|
+
this.#name = name;
|
|
65
|
+
/** The props as last given. */
|
|
66
|
+
this.props = props ?? {};
|
|
67
|
+
/** The element the component renders, while attached. */
|
|
68
|
+
this.element = null;
|
|
69
|
+
/** The `@pngwasi/config-editor` instance, while mounted. */
|
|
70
|
+
this.instance = null;
|
|
71
|
+
/** Whether the last mount threw (a bad prop): a changed prop retries it. */
|
|
72
|
+
this.failed = false;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** The component's element exists: load the module and mount into it. */
|
|
76
|
+
attach(element, props = this.props) {
|
|
77
|
+
this.element = element;
|
|
78
|
+
this.props = props;
|
|
79
|
+
this.#mount();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The props changed (call after every change; unchanged ones cost nothing). */
|
|
83
|
+
update(props) {
|
|
84
|
+
this.props = props;
|
|
85
|
+
if (!this.element) return;
|
|
86
|
+
if (this.mountKey() !== this.#mountKey) return this.#mount();
|
|
87
|
+
const settings = settingsOf(props);
|
|
88
|
+
if (this.#settings !== null && settings !== this.#settings) {
|
|
89
|
+
const before = JSON.parse(this.#settings);
|
|
90
|
+
this.#settings = settings;
|
|
91
|
+
if (this.failed) return this.#mount();
|
|
92
|
+
this.guard((instance) => instance.update(settingsChange(before, JSON.parse(settings))));
|
|
93
|
+
}
|
|
94
|
+
const retryKey = this.retryKey();
|
|
95
|
+
if (retryKey !== this.#retryKey) {
|
|
96
|
+
this.#retryKey = retryKey;
|
|
97
|
+
if (this.failed) return this.#mount();
|
|
98
|
+
}
|
|
99
|
+
this.sync(false);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** The component unmounts: the instance is destroyed and freed. */
|
|
103
|
+
detach() {
|
|
104
|
+
this.#generation += 1;
|
|
105
|
+
this.#cancelRetry();
|
|
106
|
+
this.unmount();
|
|
107
|
+
this.element = null;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Reports an error to `onError`, else to the console. */
|
|
111
|
+
report(error) {
|
|
112
|
+
if (!this.props.onError) {
|
|
113
|
+
console.error(`${this.#name}:`, error);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
// A handler that throws is the host's bug: it is logged, and the
|
|
117
|
+
// editor stays (in every framework alike).
|
|
118
|
+
try {
|
|
119
|
+
this.props.onError(error);
|
|
120
|
+
} catch (thrown) {
|
|
121
|
+
console.error(`${this.#name}: onError threw`, thrown, "reporting", error);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Runs `apply` on the instance, if mounted, reporting what it throws. */
|
|
126
|
+
guard(apply) {
|
|
127
|
+
const instance = this.instance;
|
|
128
|
+
if (!instance) return;
|
|
129
|
+
try {
|
|
130
|
+
apply(instance);
|
|
131
|
+
} catch (error) {
|
|
132
|
+
this.report(error);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Runs `query` on the instance; `fallback` before it mounts or when it throws. */
|
|
137
|
+
ask(query, fallback) {
|
|
138
|
+
const instance = this.instance;
|
|
139
|
+
if (!instance) return fallback;
|
|
140
|
+
try {
|
|
141
|
+
return query(instance);
|
|
142
|
+
} catch (error) {
|
|
143
|
+
this.report(error);
|
|
144
|
+
return fallback;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
#cancelRetry() {
|
|
149
|
+
this.#onLoad?.();
|
|
150
|
+
this.#onLoad = null;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
#mount() {
|
|
154
|
+
this.unmount();
|
|
155
|
+
this.#cancelRetry();
|
|
156
|
+
const generation = ++this.#generation;
|
|
157
|
+
this.#mountKey = this.mountKey();
|
|
158
|
+
this.#retryKey = this.retryKey();
|
|
159
|
+
loadConfigEditor().then(
|
|
160
|
+
() => {
|
|
161
|
+
if (generation !== this.#generation || !this.element) return;
|
|
162
|
+
const settings = settingsOf(this.props);
|
|
163
|
+
this.#settings = settings;
|
|
164
|
+
try {
|
|
165
|
+
this.instance = this.create(this.element, JSON.parse(settings));
|
|
166
|
+
this.failed = false;
|
|
167
|
+
} catch (error) {
|
|
168
|
+
this.instance = null;
|
|
169
|
+
this.failed = true;
|
|
170
|
+
this.report(error);
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
this.mounted();
|
|
174
|
+
},
|
|
175
|
+
(error) => {
|
|
176
|
+
if (generation !== this.#generation) return;
|
|
177
|
+
// When this load fails, the app's own `loadConfigEditor(url)`
|
|
178
|
+
// succeeding later mounts the editor.
|
|
179
|
+
this.#onLoad = whenLoaded(() => {
|
|
180
|
+
this.#cancelRetry();
|
|
181
|
+
if (generation === this.#generation && this.element) this.#mount();
|
|
182
|
+
});
|
|
183
|
+
this.report(error);
|
|
184
|
+
},
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** The props an `EditorBinding` reads (React-shaped; each wrapper maps its own). */
|
|
190
|
+
export class EditorBinding extends Binding {
|
|
191
|
+
// The `value` prop as last handed to the editor: an unchanged prop is not
|
|
192
|
+
// a new value from the parent.
|
|
193
|
+
#handledValue = undefined;
|
|
194
|
+
#handledRevision = undefined;
|
|
195
|
+
// What a re-mount (language, schema, … changed) starts from, so an
|
|
196
|
+
// uncontrolled document and its diff baseline survive it.
|
|
197
|
+
#carried = null;
|
|
198
|
+
// Where the caret was when a re-mount took the editor away while it had
|
|
199
|
+
// focus: the new one takes focus and the caret back, so typing goes on.
|
|
200
|
+
#carriedFocus = null;
|
|
201
|
+
// The channel of the editor a re-mount took away (`editor.channel`): the
|
|
202
|
+
// new one takes it over, so the echoes still on their way to the parent
|
|
203
|
+
// are told apart there too and none rolls the typing back.
|
|
204
|
+
#channel = null;
|
|
205
|
+
// A `setValue` (the handle's) made while there is no editor (before the first mount, or
|
|
206
|
+
// while a re-mount has failed): applied and reported on mount.
|
|
207
|
+
#reset = null;
|
|
208
|
+
// Each live prop as last applied.
|
|
209
|
+
#applied = {};
|
|
210
|
+
// The bound value each positional prop (`readOnlyRanges`,
|
|
211
|
+
// `hostDiagnostics`) was given with, when its list last changed: its
|
|
212
|
+
// places are in that text, sent along as `rangesFor`, on a re-mount too.
|
|
213
|
+
#givenFor = {};
|
|
214
|
+
#followPath = (path) => this.props.onPathChange?.(path);
|
|
215
|
+
#save = (value, language) => this.props.onSave?.(value, language);
|
|
216
|
+
|
|
217
|
+
constructor(name, props) {
|
|
218
|
+
super(name, props);
|
|
219
|
+
/** The component's imperative API (React's ref, Vue's exposed instance, Svelte's exports). */
|
|
220
|
+
this.handle = this.#handle();
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
mountKey() {
|
|
224
|
+
const { language, schema, fileName, editorClassName, secrets } = this.props;
|
|
225
|
+
// The secrets options that re-mount (the check function does not).
|
|
226
|
+
const patterns = secrets
|
|
227
|
+
? [secrets.placeholder, secrets.hint, secrets.referencePrefixes, !!secrets.isPlaceholder]
|
|
228
|
+
: null;
|
|
229
|
+
return JSON.stringify([language ?? null, schema ?? null, fileName ?? null, editorClassName ?? null, patterns]);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
retryKey() {
|
|
233
|
+
return JSON.stringify(this.props.editAs ?? null);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
create(element, settings) {
|
|
237
|
+
const props = this.props;
|
|
238
|
+
// A re-mount starts from the newest document: the editor's own, since a
|
|
239
|
+
// late host's `value` may not have caught up. Values still pending stay
|
|
240
|
+
// pending, so their late echoes are recognized.
|
|
241
|
+
const value = this.#reset ?? this.#carried?.value ?? props.value ?? props.defaultValue ?? "";
|
|
242
|
+
const baseline = props.baseline ?? this.#carried?.baseline ?? value;
|
|
243
|
+
this.#carried = { value, baseline };
|
|
244
|
+
// Ranges are in the text they were given with (the bound value when
|
|
245
|
+
// their list last changed, or now for a new list): on a document
|
|
246
|
+
// carried past it, they are moved from it to the document, never read
|
|
247
|
+
// again in a text they were not given for.
|
|
248
|
+
const givenFor = (name) =>
|
|
249
|
+
this.#applied[name] === JSON.stringify(props[name] ?? []) && name in this.#givenFor
|
|
250
|
+
? this.#givenFor[name]
|
|
251
|
+
: props.value;
|
|
252
|
+
// The ranges the old editor was given last, rendered again: the channel
|
|
253
|
+
// taken over holds them where they are, so they are not given (a value
|
|
254
|
+
// they were rendered with since may be an echo of the editor's own
|
|
255
|
+
// text, where their numbers name other places).
|
|
256
|
+
const held = this.#channel !== null && this.#applied.readOnlyRanges === JSON.stringify(props.readOnlyRanges ?? []);
|
|
257
|
+
const textFor = held ? value : givenFor("readOnlyRanges");
|
|
258
|
+
const rangesFor = textFor === undefined || textFor === value ? undefined : textFor;
|
|
259
|
+
// Diagnostics given with another text come with the first update.
|
|
260
|
+
const diagnostics = givenFor("hostDiagnostics") === textFor ? props.hostDiagnostics : undefined;
|
|
261
|
+
return new ConfigEditor(element, {
|
|
262
|
+
...settings,
|
|
263
|
+
value,
|
|
264
|
+
// The parent binds the value two ways: every `onChange` text comes
|
|
265
|
+
// back as the value, maybe late and in any order.
|
|
266
|
+
echoes: true,
|
|
267
|
+
channel: this.#channel ?? undefined,
|
|
268
|
+
language: props.language,
|
|
269
|
+
editAs: props.editAs ?? undefined,
|
|
270
|
+
schema: props.schema,
|
|
271
|
+
baseline,
|
|
272
|
+
fileName: props.fileName,
|
|
273
|
+
readOnly: props.readOnly,
|
|
274
|
+
readOnlyReason: props.readOnlyReason ?? undefined,
|
|
275
|
+
className: props.editorClassName,
|
|
276
|
+
onChange: (text, revision) => {
|
|
277
|
+
if (this.#carried) this.#carried.value = text;
|
|
278
|
+
this.props.onChange?.(text, revision);
|
|
279
|
+
},
|
|
280
|
+
onEditAsChange: (language) => this.props.onEditAsChange?.(language),
|
|
281
|
+
onSyncChange: (inSync) => this.props.onSyncChange?.(inSync),
|
|
282
|
+
onChangeSummary: (summary) => this.props.onChangeSummary?.(summary),
|
|
283
|
+
onProblems: (problems) => this.props.onProblems?.(problems),
|
|
284
|
+
onPathChange: props.onPathChange ? this.#followPath : undefined,
|
|
285
|
+
hostDiagnostics: diagnostics,
|
|
286
|
+
readOnlyPaths: props.readOnlyPaths,
|
|
287
|
+
readOnlyRanges: held ? undefined : (props.readOnlyRanges ?? []),
|
|
288
|
+
rangesFor,
|
|
289
|
+
secrets: props.secrets && {
|
|
290
|
+
...props.secrets,
|
|
291
|
+
isPlaceholder: props.secrets.isPlaceholder
|
|
292
|
+
? (token) => this.props.secrets?.isPlaceholder?.(token) ?? false
|
|
293
|
+
: undefined,
|
|
294
|
+
},
|
|
295
|
+
onSave: props.onSave ? this.#save : undefined,
|
|
296
|
+
onDirtyChange: (dirty) => this.props.onDirtyChange?.(dirty),
|
|
297
|
+
onBaselineConflict: (event) => this.props.onBaselineConflict?.(event),
|
|
298
|
+
onSchemaWarning: (message) => {
|
|
299
|
+
const warn = this.props.onSchemaWarning;
|
|
300
|
+
if (warn) warn(message);
|
|
301
|
+
else console.warn(`config-editor: ${message}`);
|
|
302
|
+
},
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
mounted() {
|
|
307
|
+
// The new editor took the channel over (a constructor that threw left
|
|
308
|
+
// it for the next try).
|
|
309
|
+
this.#channel = null;
|
|
310
|
+
const focus = this.#carriedFocus;
|
|
311
|
+
this.#carriedFocus = null;
|
|
312
|
+
const textarea = this.element.querySelector("textarea");
|
|
313
|
+
if (focus && textarea) {
|
|
314
|
+
textarea.focus();
|
|
315
|
+
textarea.setSelectionRange(focus.start, focus.end);
|
|
316
|
+
textarea.scrollTop = focus.scrollTop;
|
|
317
|
+
}
|
|
318
|
+
// A reset made before there was an editor reaches the host now.
|
|
319
|
+
if (this.#reset !== null) {
|
|
320
|
+
const value = this.#reset;
|
|
321
|
+
this.#reset = null;
|
|
322
|
+
// The `value` prop still holds what the host had before the reset
|
|
323
|
+
// reaches it: not a new value of its own.
|
|
324
|
+
this.#handledValue = this.props.value;
|
|
325
|
+
this.props.onChange?.(value);
|
|
326
|
+
}
|
|
327
|
+
this.sync(true);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
unmount() {
|
|
331
|
+
const editor = this.instance;
|
|
332
|
+
if (!editor) return;
|
|
333
|
+
const textarea = this.element?.querySelector("textarea");
|
|
334
|
+
if (textarea && document.activeElement === textarea) {
|
|
335
|
+
this.#carriedFocus = {
|
|
336
|
+
start: textarea.selectionStart,
|
|
337
|
+
end: textarea.selectionEnd,
|
|
338
|
+
scrollTop: textarea.scrollTop,
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
this.instance = null;
|
|
342
|
+
// destroy() first: it sends an edit still waiting to convert.
|
|
343
|
+
editor.destroy();
|
|
344
|
+
if (this.#carried) this.#carried.value = editor.value;
|
|
345
|
+
this.#channel?.free();
|
|
346
|
+
this.#channel = editor.channel;
|
|
347
|
+
editor.free();
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
detach() {
|
|
351
|
+
super.detach();
|
|
352
|
+
this.#channel?.free();
|
|
353
|
+
this.#channel = null;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/** Applies the live props that changed (all of them right after a mount). */
|
|
357
|
+
sync(force) {
|
|
358
|
+
const props = this.props;
|
|
359
|
+
const changed = (name, key) => {
|
|
360
|
+
if (!force && this.#applied[name] === key) return false;
|
|
361
|
+
this.#applied[name] = key;
|
|
362
|
+
return true;
|
|
363
|
+
};
|
|
364
|
+
const value = props.value;
|
|
365
|
+
// The editor tells the parent's echoes from its own values (`echoes`).
|
|
366
|
+
const handled = value === this.#handledValue && props.valueRevision === this.#handledRevision;
|
|
367
|
+
// A new value goes with the read-only ranges the parent renders with
|
|
368
|
+
// it when their list changed: worked out for that text, they are read
|
|
369
|
+
// in it. The same list rendered again stays in the text it was given
|
|
370
|
+
// with and is carried into the new value: that value may be an echo of
|
|
371
|
+
// an editor's own text (one this editor never sent, after the parent
|
|
372
|
+
// hid and showed it), where the same numbers name other places.
|
|
373
|
+
let rangesWithValue = false;
|
|
374
|
+
if (this.instance && value !== undefined && !handled) {
|
|
375
|
+
this.#handledValue = value;
|
|
376
|
+
this.#handledRevision = props.valueRevision;
|
|
377
|
+
const ranges = JSON.stringify(props.readOnlyRanges ?? []);
|
|
378
|
+
rangesWithValue = this.#applied.readOnlyRanges !== ranges;
|
|
379
|
+
// The revision the parent stored with it, when it passes it back:
|
|
380
|
+
// its echo is then matched exactly.
|
|
381
|
+
const revision = props.valueRevision;
|
|
382
|
+
const update = revision === undefined ? { value } : { value, valueRevision: revision };
|
|
383
|
+
if (rangesWithValue) {
|
|
384
|
+
this.#applied.readOnlyRanges = ranges;
|
|
385
|
+
this.#givenFor.readOnlyRanges = value;
|
|
386
|
+
}
|
|
387
|
+
this.guard((editor) => {
|
|
388
|
+
if (!rangesWithValue) return editor.update(update);
|
|
389
|
+
try {
|
|
390
|
+
editor.update({ ...update, readOnlyRanges: JSON.parse(ranges) });
|
|
391
|
+
} catch (error) {
|
|
392
|
+
// Ranges that fit neither this text nor the one the last ones
|
|
393
|
+
// came with are reported once (rendered again, they are no new
|
|
394
|
+
// list); the value still applies, the editor keeping the locks it
|
|
395
|
+
// had, carried into it.
|
|
396
|
+
this.report(error);
|
|
397
|
+
editor.update(update);
|
|
398
|
+
}
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
// The live props that changed, each in its own `update`: a bad one
|
|
402
|
+
// (reported) does not hold the others back.
|
|
403
|
+
const update = {};
|
|
404
|
+
if (changed("baseline", props.baseline) && props.baseline !== undefined) {
|
|
405
|
+
if (this.#carried) this.#carried.baseline = props.baseline;
|
|
406
|
+
update.baseline = props.baseline;
|
|
407
|
+
}
|
|
408
|
+
if (changed("editAs", props.editAs ?? null)) update.editAs = props.editAs ?? null;
|
|
409
|
+
if (changed("readOnly", props.readOnly ?? false)) update.readOnly = props.readOnly ?? false;
|
|
410
|
+
if (changed("readOnlyReason", props.readOnlyReason ?? null)) update.readOnlyReason = props.readOnlyReason ?? null;
|
|
411
|
+
for (const name of ["hostDiagnostics", "readOnlyPaths", "readOnlyRanges"]) {
|
|
412
|
+
if (name === "readOnlyRanges" && rangesWithValue) continue;
|
|
413
|
+
const list = JSON.stringify(props[name] ?? []);
|
|
414
|
+
// A new list is given with the bound value now.
|
|
415
|
+
if (this.#applied[name] !== list) this.#givenFor[name] = value;
|
|
416
|
+
if (changed(name, list)) update[name] = JSON.parse(list);
|
|
417
|
+
}
|
|
418
|
+
// Right after a mount the editor has the ranges: given to it, or held
|
|
419
|
+
// by the channel it took over (given again, they would be read anew).
|
|
420
|
+
if (force) delete update.readOnlyRanges;
|
|
421
|
+
// Ranges are in the bound value they were given with, which may be an
|
|
422
|
+
// echo the user typed past: the editor maps them from it to the
|
|
423
|
+
// document (also when a re-mount sends them again).
|
|
424
|
+
const ranged = (key) => key === "hostDiagnostics" || key === "readOnlyRanges";
|
|
425
|
+
const rangesFor = (key) => {
|
|
426
|
+
const text = this.#givenFor[key] ?? value;
|
|
427
|
+
return ranged(key) && text !== undefined ? { rangesFor: text } : {};
|
|
428
|
+
};
|
|
429
|
+
const followsPath = props.onPathChange !== undefined;
|
|
430
|
+
if (changed("followsPath", followsPath)) update.onPathChange = followsPath ? this.#followPath : null;
|
|
431
|
+
// `Ctrl+S` is the editor's only while `onSave` is set.
|
|
432
|
+
const saves = props.onSave !== undefined;
|
|
433
|
+
if (changed("saves", saves)) update.onSave = saves ? this.#save : null;
|
|
434
|
+
for (const [key, list] of Object.entries(update)) {
|
|
435
|
+
this.guard((editor) => editor.update({ [key]: list, ...rangesFor(key) }));
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/** The document while there is no editor: a pending reset, the carried one, the props'. */
|
|
440
|
+
#document() {
|
|
441
|
+
return this.#reset ?? this.#carried?.value ?? this.props.value ?? this.props.defaultValue ?? "";
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
#setValue(value) {
|
|
445
|
+
if (typeof value !== "string") {
|
|
446
|
+
this.report(new TypeError("setValue needs a string"));
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
449
|
+
if (this.#carried) this.#carried.value = value;
|
|
450
|
+
if (!this.instance) {
|
|
451
|
+
this.#reset = value;
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
this.guard((editor) => {
|
|
455
|
+
// A reset to the document as it is changes nothing, and is not reported.
|
|
456
|
+
if (!editor.replaceValue(value)) return;
|
|
457
|
+
// The host's state follows the reset: it is reported like an edit,
|
|
458
|
+
// and the editor tells its echo apart like one.
|
|
459
|
+
this.props.onChange?.(value);
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
#unifiedDiff(context) {
|
|
464
|
+
if (this.instance) return this.instance.unifiedDiff(context);
|
|
465
|
+
// While a re-mount has failed: the document and baseline kept for it
|
|
466
|
+
// (no diff while the module has not loaded).
|
|
467
|
+
const value = this.#document();
|
|
468
|
+
try {
|
|
469
|
+
return unifiedDiff(this.#carried?.baseline ?? this.props.baseline ?? value, value, context);
|
|
470
|
+
} catch (error) {
|
|
471
|
+
this.report(error);
|
|
472
|
+
return "";
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
#handle() {
|
|
477
|
+
const binding = this;
|
|
478
|
+
return {
|
|
479
|
+
get value() {
|
|
480
|
+
// While a re-mount has failed, the document kept for it.
|
|
481
|
+
return binding.instance?.value ?? binding.#document();
|
|
482
|
+
},
|
|
483
|
+
get editor() {
|
|
484
|
+
return binding.instance;
|
|
485
|
+
},
|
|
486
|
+
get currentPath() {
|
|
487
|
+
return binding.ask((editor) => editor.currentPath, null);
|
|
488
|
+
},
|
|
489
|
+
get text() {
|
|
490
|
+
return binding.ask((editor) => editor.text, "");
|
|
491
|
+
},
|
|
492
|
+
get foldedLines() {
|
|
493
|
+
return binding.ask((editor) => editor.foldedLines, []);
|
|
494
|
+
},
|
|
495
|
+
get isDirty() {
|
|
496
|
+
return binding.ask((editor) => editor.isDirty, false);
|
|
497
|
+
},
|
|
498
|
+
rangeOf: (path) => binding.ask((editor) => editor.rangeOf(path), null),
|
|
499
|
+
pathAt: (offset) => binding.ask((editor) => editor.pathAt(offset), null),
|
|
500
|
+
reveal: (path) => binding.ask((editor) => editor.reveal(path), false),
|
|
501
|
+
revealRange: (range) => binding.ask((editor) => editor.revealRange(range), false),
|
|
502
|
+
fold: (path) => binding.ask((editor) => editor.fold(path), false),
|
|
503
|
+
unfold: (path) => binding.ask((editor) => editor.unfold(path), false),
|
|
504
|
+
foldAll: () => binding.guard((editor) => editor.foldAll()),
|
|
505
|
+
unfoldAll: () => binding.guard((editor) => editor.unfoldAll()),
|
|
506
|
+
focus: () => binding.guard((editor) => editor.focus()),
|
|
507
|
+
save: () => binding.ask((editor) => editor.save(), false),
|
|
508
|
+
editAs: (language) =>
|
|
509
|
+
binding.ask((editor) => {
|
|
510
|
+
editor.update({ editAs: language });
|
|
511
|
+
return true;
|
|
512
|
+
}, false),
|
|
513
|
+
setValue: (value) => binding.#setValue(value),
|
|
514
|
+
unifiedDiff: (context) => binding.#unifiedDiff(context),
|
|
515
|
+
};
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
/** A `ConfigDocuments` view driven by a component's props. The view tells
|
|
520
|
+
* an echo of an edit in the list (the component lists every one back) from
|
|
521
|
+
* a value of the host's own (`echoes: true`). */
|
|
522
|
+
export class DocumentsBinding extends Binding {
|
|
523
|
+
#applied = {};
|
|
524
|
+
#save = (id, value, language) => this.props.onSave?.(id, value, language);
|
|
525
|
+
|
|
526
|
+
constructor(name, props) {
|
|
527
|
+
super(name, props);
|
|
528
|
+
/** The component's imperative API. */
|
|
529
|
+
this.handle = this.#handle();
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
mountKey() {
|
|
533
|
+
const { label, viewClassName, onClose, onAdd } = this.props;
|
|
534
|
+
return JSON.stringify([label ?? null, viewClassName ?? null, onClose !== undefined, onAdd !== undefined]);
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
retryKey() {
|
|
538
|
+
return null;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
create(element, settings) {
|
|
542
|
+
const props = this.props;
|
|
543
|
+
return new ConfigDocuments(element, {
|
|
544
|
+
...settings,
|
|
545
|
+
documents: props.documents ?? [],
|
|
546
|
+
activeId: props.activeId,
|
|
547
|
+
className: props.viewClassName,
|
|
548
|
+
label: props.label,
|
|
549
|
+
// The parent lists every `onChange` text back, possibly late.
|
|
550
|
+
echoes: true,
|
|
551
|
+
onSelect: (id) => this.props.onSelect?.(id),
|
|
552
|
+
onClose: props.onClose ? (request) => this.props.onClose?.(request) : undefined,
|
|
553
|
+
onAdd: props.onAdd ? () => this.props.onAdd?.() : undefined,
|
|
554
|
+
onChange: (id, value) => this.props.onChange?.(id, value),
|
|
555
|
+
onSave: this.#save,
|
|
556
|
+
onDirtyChange: (id, dirty) => this.props.onDirtyChange?.(id, dirty),
|
|
557
|
+
onProblems: (id, problems) => this.props.onProblems?.(id, problems),
|
|
558
|
+
onEditAsChange: (id, language) => this.props.onEditAsChange?.(id, language),
|
|
559
|
+
onSyncChange: (id, inSync) => this.props.onSyncChange?.(id, inSync),
|
|
560
|
+
});
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
mounted() {
|
|
564
|
+
this.sync(true);
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
unmount() {
|
|
568
|
+
const view = this.instance;
|
|
569
|
+
if (!view) return;
|
|
570
|
+
this.instance = null;
|
|
571
|
+
view.destroy();
|
|
572
|
+
view.free();
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
sync(force) {
|
|
576
|
+
const props = this.props;
|
|
577
|
+
const changed = (name, key) => {
|
|
578
|
+
if (!force && this.#applied[name] === key) return false;
|
|
579
|
+
this.#applied[name] = key;
|
|
580
|
+
return true;
|
|
581
|
+
};
|
|
582
|
+
const update = {};
|
|
583
|
+
// `Ctrl+S` is the view's only while `onSave` is set.
|
|
584
|
+
const saves = props.onSave !== undefined;
|
|
585
|
+
if (changed("saves", saves)) update.onSave = saves ? this.#save : null;
|
|
586
|
+
// The list goes to the view on each render, which reads again only what
|
|
587
|
+
// changed in it (a text or schema given again is not even copied): no
|
|
588
|
+
// key of it here, which would serialize every document on every key.
|
|
589
|
+
const documents = props.documents ?? [];
|
|
590
|
+
this.guard((view) => view.update({ documents }));
|
|
591
|
+
if ("onSave" in update) this.guard((view) => view.update({ onSave: update.onSave }));
|
|
592
|
+
// A new `activeId` shows that document (the mount took the first one).
|
|
593
|
+
const activeId = props.activeId ?? null;
|
|
594
|
+
if (changed("activeId", activeId) && !force && activeId !== null) {
|
|
595
|
+
this.guard((view) => view.update({ activeId }));
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
#handle() {
|
|
600
|
+
const binding = this;
|
|
601
|
+
return {
|
|
602
|
+
get instance() {
|
|
603
|
+
return binding.instance;
|
|
604
|
+
},
|
|
605
|
+
get activeId() {
|
|
606
|
+
return binding.ask((view) => view.activeId ?? null, null);
|
|
607
|
+
},
|
|
608
|
+
select: (id) => binding.ask((view) => view.select(id), false),
|
|
609
|
+
isDirty: (id) => binding.ask((view) => view.isDirty(id), false),
|
|
610
|
+
save: (id) => binding.ask((view) => view.save(id), false),
|
|
611
|
+
saveAll: () => binding.ask((view) => view.saveAll(), []),
|
|
612
|
+
editAs: (id, language) => binding.ask((view) => view.editAs(id, language), false),
|
|
613
|
+
focus: () => binding.guard((view) => view.focus()),
|
|
614
|
+
editor: (id) => binding.ask((view) => view.editor(id) ?? null, null),
|
|
615
|
+
setValue: (id, value) => binding.#setValue(id, value),
|
|
616
|
+
};
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
/** Replaces a document's text, always, and reports it through `onChange`. */
|
|
620
|
+
#setValue(id, value) {
|
|
621
|
+
if (typeof id !== "string" || typeof value !== "string") {
|
|
622
|
+
this.report(new TypeError("setValue needs an id and a string"));
|
|
623
|
+
return false;
|
|
624
|
+
}
|
|
625
|
+
return this.ask((view) => {
|
|
626
|
+
// A reset to the text the document holds changes nothing, and is not
|
|
627
|
+
// reported: the parent's list holds it once.
|
|
628
|
+
if (!view.replaceValue(id, value)) return false;
|
|
629
|
+
// The parent's list follows, as after an edit.
|
|
630
|
+
this.props.onChange?.(id, value);
|
|
631
|
+
return true;
|
|
632
|
+
}, false);
|
|
633
|
+
}
|
|
634
|
+
}
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export {
|
|
2
|
+
ConfigDocuments,
|
|
3
|
+
ConfigEditor,
|
|
4
|
+
languageForFile,
|
|
5
|
+
languages,
|
|
6
|
+
registerLanguage,
|
|
7
|
+
themes,
|
|
8
|
+
type BaselineConflict,
|
|
9
|
+
type ChangeSummary,
|
|
10
|
+
type CloseRequest,
|
|
11
|
+
type CustomLanguage,
|
|
12
|
+
type CustomLanguageContext,
|
|
13
|
+
type Diagnostic,
|
|
14
|
+
type DocumentSpec,
|
|
15
|
+
type DocumentsOptions,
|
|
16
|
+
type DocumentsUpdate,
|
|
17
|
+
type EditorOptions,
|
|
18
|
+
type EditorPath,
|
|
19
|
+
type EditorRange,
|
|
20
|
+
type EditorSettings,
|
|
21
|
+
type EditorStrings,
|
|
22
|
+
type EditorUpdate,
|
|
23
|
+
type EditorView,
|
|
24
|
+
type FieldPath,
|
|
25
|
+
type HostChannel,
|
|
26
|
+
type HostDiagnostic,
|
|
27
|
+
type InitInput,
|
|
28
|
+
type InitOutput,
|
|
29
|
+
type LanguageInfo,
|
|
30
|
+
type LanguageName,
|
|
31
|
+
type LineRange,
|
|
32
|
+
type SecretOptions,
|
|
33
|
+
type SettingsUpdate,
|
|
34
|
+
type Severity,
|
|
35
|
+
type TextRangeInput,
|
|
36
|
+
type ThemeColorMap,
|
|
37
|
+
type ThemeColorToken,
|
|
38
|
+
type ThemeInfo,
|
|
39
|
+
type ThemeName,
|
|
40
|
+
} from "./pkg/config_editor.js";
|
|
41
|
+
export { loadConfigEditor } from "./load.js";
|
package/index.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// The package entry, what an app uses: loading the WebAssembly module, the
|
|
2
|
+
// editor, the documents view and the language and theme catalogs. Text
|
|
3
|
+
// without an editor is `@pngwasi/config-editor/tools`; what a framework
|
|
4
|
+
// package builds on is `@pngwasi/config-editor/binding`. An explicit list:
|
|
5
|
+
// the generated module also holds wasm-bindgen's internals.
|
|
6
|
+
export {
|
|
7
|
+
ConfigDocuments,
|
|
8
|
+
ConfigEditor,
|
|
9
|
+
languageForFile,
|
|
10
|
+
languages,
|
|
11
|
+
registerLanguage,
|
|
12
|
+
themes,
|
|
13
|
+
} from "./pkg/config_editor.js";
|
|
14
|
+
export { loadConfigEditor } from "./load.js";
|