Files
docuseal/app/javascript/template_builder/contenteditable.vue
T

133 lines
3.0 KiB
Vue
Raw Normal View History

2023-05-31 00:50:43 +03:00
<template>
2023-06-03 00:53:46 +03:00
<div
2023-06-04 01:08:45 +03:00
class="group/contenteditable relative overflow-visible"
2023-06-03 00:53:46 +03:00
:class="{ 'flex items-center': !iconInline }"
>
<span
2023-05-31 00:50:43 +03:00
ref="contenteditable"
2024-01-07 14:19:21 +02:00
dir="auto"
2023-11-04 01:19:32 +02:00
:contenteditable="editable"
2023-05-31 00:50:43 +03:00
style="min-width: 2px"
2023-06-03 00:53:46 +03:00
:class="iconInline ? 'inline' : 'block'"
class="peer outline-none focus:block"
2024-06-13 10:26:36 +03:00
@paste.prevent="onPaste"
2023-06-20 00:24:39 +03:00
@keydown.enter.prevent="blurContenteditable"
2023-06-03 00:53:46 +03:00
@focus="$emit('focus', $event)"
2023-05-31 00:50:43 +03:00
@blur="onBlur"
>
2024-05-02 16:33:41 +03:00
{{ value }}
2023-06-03 00:53:46 +03:00
</span>
2023-06-20 00:24:39 +03:00
<span
v-if="withRequired"
title="Required"
class="text-red-500 peer-focus:hidden"
@click="focusContenteditable"
>
*
</span>
2023-05-31 00:50:43 +03:00
<IconPencil
2023-10-25 01:08:39 +03:00
class="cursor-pointer flex-none opacity-0 group-hover/contenteditable-container:opacity-100 group-hover/contenteditable:opacity-100 align-middle peer-focus:hidden"
2023-06-03 00:53:46 +03:00
:style="iconInline ? {} : { right: -(1.1 * iconWidth) + 'px' }"
2024-02-19 23:52:47 +02:00
:title="t('edit')"
2023-11-24 22:44:27 +02:00
:class="{ invisible: !editable, 'ml-1': !withRequired, 'absolute': !iconInline, 'inline align-bottom': iconInline }"
2023-05-31 00:50:43 +03:00
:width="iconWidth"
2023-06-04 01:08:45 +03:00
:stroke-width="iconStrokeWidth"
2023-09-23 18:23:34 +03:00
@click="[focusContenteditable(), selectOnEditClick && selectContent()]"
2023-05-31 00:50:43 +03:00
/>
</div>
</template>
<script>
import { IconPencil } from '@tabler/icons-vue'
export default {
name: 'ContenteditableField',
components: {
IconPencil
},
2024-02-19 23:52:47 +02:00
inject: ['t'],
2023-05-31 00:50:43 +03:00
props: {
modelValue: {
type: String,
required: false,
default: ''
},
2023-06-03 00:53:46 +03:00
iconInline: {
type: Boolean,
required: false,
default: false
},
2023-05-31 00:50:43 +03:00
iconWidth: {
type: Number,
required: false,
default: 30
2023-06-04 01:08:45 +03:00
},
2023-06-20 00:24:39 +03:00
withRequired: {
type: Boolean,
required: false,
default: false
},
2023-09-23 18:23:34 +03:00
selectOnEditClick: {
type: Boolean,
required: false,
default: false
},
2023-11-04 01:19:32 +02:00
editable: {
type: Boolean,
required: false,
default: true
},
2023-06-04 01:08:45 +03:00
iconStrokeWidth: {
type: Number,
required: false,
default: 2
2023-05-31 00:50:43 +03:00
}
},
2023-06-03 00:53:46 +03:00
emits: ['update:model-value', 'focus', 'blur'],
2023-05-31 00:50:43 +03:00
data () {
return {
value: ''
}
},
watch: {
modelValue: {
handler (value) {
this.value = value
},
immediate: true
}
},
methods: {
2024-06-13 10:26:36 +03:00
onPaste (e) {
e.target.innerText = (e.clipboardData || window.clipboardData).getData('text/plain')
},
2023-09-23 18:23:34 +03:00
selectContent () {
const el = this.$refs.contenteditable
const range = document.createRange()
range.selectNodeContents(el)
const sel = window.getSelection()
sel.removeAllRanges()
sel.addRange(range)
},
2023-05-31 00:50:43 +03:00
onBlur (e) {
2023-07-01 00:53:32 +03:00
setTimeout(() => {
this.value = this.$refs.contenteditable.innerText.trim() || this.modelValue
this.$emit('update:model-value', this.value)
this.$emit('blur', e)
}, 1)
2023-05-31 00:50:43 +03:00
},
2023-06-20 00:24:39 +03:00
focusContenteditable () {
2023-05-31 00:50:43 +03:00
this.$refs.contenteditable.focus()
},
2023-06-20 00:24:39 +03:00
blurContenteditable () {
2023-05-31 00:50:43 +03:00
this.$refs.contenteditable.blur()
}
}
}
</script>