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

204 lines
5.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"
2026-01-26 11:11:19 +02:00
:contenteditable="editable && (!editableOnButton || isEditable)"
:data-placeholder="placeholder"
:data-empty="isEmpty"
:style="{ minWidth }"
:class="[iconInline ? (isEmpty ? 'inline-block' : 'inline') : 'block', hideIcon ? 'focus:block' : '']"
class="peer relative inline-block outline-none before:pointer-events-none before:absolute before:left-0 before:top-0 before:select-none before:whitespace-pre before:text-neutral-400 before:content-[attr(data-placeholder)] before:opacity-0 data-[empty=true]:before:opacity-100"
2024-06-13 10:26:36 +03:00
@paste.prevent="onPaste"
2023-06-20 00:24:39 +03:00
@keydown.enter.prevent="blurContenteditable"
2026-01-26 11:11:19 +02:00
@input="updateInputValue"
@cut="updateInputValue"
2023-06-03 00:53:46 +03:00
@focus="$emit('focus', $event)"
2023-05-31 00:50:43 +03:00
@blur="onBlur"
2026-01-26 11:11:19 +02:00
@click="editable && (!editableOnButton || isEditable) ? '' : $emit('click-contenteditable')"
2023-05-31 00:50:43 +03:00
>
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
2026-01-26 11:11:19 +02:00
v-if="withButton"
2025-10-11 10:58:09 +03:00
class="relative inline"
:class="{ 'peer-focus:hidden': hideIcon, 'peer-focus:invisible': !hideIcon }"
2023-06-20 00:24:39 +03:00
>
2025-10-11 10:58:09 +03:00
<IconPencil
class="cursor-pointer flex-none opacity-0 group-hover/contenteditable-container:opacity-100 group-hover/contenteditable:opacity-100 align-middle pl-1"
:style="iconInline ? {} : { right: -(1.1 * iconWidth) + 'px' }"
:title="t('edit')"
:class="{ invisible: !editable, 'absolute top-1/2 -translate-y-1/2': !iconInline || floatIcon, 'inline align-bottom': iconInline, 'left-0': floatIcon }"
:width="iconWidth + 4"
:stroke-width="iconStrokeWidth"
2026-01-26 11:11:19 +02:00
@click="clickEdit"
2025-10-11 10:58:09 +03:00
/>
2023-06-20 00:24:39 +03:00
</span>
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: ''
},
2026-01-26 11:11:19 +02:00
placeholder: {
type: String,
required: false,
default: ''
},
withButton: {
type: Boolean,
required: false,
default: true
},
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
},
2025-10-09 16:43:11 +03:00
hideIcon: {
type: Boolean,
required: false,
default: true
},
2025-10-11 10:58:09 +03:00
floatIcon: {
2023-06-20 00:24:39 +03:00
type: Boolean,
required: false,
default: false
},
2023-09-23 18:23:34 +03:00
selectOnEditClick: {
type: Boolean,
required: false,
default: false
},
2026-01-26 11:11:19 +02:00
editableOnButton: {
type: Boolean,
required: false,
default: false
},
minWidth: {
type: String,
required: false,
default: '2px'
},
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
}
},
2026-01-26 11:11:19 +02:00
emits: ['update:model-value', 'focus', 'blur', 'click-contenteditable'],
2023-05-31 00:50:43 +03:00
data () {
return {
2026-01-26 11:11:19 +02:00
isEditable: false,
inputValue: '',
2023-05-31 00:50:43 +03:00
value: ''
}
},
2026-01-26 11:11:19 +02:00
computed: {
isEmpty () {
return !this.inputValue.replace(/\u200B/g, '').replace(/\u00A0/g, ' ').trim()
}
},
2023-05-31 00:50:43 +03:00
watch: {
modelValue: {
handler (value) {
2026-01-26 11:11:19 +02:00
this.value = value || ''
2023-05-31 00:50:43 +03:00
},
immediate: true
}
},
2026-01-26 11:11:19 +02:00
mounted () {
this.updateInputValue()
},
2023-05-31 00:50:43 +03:00
methods: {
2026-01-26 11:11:19 +02:00
updateInputValue () {
this.inputValue = this.$refs.contenteditable?.textContent || ''
},
2024-06-13 10:26:36 +03:00
onPaste (e) {
2024-06-27 15:14:32 +03:00
const text = (e.clipboardData || window.clipboardData).getData('text/plain')
2024-07-18 10:58:12 +03:00
const selection = this.$el.getRootNode().getSelection()
2024-06-27 15:14:32 +03:00
if (selection.rangeCount) {
selection.deleteFromDocument()
selection.getRangeAt(0).insertNode(document.createTextNode(text))
selection.collapseToEnd()
}
2026-01-26 11:11:19 +02:00
this.updateInputValue()
},
clickEdit (e) {
this.focusContenteditable()
if (this.selectOnEditClick) {
this.selectContent()
}
},
setText (text) {
this.$refs.contenteditable.innerText = text
this.updateInputValue()
2024-06-13 10:26:36 +03:00
},
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)
2026-01-26 11:11:19 +02:00
this.isEditable = false
2023-07-01 00:53:32 +03:00
}, 1)
2023-05-31 00:50:43 +03:00
},
2023-06-20 00:24:39 +03:00
focusContenteditable () {
2026-01-26 11:11:19 +02:00
this.isEditable = true
this.$nextTick(() => {
this.$refs.contenteditable.focus()
this.updateInputValue()
})
2023-05-31 00:50:43 +03:00
},
2023-06-20 00:24:39 +03:00
blurContenteditable () {
2023-05-31 00:50:43 +03:00
this.$refs.contenteditable.blur()
}
}
}
</script>