From 7eb7f97d21846fcd269547df384c9a6c869a0d28 Mon Sep 17 00:00:00 2001 From: Romein van Buren Date: Fri, 2 Jun 2023 22:33:43 +0200 Subject: [PATCH] Find view: make changes to single documents from within the object editor --- CHANGELOG.md | 1 + frontend/src/components/grid-items.svelte | 12 ++--- frontend/src/components/objecteditor.svelte | 1 + frontend/src/components/objectviewer.svelte | 30 +++++------ .../connection/collection/find.svelte | 31 ++++++++--- frontend/wailsjs/go/app/App.d.ts | 2 + frontend/wailsjs/go/app/App.js | 4 ++ internal/app/collection_find.go | 51 ++++++++++++++----- 8 files changed, 92 insertions(+), 40 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2af08df..3e84487 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ * Added some external links related to Rolens to the application menu. * Fix an infinite loop bug when a document in the find view has been double clicked. +* Find view: added the ability to make changes to single documents from within the object editor. ## [v0.1.0](https://github.com/garraflavatra/rolens/releases/tag/v0.1.0) diff --git a/frontend/src/components/grid-items.svelte b/frontend/src/components/grid-items.svelte index 1cbe8f2..ba0a64f 100644 --- a/frontend/src/components/grid-items.svelte +++ b/frontend/src/components/grid-items.svelte @@ -57,7 +57,7 @@ } } - function select(itemKey) { + function select(itemKey, index) { if (!canSelect) { return false; } @@ -72,7 +72,7 @@ else { activePath = [ ...path, itemKey ]; } - dispatch('select', { level, itemKey }); + dispatch('select', { level, itemKey, index }); } } @@ -88,9 +88,9 @@ } } - function doubleClick(itemKey) { + function doubleClick(itemKey, index) { // toggleChildren(itemKey, false); - dispatch('trigger', { level, itemKey }); + dispatch('trigger', { level, itemKey, index }); childrenOpen[itemKey] = true; } @@ -132,8 +132,8 @@ {#each _items as item, index} select(item[key])} - on:dblclick={() => doubleClick(item[key])} + on:click={() => select(item[key], index)} + on:dblclick={() => doubleClick(item[key], index)} on:contextmenu|preventDefault={evt => showContextMenu(evt, item)} class:selectable={canSelect} class:selected={canSelect && !activePath[level + 1] && activePath.every(k => path.includes(k) || k === item[key]) && (activePath[level] === item[key])} diff --git a/frontend/src/components/objecteditor.svelte b/frontend/src/components/objecteditor.svelte index c112a10..ecb7359 100644 --- a/frontend/src/components/objecteditor.svelte +++ b/frontend/src/components/objecteditor.svelte @@ -25,6 +25,7 @@ return; } text = e.state.doc.toString(); + dispatch('updated', { text }); }), ], }); diff --git a/frontend/src/components/objectviewer.svelte b/frontend/src/components/objectviewer.svelte index ea7b820..47f054c 100644 --- a/frontend/src/components/objectviewer.svelte +++ b/frontend/src/components/objectviewer.svelte @@ -1,28 +1,20 @@ @@ -195,7 +214,7 @@ data={result.results} hideObjectIndicators={$views[collection.viewKey]?.hideObjectIndicators} bind:activePath - on:trigger={e => openJson(e.detail?.itemKey)} + on:trigger={e => openJson(e.detail?.index)} /> {:else} EJSON.deserialize(r)) : []} bind:activePath - on:trigger={e => openJson(e.detail?.itemKey)} + on:trigger={e => openJson(e.detail?.index)} /> {/if} {/key} @@ -258,7 +277,7 @@ {#if objectViewerData} - + {/if} diff --git a/frontend/wailsjs/go/app/App.d.ts b/frontend/wailsjs/go/app/App.d.ts index 75a479e..487697e 100755 --- a/frontend/wailsjs/go/app/App.d.ts +++ b/frontend/wailsjs/go/app/App.d.ts @@ -67,6 +67,8 @@ export function Startup(arg1:context.Context,arg2:ui.UI):Promise; export function TruncateCollection(arg1:string,arg2:string,arg3:string):Promise; +export function UpdateFoundDocument(arg1:string,arg2:string,arg3:string,arg4:string,arg5:string):Promise; + export function UpdateHost(arg1:string,arg2:string):Promise; export function UpdateItems(arg1:string,arg2:string,arg3:string,arg4:string):Promise; diff --git a/frontend/wailsjs/go/app/App.js b/frontend/wailsjs/go/app/App.js index ba254b8..5552e87 100755 --- a/frontend/wailsjs/go/app/App.js +++ b/frontend/wailsjs/go/app/App.js @@ -122,6 +122,10 @@ export function TruncateCollection(arg1, arg2, arg3) { return window['go']['app']['App']['TruncateCollection'](arg1, arg2, arg3); } +export function UpdateFoundDocument(arg1, arg2, arg3, arg4, arg5) { + return window['go']['app']['App']['UpdateFoundDocument'](arg1, arg2, arg3, arg4, arg5); +} + export function UpdateHost(arg1, arg2) { return window['go']['app']['App']['UpdateHost'](arg1, arg2); } diff --git a/internal/app/collection_find.go b/internal/app/collection_find.go index 316a253..dacfbbf 100644 --- a/internal/app/collection_find.go +++ b/internal/app/collection_find.go @@ -46,24 +46,21 @@ func (a *App) FindItems(hostKey, dbKey, collKey, formJson string) QueryResult { err = bson.UnmarshalExtJSON([]byte(form.Query), true, &query) if err != nil { - runtime.LogInfo(a.ctx, "Invalid find query:") - runtime.LogInfo(a.ctx, err.Error()) + runtime.LogInfof(a.ctx, "Invalid find query: %s", err.Error()) zenity.Error(err.Error(), zenity.Title("Invalid query"), zenity.ErrorIcon) return out } err = json.Unmarshal([]byte(form.Fields), &projection) if err != nil { - runtime.LogInfo(a.ctx, "Invalid find projection:") - runtime.LogInfo(a.ctx, err.Error()) + runtime.LogInfof(a.ctx, "Invalid find projection: %s", err.Error()) zenity.Error(err.Error(), zenity.Title("Invalid projection"), zenity.ErrorIcon) return out } err = json.Unmarshal([]byte(form.Sort), &sort) if err != nil { - runtime.LogInfo(a.ctx, "Invalid find sort:") - runtime.LogInfo(a.ctx, err.Error()) + runtime.LogInfof(a.ctx, "Invalid find sort: %s", err.Error()) zenity.Error(err.Error(), zenity.Title("Invalid sort"), zenity.ErrorIcon) return out } @@ -77,16 +74,14 @@ func (a *App) FindItems(hostKey, dbKey, collKey, formJson string) QueryResult { total, err := client.Database(dbKey).Collection(collKey).CountDocuments(ctx, query, nil) if err != nil { - runtime.LogWarning(a.ctx, "Encountered an error while counting documents:") - runtime.LogWarning(a.ctx, err.Error()) + runtime.LogWarningf(a.ctx, "Encountered an error while counting documents: %s", err.Error()) zenity.Error(err.Error(), zenity.Title("Error while counting docs"), zenity.ErrorIcon) return out } cur, err := client.Database(dbKey).Collection(collKey).Find(ctx, query, &opt) if err != nil { - runtime.LogWarning(a.ctx, "Encountered an error while performing query:") - runtime.LogWarning(a.ctx, err.Error()) + runtime.LogWarningf(a.ctx, "Encountered an error while performing query: %s", err.Error()) zenity.Error(err.Error(), zenity.Title("Error while querying"), zenity.ErrorIcon) return out } @@ -96,14 +91,14 @@ func (a *App) FindItems(hostKey, dbKey, collKey, formJson string) QueryResult { err = cur.All(ctx, &results) if err != nil { - runtime.LogWarning(a.ctx, "Encountered an error while performing query:") - runtime.LogWarning(a.ctx, err.Error()) + runtime.LogWarningf(a.ctx, "Encountered an error while performing query: %s", err.Error()) zenity.Error(err.Error(), zenity.Title("Error while querying"), zenity.ErrorIcon) return out } - out.Results = make([]string, 0) out.Total = total + out.Results = make([]string, 0) + for _, r := range results { marshalled, err := bson.MarshalExtJSON(r, true, true) if err != nil { @@ -117,3 +112,33 @@ func (a *App) FindItems(hostKey, dbKey, collKey, formJson string) QueryResult { return out } + +func (a *App) UpdateFoundDocument(hostKey, dbKey, collKey, idJson, newDocJson string) bool { + var id bson.M + if err := bson.UnmarshalExtJSON([]byte(idJson), true, &id); err != nil { + runtime.LogWarningf(a.ctx, "Could not parse find/update query: %s", err.Error()) + zenity.Error(err.Error(), zenity.Title("Couldn't parse update query"), zenity.ErrorIcon) + return false + } + + var newDoc bson.M + if err := bson.UnmarshalExtJSON([]byte(newDocJson), true, &newDoc); err != nil { + runtime.LogWarningf(a.ctx, "Could not parse new find/update document: %s", err.Error()) + zenity.Error(err.Error(), zenity.Title("Couldn't parse document"), zenity.ErrorIcon) + return false + } + + client, ctx, close, err := a.connectToHost(hostKey) + if err != nil { + return false + } + defer close() + + if _, err := client.Database(dbKey).Collection(collKey).UpdateOne(ctx, id, bson.M{"$set": newDoc}); err != nil { + runtime.LogInfof(a.ctx, "Error while performing find/update: %s", err.Error()) + zenity.Error(err.Error(), zenity.Title("Unable to perform update"), zenity.ErrorIcon) + return false + } + + return true +}