forked from halo-sigs/richtext-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageView.vue
99 lines (92 loc) · 2.38 KB
/
ImageView.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
<script lang="ts" setup>
import { i18n } from "@/locales";
import { Editor, Node, NodeViewWrapper } from "@tiptap/vue-3";
import type { Node as ProseMirrorNode } from "prosemirror-model";
import type { Decoration } from "prosemirror-view";
import { computed, onMounted, ref } from "vue";
import { useResizeObserver } from "@vueuse/core";
const props = defineProps<{
editor: Editor;
node: ProseMirrorNode;
decorations: Decoration[];
selected: boolean;
extension: Node<any, any>;
getPos: () => number;
updateAttributes: (attributes: Record<string, any>) => void;
deleteNode: () => void;
}>();
const src = computed({
get: () => {
return props.node?.attrs.src;
},
set: (src: string) => {
props.updateAttributes({
src: src,
});
},
});
const alt = computed({
get: () => {
return props.node?.attrs.alt;
},
set: (alt: string) => {
props.updateAttributes({ alt: alt });
},
});
function handleSetFocus() {
props.editor.commands.setNodeSelection(props.getPos());
}
const inputRef = ref();
const resizeRef = ref();
const init = ref(true);
onMounted(() => {
if (!src.value) {
inputRef.value.focus();
} else {
useResizeObserver(resizeRef.value, (entries) => {
const entry = entries[0];
const { height } = entry.contentRect;
if (height == 0) {
return;
}
if (!props.selected && !init.value) {
handleSetFocus();
}
init.value = false;
});
}
});
</script>
<template>
<node-view-wrapper as="div" class="inline-block" :class="{ 'w-full': !src }">
<div v-if="!src" class="p-1.5 w-full">
<input
ref="inputRef"
v-model.lazy="src"
class="block px-2 w-full py-1.5 text-sm text-gray-900 border border-gray-300 rounded-md bg-gray-50 focus:ring-blue-500 focus:border-blue-500"
:placeholder="i18n.global.t('editor.common.placeholder.link_input')"
tabindex="-1"
@focus="handleSetFocus"
/>
</div>
<div
v-else
ref="resizeRef"
class="resize-x inline-block overflow-hidden text-center relative rounded-md"
:class="{
'ring-2 rounded': selected,
}"
:style="{
width: node.attrs.width,
height: node.attrs.height,
}"
>
<img
:src="src"
:title="node.attrs.title"
:alt="alt"
class="w-full h-full"
/>
</div>
</node-view-wrapper>
</template>