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 }