-
Notifications
You must be signed in to change notification settings - Fork 422
Expand file tree
/
Copy pathExampleView.vue
More file actions
535 lines (495 loc) · 16.9 KB
/
ExampleView.vue
File metadata and controls
535 lines (495 loc) · 16.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
<script setup lang="ts">
import type { JsonFormsChangeEvent } from '@jsonforms/vue';
import type { ErrorObject } from 'ajv';
import cloneDeep from 'lodash/cloneDeep';
import find from 'lodash/find';
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api';
import {
markRaw,
onMounted,
provide,
ref,
shallowReactive,
shallowRef,
watch,
type ShallowRef,
} from 'vue';
import type { ExampleDescription } from '../../../examples/lib';
import { ValidationIcon, defaultStyles, mergeStyles } from '../../src';
import ExampleForm from '../components/ExampleForm.vue';
import MonacoEditor from '../components/MonacoEditor.vue';
import {
configureDataValidation,
configureJsonSchemaValidation,
configureUISchemaValidation,
getMonacoModelForUri,
} from '../core/jsonSchemaValidation';
import type { MonacoApi } from '../core/monaco';
import examples from '../examples';
import { useAppStore } from '../store';
import { createAjv } from '../validate';
import { Pane, Splitpanes } from 'splitpanes';
import 'splitpanes/dist/splitpanes.css';
import { getCustomRenderersForExample } from '../renderers';
const { extendedVuetifyRenderers } = await import('../../src');
const props = defineProps<{
example: ExampleDescription;
}>();
const appStore = useAppStore();
const myStyles = mergeStyles(defaultStyles, {
control: { root: 'my-control' },
});
provide('styles', myStyles);
const ajv = createAjv();
const errors = ref<
ErrorObject<string, Record<string, any>, unknown>[] | undefined
>(undefined);
const snackbar = ref(false);
const snackbarText = ref('');
const snackbarTimeout = ref(3000);
const schemaModel = shallowRef<monaco.editor.ITextModel | undefined>(undefined);
const uischemaModel = shallowRef<monaco.editor.ITextModel | undefined>(
undefined,
);
const dataModel = shallowRef<monaco.editor.ITextModel | undefined>(undefined);
const initialState = (exampleProp: ExampleDescription) => {
const example = cloneDeep(exampleProp);
// Get custom renderers for this example (if any)
const customRenderers = getCustomRenderersForExample(example.name);
const renderers = markRaw([...customRenderers, ...extendedVuetifyRenderers]);
return {
data: example.data,
schema: example.schema,
uischema: example.uischema,
renderers: renderers,
cells: undefined, // not defined
config: appStore.jsonforms.config,
readonly: appStore.jsonforms.readonly,
uischemas: example.uischemas,
validationMode: appStore.jsonforms.validationMode,
ajv: ajv,
i18n: exampleProp.i18n ?? {
locale: appStore.jsonforms.locale,
},
additionalErrors: undefined,
middleware: undefined,
};
};
const state = shallowReactive(initialState(props.example));
const onChange = (event: JsonFormsChangeEvent): void => {
if (props.example.name) {
dataModel.value = getMonacoModelForUri(
monaco.Uri.parse(toDataUri(props.example.name)),
event.data !== undefined ? JSON.stringify(event.data, null, 2) : '',
);
}
errors.value = event.errors;
};
const reloadMonacoSchema = () => {
const example = find(
examples,
(example) => example.name === appStore.exampleName,
);
if (example) {
schemaModel.value = getMonacoModelForUri(
monaco.Uri.parse(toSchemaUri(example.name)),
example.schema ? JSON.stringify(example.schema, null, 2) : '',
);
toast('Original example schema loaded. Apply it to take effect.');
}
};
const saveMonacoSchema = () => {
saveMonacoModel(
schemaModel,
(modelValue) =>
(state.schema = modelValue ? JSON.parse(modelValue) : undefined),
'New schema applied',
);
if (state.schema) {
configureDataValidation(
monaco,
`inmemory://${toSchemaUri(props.example.name)}`,
toDataUri(props.example.name),
cloneDeep(state.schema),
);
}
};
const reloadMonacoUiSchema = () => {
const example = find(
examples,
(example) => example.name === appStore.exampleName,
);
if (example) {
uischemaModel.value = getMonacoModelForUri(
monaco.Uri.parse(toUiSchemaUri(example.name)),
example.uischema ? JSON.stringify(example.uischema, null, 2) : '',
);
toast('Original example UI schema loaded. Apply it to take effect.');
}
};
const saveMonacoUiSchema = () => {
saveMonacoModel(
uischemaModel,
(modelValue) =>
(state.uischema = modelValue ? JSON.parse(modelValue) : undefined),
'New UI schema applied',
);
};
const reloadMonacoData = () => {
const example = find(
examples,
(example) => example.name === appStore.exampleName,
);
if (example) {
dataModel.value = getMonacoModelForUri(
monaco.Uri.parse(toDataUri(example.name)),
example.data !== undefined ? JSON.stringify(example.data, null, 2) : '',
);
toast('Original example data loaded. Apply it to take effect.');
}
};
const saveMonacoData = () => {
saveMonacoModel(
dataModel,
(modelValue) => {
state.data = modelValue === '' ? undefined : JSON.parse(modelValue);
},
'New data applied',
);
};
const saveMonacoModel = (
model: ShallowRef<monaco.editor.ITextModel | undefined>,
apply: (value: string) => void,
successToast: string,
) => {
if (model.value) {
const modelValue = model.value.getValue();
try {
apply(modelValue);
toast(successToast);
} catch (error) {
toast(`Error: ${error}`);
}
}
};
const registerValidations = (editor: MonacoApi) => {
configureJsonSchemaValidation(editor, ['*.schema.json']);
configureUISchemaValidation(editor, ['*.uischema.json']);
for (const example of examples) {
const schema = {
...example.schema,
title: example.label,
};
if (example && example.schema) {
configureDataValidation(
editor,
`inmemory://${toSchemaUri(example.name)}`,
toDataUri(example.name),
schema,
);
}
}
};
const updateMonacoModels = (example: ExampleDescription) => {
schemaModel.value = getMonacoModelForUri(
monaco.Uri.parse(toSchemaUri(example.name)),
example.schema ? JSON.stringify(example.schema, null, 2) : '',
);
uischemaModel.value = getMonacoModelForUri(
monaco.Uri.parse(toUiSchemaUri(example.name)),
example.uischema ? JSON.stringify(example.uischema, null, 2) : '',
);
dataModel.value = getMonacoModelForUri(
monaco.Uri.parse(toDataUri(example.name)),
example.data !== undefined ? JSON.stringify(example.data, null, 2) : '',
);
};
const toSchemaUri = (id: string): string => {
return `${id}.schema.json`;
};
const toUiSchemaUri = (id: string): string => {
return `${id}.uischema.json`;
};
const toDataUri = (id: string): string => {
return `${id}.data.json`;
};
const toI18NUri = (id: string): string => {
return `${id}.i18n.json`;
};
const toast = (message: string): void => {
snackbar.value = true;
snackbarText.value = message;
};
onMounted(() => {
updateMonacoModels(props.example);
});
watch(
() => props.example,
(value) => {
updateMonacoModels(props.example);
// reset state when example changes
Object.assign(state, initialState(value));
},
);
watch(
() => appStore.formOnly,
(value) => {
if (!value) {
// we need to show the wrapper so make sure that the monaco models are updated
updateMonacoModels(props.example);
}
},
);
type Action = NonNullable<ExampleDescription['actions']>[number];
const handleAction = (action: Action) => {
if (action) {
const newState = action.apply(state);
if (newState) {
if (newState.renderers) {
newState.renderers = markRaw(newState.renderers);
}
Object.assign(state, newState);
}
}
};
</script>
<template>
<div>
<v-container fluid class="demo" v-if="!appStore.formOnly">
<v-card>
<v-card-title>{{ example.label }}</v-card-title>
<v-card-text>
<v-tabs v-model="appStore.activeTab">
<v-tab :key="0"
>{{ appStore.layout == 'demo-and-data' ? 'Demo and Data' : 'Demo'
}}<validation-icon
v-if="errors"
:errors="errors"
></validation-icon
></v-tab>
<v-spacer expand />
<v-tab :key="1">Schema</v-tab>
<v-tab :key="2">UI Schema</v-tab>
<v-tab v-if="appStore.layout !== 'demo-and-data'" :key="3"
>Data</v-tab
>
</v-tabs>
</v-card-text>
<v-window v-model="appStore.activeTab">
<v-window-item :key="0">
<v-card>
<v-card-title>
<v-toolbar flat>
<v-toolbar-title>JSONForm</v-toolbar-title>
<v-spacer></v-spacer>
<v-toolbar-items v-if="example.actions">
<v-btn
v-for="(action, index) in example.actions"
:key="index"
@click="() => handleAction(action)"
>{{ action.label }}</v-btn
>
</v-toolbar-items>
</v-toolbar>
</v-card-title>
<v-divider class="mx-4"></v-divider>
<div class="json-forms">
<splitpanes
:class="['default-theme', 'splitpanes-vuetify']"
:rtl="appStore.rtl"
v-if="appStore.layout === 'demo-and-data'"
>
<pane min-size="20">
<v-card>
<v-card-title>
<v-toolbar flat>
<v-toolbar-title>Demo</v-toolbar-title>
<v-spacer></v-spacer>
</v-toolbar>
</v-card-title>
<v-divider class="mx-4"></v-divider>
<example-form :state="state" @jsfchange="onChange" />
</v-card>
</pane>
<pane>
<v-card>
<v-card-title>
<v-toolbar flat>
<v-toolbar-title>Data</v-toolbar-title>
<v-spacer></v-spacer>
<v-tooltip location="bottom">
<template v-slot:activator="{ props }">
<v-btn
icon
@click="reloadMonacoData"
v-bind="props"
>
<v-icon>$reload</v-icon>
</v-btn>
</template>
{{ `Reload Example Data` }}
</v-tooltip>
<v-tooltip location="bottom">
<template v-slot:activator="{ props }">
<v-btn
icon
@click="saveMonacoData"
v-bind="props"
>
<v-icon>$save</v-icon>
</v-btn>
</template>
{{ `Apply Change To Example Data` }}
</v-tooltip>
</v-toolbar>
</v-card-title>
<v-divider class="mx-4"></v-divider>
<monaco-editor
language="json"
v-model="dataModel"
style="height: calc(100vh - 200px)"
:editorBeforeMount="registerValidations"
></monaco-editor>
</v-card>
</pane>
</splitpanes>
<example-form :state="state" @jsfchange="onChange" v-else />
</div>
</v-card>
</v-window-item>
<v-window-item :key="1">
<v-card>
<v-card-title>
<v-toolbar flat>
<v-toolbar-title>Schema</v-toolbar-title>
<v-spacer></v-spacer>
<v-tooltip location="bottom">
<template v-slot:activator="{ props }">
<v-btn icon @click="reloadMonacoSchema" v-bind="props">
<v-icon>$reload</v-icon>
</v-btn>
</template>
{{ `Reload Example Schema` }}
</v-tooltip>
<v-tooltip location="bottom">
<template v-slot:activator="{ props }">
<v-btn icon @click="saveMonacoSchema" v-bind="props">
<v-icon>$save</v-icon>
</v-btn>
</template>
{{ `Apply Change To Example Schema` }}
</v-tooltip>
</v-toolbar>
</v-card-title>
<v-divider class="mx-4"></v-divider>
<monaco-editor
:language="`json`"
v-model="schemaModel"
style="height: calc(100vh - 100px)"
:editorBeforeMount="registerValidations"
></monaco-editor>
</v-card>
</v-window-item>
<v-window-item :key="2">
<v-card>
<v-card-title>
<v-toolbar flat>
<v-toolbar-title>UI Schema</v-toolbar-title>
<v-spacer></v-spacer>
<v-tooltip location="bottom">
<template v-slot:activator="{ props }">
<v-btn icon @click="reloadMonacoUiSchema" v-bind="props">
<v-icon>$reload</v-icon>
</v-btn>
</template>
{{ `Reload Example UI Schema` }}
</v-tooltip>
<v-tooltip location="bottom">
<template v-slot:activator="{ props }">
<v-btn icon @click="saveMonacoUiSchema" v-bind="props">
<v-icon>$save</v-icon>
</v-btn>
</template>
{{ `Apply Change To Example UI Schema` }}
</v-tooltip>
</v-toolbar>
</v-card-title>
<v-divider class="mx-4"></v-divider>
<monaco-editor
language="json"
v-model="uischemaModel"
style="height: calc(100vh - 100px)"
:editorBeforeMount="registerValidations"
></monaco-editor>
</v-card>
</v-window-item>
<v-window-item :key="3">
<v-card>
<v-card-title>
<v-toolbar flat>
<v-toolbar-title>Data</v-toolbar-title>
<v-spacer></v-spacer>
<v-tooltip location="bottom">
<template v-slot:activator="{ props }">
<v-btn icon @click="reloadMonacoData" v-bind="props">
<v-icon>$reload</v-icon>
</v-btn>
</template>
{{ `Reload Example Data` }}
</v-tooltip>
<v-tooltip location="bottom">
<template v-slot:activator="{ props }">
<v-btn icon @click="saveMonacoData" v-bind="props">
<v-icon>$save</v-icon>
</v-btn>
</template>
{{ `Apply Change To Example Data` }}
</v-tooltip>
</v-toolbar>
</v-card-title>
<v-divider class="mx-4"></v-divider>
<monaco-editor
language="json"
v-model="dataModel"
style="height: calc(100vh - 100px)"
:editorBeforeMount="registerValidations"
></monaco-editor>
</v-card>
</v-window-item>
</v-window>
</v-card>
<v-snackbar v-model="snackbar" :timeout="snackbarTimeout">
{{ snackbarText }}
<template v-slot:actions>
<v-btn variant="text" @click="snackbar = false"> Close </v-btn>
</template>
</v-snackbar>
</v-container>
<div class="json-forms" v-else>
<example-form :state="state" @jsfchange="onChange" />
</div>
</div>
</template>
<style lang="scss" scoped>
:deep(.default-theme) {
&.splitpanes--vertical > .splitpanes__splitter,
.splitpanes--vertical > .splitpanes__splitter {
border-left: 1px solid rgb(var(--v-theme-on-surface-variant));
}
&.splitpanes--horizontal > .splitpanes__splitter,
.splitpanes--horizontal > .splitpanes__splitter {
border-top: 1px solid rgb(var(--v-theme-on-surface-variant));
}
.splitpanes__splitter {
background-color: rgb(var(--v-theme-surface));
&:before,
&:after {
background-color: rgb(var(--v-theme-on-surface-variant));
}
&:hover:before,
&:hover:after {
background-color: rgb(var(--v-theme-on-surface-variant));
}
}
}
</style>