1
0
mirror of https://github.com/garraflavatra/rolens.git synced 2025-01-18 21:17:59 +00:00

Use objecteditor throughout the app

This commit is contained in:
Romein van Buren 2023-05-29 20:51:54 +02:00
parent 035d5211d9
commit 890dac30ed
Signed by: romein
GPG Key ID: 0EFF8478ADDF6C49
4 changed files with 106 additions and 36 deletions

View File

@ -5,9 +5,13 @@
import { EditorState } from '@codemirror/state'; import { EditorState } from '@codemirror/state';
import { EditorView, keymap } from '@codemirror/view'; import { EditorView, keymap } from '@codemirror/view';
import { basicSetup } from 'codemirror'; import { basicSetup } from 'codemirror';
import { onMount } from 'svelte'; import { createEventDispatcher, onMount } from 'svelte';
export let text = ''; export let text = '';
export let editor = undefined;
const dispatch = createEventDispatcher();
let editorParent;
const editorState = EditorState.create({ const editorState = EditorState.create({
doc: '', doc: '',
@ -17,17 +21,14 @@
javascript(), javascript(),
EditorState.tabSize.of(4), EditorState.tabSize.of(4),
EditorView.updateListener.of(e => { EditorView.updateListener.of(e => {
// if (!e.docChanged) { if (!e.docChanged) {
// return; return;
// } }
text = e.state.doc.toString(); text = e.state.doc.toString();
}), }),
], ],
}); });
let editorParent;
let editor;
onMount(() => { onMount(() => {
editor = new EditorView({ editor = new EditorView({
parent: editorParent, parent: editorParent,
@ -37,10 +38,12 @@
editor.dispatch({ editor.dispatch({
changes: { changes: {
from: 0, from: 0,
to: editorState.doc.length, to: editor.state.doc.length,
insert: text, insert: text,
}, },
}); });
dispatch('inited', { editor });
}); });
</script> </script>
@ -49,9 +52,11 @@
<style> <style>
.editor { .editor {
width: 100%; width: 100%;
background-color: #fff;
} }
.editor :global(.cm-editor) { .editor :global(.cm-editor) {
overflow: auto; overflow: auto;
height: 100%;
} }
</style> </style>

View File

@ -3,7 +3,7 @@
import Icon from '$components/icon.svelte'; import Icon from '$components/icon.svelte';
import Modal from '$components/modal.svelte'; import Modal from '$components/modal.svelte';
import MongoCollation from '$components/mongo-collation.svelte'; import MongoCollation from '$components/mongo-collation.svelte';
import input from '$lib/actions/input'; import ObjectEditor from '$components/objecteditor.svelte';
import { aggregationStageDocumentationURL, aggregationStages } from '$lib/mongo'; import { aggregationStageDocumentationURL, aggregationStages } from '$lib/mongo';
import { jsonLooseParse, looseJsonIsValid } from '$lib/strings'; import { jsonLooseParse, looseJsonIsValid } from '$lib/strings';
import { Aggregate } from '$wails/go/app/App'; import { Aggregate } from '$wails/go/app/App';
@ -32,7 +32,13 @@
async function run() { async function run() {
const pipeline = stages.map(stage => ({ [stage.type]: jsonLooseParse(stage.data) })); const pipeline = stages.map(stage => ({ [stage.type]: jsonLooseParse(stage.data) }));
await Aggregate(collection.hostKey, collection.dbKey, collection.key, JSON.stringify(pipeline), JSON.stringify(options)); await Aggregate(
collection.hostKey,
collection.dbKey,
collection.key,
JSON.stringify(pipeline),
JSON.stringify(options)
);
} }
onMount(() => { onMount(() => {
@ -57,8 +63,22 @@
<Icon name="info" /> <Icon name="info" />
</button> </button>
</label> </label>
<!-- svelte-ignore a11y-label-has-associated-control -->
<label class="field"> <label class="field">
<textarea bind:value={stage.data} class="code" use:input={{ type: 'json' }}></textarea> <ObjectEditor bind:text={stage.data} on:inited={e => {
e.detail.editor.dispatch({
changes: {
from: 0,
to: e.detail.editor.state.doc.length,
insert: '{\n\t\n}',
},
selection: {
anchor: 3,
},
});
e.detail.editor.focus();
}} />
</label> </label>
</Details> </Details>
{/each} {/each}
@ -102,6 +122,21 @@
grid-template: 1fr auto / 1fr; grid-template: 1fr auto / 1fr;
} }
.aggregate :global(details) {
position: relative;
z-index: 2;
}
.aggregate :global(details + details::before) {
content: "";
position: absolute;
top: -0.6rem;
left: 50%;
width: 1px;
height: 0.6rem;
background-color: #888;
z-index: 1;
}
.settinggrid { .settinggrid {
margin-bottom: 0.5rem; margin-bottom: 0.5rem;
} }
@ -112,8 +147,7 @@
align-items: center; align-items: center;
} }
textarea { .field + .field {
min-height: 100px;
margin-top: 0.5rem; margin-top: 0.5rem;
} }
</style> </style>

View File

@ -3,21 +3,22 @@
import Grid from '$components/grid.svelte'; import Grid from '$components/grid.svelte';
import Icon from '$components/icon.svelte'; import Icon from '$components/icon.svelte';
import ObjectViewer from '$components/objectviewer.svelte'; import ObjectViewer from '$components/objectviewer.svelte';
import input from '$lib/actions/input';
import { randomString } from '$lib/math'; import { randomString } from '$lib/math';
import { inputTypes } from '$lib/mongo'; import { inputTypes } from '$lib/mongo';
import views from '$lib/stores/views'; import views from '$lib/stores/views';
import { capitalise, convertLooseJson, jsonLooseParse } from '$lib/strings'; import { capitalise, convertLooseJson, jsonLooseParse } from '$lib/strings';
import { InsertItems } from '$wails/go/app/App'; import { InsertItems } from '$wails/go/app/App';
import { EJSON } from 'bson'; import { EJSON } from 'bson';
import { createEventDispatcher } from 'svelte'; import { createEventDispatcher, onMount } from 'svelte';
import Form from './components/form.svelte'; import Form from './components/form.svelte';
import ObjectEditor from '$components/objecteditor.svelte';
export let collection; export let collection;
const dispatch = createEventDispatcher(); const dispatch = createEventDispatcher();
const formValidity = {}; const formValidity = {};
let editor;
let json = ''; let json = '';
let newItems = []; let newItems = [];
let insertedIds; let insertedIds;
@ -46,7 +47,12 @@
} }
async function insert() { async function insert() {
insertedIds = await InsertItems(collection.hostKey, collection.dbKey, collection.key, convertLooseJson(json)); insertedIds = await InsertItems(
collection.hostKey,
collection.dbKey,
collection.key,
convertLooseJson(json)
);
if ((collection.viewKey === 'list') && insertedIds) { if ((collection.viewKey === 'list') && insertedIds) {
newItems = []; newItems = [];
} }
@ -90,13 +96,30 @@
newItems.splice(index, 1); newItems.splice(index, 1);
newItems = newItems; newItems = newItems;
} }
onMount(() => {
if (collection.viewKey === 'list') {
editor.dispatch({
changes: {
from: 0,
to: editor.state.doc.length,
insert: '{\n\t\n}',
},
selection: {
anchor: 3,
},
});
editor.focus();
}
});
</script> </script>
<form on:submit|preventDefault={insert}> <form on:submit|preventDefault={insert}>
<div class="items"> <div class="items">
{#if collection.viewKey === 'list'} {#if collection.viewKey === 'list'}
<!-- svelte-ignore a11y-label-has-associated-control -->
<label class="field json"> <label class="field json">
<textarea placeholder="[]" class="code" bind:value={json} use:input={{ type: 'json', autofocus: true }}></textarea> <ObjectEditor bind:text={json} bind:editor />
</label> </label>
{:else if viewType === 'form'} {:else if viewType === 'form'}
<div class="form"> <div class="form">
@ -205,7 +228,4 @@
.field.json { .field.json {
height: 100%; height: 100%;
} }
.field.json textarea {
resize: none;
}
</style> </style>

View File

@ -1,32 +1,47 @@
<script> <script>
import Icon from '$components/icon.svelte'; import Icon from '$components/icon.svelte';
import input from '$lib/actions/input'; import ObjectEditor from '$components/objecteditor.svelte';
import { convertLooseJson } from '$lib/strings';
import { RemoveItems } from '$wails/go/app/App'; import { RemoveItems } from '$wails/go/app/App';
import { onMount } from 'svelte';
export let collection; export let collection;
let json = ''; let json = '';
let many = true; let many = true;
let result = undefined; let result = undefined;
let editor;
$: code = `db.${collection.key}.remove(${json});`; $: code = `db.${collection.key}.remove(${json});`;
async function removeItems() { async function removeItems() {
result = await RemoveItems(collection.hostKey, collection.dbKey, collection.key, json, many); result = await RemoveItems(
collection.hostKey,
collection.dbKey,
collection.key,
convertLooseJson(json),
many
);
} }
onMount(() => {
editor.dispatch({
changes: {
from: 0,
to: editor.state.doc.length,
insert: '{\n\t\n}',
},
selection: {
anchor: 3,
},
});
editor.focus();
});
</script> </script>
<form on:submit|preventDefault={removeItems}> <form on:submit|preventDefault={removeItems}>
<!-- <CodeExample {code} /> --> <!-- svelte-ignore a11y-label-has-associated-control -->
<label class="field"> <label class="field">
<textarea <ObjectEditor bind:text={json} bind:editor />
cols="30"
rows="10"
placeholder={'{}'}
class="code"
bind:value={json}
use:input={{ type: 'json', autofocus: true }}
></textarea>
</label> </label>
<div class="actions"> <div class="actions">
@ -59,8 +74,4 @@
.many { .many {
display: inline-flex; display: inline-flex;
} }
textarea {
resize: none;
}
</style> </style>