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

488 lines
14 KiB
Vue
Raw Normal View History

2023-05-21 17:59:36 +03:00
<template>
<div
2023-05-31 00:50:43 +03:00
style="max-width: 1600px"
2023-06-03 00:53:46 +03:00
class="mx-auto pl-4"
2023-05-21 17:59:36 +03:00
>
2023-06-03 00:53:46 +03:00
<div class="flex justify-between py-1.5 items-center pr-4">
2023-06-01 00:46:50 +03:00
<div class="flex space-x-3">
<a href="/">
<Logo />
</a>
<Contenteditable
:model-value="template.name"
class="text-3xl font-semibold focus:text-clip"
2023-06-04 01:08:45 +03:00
:icon-stroke-width="2.3"
2023-06-01 00:46:50 +03:00
@update:model-value="updateName"
/>
</div>
2023-05-31 00:50:43 +03:00
<div class="space-x-3 flex items-center">
<a
2023-06-24 16:17:07 +03:00
:href="`/templates/${template.id}/submissions/new`"
data-turbo-frame="modal"
2023-05-31 00:50:43 +03:00
class="btn btn-primary"
>
<IconUsersPlus
width="20"
2023-06-24 16:17:07 +03:00
class="inline"
2023-05-31 00:50:43 +03:00
/>
2023-07-02 00:14:26 +03:00
<span class="hidden md:inline">
Recipients
</span>
2023-05-31 00:50:43 +03:00
</a>
2023-06-24 16:17:07 +03:00
<button
2023-05-31 00:50:43 +03:00
class="base-button"
2023-06-24 16:17:07 +03:00
:class="{ disabled: isSaving }"
2023-05-31 00:50:43 +03:00
v-bind="isSaving ? { disabled: true } : {}"
@click.prevent="onSaveClick"
2023-06-24 16:17:07 +03:00
>
<IconInnerShadowTop
v-if="isSaving"
width="20"
class="animate-spin"
/>
<IconDeviceFloppy
v-else
width="20"
2023-07-02 00:14:26 +03:00
/>
<span class="hidden md:inline">
Save
</span>
2023-06-24 16:17:07 +03:00
</button>
2023-05-31 00:50:43 +03:00
</div>
2023-05-21 17:59:36 +03:00
</div>
<div
2023-05-31 00:50:43 +03:00
class="flex"
style="max-height: calc(100vh - 60px)"
2023-05-21 17:59:36 +03:00
>
<div
2023-05-31 00:50:43 +03:00
ref="previews"
2023-07-02 00:14:26 +03:00
class="overflow-y-auto overflow-x-hidden w-52 flex-none pr-3 mt-0.5 pt-0.5 hidden lg:block"
2023-05-21 17:59:36 +03:00
>
2023-05-31 00:50:43 +03:00
<DocumentPreview
2023-05-31 23:19:20 +03:00
v-for="(item, index) in template.schema"
2023-05-31 00:50:43 +03:00
:key="index"
2023-05-31 23:19:20 +03:00
:with-arrows="template.schema.length > 1"
2023-05-31 00:50:43 +03:00
:item="item"
:document="sortedDocuments[index]"
2023-07-26 20:35:53 +03:00
:template="template"
:is-direct-upload="isDirectUpload"
2023-05-31 00:50:43 +03:00
@scroll-to="scrollIntoDocument(item)"
@remove="onDocumentRemove"
2023-07-26 20:35:53 +03:00
@replace="onDocumentReplace"
2023-05-31 00:50:43 +03:00
@up="moveDocument(item, -1)"
@down="moveDocument(item, 1)"
@change="save"
2023-05-21 17:59:36 +03:00
/>
2023-05-31 00:50:43 +03:00
<div class="sticky bottom-0 bg-base-100 py-2">
<Upload
2023-07-01 00:15:15 +03:00
v-if="sortedDocuments.length"
2023-05-31 23:19:20 +03:00
:template-id="template.id"
:is-direct-upload="isDirectUpload"
2023-05-31 00:50:43 +03:00
@success="updateFromUpload"
/>
</div>
</div>
<div class="w-full overflow-y-auto overflow-x-hidden mt-0.5 pt-0.5">
2023-06-04 01:08:45 +03:00
<div
ref="documents"
class="pr-3.5 pl-0.5"
>
<Dropzone
v-if="!sortedDocuments.length"
:template-id="template.id"
:is-direct-upload="isDirectUpload"
@success="updateFromUpload"
/>
2023-07-01 00:15:15 +03:00
<template v-else>
<Document
v-for="document in sortedDocuments"
:key="document.uuid"
:ref="setDocumentRefs"
:areas-index="fieldAreasIndex[document.uuid]"
:selected-submitter="selectedSubmitter"
:document="document"
:is-drag="!!dragFieldType"
:draw-field="drawField"
@draw="onDraw"
@drop-field="onDropfield"
@remove-area="removeArea"
/>
</template>
2023-05-31 00:50:43 +03:00
</div>
2023-07-02 00:14:26 +03:00
<div
v-if="sortedDocuments.length"
class="sticky md:hidden"
style="bottom: 100px"
>
<div class="px-4 py-3 rounded-2xl bg-base-200 flex items-center justify-between ml-4 mr-6">
<span class="w-full text-center text-lg">
You need a larger screen to use builder tools.
</span>
</div>
</div>
2023-05-31 00:50:43 +03:00
</div>
<div
2023-07-02 00:14:26 +03:00
class="relative w-80 flex-none pt-0.5 pr-4 pl-0.5 hidden md:block"
2023-05-31 00:50:43 +03:00
:class="drawField ? 'overflow-hidden' : 'overflow-auto'"
>
<div
v-if="drawField"
2023-06-03 00:53:46 +03:00
class="sticky inset-0 bg-base-100 h-full"
2023-05-31 00:50:43 +03:00
>
2023-06-08 00:20:42 +03:00
<div class="bg-base-300 rounded-lg p-5 text-center space-y-4">
<p>
Draw {{ drawField.name }} field on the document
</p>
<p>
<button
class="base-button"
@click="drawField = false"
>
Cancel
</button>
</p>
</div>
2023-05-31 00:50:43 +03:00
</div>
<div>
<Fields
ref="fields"
2023-06-07 01:16:43 +03:00
:fields="template.fields"
:submitters="template.submitters"
:selected-submitter="selectedSubmitter"
2023-05-31 00:50:43 +03:00
@set-draw="drawField = $event"
@set-drag="dragFieldType = $event"
2023-06-07 01:16:43 +03:00
@change-submitter="selectedSubmitter = $event"
2023-05-31 00:50:43 +03:00
@drag-end="dragFieldType = null"
2023-06-03 00:53:46 +03:00
@scroll-to-area="scrollToArea"
2023-05-31 00:50:43 +03:00
/>
</div>
2023-05-21 17:59:36 +03:00
</div>
</div>
</div>
</template>
<script>
import Upload from './upload'
2023-07-01 00:15:15 +03:00
import Dropzone from './dropzone'
2023-05-21 17:59:36 +03:00
import Fields from './fields'
import Document from './document'
2023-06-01 00:46:50 +03:00
import Logo from './logo'
2023-05-31 00:50:43 +03:00
import Contenteditable from './contenteditable'
import DocumentPreview from './preview'
2023-06-24 16:17:07 +03:00
import { IconUsersPlus, IconDeviceFloppy, IconInnerShadowTop } from '@tabler/icons-vue'
2023-06-03 00:53:46 +03:00
import { v4 } from 'uuid'
2023-06-04 01:08:45 +03:00
import { ref, computed } from 'vue'
2023-05-21 17:59:36 +03:00
export default {
2023-05-31 23:19:20 +03:00
name: 'TemplateBuilder',
2023-05-21 17:59:36 +03:00
components: {
Upload,
Document,
2023-05-31 00:50:43 +03:00
Fields,
2023-06-01 00:46:50 +03:00
Logo,
2023-07-01 00:15:15 +03:00
Dropzone,
2023-05-31 00:50:43 +03:00
DocumentPreview,
2023-06-24 16:17:07 +03:00
IconInnerShadowTop,
2023-05-31 00:50:43 +03:00
Contenteditable,
IconUsersPlus,
IconDeviceFloppy
2023-05-21 17:59:36 +03:00
},
2023-06-04 01:08:45 +03:00
provide () {
return {
template: this.template,
2023-06-24 16:17:07 +03:00
save: this.save,
2023-06-09 01:43:18 +03:00
selectedAreaRef: computed(() => this.selectedAreaRef)
2023-06-04 01:08:45 +03:00
}
},
2023-05-21 17:59:36 +03:00
props: {
2023-05-31 23:19:20 +03:00
template: {
2023-05-25 22:47:52 +03:00
type: Object,
required: true
},
isDirectUpload: {
type: Boolean,
required: true,
default: false
2023-05-21 17:59:36 +03:00
}
},
data () {
return {
2023-05-31 00:50:43 +03:00
documentRefs: [],
isSaving: false,
2023-06-07 01:16:43 +03:00
selectedSubmitter: null,
2023-05-21 17:59:36 +03:00
drawField: null,
2023-05-25 22:47:52 +03:00
dragFieldType: null
2023-05-21 17:59:36 +03:00
}
},
computed: {
2023-06-09 01:43:18 +03:00
selectedAreaRef: () => ref(),
2023-05-21 17:59:36 +03:00
fieldAreasIndex () {
const areas = {}
2023-05-31 23:19:20 +03:00
this.template.fields.forEach((f) => {
2023-05-21 17:59:36 +03:00
(f.areas || []).forEach((a) => {
areas[a.attachment_uuid] ||= {}
const acc = (areas[a.attachment_uuid][a.page] ||= [])
acc.push({ area: a, field: f })
})
})
return areas
},
2023-06-04 01:08:45 +03:00
selectedField () {
2023-06-09 01:43:18 +03:00
return this.template.fields.find((f) => f.areas?.includes(this.selectedAreaRef.value))
2023-06-04 01:08:45 +03:00
},
2023-05-21 17:59:36 +03:00
sortedDocuments () {
2023-05-31 23:19:20 +03:00
return this.template.schema.map((item) => {
return this.template.documents.find(doc => doc.uuid === item.attachment_uuid)
2023-05-21 17:59:36 +03:00
})
}
},
2023-06-07 01:16:43 +03:00
created () {
this.selectedSubmitter = this.template.submitters[0]
},
2023-05-21 17:59:36 +03:00
mounted () {
2023-06-07 01:16:43 +03:00
document.addEventListener('keyup', this.onKeyUp)
2023-05-21 17:59:36 +03:00
},
unmounted () {
2023-06-07 01:16:43 +03:00
document.removeEventListener('keyup', this.onKeyUp)
2023-05-21 17:59:36 +03:00
},
2023-05-31 00:50:43 +03:00
beforeUpdate () {
this.documentRefs = []
},
2023-05-21 17:59:36 +03:00
methods: {
2023-05-31 00:50:43 +03:00
setDocumentRefs (el) {
if (el) {
this.documentRefs.push(el)
}
},
scrollIntoDocument (item) {
const ref = this.documentRefs.find((e) => e.document.uuid === item.attachment_uuid)
ref.$el.scrollIntoView({ behavior: 'smooth', block: 'start' })
},
2023-06-07 01:16:43 +03:00
onKeyUp (e) {
2023-05-21 17:59:36 +03:00
if (e.code === 'Escape') {
this.drawField = null
2023-06-09 01:43:18 +03:00
this.selectedAreaRef.value = null
2023-06-07 01:16:43 +03:00
}
2023-06-09 01:43:18 +03:00
if (['Backspace', 'Delete'].includes(e.key) && this.selectedAreaRef.value && document.activeElement === document.body) {
this.removeArea(this.selectedAreaRef.value)
2023-06-07 01:16:43 +03:00
2023-06-09 01:43:18 +03:00
this.selectedAreaRef.value = null
2023-05-21 17:59:36 +03:00
}
},
2023-06-09 01:43:18 +03:00
removeArea (area) {
2023-06-07 01:16:43 +03:00
const field = this.template.fields.find((f) => f.areas?.includes(area))
2023-06-04 01:08:45 +03:00
field.areas.splice(field.areas.indexOf(area), 1)
if (!field.areas.length) {
this.template.fields.splice(this.template.fields.indexOf(field), 1)
}
2023-06-24 16:17:07 +03:00
this.save()
2023-06-04 01:08:45 +03:00
},
2023-05-21 17:59:36 +03:00
onDraw (area) {
2023-06-03 00:53:46 +03:00
if (this.drawField) {
this.drawField.areas ||= []
this.drawField.areas.push(area)
2023-05-21 17:59:36 +03:00
2023-06-03 00:53:46 +03:00
this.drawField = null
} else {
2023-06-04 01:08:45 +03:00
const documentRef = this.documentRefs.find((e) => e.document.uuid === area.attachment_uuid)
const pageMask = documentRef.pageRefs[area.page].$refs.mask
const type = (pageMask.clientWidth * area.w) < 35 ? 'checkbox' : 'text'
if (type === 'checkbox') {
2023-06-08 00:20:42 +03:00
const previousField = [...this.template.fields].reverse().find((f) => f.type === type)
const previousArea = previousField?.areas?.[previousField.areas.length - 1]
const areaW = previousArea?.w || (30 / pageMask.clientWidth)
const areaH = previousArea?.h || (30 / pageMask.clientHeight)
2023-06-04 01:08:45 +03:00
if ((pageMask.clientWidth * area.w) < 5) {
2023-06-08 00:20:42 +03:00
area.x = area.x - (areaW / 2)
area.y = area.y - (areaH / 2)
2023-06-04 01:08:45 +03:00
}
2023-06-08 00:20:42 +03:00
area.w = areaW
area.h = areaH
2023-06-04 01:08:45 +03:00
}
2023-06-03 00:53:46 +03:00
const field = {
name: '',
uuid: v4(),
2023-07-22 20:00:28 +03:00
required: type !== 'checkbox',
2023-06-04 01:08:45 +03:00
type,
2023-06-07 01:16:43 +03:00
submitter_uuid: this.selectedSubmitter.uuid,
2023-06-03 00:53:46 +03:00
areas: [area]
}
this.template.fields.push(field)
}
2023-06-07 01:16:43 +03:00
2023-06-09 01:43:18 +03:00
this.selectedAreaRef.value = area
2023-06-24 16:17:07 +03:00
this.save()
2023-05-21 17:59:36 +03:00
},
2023-05-23 00:03:26 +03:00
onDropfield (area) {
2023-06-04 01:08:45 +03:00
const field = {
name: '',
type: this.dragFieldType,
uuid: v4(),
2023-06-07 01:16:43 +03:00
submitter_uuid: this.selectedSubmitter.uuid,
2023-07-22 20:00:28 +03:00
required: this.dragFieldType !== 'checkbox'
2023-06-04 01:08:45 +03:00
}
2023-06-16 00:04:05 +03:00
if (['select', 'multiple', 'radio'].includes(this.dragFieldType)) {
2023-06-15 01:05:04 +03:00
field.options = ['']
}
2023-06-04 01:08:45 +03:00
const fieldArea = {
x: (area.x - 6) / area.maskW,
y: area.y / area.maskH,
page: area.page,
attachment_uuid: area.attachment_uuid
}
const previousField = [...this.template.fields].reverse().find((f) => f.type === field.type)
let baseArea
if (this.selectedField?.type === this.dragFieldType) {
2023-06-09 01:43:18 +03:00
baseArea = this.selectedAreaRef.value
2023-06-07 01:16:43 +03:00
} else if (previousField?.areas) {
2023-06-04 01:08:45 +03:00
baseArea = previousField.areas[previousField.areas.length - 1]
} else {
2023-06-16 00:04:05 +03:00
if (['checkbox'].includes(this.dragFieldType)) {
2023-06-04 01:08:45 +03:00
baseArea = {
w: area.maskW / 30 / area.maskW,
h: area.maskW / 30 / area.maskW * (area.maskW / area.maskH)
}
} else if (this.dragFieldType === 'image') {
baseArea = {
w: area.maskW / 5 / area.maskW,
h: (area.maskW / 5 / area.maskW) * (area.maskW / area.maskH)
}
2023-06-20 00:24:39 +03:00
} else if (this.dragFieldType === 'signature') {
baseArea = {
w: area.maskW / 5 / area.maskW,
h: (area.maskW / 5 / area.maskW) * (area.maskW / area.maskH) / 2
}
2023-06-04 01:08:45 +03:00
} else {
baseArea = {
w: area.maskW / 5 / area.maskW,
h: area.maskW / 35 / area.maskW
}
}
}
fieldArea.w = baseArea.w
fieldArea.h = baseArea.h
fieldArea.y = fieldArea.y - baseArea.h / 2
2023-06-27 00:57:29 +03:00
if (this.dragFieldType === 'cells') {
fieldArea.cell_w = baseArea.cell_w || (baseArea.w / 5)
}
2023-06-04 01:08:45 +03:00
field.areas = [fieldArea]
2023-06-09 01:43:18 +03:00
this.selectedAreaRef.value = fieldArea
2023-06-04 01:08:45 +03:00
this.template.fields.push(field)
2023-06-24 16:17:07 +03:00
this.save()
2023-05-23 00:03:26 +03:00
},
2023-05-21 17:59:36 +03:00
updateFromUpload ({ schema, documents }) {
2023-05-31 23:19:20 +03:00
this.template.schema.push(...schema)
this.template.documents.push(...documents)
2023-05-21 17:59:36 +03:00
2023-05-31 00:50:43 +03:00
this.$nextTick(() => {
this.$refs.previews.scrollTop = this.$refs.previews.scrollHeight
this.scrollIntoDocument(schema[0])
})
this.save()
},
updateName (value) {
2023-05-31 23:19:20 +03:00
this.template.name = value
2023-05-31 00:50:43 +03:00
this.save()
},
onDocumentRemove (item) {
if (window.confirm('Are you sure?')) {
2023-05-31 23:19:20 +03:00
this.template.schema.splice(this.template.schema.indexOf(item), 1)
2023-05-31 00:50:43 +03:00
}
2023-05-21 17:59:36 +03:00
this.save()
},
2023-07-26 20:35:53 +03:00
onDocumentReplace ({ replaceSchemaItem, schema, documents }) {
this.template.schema.splice(this.template.schema.indexOf(replaceSchemaItem), 1, schema[0])
this.template.documents.push(...documents)
this.template.fields.forEach((field) => {
field.areas.forEach((area) => {
if (area.attachment_uuid === replaceSchemaItem.attachment_uuid) {
area.attachment_uuid = schema[0].attachment_uuid
}
})
})
this.save()
},
2023-05-31 00:50:43 +03:00
moveDocument (item, direction) {
2023-05-31 23:19:20 +03:00
const currentIndex = this.template.schema.indexOf(item)
2023-05-31 00:50:43 +03:00
2023-05-31 23:19:20 +03:00
this.template.schema.splice(currentIndex, 1)
2023-05-31 00:50:43 +03:00
2023-05-31 23:19:20 +03:00
if (currentIndex + direction > this.template.schema.length) {
this.template.schema.unshift(item)
2023-05-31 00:50:43 +03:00
} else if (currentIndex + direction < 0) {
2023-05-31 23:19:20 +03:00
this.template.schema.push(item)
2023-05-31 00:50:43 +03:00
} else {
2023-05-31 23:19:20 +03:00
this.template.schema.splice(currentIndex + direction, 0, item)
2023-05-31 00:50:43 +03:00
}
this.save()
},
onSaveClick () {
this.isSaving = true
this.save().then(() => {
2023-06-28 00:22:34 +03:00
window.Turbo.visit(`/templates/${this.template.id}`)
2023-05-31 00:50:43 +03:00
}).finally(() => {
this.isSaving = false
})
},
2023-06-03 00:53:46 +03:00
scrollToArea (area) {
const documentRef = this.documentRefs.find((a) => a.document.uuid === area.attachment_uuid)
documentRef.scrollToArea(area)
2023-06-07 01:16:43 +03:00
2023-06-09 01:43:18 +03:00
this.selectedAreaRef.value = area
2023-06-03 00:53:46 +03:00
},
2023-05-21 17:59:36 +03:00
save () {
2023-06-04 01:08:45 +03:00
this.$el.closest('template-builder').dataset.template = JSON.stringify(this.template)
2023-05-31 23:19:20 +03:00
return fetch(`/api/templates/${this.template.id}`, {
2023-05-21 17:59:36 +03:00
method: 'PUT',
2023-06-24 16:17:07 +03:00
body: JSON.stringify({
template: {
name: this.template.name,
schema: this.template.schema,
submitters: this.template.submitters,
fields: this.template.fields
}
}),
2023-05-21 17:59:36 +03:00
headers: { 'Content-Type': 'application/json' }
})
}
}
}
</script>