This repository has been archived by the owner on Jan 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
FabricSVGFromURL.vue
118 lines (114 loc) · 2.67 KB
/
FabricSVGFromURL.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<template>
<fabric-group :id="id" v-bind.sync="groupProps">
<fabric-path
v-for="(path, index) in objs"
:id="id + '_' + index"
:key="id + '_' + index"
v-bind="path"
></fabric-path>
</fabric-group>
</template>
<script>
import fabricGroup from "./FabricGroup";
import fabricPath from "./FabricPath";
//Props to change via interaction and need to be emitted for prop.sync usage
const EMIT_PROPS = [
"angle",
"height",
"left",
"originX",
"originY",
"scaleX",
"scaleY",
"skewX",
"skewY",
"top",
"width"
];
//Monitor the group Object and emit an update to allow .sync usage
const watchEmitProp = (key, deep) => ({
handler(newValue) {
//If the prop caused the update there is no point emitting it back
if (this.$attrs[key] === newValue) {
return;
}
this.$emit("update:" + key, newValue);
},
deep
});
//Monitor the Props and update the item with the changed value
const watchAttrs = (key, deep) => ({
handler(newValue) {
if (this.groupProps[key] === newValue) {
return;
}
this.groupProps[key] = newValue;
},
deep
});
export default {
name: "fabric-svg-from-URL",
inject: ["fabric"],
inheritAttrs: false,
components: {
fabricGroup,
fabricPath
},
props: {
id: { type: [Number, String], required: true },
url: { type: String, default: "../svg/pipe.svg" }
},
data() {
return {
objs: null,
groupProps: null,
customWatch: ["url"]
};
},
created() {
this.groupProps = this.$attrs; //any props that are not in the prop definition are assuemed to be for the group
this.createSVG();
this.createGroupAttrWatchers();
this.createFabricItemWatchers();
},
watch: {
url(newValue) {
if (this.objs) {
this.destroySVG();
}
this.createSVG();
}
},
methods: {
createSVG() {
this.fabric.loadSVGFromURL(this.url, (objs, options) => {
this.objs = objs;
});
},
destroySVG() {
this.objs = null;
},
createFabricItemWatchers() {
//Setup Watchers for emmit sync option
EMIT_PROPS.forEach(prop => {
this.$watch("groupProps." + prop, watchEmitProp(prop, true));
});
},
createGroupAttrWatchers() {
//Setup prop watches to sync with fabric
Object.keys(this.$attrs).forEach(key => {
//Custom watch check to make sure the mixin also does not genearte a watch
if (typeof this.customWatch !== typeof undefined) {
if (this.customWatch.includes(key)) {
return;
}
}
this.$watch("$attrs." + key, watchAttrs(key, true));
});
}
},
beforeDestroy() {
this.destroySVG();
}
};
</script>