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

1018 lines
33 KiB
Vue
Raw Normal View History

2023-05-21 17:59:36 +03:00
<template>
<div
2025-03-19 15:12:22 +02:00
class="absolute overflow-visible group field-area-container"
2023-05-21 17:59:36 +03:00
:style="positionStyle"
2024-12-28 13:08:00 +02:00
:class="{ 'z-[1]': isMoved || isDragged }"
2023-06-03 00:53:46 +03:00
@pointerdown.stop
2024-06-12 20:05:55 +03:00
@mousedown="startMouseMove"
2023-10-06 03:21:14 +03:00
@touchstart="startTouchDrag"
2023-05-21 17:59:36 +03:00
>
<div
2023-06-27 00:57:29 +03:00
v-if="isSelected || isDraw"
2023-06-07 01:16:43 +03:00
class="top-0 bottom-0 right-0 left-0 absolute border border-1.5 pointer-events-none"
2025-01-10 15:12:09 +02:00
:class="field.type === 'heading' ? '' : borderColors[submitterIndex % borderColors.length]"
2023-06-07 01:16:43 +03:00
/>
2023-06-27 00:57:29 +03:00
<div
v-if="field.type === 'cells' && (isSelected || isDraw)"
class="top-0 bottom-0 right-0 left-0 absolute"
>
<div
v-for="(cellW, index) in cells"
:key="index"
class="absolute top-0 bottom-0 border-r"
2025-01-10 15:12:09 +02:00
:class="field.type === 'heading' ? '' : borderColors[submitterIndex % borderColors.length]"
2023-06-27 00:57:29 +03:00
:style="{ left: (cellW / area.w * 100) + '%' }"
>
<span
2023-11-04 01:19:32 +02:00
v-if="index === 0 && editable"
2023-06-27 00:57:29 +03:00
class="h-2.5 w-2.5 rounded-full -bottom-1 border-gray-400 bg-white shadow-md border absolute cursor-ew-resize z-10"
style="left: -4px"
@mousedown.stop="startResizeCell"
/>
</div>
</div>
2023-06-07 01:16:43 +03:00
<div
2024-07-11 21:40:15 +03:00
v-if="field?.type && (isSelected || isNameFocus)"
2025-03-19 15:12:22 +02:00
class="absolute bg-white rounded-t border overflow-visible whitespace-nowrap flex z-10 field-area-controls"
2023-06-03 00:53:46 +03:00
style="top: -25px; height: 25px"
@mousedown.stop
@pointerdown.stop
2023-05-21 17:59:36 +03:00
>
2023-06-07 01:16:43 +03:00
<FieldSubmitter
2024-07-11 21:40:15 +03:00
v-if="field.type != 'heading'"
2023-06-07 01:16:43 +03:00
v-model="field.submitter_uuid"
2025-03-19 15:12:22 +02:00
class="border-r roles-dropdown"
2023-06-07 01:16:43 +03:00
:compact="true"
2024-03-19 01:21:44 +02:00
:editable="editable && (!defaultField || defaultField.role !== submitter?.name)"
2024-01-23 23:20:19 +02:00
:allow-add-new="!defaultSubmitters.length"
:menu-classes="'dropdown-content bg-white menu menu-xs p-2 shadow rounded-box w-52 rounded-t-none -left-[1px] mt-[1px]'"
2023-06-07 01:16:43 +03:00
:submitters="template.submitters"
2023-06-24 16:17:07 +03:00
@update:model-value="save"
2023-06-07 01:16:43 +03:00
@click="selectedAreaRef.value = area"
/>
2023-06-04 01:08:45 +03:00
<FieldType
v-model="field.type"
:button-width="27"
2023-11-27 20:24:58 +02:00
:editable="editable && !defaultField"
2023-06-04 01:08:45 +03:00
:button-classes="'px-1'"
:menu-classes="'bg-white rounded-t-none'"
2023-06-24 16:17:07 +03:00
@update:model-value="[maybeUpdateOptions(), save()]"
2023-06-04 01:08:45 +03:00
@click="selectedAreaRef.value = area"
/>
<span
2023-06-16 00:04:05 +03:00
v-if="field.type !== 'checkbox' || field.name"
2023-06-04 01:08:45 +03:00
ref="name"
2024-07-11 21:40:15 +03:00
:contenteditable="editable && !defaultField && field.type !== 'heading'"
2024-01-07 14:19:21 +02:00
dir="auto"
2023-06-04 01:08:45 +03:00
class="pr-1 cursor-text outline-none block"
style="min-width: 2px"
2024-06-13 10:26:36 +03:00
@paste.prevent="onPaste"
2023-06-04 01:08:45 +03:00
@keydown.enter.prevent="onNameEnter"
@focus="onNameFocus"
@blur="onNameBlur"
2025-01-07 17:30:21 +02:00
>{{ optionIndexText }} {{ (defaultField ? (defaultField.title || field.title || field.name) : field.name) || defaultName }}</span>
2023-06-20 00:24:39 +03:00
<div
2025-08-23 20:11:10 +03:00
v-if="isSettingsFocus || isSelectInput || (isValueInput && field.type !== 'heading') || (isNameFocus && !['checkbox', 'phone'].includes(field.type))"
2023-06-20 00:24:39 +03:00
class="flex items-center ml-1.5"
>
<input
2025-08-23 20:11:10 +03:00
v-if="!isValueInput && !isSelectInput"
2023-06-20 00:24:39 +03:00
:id="`required-checkbox-${field.uuid}`"
v-model="field.required"
type="checkbox"
class="checkbox checkbox-xs no-animation rounded"
@mousedown.prevent
>
<label
2025-08-23 20:11:10 +03:00
v-if="!isValueInput && !isSelectInput"
2023-06-20 00:24:39 +03:00
:for="`required-checkbox-${field.uuid}`"
class="label text-xs"
@click.prevent="field.required = !field.required"
@mousedown.prevent
2024-01-07 14:19:21 +02:00
>{{ t('required') }}</label>
2024-06-12 20:05:55 +03:00
<input
2025-08-23 20:11:10 +03:00
v-if="isValueInput || isSelectInput"
2024-06-12 20:05:55 +03:00
:id="`readonly-checkbox-${field.uuid}`"
type="checkbox"
class="checkbox checkbox-xs no-animation rounded"
:checked="!(field.readonly ?? true)"
@change="field.readonly = !(field.readonly ?? true)"
@mousedown.prevent
>
<label
2025-08-23 20:11:10 +03:00
v-if="isValueInput || isSelectInput"
2024-06-12 20:05:55 +03:00
:for="`readonly-checkbox-${field.uuid}`"
class="label text-xs"
@click.prevent="field.readonly = !(field.readonly ?? true)"
@mousedown.prevent
>{{ t('editable') }}</label>
2024-05-05 16:57:04 +03:00
<span
2024-07-11 21:40:15 +03:00
v-if="field.type !== 'payment' && !isValueInput"
2025-03-19 15:12:22 +02:00
class="dropdown dropdown-end field-area-settings-dropdown"
2024-05-05 16:57:04 +03:00
@mouseenter="renderDropdown = true"
@touchstart="renderDropdown = true"
>
<label
ref="settingsButton"
tabindex="0"
:title="t('settings')"
class="cursor-pointer flex items-center"
style="height: 25px"
@focus="isSettingsFocus = true"
@blur="maybeBlurSettings"
>
<IconDotsVertical class="w-5 h-5" />
</label>
<ul
v-if="renderDropdown"
ref="settingsDropdown"
tabindex="0"
class="dropdown-content menu menu-xs px-2 pb-2 pt-1 shadow rounded-box w-52 z-10 rounded-t-none"
:style="{ backgroundColor: 'white' }"
@dragstart.prevent.stop
@click="closeDropdown"
@focusout="maybeBlurSettings"
>
<FieldSettings
:field="field"
:default-field="defaultField"
:editable="editable"
:background-color="'white'"
:with-required="false"
:with-areas="false"
2025-07-17 10:54:44 +03:00
:with-signature-id="withSignatureId"
2025-07-27 10:27:12 +03:00
:with-prefillable="withPrefillable"
2024-05-05 16:57:04 +03:00
@click-formula="isShowFormulaModal = true"
2025-03-09 02:08:44 +02:00
@click-font="isShowFontModal = true"
2024-05-05 16:57:04 +03:00
@click-description="isShowDescriptionModal = true"
@click-condition="isShowConditionsModal = true"
@scroll-to="[selectedAreaRef.value = $event, $emit('scroll-to', $event)]"
/>
</ul>
</span>
2023-06-20 00:24:39 +03:00
</div>
2023-06-03 00:53:46 +03:00
<button
2023-11-04 01:19:32 +02:00
v-else-if="editable"
2023-06-08 00:20:42 +03:00
class="pr-1"
2024-02-19 23:52:47 +02:00
:title="t('remove')"
2023-06-04 01:08:45 +03:00
@click.prevent="$emit('remove')"
2023-06-03 00:53:46 +03:00
>
2023-06-08 00:20:42 +03:00
<IconX width="14" />
2023-06-03 00:53:46 +03:00
</button>
</div>
<div
2024-07-11 21:40:15 +03:00
ref="touchValueTarget"
2025-01-24 23:08:44 +02:00
class="flex h-full w-full field-area"
2024-02-05 23:03:45 +02:00
dir="auto"
2025-08-23 20:11:10 +03:00
:class="[isValueInput ? 'cursor-text' : '', isValueInput || isCheckboxInput || isSelectInput ? 'bg-opacity-50' : 'bg-opacity-80', field.type === 'heading' ? 'bg-gray-50' : bgColors[submitterIndex % bgColors.length], isDefaultValuePresent || isValueInput || (withFieldPlaceholder && field.areas) ? fontClasses : 'justify-center items-center']"
2024-07-11 21:40:15 +03:00
@click="focusValueInput"
2023-06-03 00:53:46 +03:00
>
2023-06-04 01:08:45 +03:00
<span
v-if="field"
2024-07-11 21:40:15 +03:00
class="flex justify-center items-center space-x-1"
2025-08-23 20:11:10 +03:00
:class="{ 'w-full': isWFullType, 'h-full': !isValueInput && !isDefaultValuePresent }"
2023-06-04 01:08:45 +03:00
>
2023-11-05 01:41:54 +02:00
<div
2025-08-23 20:11:10 +03:00
v-if="isDefaultValuePresent || isValueInput || isSelectInput || (withFieldPlaceholder && field.areas && field.type !== 'checkbox')"
:class="{ 'w-full h-full': isWFullType }"
2025-07-14 12:53:15 +03:00
:style="fontStyle"
2023-11-05 01:41:54 +02:00
>
2023-11-07 11:36:11 +02:00
<div
ref="textContainer"
class="flex items-center px-0.5"
2024-11-10 12:48:53 +02:00
:style="{ color: field.preferences?.color }"
2025-08-23 20:11:10 +03:00
:class="{ 'w-full h-full': isWFullType }"
2023-11-07 11:36:11 +02:00
>
2024-02-05 23:03:45 +02:00
<IconCheck
v-if="field.type == 'checkbox'"
class="aspect-square mx-auto"
:class="{ '!w-auto !h-full': area.w > area.h, '!w-full !h-auto': area.w <= area.h }"
/>
2025-08-23 20:11:10 +03:00
<template
v-else-if="(field.type === 'radio' || field.type === 'multiple') && field?.areas?.length > 1"
>
<IconCheck
2025-08-23 21:33:46 +03:00
v-if="field.type === 'multiple' ? field.default_value.includes(buildAreaOptionValue(area)) : buildAreaOptionValue(area) === field.default_value"
2025-08-23 20:11:10 +03:00
class="aspect-square mx-auto"
:class="{ '!w-auto !h-full': area.w > area.h, '!w-full !h-auto': area.w <= area.h }"
/>
</template>
2024-05-12 11:12:07 +03:00
<span
2024-07-15 14:03:36 +03:00
v-else-if="field.type === 'number' && !isValueInput && (field.default_value || field.default_value == 0)"
2024-05-12 11:12:07 +03:00
class="whitespace-pre-wrap"
>{{ formatNumber(field.default_value, field.preferences?.format) }}</span>
2024-06-27 12:40:10 +03:00
<span
v-else-if="field.default_value === '{{date}}'"
>
{{ t('signing_date') }}
</span>
2024-06-22 13:16:22 +03:00
<div
v-else-if="field.type === 'cells' && field.default_value"
class="w-full flex items-center"
>
<div
v-for="(char, index) in field.default_value"
:key="index"
class="text-center flex-none"
:style="{ width: (area.cell_w / area.w * 100) + '%' }"
>
{{ char }}
</div>
</div>
2025-08-23 20:11:10 +03:00
<select
v-else-if="isSelectInput"
ref="defaultValueSelect"
class="bg-transparent outline-none focus:outline-none w-full"
@change="[field.default_value = $event.target.value, field.readonly = !!field.default_value?.length, save()]"
@focus="selectedAreaRef.value = area"
@keydown.enter="onDefaultValueEnter"
>
<option
:disabled="!field.default_value?.length"
:selected="!field.default_value?.length"
:value="''"
>
{{ t(field.default_value?.length ? 'none' : 'select') }}
</option>
<option
v-for="(option, index) in field.options"
:key="index"
:selected="field.default_value === option.value"
:value="option.value"
>
{{ option.value }}
</option>
</select>
2024-02-05 23:03:45 +02:00
<span
v-else
2024-06-12 20:05:55 +03:00
ref="defaultValue"
2024-07-11 21:40:15 +03:00
:contenteditable="isValueInput"
2025-08-23 20:11:10 +03:00
class="whitespace-pre-wrap outline-none empty:before:content-[attr(placeholder)] before:text-base-content/30"
2024-07-11 21:40:15 +03:00
:class="{ 'cursor-text': isValueInput }"
2025-08-24 10:08:47 +03:00
:placeholder="withFieldPlaceholder && !isValueInput ? defaultField?.title || field.title || field.name || defaultName : (field.type === 'date' ? field.preferences?.format || t('type_value') : t('type_value'))"
2024-06-12 20:05:55 +03:00
@blur="onDefaultValueBlur"
2025-08-23 20:11:10 +03:00
@focus="selectedAreaRef.value = area"
2024-06-13 10:26:36 +03:00
@paste.prevent="onPaste"
2024-07-11 21:40:15 +03:00
@keydown.enter="onDefaultValueEnter"
2024-02-05 23:03:45 +02:00
>{{ field.default_value }}</span>
2023-11-05 01:41:54 +02:00
</div>
</div>
2023-06-08 00:20:42 +03:00
<component
:is="fieldIcons[field.type]"
2025-08-23 20:11:10 +03:00
v-else-if="!isCheckboxInput"
2023-06-08 00:20:42 +03:00
width="100%"
height="100%"
2023-11-05 01:41:54 +02:00
class="max-h-10 opacity-50"
2023-06-08 00:20:42 +03:00
/>
2023-06-04 01:08:45 +03:00
</span>
2023-05-21 17:59:36 +03:00
</div>
2023-06-04 01:08:45 +03:00
<div
2025-08-23 20:11:10 +03:00
v-if="!isValueInput && !isSelectInput"
2023-10-06 03:21:14 +03:00
ref="touchTarget"
2024-06-12 20:05:55 +03:00
class="absolute top-0 bottom-0 right-0 left-0"
:class="isDragged ? 'cursor-grab' : 'cursor-pointer'"
2024-06-27 12:40:10 +03:00
@dblclick="maybeToggleDefaultValue"
2025-08-23 20:11:10 +03:00
@click="inputMode && maybeToggleCheckboxValue()"
2023-06-04 01:08:45 +03:00
/>
2023-05-21 17:59:36 +03:00
<span
2023-11-04 01:19:32 +02:00
v-if="field?.type && editable"
2024-12-08 22:34:52 +02:00
class="h-4 w-4 lg:h-2.5 lg:w-2.5 -right-1 rounded-full -bottom-1 border-gray-400 bg-white shadow-md border absolute cursor-nwse-resize"
2023-05-21 17:59:36 +03:00
@mousedown.stop="startResize"
2023-10-06 03:21:14 +03:00
@touchstart="startTouchResize"
2023-05-21 17:59:36 +03:00
/>
2024-05-05 16:57:04 +03:00
<Teleport
v-if="isShowFormulaModal"
:to="modalContainerEl"
>
<FormulaModal
:field="field"
:editable="editable && !defaultField"
2025-08-23 21:46:51 +03:00
:default-field="defaultField"
2024-05-05 16:57:04 +03:00
:build-default-name="buildDefaultName"
@close="isShowFormulaModal = false"
/>
</Teleport>
2025-03-09 02:08:44 +02:00
<Teleport
v-if="isShowFontModal"
:to="modalContainerEl"
>
<FontModal
:field="field"
:editable="editable && !defaultField"
2025-08-23 21:46:51 +03:00
:default-field="defaultField"
2025-03-09 02:08:44 +02:00
:build-default-name="buildDefaultName"
@close="isShowFontModal = false"
/>
</Teleport>
2024-05-05 16:57:04 +03:00
<Teleport
v-if="isShowConditionsModal"
:to="modalContainerEl"
>
<ConditionsModal
2024-12-07 01:35:59 +02:00
:item="field"
2024-05-05 16:57:04 +03:00
:build-default-name="buildDefaultName"
2025-08-23 21:46:51 +03:00
:default-field="defaultField"
2024-05-05 16:57:04 +03:00
@close="isShowConditionsModal = false"
/>
</Teleport>
<Teleport
v-if="isShowDescriptionModal"
:to="modalContainerEl"
>
<DescriptionModal
:field="field"
:editable="editable && !defaultField"
2025-08-23 21:46:51 +03:00
:default-field="defaultField"
2024-05-05 16:57:04 +03:00
:build-default-name="buildDefaultName"
@close="isShowDescriptionModal = false"
/>
</Teleport>
2023-05-21 17:59:36 +03:00
</div>
</template>
<script>
2023-06-07 01:16:43 +03:00
import FieldSubmitter from './field_submitter'
2023-06-04 01:08:45 +03:00
import FieldType from './field_type'
import Field from './field'
2024-05-05 16:57:04 +03:00
import FieldSettings from './field_settings'
import FormulaModal from './formula_modal'
2025-03-09 02:08:44 +02:00
import FontModal from './font_modal'
2024-05-05 16:57:04 +03:00
import ConditionsModal from './conditions_modal'
import DescriptionModal from './description_modal'
import { IconX, IconCheck, IconDotsVertical } from '@tabler/icons-vue'
2023-11-13 01:46:28 +02:00
import { v4 } from 'uuid'
2023-06-03 00:53:46 +03:00
2023-05-21 17:59:36 +03:00
export default {
name: 'FieldArea',
2023-06-04 01:08:45 +03:00
components: {
2023-06-07 01:16:43 +03:00
FieldType,
2024-02-05 23:03:45 +02:00
IconCheck,
2024-05-05 16:57:04 +03:00
FieldSettings,
FormulaModal,
2025-03-09 02:08:44 +02:00
FontModal,
2024-05-05 16:57:04 +03:00
IconDotsVertical,
DescriptionModal,
ConditionsModal,
2023-06-08 00:20:42 +03:00
FieldSubmitter,
IconX
2023-06-04 01:08:45 +03:00
},
2025-07-14 12:53:15 +03:00
inject: ['template', 'selectedAreaRef', 'save', 't', 'isInlineSize'],
2023-05-21 17:59:36 +03:00
props: {
2023-06-04 01:08:45 +03:00
area: {
2023-05-21 17:59:36 +03:00
type: Object,
2023-06-04 01:08:45 +03:00
required: true
2023-05-21 17:59:36 +03:00
},
2024-07-16 19:01:44 +03:00
inputMode: {
type: Boolean,
required: false,
default: false
},
2023-06-27 00:57:29 +03:00
isDraw: {
type: Boolean,
required: false,
default: false
},
2024-12-28 13:08:00 +02:00
maxPage: {
type: Number,
required: false,
default: null
},
2023-11-27 20:24:58 +02:00
defaultField: {
type: Object,
required: false,
default: null
},
2024-07-15 14:03:36 +03:00
withFieldPlaceholder: {
type: Boolean,
required: false,
default: false
},
2025-07-17 10:54:44 +03:00
withSignatureId: {
type: Boolean,
required: false,
default: null
},
2025-07-27 10:27:12 +03:00
withPrefillable: {
type: Boolean,
required: false,
default: false
},
2024-01-23 23:20:19 +02:00
defaultSubmitters: {
type: Array,
required: false,
default: () => []
},
2023-11-04 01:19:32 +02:00
editable: {
type: Boolean,
required: false,
default: true
},
2023-05-21 17:59:36 +03:00
field: {
type: Object,
required: false,
default: null
}
},
2024-05-05 16:57:04 +03:00
emits: ['start-resize', 'stop-resize', 'start-drag', 'stop-drag', 'remove', 'scroll-to'],
2023-05-21 17:59:36 +03:00
data () {
return {
2024-05-05 16:57:04 +03:00
isShowFormulaModal: false,
2025-03-09 02:08:44 +02:00
isShowFontModal: false,
2024-05-05 16:57:04 +03:00
isShowConditionsModal: false,
2024-06-12 20:05:55 +03:00
isContenteditable: false,
2024-05-05 16:57:04 +03:00
isSettingsFocus: false,
isShowDescriptionModal: false,
2023-05-21 17:59:36 +03:00
isResize: false,
2023-06-24 16:17:07 +03:00
isDragged: false,
2024-07-02 10:25:05 +03:00
isMoved: false,
2024-05-05 16:57:04 +03:00
renderDropdown: false,
2023-06-04 01:08:45 +03:00
isNameFocus: false,
2025-01-24 22:32:20 +02:00
isHeadingSelected: false,
2023-11-07 11:36:11 +02:00
textOverflowChars: 0,
2023-05-21 17:59:36 +03:00
dragFrom: { x: 0, y: 0 }
}
},
computed: {
2023-06-27 00:57:29 +03:00
fieldNames: FieldType.computed.fieldNames,
2024-10-02 17:57:31 +03:00
fieldLabels: FieldType.computed.fieldLabels,
2023-06-04 01:08:45 +03:00
fieldIcons: FieldType.computed.fieldIcons,
2025-08-23 20:11:10 +03:00
isWFullType () {
return ['cells', 'checkbox', 'radio', 'multiple', 'select'].includes(this.field.type)
},
2025-07-14 12:53:15 +03:00
fontStyle () {
let fontSize = ''
if (this.isInlineSize) {
if (this.textOverflowChars) {
fontSize = `${this.fontSizePx / 1.5 / 10}cqmin`
} else {
fontSize = `${this.fontSizePx / 10}cqmin`
}
} else {
if (this.textOverflowChars) {
fontSize = `clamp(1pt, ${this.fontSizePx / 1.5 / 10}vw, ${this.fontSizePx / 1.5}px)`
} else {
fontSize = `clamp(1pt, ${this.fontSizePx / 10}vw, ${this.fontSizePx}px)`
}
}
return { fontSize, lineHeight: `calc(${fontSize} * ${this.lineHeight})` }
},
2025-08-23 20:11:10 +03:00
optionsUuidIndex () {
return this.field.options.reduce((acc, option) => {
acc[option.uuid] = option
return acc
}, {})
},
2025-07-14 12:53:15 +03:00
fontSizePx () {
return parseInt(this.field?.preferences?.font_size || 11) * this.fontScale
},
lineHeight () {
return 1.3
},
fontScale () {
return 1040 / 612.0
},
2024-06-12 20:05:55 +03:00
isDefaultValuePresent () {
2025-08-23 20:11:10 +03:00
return this.field?.default_value || this.field?.default_value === 0
},
isSelectInput () {
return this.inputMode && (this.field.type === 'select' || (this.field.type === 'radio' && this.field.areas?.length < 2))
},
isCheckboxInput () {
return this.inputMode && (this.field.type === 'checkbox' || (['radio', 'multiple'].includes(this.field.type) && this.area.option_uuid))
2024-06-12 20:05:55 +03:00
},
2024-07-11 21:40:15 +03:00
isValueInput () {
2025-08-23 20:11:10 +03:00
return (this.field.type === 'heading' && this.isHeadingSelected) || this.isContenteditable ||
(this.inputMode && (['text', 'number'].includes(this.field.type) || (this.field.type === 'date' && this.field.default_value !== '{{date}}')))
2024-07-11 21:40:15 +03:00
},
2024-05-05 16:57:04 +03:00
modalContainerEl () {
return this.$el.getRootNode().querySelector('#docuseal_modal_container')
},
2024-02-24 01:01:36 +02:00
defaultName () {
return this.buildDefaultName(this.field, this.template.fields)
},
2025-03-05 12:12:16 +02:00
fontClasses () {
if (!this.field.preferences) {
2025-01-24 23:08:44 +02:00
return { 'items-center': true }
2025-03-05 12:12:16 +02:00
}
2024-03-17 19:06:00 +02:00
return {
2025-01-24 23:08:44 +02:00
'items-center': !this.field.preferences.valign || this.field.preferences.valign === 'center',
'items-start': this.field.preferences.valign === 'top',
'items-end': this.field.preferences.valign === 'bottom',
2025-03-05 12:12:16 +02:00
'justify-center': this.field.preferences.align === 'center',
'justify-start': this.field.preferences.align === 'left',
'justify-end': this.field.preferences.align === 'right',
2025-07-14 12:53:15 +03:00
'font-courier': this.field.preferences.font === 'Courier',
'font-times': this.field.preferences.font === 'Times',
2025-03-05 12:12:16 +02:00
'font-bold': ['bold_italic', 'bold'].includes(this.field.preferences.font_type),
italic: ['bold_italic', 'italic'].includes(this.field.preferences.font_type)
2024-03-17 19:06:00 +02:00
}
},
2023-11-14 01:30:00 +02:00
optionIndexText () {
if (this.area.option_uuid && this.field.options) {
return `${this.field.options.findIndex((o) => o.uuid === this.area.option_uuid) + 1}.`
} else {
return ''
}
},
2023-06-27 00:57:29 +03:00
cells () {
const cells = []
let currentWidth = 0
while (currentWidth + (this.area.cell_w + this.area.cell_w / 4) < this.area.w) {
currentWidth += this.area.cell_w || 9999999
cells.push(currentWidth)
}
return cells
},
2023-06-07 01:16:43 +03:00
submitter () {
return this.template.submitters.find((s) => s.uuid === this.field.submitter_uuid)
},
submitterIndex () {
return this.template.submitters.indexOf(this.submitter)
},
borderColors () {
return [
2024-02-08 11:54:54 +02:00
'border-red-500/80',
'border-sky-500/80',
'border-emerald-500/80',
'border-yellow-300/80',
'border-purple-600/80',
'border-pink-500/80',
'border-cyan-500/80',
2024-02-10 01:19:02 +02:00
'border-orange-500/80',
'border-lime-500/80',
'border-indigo-500/80'
2023-06-07 01:16:43 +03:00
]
},
bgColors () {
return [
2024-06-12 20:05:55 +03:00
'bg-red-100',
'bg-sky-100',
'bg-emerald-100',
'bg-yellow-100',
'bg-purple-100',
'bg-pink-100',
'bg-cyan-100',
'bg-orange-100',
'bg-lime-100',
'bg-indigo-100'
2023-06-07 01:16:43 +03:00
]
},
2023-06-04 01:08:45 +03:00
isSelected () {
return this.selectedAreaRef.value === this.area
2023-06-03 00:53:46 +03:00
},
2023-05-21 17:59:36 +03:00
positionStyle () {
2023-06-04 01:08:45 +03:00
const { x, y, w, h } = this.area
2023-05-21 17:59:36 +03:00
return {
2023-05-23 00:22:17 +03:00
top: y * 100 + '%',
left: x * 100 + '%',
width: w * 100 + '%',
height: h * 100 + '%'
2023-05-21 17:59:36 +03:00
}
}
},
2023-11-07 11:36:11 +02:00
watch: {
'field.default_value' () {
this.$nextTick(() => {
2024-03-17 17:04:39 +02:00
if (['date', 'text', 'number'].includes(this.field.type) && this.field.default_value && this.$refs.textContainer && (this.textOverflowChars === 0 || (this.textOverflowChars - 4) > `${this.field.default_value}`.length)) {
2025-07-14 12:53:15 +03:00
this.textOverflowChars = (this.$el.clientHeight + 1) < this.$refs.textContainer.clientHeight ? `${this.field.default_value}`.length : 0
2024-03-15 22:46:07 +02:00
}
2023-11-07 11:36:11 +02:00
})
}
},
2024-03-15 22:46:07 +02:00
mounted () {
this.$nextTick(() => {
2024-03-17 17:04:39 +02:00
if (['date', 'text', 'number'].includes(this.field.type) && this.field.default_value && this.$refs.textContainer && (this.textOverflowChars === 0 || (this.textOverflowChars - 4) > `${this.field.default_value}`.length)) {
2025-07-14 12:53:15 +03:00
this.textOverflowChars = (this.$el.clientHeight + 1) < this.$refs.textContainer.clientHeight ? `${this.field.default_value}`.length : 0
2024-03-15 22:46:07 +02:00
}
})
},
2023-05-21 17:59:36 +03:00
methods: {
2024-02-24 01:01:36 +02:00
buildDefaultName: Field.methods.buildDefaultName,
2024-05-05 16:57:04 +03:00
closeDropdown () {
2025-01-14 00:10:50 +02:00
this.$el.getRootNode().activeElement.blur()
2024-05-05 16:57:04 +03:00
},
2025-08-23 21:33:46 +03:00
buildAreaOptionValue (area) {
const option = this.optionsUuidIndex[area.option_uuid]
return option.value || `${this.t('option')} ${this.field.options.indexOf(option) + 1}`
},
2024-06-27 12:40:10 +03:00
maybeToggleDefaultValue () {
2025-03-04 19:46:55 +02:00
if (!this.editable) {
return
}
2024-06-12 20:05:55 +03:00
if (['text', 'number'].includes(this.field.type)) {
this.isContenteditable = true
2025-01-24 22:32:20 +02:00
this.focusValueInput()
2024-06-27 12:40:10 +03:00
} else if (this.field.type === 'date') {
this.field.readonly = !this.field.readonly
this.field.default_value === '{{date}}' ? delete this.field.default_value : this.field.default_value = '{{date}}'
this.save()
2025-08-23 20:11:10 +03:00
} else {
this.maybeToggleCheckboxValue()
2024-06-12 20:05:55 +03:00
}
},
2024-07-16 19:01:44 +03:00
maybeToggleCheckboxValue () {
2025-08-23 20:11:10 +03:00
if (this.field.type === 'checkbox') {
2024-07-16 19:01:44 +03:00
this.field.default_value === true ? delete this.field.default_value : this.field.default_value = true
2025-08-23 20:11:10 +03:00
this.field.readonly = this.field.default_value === true
this.save()
} else if (this.field.type === 'radio' && this.area.option_uuid) {
2025-08-23 21:33:46 +03:00
const value = this.buildAreaOptionValue(this.area)
2025-08-23 20:11:10 +03:00
this.field.default_value === value ? delete this.field.default_value : this.field.default_value = value
this.field.readonly = !!this.field.default_value?.length
this.save()
} else if (this.field.type === 'multiple' && this.area.option_uuid) {
2025-08-23 21:33:46 +03:00
const value = this.buildAreaOptionValue(this.area)
2025-08-23 20:11:10 +03:00
if (this.field.default_value?.includes(value)) {
this.field.default_value.splice(this.field.default_value.indexOf(value), 1)
if (!this.field.default_value?.length) delete this.field.default_value
} else {
Array.isArray(this.field.default_value) ? this.field.default_value.push(value) : this.field.default_value = [value]
}
this.field.readonly = !!this.field.default_value?.length
2024-07-16 19:01:44 +03:00
this.save()
}
},
focusValueInput (e) {
2025-01-24 22:32:20 +02:00
this.$nextTick(() => {
if (this.$refs.defaultValue && this.$refs.defaultValue !== document.activeElement) {
this.$refs.defaultValue.focus()
if (this.$refs.defaultValue.innerText.length && this.$refs.defaultValue !== e?.target) {
window.getSelection().collapse(
this.$refs.defaultValue.firstChild,
this.$refs.defaultValue.innerText.length
)
}
2024-07-11 21:40:15 +03:00
}
2025-01-24 22:32:20 +02:00
})
2024-07-11 21:40:15 +03:00
},
2024-05-12 11:12:07 +03:00
formatNumber (number, format) {
if (format === 'comma') {
return new Intl.NumberFormat('en-US').format(number)
2025-01-17 21:08:13 +02:00
} else if (format === 'usd') {
return new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', minimumFractionDigits: 2, maximumFractionDigits: 2 }).format(number)
} else if (format === 'gbp') {
return new Intl.NumberFormat('en-GB', { style: 'currency', currency: 'GBP', minimumFractionDigits: 2, maximumFractionDigits: 2 }).format(number)
} else if (format === 'eur') {
return new Intl.NumberFormat('fr-FR', { style: 'currency', currency: 'EUR', minimumFractionDigits: 2, maximumFractionDigits: 2 }).format(number)
2024-05-12 11:12:07 +03:00
} else if (format === 'dot') {
return new Intl.NumberFormat('de-DE').format(number)
} else if (format === 'space') {
return new Intl.NumberFormat('fr-FR').format(number)
} else {
return number
}
},
2024-05-05 16:57:04 +03:00
maybeBlurSettings (e) {
if (!e.relatedTarget || !this.$refs.settingsDropdown.contains(e.relatedTarget)) {
this.isSettingsFocus = false
}
},
2023-06-04 01:08:45 +03:00
onNameFocus (e) {
this.selectedAreaRef.value = this.area
this.isNameFocus = true
this.$refs.name.style.minWidth = this.$refs.name.clientWidth + 'px'
if (!this.field.name) {
setTimeout(() => {
this.$refs.name.innerText = ' '
}, 1)
}
},
2023-06-27 00:57:29 +03:00
startResizeCell (e) {
2023-08-19 21:05:54 +03:00
this.$el.getRootNode().addEventListener('mousemove', this.onResizeCell)
this.$el.getRootNode().addEventListener('mouseup', this.stopResizeCell)
2023-06-27 00:57:29 +03:00
this.$emit('start-resize', 'ew')
},
stopResizeCell (e) {
2023-08-19 21:05:54 +03:00
this.$el.getRootNode().removeEventListener('mousemove', this.onResizeCell)
this.$el.getRootNode().removeEventListener('mouseup', this.stopResizeCell)
2023-06-27 00:57:29 +03:00
this.$emit('stop-resize')
this.save()
},
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()
}
2024-06-13 10:26:36 +03:00
},
2023-06-27 00:57:29 +03:00
onResizeCell (e) {
2023-06-30 23:22:57 +03:00
if (e.target.id === 'mask') {
2024-03-05 20:53:54 +02:00
const positionX = e.offsetX / (e.target.clientWidth - 1)
2023-06-27 00:57:29 +03:00
if (positionX > this.area.x) {
this.area.cell_w = positionX - this.area.x
}
}
},
2023-06-08 00:20:42 +03:00
maybeUpdateOptions () {
2023-11-05 01:41:54 +02:00
delete this.field.default_value
2023-06-16 00:04:05 +03:00
if (!['radio', 'multiple', 'select'].includes(this.field.type)) {
2023-06-07 01:16:43 +03:00
delete this.field.options
}
2023-06-08 00:20:42 +03:00
2024-07-11 21:40:15 +03:00
if (['heading'].includes(this.field.type)) {
this.field.readonly = true
}
2023-06-16 00:04:05 +03:00
if (['select', 'multiple', 'radio'].includes(this.field.type)) {
2023-11-13 01:46:28 +02:00
this.field.options ||= [{ value: '', uuid: v4() }]
2023-06-08 00:20:42 +03:00
}
2023-06-27 00:57:29 +03:00
(this.field.areas || []).forEach((area) => {
if (this.field.type === 'cells') {
area.cell_w = area.w * 2 / Math.floor(area.w / area.h)
} else {
delete area.cell_w
}
})
2023-06-07 01:16:43 +03:00
},
2023-06-04 01:08:45 +03:00
onNameBlur (e) {
2024-05-05 16:57:04 +03:00
if (e.relatedTarget === this.$refs.settingsButton) {
this.isSettingsFocus = true
}
2023-08-19 21:05:54 +03:00
const text = this.$refs.name.innerText.trim()
2023-06-04 01:08:45 +03:00
this.isNameFocus = false
this.$refs.name.style.minWidth = ''
2023-08-19 21:05:54 +03:00
if (text) {
this.field.name = text
2023-06-04 01:08:45 +03:00
} else {
this.field.name = ''
this.$refs.name.innerText = this.defaultName
}
2023-06-24 16:17:07 +03:00
this.save()
2023-06-04 01:08:45 +03:00
},
2024-06-12 20:05:55 +03:00
onDefaultValueBlur (e) {
const text = this.$refs.defaultValue.innerText.trim()
this.isContenteditable = false
2025-01-24 22:32:20 +02:00
this.isHeadingSelected = false
2024-06-12 20:05:55 +03:00
if (text) {
if (this.field.type === 'number') {
const number = parseFloat(text)
if (number || number === 0) {
this.field.default_value = parseFloat(text)
}
} else {
this.field.default_value = text
}
if (![true, false].includes(this.field.readonly)) {
this.field.readonly = true
}
this.$refs.defaultValue.innerText = text
} else {
delete this.field.readonly
delete this.field.default_value
this.$refs.defaultValue.innerText = ''
}
this.save()
},
onDefaultValueEnter (e) {
2024-07-11 21:40:15 +03:00
if (this.field.type !== 'heading') {
e.preventDefault()
this.$refs.defaultValue.blur()
}
2024-06-12 20:05:55 +03:00
},
2023-06-04 01:08:45 +03:00
onNameEnter (e) {
this.$refs.name.blur()
2023-06-03 00:53:46 +03:00
},
2023-05-21 17:59:36 +03:00
resize (e) {
2023-06-30 23:22:57 +03:00
if (e.target.id === 'mask') {
2024-03-05 20:53:54 +02:00
this.area.w = e.offsetX / e.target.clientWidth - this.area.x
this.area.h = e.offsetY / e.target.clientHeight - this.area.y
2023-06-03 00:53:46 +03:00
}
2023-05-21 17:59:36 +03:00
},
drag (e) {
2025-08-23 20:11:10 +03:00
if (e.target.id === 'mask' && this.editable) {
2023-06-24 16:17:07 +03:00
this.isDragged = true
2024-03-05 20:53:54 +02:00
this.area.x = (e.offsetX - this.dragFrom.x) / e.target.clientWidth
this.area.y = (e.offsetY - this.dragFrom.y) / e.target.clientHeight
2023-05-21 17:59:36 +03:00
}
},
2023-10-06 03:21:14 +03:00
startTouchDrag (e) {
2024-07-11 21:40:15 +03:00
if (e.target !== this.$refs.touchTarget && e.target !== this.$refs.touchValueTarget) {
2023-10-06 03:21:14 +03:00
return
}
2024-06-13 10:54:07 +03:00
document.activeElement?.blur()
2023-10-06 03:21:14 +03:00
e.preventDefault()
2025-08-23 20:11:10 +03:00
if (this.editable) {
this.isDragged = true
}
2023-10-06 03:21:14 +03:00
const rect = e.target.getBoundingClientRect()
this.selectedAreaRef.value = this.area
this.dragFrom = { x: rect.left - e.touches[0].clientX, y: rect.top - e.touches[0].clientY }
this.$el.getRootNode().addEventListener('touchmove', this.touchDrag)
this.$el.getRootNode().addEventListener('touchend', this.stopTouchDrag)
this.$emit('start-drag')
},
touchDrag (e) {
2024-07-02 13:17:39 +03:00
if (!this.editable) {
return
}
2023-10-06 03:21:14 +03:00
const page = this.$parent.$refs.mask.previousSibling
const rect = page.getBoundingClientRect()
2024-06-12 20:05:55 +03:00
this.area.x = Math.min(Math.max((this.dragFrom.x + e.touches[0].clientX - rect.left) / rect.width, 0), 1 - this.area.w)
2024-12-28 13:08:00 +02:00
this.area.y = (this.dragFrom.y + e.touches[0].clientY - rect.top) / rect.height
if ((this.area.page === 0 && this.area.y < 0) || (this.area.page === this.maxPage && this.area.y > 1 - this.area.h)) {
this.area.y = Math.min(Math.max(this.area.y, 0), 1 - this.area.h)
}
2023-10-06 03:21:14 +03:00
},
stopTouchDrag () {
this.$el.getRootNode().removeEventListener('touchmove', this.touchDrag)
this.$el.getRootNode().removeEventListener('touchend', this.stopTouchDrag)
2024-12-28 13:08:00 +02:00
this.maybeChangeAreaPage(this.area)
2023-10-06 03:21:14 +03:00
if (this.isDragged) {
this.save()
}
this.isDragged = false
this.$emit('stop-drag')
},
2024-06-12 20:05:55 +03:00
startMouseMove (e) {
2024-07-11 21:40:15 +03:00
if (e.target !== this.$refs.touchTarget && e.target !== this.$refs.touchValueTarget) {
2024-06-12 20:05:55 +03:00
return
}
2024-07-11 21:40:15 +03:00
if (document.activeElement !== this.$refs.defaultValue) {
document.activeElement?.blur()
}
2024-06-12 20:05:55 +03:00
e.preventDefault()
2025-08-23 20:11:10 +03:00
if (this.editable) {
this.isDragged = true
}
2024-06-12 20:05:55 +03:00
const rect = e.target.getBoundingClientRect()
this.selectedAreaRef.value = this.area
this.dragFrom = { x: rect.left - e.clientX, y: rect.top - e.clientY }
this.$el.getRootNode().addEventListener('mousemove', this.mouseMove)
this.$el.getRootNode().addEventListener('mouseup', this.stopMouseMove)
this.$emit('start-drag')
},
mouseMove (e) {
2024-07-02 13:17:39 +03:00
if (!this.editable) {
return
}
2024-07-02 10:25:05 +03:00
this.isMoved = true
2024-06-12 20:05:55 +03:00
const page = this.$parent.$refs.mask.previousSibling
const rect = page.getBoundingClientRect()
this.area.x = Math.min(Math.max((this.dragFrom.x + e.clientX - rect.left) / rect.width, 0), 1 - this.area.w)
2024-12-28 13:08:00 +02:00
this.area.y = (this.dragFrom.y + e.clientY - rect.top) / rect.height
if ((this.area.page === 0 && this.area.y < 0) || (this.area.page === this.maxPage && this.area.y > 1 - this.area.h)) {
this.area.y = Math.min(Math.max(this.area.y, 0), 1 - this.area.h)
}
2024-06-12 20:05:55 +03:00
},
stopMouseMove (e) {
this.$el.getRootNode().removeEventListener('mousemove', this.mouseMove)
this.$el.getRootNode().removeEventListener('mouseup', this.stopMouseMove)
2024-12-28 13:08:00 +02:00
this.maybeChangeAreaPage(this.area)
2024-07-02 10:25:05 +03:00
if (this.isMoved) {
2024-06-12 20:05:55 +03:00
this.save()
}
2025-01-24 22:32:20 +02:00
if (this.field.type === 'heading') {
this.isHeadingSelected = !this.isMoved
this.focusValueInput()
}
2024-06-12 20:05:55 +03:00
this.isDragged = false
2024-07-02 10:25:05 +03:00
this.isMoved = false
2024-06-12 20:05:55 +03:00
this.$emit('stop-drag')
},
2024-12-28 13:08:00 +02:00
maybeChangeAreaPage (area) {
if (area.y < -(area.h / 2)) {
area.page -= 1
area.y = 1 + area.y + (16.0 / this.$parent.$refs.mask.previousSibling.offsetHeight)
} else if (area.y > 1 - (area.h / 2)) {
area.page += 1
area.y = area.y - 1 - (16.0 / this.$parent.$refs.mask.previousSibling.offsetHeight)
}
},
2023-05-21 17:59:36 +03:00
stopDrag () {
2023-08-19 21:05:54 +03:00
this.$el.getRootNode().removeEventListener('mousemove', this.drag)
this.$el.getRootNode().removeEventListener('mouseup', this.stopDrag)
2023-05-21 17:59:36 +03:00
2023-06-24 16:17:07 +03:00
if (this.isDragged) {
this.save()
}
this.isDragged = false
2023-05-21 17:59:36 +03:00
this.$emit('stop-drag')
},
startResize () {
2023-06-04 01:08:45 +03:00
this.selectedAreaRef.value = this.area
2023-08-19 21:05:54 +03:00
this.$el.getRootNode().addEventListener('mousemove', this.resize)
this.$el.getRootNode().addEventListener('mouseup', this.stopResize)
2023-05-21 17:59:36 +03:00
2023-06-27 00:57:29 +03:00
this.$emit('start-resize', 'nwse')
2023-05-21 17:59:36 +03:00
},
stopResize () {
2023-08-19 21:05:54 +03:00
this.$el.getRootNode().removeEventListener('mousemove', this.resize)
this.$el.getRootNode().removeEventListener('mouseup', this.stopResize)
2023-05-21 17:59:36 +03:00
this.$emit('stop-resize')
2023-06-24 16:17:07 +03:00
2023-10-06 03:21:14 +03:00
this.save()
},
startTouchResize (e) {
this.selectedAreaRef.value = this.area
this.$refs?.name?.blur()
e.preventDefault()
this.$el.getRootNode().addEventListener('touchmove', this.touchResize)
this.$el.getRootNode().addEventListener('touchend', this.stopTouchResize)
this.$emit('start-resize', 'nwse')
},
touchResize (e) {
const page = this.$parent.$refs.mask.previousSibling
const rect = page.getBoundingClientRect()
this.area.w = (e.touches[0].clientX - rect.left) / rect.width - this.area.x
this.area.h = (e.touches[0].clientY - rect.top) / rect.height - this.area.y
},
stopTouchResize () {
this.$el.getRootNode().removeEventListener('touchmove', this.touchResize)
this.$el.getRootNode().removeEventListener('touchend', this.stopTouchResize)
this.$emit('stop-resize')
2023-06-24 16:17:07 +03:00
this.save()
2023-05-21 17:59:36 +03:00
}
}
}
</script>