From 5b5b654c33efdd6b1f97a75f94f45115d6a173e0 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Wed, 26 May 2021 13:20:24 +1000 Subject: [PATCH] add option to clone current config --- ts/deckoptions/ConfigSelector.svelte | 57 ++++++++++++++++++---------- ts/deckoptions/SaveButton.svelte | 3 ++ ts/deckoptions/lib.ts | 15 +++++++- 3 files changed, 55 insertions(+), 20 deletions(-) diff --git a/ts/deckoptions/ConfigSelector.svelte b/ts/deckoptions/ConfigSelector.svelte index 44cb5920f..60e8e81e2 100644 --- a/ts/deckoptions/ConfigSelector.svelte +++ b/ts/deckoptions/ConfigSelector.svelte @@ -40,38 +40,52 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html } } + function onCloneConfig(text: string): void { + const trimmed = text.trim(); + if (trimmed.length) { + state.cloneConfig(trimmed); + } + } + function onRenameConfig(text: string): void { state.setCurrentName(text); } const modals = getContext>(modalsKey); - let addModalKey: string; - let renameModalKey: string; - let oldName = ""; + let modalKey: string; + let modalStartingValue = ""; + let modalTitle = ""; + let modalSuccess = (_text: string) => {}; - function onAdd() { - modals.get(addModalKey)!.show(); + function promptToAdd() { + modalTitle = "Add Config"; + modalSuccess = onAddConfig; + modalStartingValue = ""; + modals.get(modalKey)!.show(); } - function onRename() { - oldName = state.getCurrentName(); - modals.get(renameModalKey)!.show(); + function promptToClone() { + modalTitle = "Clone Config"; + modalSuccess = onCloneConfig; + modalStartingValue = state.getCurrentName(); + modals.get(modalKey)!.show(); + } + + function promptToRename() { + modalTitle = "Rename Config"; + modalSuccess = onRenameConfig; + modalStartingValue = state.getCurrentName(); + modals.get(modalKey)!.show(); } - @@ -95,7 +109,12 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html - + diff --git a/ts/deckoptions/SaveButton.svelte b/ts/deckoptions/SaveButton.svelte index f1c29c327..c2fd0d25b 100644 --- a/ts/deckoptions/SaveButton.svelte +++ b/ts/deckoptions/SaveButton.svelte @@ -57,6 +57,9 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html dispatch("add")}>Add Config + dispatch("clone")} + >Clone Config dispatch("rename")}> Rename Config diff --git a/ts/deckoptions/lib.ts b/ts/deckoptions/lib.ts index 8a9e456af..3f183cb12 100644 --- a/ts/deckoptions/lib.ts +++ b/ts/deckoptions/lib.ts @@ -140,11 +140,24 @@ export class DeckOptionsState { /// Adds a new config, making it current. addConfig(name: string): void { + this.addConfigFrom(name, this.defaults); + } + + /// Clone the current config, making it current. + cloneConfig(name: string): void { + this.addConfigFrom(name, this.configs[this.selectedIdx].config.config!); + } + + /// Clone the current config, making it current. + private addConfigFrom( + name: string, + source: pb.BackendProto.DeckConfig.IConfig + ): void { const uniqueName = this.ensureNewNameUnique(name); const config = pb.BackendProto.DeckConfig.create({ id: 0, name: uniqueName, - config: cloneDeep(this.defaults), + config: cloneDeep(source), }); const configWithCount = { config, useCount: 0 }; this.configs.push(configWithCount);