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

91 lines
1.9 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"
contenteditable
style="min-width: 2px"
2023-06-03 00:53:46 +03:00
:class="iconInline ? 'inline' : 'block'"
class="peer outline-none focus:block"
2023-05-31 00:50:43 +03:00
@keydown.enter.prevent="onEnter"
2023-06-03 00:53:46 +03:00
@focus="$emit('focus', $event)"
2023-05-31 00:50:43 +03:00
@blur="onBlur"
>
{{ value }}
2023-06-03 00:53:46 +03:00
</span>
2023-05-31 00:50:43 +03:00
<IconPencil
contenteditable="false"
2023-06-04 01:08:45 +03:00
class="cursor-pointer ml-1 flex-none opacity-0 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' }"
2023-06-04 01:08:45 +03:00
title="Edit"
2023-06-03 00:53:46 +03:00
:class="{ '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-05-31 00:50:43 +03:00
@click="onPencilClick"
/>
</div>
</template>
<script>
import { IconPencil } from '@tabler/icons-vue'
export default {
name: 'ContenteditableField',
components: {
IconPencil
},
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
},
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: {
onBlur (e) {
this.value = this.$refs.contenteditable.innerText.trim() || this.modelValue
this.$emit('update:model-value', this.value)
2023-06-03 00:53:46 +03:00
this.$emit('blur', e)
2023-05-31 00:50:43 +03:00
},
onPencilClick () {
this.$refs.contenteditable.focus()
},
onEnter () {
this.$refs.contenteditable.blur()
}
}
}
</script>