-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmobx.ts
More file actions
52 lines (43 loc) · 1.09 KB
/
mobx.ts
File metadata and controls
52 lines (43 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* MobX integration example.
*/
import { makeAutoObservable } from 'mobx';
import { createTravels } from '../src/index';
type Layer = {
id: string;
name: string;
visible: boolean;
};
class LayerDocument {
layers: Layer[] = [];
selectedLayerId: string | null = null;
constructor() {
makeAutoObservable(this);
}
}
export const document = new LayerDocument();
const travels = createTravels(document, {
mutable: true,
maxHistory: 200,
// MobX adds runtime metadata on observables; keep durable snapshots plain in
// production apps if you persist this state.
warnOnUnsupportedState: false,
});
export const layerHistory = travels.getControls();
export function addLayer(name: string) {
travels.setState((draft) => {
const id = crypto.randomUUID();
draft.layers.push({ id, name, visible: true });
draft.selectedLayerId = id;
});
}
export function renameSelectedLayer(name: string) {
travels.setState((draft) => {
const layer = draft.layers.find(
(item) => item.id === draft.selectedLayerId
);
if (layer) {
layer.name = name;
}
});
}