From 96ae3f44a17089e0edb224779dadd9ff81d614fc Mon Sep 17 00:00:00 2001 From: Matei Adriel Date: Thu, 25 Jul 2019 17:40:28 +0300 Subject: [PATCH] delete simulation --- .../core/components/OpenSimulation.tsx | 68 +++++++++++-------- .../internalisation/translations/english.ts | 6 +- .../translations/nederlands.ts | 12 ++-- .../internalisation/translations/romanian.ts | 7 +- .../types/TranslationInterface.ts | 1 + src/modules/saving/stores/saveStore.ts | 10 ++- src/modules/simulation-actions/constants.ts | 11 ++- .../helpers/deleteSimulation.ts | 24 +++++++ .../types/possibleAction.ts | 1 + .../classes/SimulationRenderer.ts | 6 +- src/modules/storage/classes/LocalStore.ts | 8 +++ 11 files changed, 112 insertions(+), 42 deletions(-) create mode 100644 src/modules/simulation-actions/helpers/deleteSimulation.ts diff --git a/src/modules/core/components/OpenSimulation.tsx b/src/modules/core/components/OpenSimulation.tsx index 6cbe577..82fca30 100644 --- a/src/modules/core/components/OpenSimulation.tsx +++ b/src/modules/core/components/OpenSimulation.tsx @@ -13,6 +13,8 @@ import { switchTo } from '../../saving/helpers/switchTo' import { SimulationError } from '../../errors/classes/SimulationError' import { icons } from '../constants' import { useTranslation } from '../../internalisation/helpers/useLanguage' +import { getTemplateSafely } from '../../logic-gates/helpers/getTemplateSafely' +import { getRendererSafely } from '../../logic-gates/helpers/getRendererSafely' /** * Returns a list with the names of all saved simulations @@ -74,36 +76,44 @@ const OpenSimulation = () => { open={Boolean(anchorEl)} onClose={handleClose} > - {simulations.map((simulationName, index) => { - const simulationData = saveStore.get(simulationName) - - if (!simulationData) { - throw new SimulationError( - `Cannot get data for simulation ${simulationName}` - ) - } - - return ( - { - switchTo(simulationName) - handleClose() - }} - > - - - { - icons.simulationMode[ - simulationData.simulation.mode - ] - } - - - {simulationName} - + {simulations + .filter( + name => + simulations.length < 2 || + name !== getRendererSafely().simulation.name ) - })} + .map((simulationName, index) => { + const simulationData = saveStore.get(simulationName) + + if (!simulationData) { + throw new SimulationError( + `Cannot get data for simulation ${simulationName}` + ) + } + + return ( + { + switchTo(simulationName) + handleClose() + }} + > + + + { + icons.simulationMode[ + simulationData.simulation.mode + ] + } + + + + {simulationName} + + + ) + })} ) diff --git a/src/modules/internalisation/translations/english.ts b/src/modules/internalisation/translations/english.ts index 060078b..50f00ab 100644 --- a/src/modules/internalisation/translations/english.ts +++ b/src/modules/internalisation/translations/english.ts @@ -30,7 +30,8 @@ export const EnglishTranslation: Translation = { refresh: 'Refresh', undo: 'Undo', 'select all': 'Select all', - 'delete selection': 'Delete selection' + 'delete selection': 'Delete selection', + 'delete simulation': 'Delete simulation' }, messages: { createdSimulation: name => `Succesfully created simulation '${name}'`, @@ -40,6 +41,7 @@ export const EnglishTranslation: Translation = { compiledIc: name => `Succesfully compiled circuit '${name}'`, cleaned: name => `Succesfully cleaned simulation '${name}'`, refreshed: name => `Succesfully refreshed simulation '${name}'`, - undone: name => `Succesfully undone simulation '${name}'` + undone: name => `Succesfully undone simulation '${name}'`, + deletedSimulation: name => `Succesfully deleted simulation '${name}'` } } diff --git a/src/modules/internalisation/translations/nederlands.ts b/src/modules/internalisation/translations/nederlands.ts index e3d9454..649857a 100644 --- a/src/modules/internalisation/translations/nederlands.ts +++ b/src/modules/internalisation/translations/nederlands.ts @@ -18,7 +18,8 @@ export const DutchTranslation: Translation = { clean: 'Todo', refresh: 'Todo', save: 'Todo', - undo: 'Todo' + undo: 'Todo', + 'delete simulation': `Todo` }, createSimulation: { mode: { @@ -38,9 +39,10 @@ export const DutchTranslation: Translation = { switchedToSimulation: name => `Succesvol veranderd naar simulatie '${name}'`, savedSimulation: name => `Simulatie succesvol opgeslagen '${name}'`, - compiledIc: name => `Todo: ${name}`, - cleaned: name => `Todo ${name}`, - refreshed: name => `Todo ${name}`, - undone: name => `Todo ${name}` + compiledIc: name => `IC gecompileerd: ${name}`, + cleaned: name => `${name} gewist`, + refreshed: name => `${name} ververst`, + undone: name => `${name} ongedaan gemaakt`, + deletedSimulation: name => `Todo` } } diff --git a/src/modules/internalisation/translations/romanian.ts b/src/modules/internalisation/translations/romanian.ts index 1a61d63..41e6935 100644 --- a/src/modules/internalisation/translations/romanian.ts +++ b/src/modules/internalisation/translations/romanian.ts @@ -30,7 +30,8 @@ export const RomanianTranslation: Translation = { 'select all': 'Selectează totul', clean: 'Curăță', refresh: 'Reîncarcă', - undo: 'Întoarce' + undo: 'Întoarce', + 'delete simulation': 'Șterge simulația' }, messages: { createdSimulation: name => @@ -41,6 +42,8 @@ export const RomanianTranslation: Translation = { compiledIc: name => `Simulația '${name}' a fost compilată cu succes`, cleaned: name => `Simulația '${name}' a fost curățată cu succes`, refreshed: name => `Simulația '${name}' a fost reîncărcată cu succes`, - undone: name => `Acțiunea a fost întoarsă` + undone: name => `Acțiunea a fost întoarsă`, + deletedSimulation: name => + `Simulația '${name}' a fost ștearsă cu succes` } } diff --git a/src/modules/internalisation/types/TranslationInterface.ts b/src/modules/internalisation/types/TranslationInterface.ts index 69e8381..eb50c4e 100644 --- a/src/modules/internalisation/types/TranslationInterface.ts +++ b/src/modules/internalisation/types/TranslationInterface.ts @@ -34,6 +34,7 @@ export interface Translation { refreshed: NameSentence cleaned: NameSentence undone: NameSentence + deletedSimulation: NameSentence } actions: Record } diff --git a/src/modules/saving/stores/saveStore.ts b/src/modules/saving/stores/saveStore.ts index dff300e..d95de6a 100644 --- a/src/modules/saving/stores/saveStore.ts +++ b/src/modules/saving/stores/saveStore.ts @@ -1,7 +1,15 @@ import { LocalStore } from '../../storage/classes/LocalStore' import { RendererState } from '../types/SimulationSave' +import { initSimulation } from '../helpers/initSimulation' +import { defaultSimulationName } from '../constants' /** * This store is used to save all simulations. */ -export const saveStore = new LocalStore('saves') +const saveStore = new LocalStore('saves') + +if (!saveStore.ls().length) { + initSimulation(defaultSimulationName, 'project') +} + +export { saveStore } diff --git a/src/modules/simulation-actions/constants.ts b/src/modules/simulation-actions/constants.ts index 0d4fa4c..7d0e742 100644 --- a/src/modules/simulation-actions/constants.ts +++ b/src/modules/simulation-actions/constants.ts @@ -7,6 +7,7 @@ import { createActionConfig } from './helpers/createActionConfig' import { selectAll } from './helpers/selectAll' import { deleteSelection } from './helpers/deleteSelection' import { cleanRenderer } from './helpers/clean' +import { deleteSimulation } from './helpers/deleteSimulation' export const actionIcons: Record = { clean: 'clear', @@ -14,7 +15,8 @@ export const actionIcons: Record = { save: 'save', undo: 'undo', 'select all': 'select_all', - 'delete selection': 'delete' + 'delete selection': 'delete', + 'delete simulation': 'delete_forever' } /** @@ -43,6 +45,13 @@ export const SidebarActions: Record = { }, ['ctrl', 'delete'] ), + ...createActionConfig( + 'delete simulation', + { + run: deleteSimulation + }, + ['ctrl', 'shift', 'delete'] + ), ...createActionConfig('select all', selectAll, ['ctrl', 'a']), ...createActionConfig('delete selection', deleteSelection, ['delete']) } diff --git a/src/modules/simulation-actions/helpers/deleteSimulation.ts b/src/modules/simulation-actions/helpers/deleteSimulation.ts new file mode 100644 index 0000000..9580fdd --- /dev/null +++ b/src/modules/simulation-actions/helpers/deleteSimulation.ts @@ -0,0 +1,24 @@ +import { SimulationRenderer } from '../../simulationRenderer/classes/SimulationRenderer' +import { saveStore } from '../../saving/stores/saveStore' +import { removeElement } from '../../../common/lang/arrays/helpers/removeElement' +import { initSimulation } from '../../saving/helpers/initSimulation' +import { defaultSimulationName } from '../../saving/constants' +import { randomItem } from '../../internalisation/helpers/randomItem' +import { switchTo } from '../../saving/helpers/switchTo' + +export const deleteSimulation = (renderer: SimulationRenderer) => { + const current = renderer.simulation.name + + const others = saveStore.ls() + removeElement(others, current) + + if (!others.length) { + initSimulation(defaultSimulationName, 'project') + } + + const switchTarget = randomItem(others) + switchTo(switchTarget) + + // actually delete simulation + saveStore.delete(current) +} diff --git a/src/modules/simulation-actions/types/possibleAction.ts b/src/modules/simulation-actions/types/possibleAction.ts index 447e0b8..f2dd0c9 100644 --- a/src/modules/simulation-actions/types/possibleAction.ts +++ b/src/modules/simulation-actions/types/possibleAction.ts @@ -8,3 +8,4 @@ export type possibleAction = | 'undo' | 'select all' | 'delete selection' + | 'delete simulation' diff --git a/src/modules/simulationRenderer/classes/SimulationRenderer.ts b/src/modules/simulationRenderer/classes/SimulationRenderer.ts index eaf286c..f41b92c 100644 --- a/src/modules/simulationRenderer/classes/SimulationRenderer.ts +++ b/src/modules/simulationRenderer/classes/SimulationRenderer.ts @@ -253,7 +253,9 @@ export class SimulationRenderer { const offset = invert( relativeTo(this.lastMousePosition, worldPosition) - ).map( + ) + + const scaledOffset = offset.map( (value, index) => value * this.camera.transform.scale[index] ) as vector2 @@ -269,7 +271,7 @@ export class SimulationRenderer { if ((this.mouseState >> 1) & 1) { this.camera.transform.position = add( this.camera.transform.position, - invert(offset) + invert(scaledOffset) ) this.spawnCount = 0 diff --git a/src/modules/storage/classes/LocalStore.ts b/src/modules/storage/classes/LocalStore.ts index 64537ae..3ccafae 100644 --- a/src/modules/storage/classes/LocalStore.ts +++ b/src/modules/storage/classes/LocalStore.ts @@ -50,4 +50,12 @@ export class LocalStore { currentData[finalKey] = finalValue localStorage.setItem(this.name, JSON.stringify(currentData)) } + + public delete(key = 'index') { + const all = this.getAll() + + delete all[key] + + localStorage.setItem(this.name, JSON.stringify(all)) + } }