This repository has been archived by the owner on Nov 9, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Moveable.vue
88 lines (86 loc) · 1.84 KB
/
Moveable.vue
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<template>
<div>
<slot/>
</div>
</template>
<script>
// eslint-disable-next-line import/no-extraneous-dependencies
import Moveable from 'moveable';
const ALLOWED_EVENTS = [
'dragStart',
'drag',
'dragEnd',
'resizeStart',
'resize',
'resizeEnd',
'scaleStart',
'scale',
'scaleEnd',
'rotateStart',
'rotate',
'rotateEnd',
'warpStart',
'warp',
'warpEnd',
'pinchStart',
'pinch',
'pinchEnd',
];
export default {
name: 'Moveable',
inheritAttrs: false,
props: {
draggable: Boolean,
resizable: Boolean,
scalable: Boolean,
rotatable: Boolean,
warpable: Boolean,
pinchable: [Boolean, Array],
origin: Boolean,
throttleDrag: Number,
throttleResize: Number,
throttleScale: Number,
throttleRotate: Number,
keepRatio: Boolean,
container: {
type: [HTMLElement, SVGElement],
default: () => document.body,
},
},
methods: {
updateRec() {
this.moveable.updateRect();
},
},
mounted() {
this.moveable = new Moveable(this.$props.container, {
...this.$props,
target: this.$el,
});
ALLOWED_EVENTS.forEach(event => (
this.moveable.on(event, this.$emit.bind(this, event))
));
window.addEventListener('resize', this.updateRec, { passive: true });
},
watch: {
$props: {
handler(newOptions) {
Object.keys(newOptions).forEach((key) => {
const existingValue = this.moveable[key];
const newValue = newOptions[key];
if (existingValue === newValue) return;
this.moveable[key] = newOptions[key];
});
},
deep: true,
},
},
beforeDestroy() {
ALLOWED_EVENTS.forEach(event => (
this.moveable.off(event, this.$emit.bind(this, event))
));
window.removeEventListener('resize', this.updateRec);
this.moveable.destroy();
},
};
</script>