diff options
Diffstat (limited to 'src/state.ts')
-rw-r--r-- | src/state.ts | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/src/state.ts b/src/state.ts new file mode 100644 index 0000000..634319a --- /dev/null +++ b/src/state.ts @@ -0,0 +1,65 @@ +import * as Serialization from 'serialization' + +const L = window.L + +// State + +var nextIndex: Index = 0 + +export type State = Marker[] +export type Index = number + +var state: State = [] + +export interface Marker { + pos: L.Pos, + name: string, + color: string, + icon: string, + radius: number, +} + +export function reset(s: State) { + state = s + nextIndex = s.length +} + +// CRUD + +export function add(marker: Marker): Index { + const index = nextIndex + state[index] = marker + nextIndex += 1 + pushState() + return index +} + +export function update(index: Index, marker: Marker) { + state[index] = marker + pushState() +} + +export function remove(index: Index) { + delete state[index] + pushState() +} + +// History + +function pushState() { + const encoded = Serialization.encode(Object.values(state)) + history.pushState('', '', `#${encoded}`) +} + +// Inspection + +export function colors() { + return [...new Set(Object.values(state).map(({ color }) => color))] +} + +export function last(): Marker | undefined { + const nonempty = Object.values(state) + return nonempty.length > 0 + ? nonempty.slice(-1)[0] + : undefined +} |