From fc0c38ffad18a9ceda44cb8406736c14ba4eb4c2 Mon Sep 17 00:00:00 2001 From: Ruben Fiszel Date: Thu, 23 Jun 2022 02:05:44 +0200 Subject: [PATCH] fix: schemaPicker does not display editor by default --- frontend/src/lib/components/Editor.svelte | 5 + .../src/lib/components/SchemaEditor.svelte | 147 +++++++++--------- 2 files changed, 81 insertions(+), 71 deletions(-) diff --git a/frontend/src/lib/components/Editor.svelte b/frontend/src/lib/components/Editor.svelte index 7f60e858f4..c6f8ea197d 100644 --- a/frontend/src/lib/components/Editor.svelte +++ b/frontend/src/lib/components/Editor.svelte @@ -15,6 +15,10 @@ import tsWorker from 'monaco-editor/esm/vs/language/typescript/ts.worker?worker' import type { DocumentUri, MessageTransports } from 'monaco-languageclient' import * as vscode from 'vscode' + import { createEventDispatcher } from 'svelte' + + const dispatch = createEventDispatcher() + let divEl: HTMLDivElement | null = null let editor: monaco.editor.IStandaloneCodeEditor @@ -281,6 +285,7 @@ editor.onDidChangeModelContent((event) => { code = getCode() + dispatch('change') }) if (lang == 'json') { diff --git a/frontend/src/lib/components/SchemaEditor.svelte b/frontend/src/lib/components/SchemaEditor.svelte index 092c1b142b..9dd5e8dfc7 100644 --- a/frontend/src/lib/components/SchemaEditor.svelte +++ b/frontend/src/lib/components/SchemaEditor.svelte @@ -19,25 +19,19 @@ let oldArgName: string | undefined // when editing argument and changing name let viewJsonSchema = false - let editor: Editor - $: schemaString = JSON.stringify(schema, null, '\t') - - export function getEditor(): Editor { - return editor - } // Binding is not enough because monaco Editor does not support two-way binding export function getSchema(): Schema { if (viewJsonSchema) { try { - schema = JSON.parse(editor.getCode()) + schema = JSON.parse(schemaString) return schema } catch (err) { throw Error(`Error: input is not a valid schema: ${err}`) } } else { try { - editor.setCode(JSON.stringify(schema, null, '\t')) + schemaString = JSON.stringify(schema, null, '\t') return schema } catch (err) { throw Error(`Error: input is not a valid schema: ${err}`) @@ -71,6 +65,8 @@ oldArgName = undefined schemaModal.closeModal() } + schema = schema + schemaString = JSON.stringify(schema, null, '\t') } function startEditArgument(argName: string): void { @@ -93,7 +89,8 @@ try { if (Object.keys(schema.properties).includes(argName)) { delete schema.properties[argName] - schema = schema //needed for reactivity, see https://svelte.dev/tutorial/updating-arrays-and-objects + schema = schema + schemaString = JSON.stringify(schema, null, '\t') } else { throw Error('Argument not found!') } @@ -105,19 +102,13 @@ function switchTab(): void { if (viewJsonSchema) { - let schemaString = editor.getCode() if (schemaString === '') { schemaString = JSON.stringify(emptySchema(), null, 4) } - try { - schema = JSON.parse(schemaString) - viewJsonSchema = false - } catch (err) { - sendUserToast(err, true) - } + viewJsonSchema = false } else { try { - editor.setCode(JSON.stringify(schema, null, '\t')) + schemaString = JSON.stringify(schema, null, '\t') viewJsonSchema = true } catch (err) { sendUserToast(err, true) @@ -159,61 +150,75 @@
-
- {#if schema.properties && Object.keys(schema.properties).length > 0 && schema.required} - - - name - type - description - default - required - - - {#each Object.entries(schema.properties) as [name, property] (name)} - - {name} - {#if !property.type} any {:else} {property.type} {/if} - {property.description} - {JSON.stringify(property.default) ?? ''} - {schema.required.includes(name) ? 'required' : 'optional'} - - + {#if schema.properties && Object.keys(schema.properties).length > 0 && schema.required} + + + name + type + description + default + required + + + {#each Object.entries(schema.properties) as [name, property] (name)} + + {name} + {#if !property.type} any {:else} {property.type} {/if} - - - {/each} - - - {:else} -
This script has no argument
- {/if} -
-
- -
+ {property.description} + {JSON.stringify(property.default) ?? ''} + {schema.required.includes(name) ? 'required' : 'optional'} + + + + + {/each} + + + {:else} +
This script has no argument
+ {/if} +
+ {:else} +
+ { + try { + schema = JSON.parse(schemaString) + } catch (err) { + sendUserToast(err.message, true) + } + }} + bind:code={schemaString} + lang={'json'} + class="small-editor" + /> +
+ {/if}