-
Notifications
You must be signed in to change notification settings - Fork 0
/
canvas.store.ts
66 lines (62 loc) · 1.74 KB
/
canvas.store.ts
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import {defineStore} from 'statebuilder';
import panzoom, {type PanZoom} from 'panzoom';
import {createEffect, createSignal, on} from 'solid-js';
import {
autocenter,
fitToCenter,
getScaleByRatio,
} from '../../components/Editor/utils/getScaleByRatio';
interface CanvasState {
scale: number;
connected: boolean;
}
export const CanvasStore = defineStore<CanvasState>(() => ({
scale: 1,
connected: false,
}))
.extend(_ => {
const [instance, setInstance] = createSignal<PanZoom | null>(null);
const [ref, setRef] = createSignal<HTMLElement | null>(null);
return {
instance,
ref,
connectRef: (el: HTMLDivElement) => {
setInstance(
panzoom(el, {
autocenter: true,
// beforeMouseDown: function (e) {
// // allow mouse-down panning only if altKey is down. Otherwise - ignore
// console.log(e.composedPath());
// const find = e.composedPath().find(el => {
// return el.getAttribute && el.getAttribute('data-node');
// });
// return !!find;
// },
}),
);
setRef(el);
_.set('connected', true);
},
};
})
.extend(_ => {
createEffect(
on([() => _.get.connected, _.instance], ([connected, instance]) => {
if (!connected) {
return;
}
instance!.on('zoom', () => {
_.set('scale', instance!.getTransform().scale);
});
}),
);
return {
fitToCenter: () => {
const instance = _.instance()!;
const ref = _.ref()!;
const {x, y, scale} = autocenter(ref);
instance.smoothMoveTo(x, y);
// instance.smoothZoom(0, 0, scale);
},
};
});