blob: 5403db5ed46fa8758e520d8933e98c83326d6443 (
plain)
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
|
<script setup>
import {onDeactivated, onMounted, useTemplateRef, watch} from 'vue';
const emit = defineEmits(['update:modelValue']);
const props = defineProps({
content: {
type: String,
default: ''
},
modelValue: {
type: String,
default: ''
}
});
const actionsRef = useTemplateRef('actions');
const onTextSelected = event => {
if (document.getSelection().toString()) {
emit('update:modelValue', document.getSelection().toString());
actionsRef.value.style.display = 'inline-flex';
actionsRef.value.style.top = event.pageY +'px';
actionsRef.value.style.left = event.pageX+'px';
}
}
const newSelectionHandler = () => {
if(!document.getSelection().toString() && actionsRef.value) {
actionsRef.value.style.display = 'none';
}
}
const removeSelection = () => {
actionsRef.value.style.display = 'none';
document.getSelection().removeAllRanges();
}
defineExpose({
removeSelection
});
onMounted(() => document.addEventListener('selectionchange', newSelectionHandler));
onDeactivated(() => {
document.removeEventListener('selectionchange', newSelectionHandler);
});
watch(() => props.modelValue, newValue => {
if (!newValue) {
removeSelection();
}
});
</script>
<template>
<div @mouseup="onTextSelected" class="with-ballon-action" v-bind="$attrs">
<div class="text-highlight m-0 post-content" v-html="content"></div>
<div class="ballon-action" ref="actions">
<slot name="actions"></slot>
</div>
</div>
</template>
|