forked from vueuse/vueuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
142 lines (121 loc) · 3.66 KB
/
index.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import type { Ref } from 'vue-demi'
import { ref, watch } from 'vue-demi'
import type { Brush, Drauu, DrawingMode, Options } from 'drauu'
import { createDrauu } from 'drauu'
import type { EventHookOn, MaybeElementRef } from '@vueuse/core'
import { createEventHook, unrefElement } from '@vueuse/core'
import type { Fn } from '@vueuse/shared'
import { tryOnScopeDispose } from '@vueuse/shared'
export type UseDrauuOptions = Omit<Options, 'el'>
export interface UseDrauuReturn {
drauuInstance: Ref<Drauu | undefined>
load: (svg: string) => void
dump: () => string | undefined
clear: () => void
cancel: () => void
undo: () => boolean | undefined
redo: () => boolean | undefined
canUndo: Ref<boolean>
canRedo: Ref<boolean>
brush: Ref<Brush>
onChanged: EventHookOn
onCommitted: EventHookOn
onStart: EventHookOn
onEnd: EventHookOn
onCanceled: EventHookOn
}
/**
* Reactive drauu
*
* @see https://vueuse.org/useDrauu
* @param target The target svg element
* @param options Drauu Options
*/
export function useDrauu(
target: MaybeElementRef,
options?: UseDrauuOptions,
): UseDrauuReturn {
const drauuInstance = ref<Drauu>()
let disposables: Fn[] = []
const onChangedHook = createEventHook<void>()
const onCanceledHook = createEventHook<void>()
const onCommittedHook = createEventHook<void>()
const onStartHook = createEventHook<void>()
const onEndHook = createEventHook<void>()
const canUndo = ref(false)
const canRedo = ref(false)
const altPressed = ref(false)
const shiftPressed = ref(false)
const brush = ref<Brush>({
color: 'black',
size: 3,
arrowEnd: false,
cornerRadius: 0,
dasharray: undefined,
fill: 'transparent',
mode: 'draw',
})
watch(brush, () => {
const instance = drauuInstance.value
if (instance) {
instance.brush = brush.value
instance.mode = brush.value.mode as DrawingMode
}
}, { deep: true })
const undo = () => drauuInstance.value?.undo()
const redo = () => drauuInstance.value?.redo()
const clear = () => drauuInstance.value?.clear()
const cancel = () => drauuInstance.value?.cancel()
const load = (svg: string) => drauuInstance.value?.load(svg)
const dump = () => drauuInstance.value?.dump()
const cleanup = () => {
disposables.forEach(dispose => dispose())
drauuInstance.value?.unmount()
}
const syncStatus = () => {
if (drauuInstance.value) {
canUndo.value = drauuInstance.value.canUndo()
canRedo.value = drauuInstance.value.canRedo()
altPressed.value = drauuInstance.value.altPressed
shiftPressed.value = drauuInstance.value.shiftPressed
}
}
watch(
() => unrefElement(target),
(el) => {
if (!el || typeof SVGSVGElement === 'undefined' || !(el instanceof SVGSVGElement))
return
if (drauuInstance.value)
cleanup()
drauuInstance.value = createDrauu({ el, ...options })
syncStatus()
disposables = [
drauuInstance.value.on('canceled', () => onCanceledHook.trigger()),
drauuInstance.value.on('committed', () => onCommittedHook.trigger()),
drauuInstance.value.on('start', () => onStartHook.trigger()),
drauuInstance.value.on('end', () => onEndHook.trigger()),
drauuInstance.value.on('changed', () => {
syncStatus()
onChangedHook.trigger()
}),
]
}, { flush: 'post' })
tryOnScopeDispose(() => cleanup())
return {
drauuInstance,
load,
dump,
clear,
cancel,
undo,
redo,
canUndo,
canRedo,
brush,
onChanged: onChangedHook.on,
onCommitted: onCommittedHook.on,
onStart: onStartHook.on,
onEnd: onEndHook.on,
onCanceled: onCanceledHook.on,
}
}