-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathResizableMediaNodeView.vue
343 lines (260 loc) Β· 9.2 KB
/
ResizableMediaNodeView.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<script setup lang="ts">
import { Editor, Node, NodeViewWrapper } from '@tiptap/vue-3'
import { ref, onMounted, computed, watch } from 'vue'
import { Node as ProseMirrorNode } from 'prosemirror-model'
import { Decoration } from 'prosemirror-view'
import InlineSvg from 'vue-inline-svg'
import { resizableMediaActions } from './resizableMediaMenuUtil'
interface Props {
editor: Editor
node: ProseMirrorNode
decorations: Decoration
selected: boolean
extension: Node<any, any>
getPos: () => number
updateAttributes: (attributes: Record<string, any>) => void
deleteNode: () => void
}
const props = defineProps<Props>()
const mediaType = computed<'img' | 'video'>(() => props.node.attrs['media-type'])
const resizableImg = ref<HTMLImageElement | HTMLVideoElement | null>(null) // template ref
const aspectRatio = ref(0)
const proseMirrorContainerWidth = ref(0)
const mediaActionActiveState = ref<Record<string, boolean>>({})
const setMediaActionActiveStates = () => {
const activeStates: Record<string, boolean> = {}
for (const { tooltip, isActive } of resizableMediaActions) activeStates[tooltip] = !!isActive?.(props.node.attrs)
mediaActionActiveState.value = activeStates
}
watch(
() => props.node.attrs,
() => setMediaActionActiveStates(),
{ deep: true }
)
const mediaSetupOnLoad = () => {
// ! TODO: move this to extension storage
const proseMirrorContainerDiv = document.querySelector('.ProseMirror')
if (proseMirrorContainerDiv) proseMirrorContainerWidth.value = proseMirrorContainerDiv?.clientWidth
// When the media has loaded
if (!resizableImg.value) return
if (mediaType.value === 'video') {
// Aspect Ratio from its original size
setTimeout(() => {
aspectRatio.value = (resizableImg.value as HTMLVideoElement).videoWidth / (resizableImg.value as HTMLVideoElement).videoHeight
// for the first time when video is added with custom width and height
// and we have to adjust the video height according to it's width
onHorizontalResize('left', 0)
}, 200)
} else {
resizableImg.value.onload = () => {
// Aspect Ratio from its original size
aspectRatio.value = (resizableImg.value as HTMLImageElement).naturalWidth
/ (resizableImg.value as HTMLImageElement).naturalHeight
onHorizontalResize('left', 0)
}
}
setTimeout(() => setMediaActionActiveStates(), 200)
}
onMounted(() => mediaSetupOnLoad())
const isHorizontalResizeActive = ref(false)
const lastCursorX = ref(-1)
interface WidthAndHeight {
width: number
height: number
}
const limitWidthOrHeightToFiftyPixels = ({ width, height }: WidthAndHeight) => width < 100 || height < 100
const startHorizontalResize = (e: MouseEvent) => {
isHorizontalResizeActive.value = true
lastCursorX.value = e.clientX
document.addEventListener('mousemove', onHorizontalMouseMove)
document.addEventListener('mouseup', stopHorizontalResize)
}
const stopHorizontalResize = () => {
isHorizontalResizeActive.value = false
lastCursorX.value = -1
document.removeEventListener('mousemove', onHorizontalMouseMove)
document.removeEventListener('mouseup', stopHorizontalResize)
}
const onHorizontalResize = (directionOfMouseMove: 'right' | 'left', diff: number) => {
if (!resizableImg.value) {
console.error('Media ref is undefined|null', { resizableImg: resizableImg.value })
return
}
const currentMediaDimensions = {
width: resizableImg.value?.width,
height: resizableImg.value?.height,
}
const newMediaDimensions = {
width: -1,
height: -1,
}
if (directionOfMouseMove === 'left') {
newMediaDimensions.width = currentMediaDimensions.width - Math.abs(diff)
} else {
newMediaDimensions.width = currentMediaDimensions.width + Math.abs(diff)
}
if (newMediaDimensions.width > proseMirrorContainerWidth.value) newMediaDimensions.width = proseMirrorContainerWidth.value
newMediaDimensions.height = newMediaDimensions.width / aspectRatio.value
if (limitWidthOrHeightToFiftyPixels(newMediaDimensions)) return
props.updateAttributes(newMediaDimensions)
}
const onHorizontalMouseMove = (e: MouseEvent) => {
if (!isHorizontalResizeActive.value) return
const { clientX } = e
const diff = lastCursorX.value - clientX
lastCursorX.value = clientX
if (diff === 0) return
const directionOfMouseMove: 'left' | 'right' = diff > 0 ? 'left' : 'right'
onHorizontalResize(directionOfMouseMove, Math.abs(diff))
}
const isVerticalResizeActive = ref(false)
const lastCursorY = ref(-1)
const startVerticalResize = (e: MouseEvent) => {
isVerticalResizeActive.value = true
lastCursorY.value = e.clientY
document.addEventListener('mousemove', onVerticalMouseMove)
document.addEventListener('mouseup', stopVerticalResize)
}
const stopVerticalResize = () => {
isVerticalResizeActive.value = false
lastCursorY.value = -1
document.removeEventListener('mousemove', onVerticalMouseMove)
document.removeEventListener('mouseup', stopVerticalResize)
}
const onVerticalMouseMove = (e: MouseEvent) => {
if (!isVerticalResizeActive.value) return
const { clientY } = e
const diff = lastCursorY.value - clientY
lastCursorY.value = clientY
if (diff === 0) return
const directionOfMouseMove: 'up' | 'down' = diff > 0 ? 'up' : 'down'
if (!resizableImg.value) {
console.error('Media ref is undefined|null', { resizableImg: resizableImg.value })
return
}
const currentMediaDimensions = {
width: resizableImg.value?.width,
height: resizableImg.value?.height,
}
const newMediaDimensions = {
width: -1,
height: -1,
}
if (directionOfMouseMove === 'up') {
newMediaDimensions.height = currentMediaDimensions.height - Math.abs(diff)
} else {
newMediaDimensions.height = currentMediaDimensions.height + Math.abs(diff)
}
newMediaDimensions.width = newMediaDimensions.height * aspectRatio.value
if (newMediaDimensions.width > proseMirrorContainerWidth.value) {
newMediaDimensions.width = proseMirrorContainerWidth.value
newMediaDimensions.height = newMediaDimensions.width / aspectRatio.value
}
if (limitWidthOrHeightToFiftyPixels(newMediaDimensions)) return
props.updateAttributes(newMediaDimensions)
}
const isFloat = computed<boolean>(() => !!props.node.attrs.dataFloat)
const isAlign = computed<boolean>(() => !!props.node.attrs.dataAlign)
</script>
<template>
<node-view-wrapper
as="article"
class="media-node-view flex pos-relative not-prose"
:class="[`${isFloat && `f-${props.node.attrs.dataFloat}` || ''}`, `${isAlign && `align-${props.node.attrs.dataAlign}` || ''}`]"
>
<tippy :interactive="true">
<div class="w-fit flex relative">
<img
v-if="mediaType === 'img'"
v-bind="node.attrs"
ref="resizableImg"
class="rounded-lg"
:class="[`${isFloat && `float-${props.node.attrs.dataFloat}` || ''}`, `${isAlign && `align-${props.node.attrs.dataAlign}` || ''}`]"
draggable="true"
>
<video
v-else-if="mediaType === 'video'"
v-bind="node.attrs"
ref="resizableImg"
class="rounded-lg"
:class="[`${isFloat && `float-${props.node.attrs.dataFloat}` || ''}`, `${isAlign && `align-${props.node.attrs.dataAlign}` || ''}`]"
draggable="true"
controls="true"
>
<source :src="node.attrs.src">
</video>
<div
class="horizontal-resize-handle"
:class="{ 'horizontal-resize-active': isHorizontalResizeActive }"
title="Resize"
@mousedown="startHorizontalResize"
@mouseup="stopHorizontalResize"
/>
<div
class="vertical-resize-handle"
:class="{ 'vertical-resize-active': isVerticalResizeActive }"
title="Resize"
@mousedown="startVerticalResize"
@mouseup="stopVerticalResize"
/>
</div>
<template #content>
<section class="image-actions-container">
<button
v-for="(mediaAction, i) in resizableMediaActions"
:key="i"
v-tippy="{ content: mediaAction.tooltip, placement: 'top' }"
:content="mediaAction.tooltip"
class="btn btn-sm btn-ghost image-action-button"
@click="mediaAction.tooltip === 'Delete'
? mediaAction.delete?.(deleteNode)
: mediaAction.action?.(updateAttributes)
"
>
<InlineSvg :src="mediaAction.icon" />
</button>
</section>
</template>
</tippy>
</node-view-wrapper>
</template>
<style lang="scss">
.media-node-view {
position: relative;
&.f-left {
@apply float-left
}
&.f-right {
@apply float-right
}
&.align-left {
@apply justify-start
}
&.align-center {
@apply justify-center
}
&.align-right {
@apply justify-end
}
.horizontal-resize-handle,
.vertical-resize-handle {
@apply absolute hover:bg-blue-200 z-50 opacity-50
}
.horizontal-resize-handle {
@apply h-full w-2 top-0 right-0 cursor-col-resize
}
.vertical-resize-handle {
@apply w-full h-2 bottom-0 left-0 cursor-row-resize
}
}
.image-actions-container {
@apply flex gap-1
}
.media-actions-container {
padding: 4px !important;
width: fit-content !important;
.ep-button+.ep-button {
margin-left: 0px;
}
}
</style>