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

253 lines
6.3 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"
class="mx-auto px-4"
2023-05-21 17:59:36 +03:00
>
2023-05-31 00:50:43 +03:00
<div class="flex justify-between py-1.5 items-center">
<Contenteditable
2023-05-31 23:19:20 +03:00
:model-value="template.name"
2023-05-31 00:50:43 +03:00
class="text-3xl focus:text-clip"
@update:model-value="updateName"
2023-05-21 17:59:36 +03:00
/>
2023-05-31 00:50:43 +03:00
<div class="space-x-3 flex items-center">
<a
2023-05-31 23:19:20 +03:00
:href="`/templates/${template.id}/submissions`"
2023-05-31 00:50:43 +03:00
class="btn btn-primary"
>
<IconUsersPlus
width="20"
class="mr-2 inline"
/>
Recipients
</a>
<a
:href="`/`"
class="base-button"
v-bind="isSaving ? { disabled: true } : {}"
@click.prevent="onSaveClick"
><IconDeviceFloppy
width="20"
class="mr-2"
/>Save</a>
</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"
class="overflow-auto w-52 flex-none pr-4 mt-0.5 pt-0.5"
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]"
@scroll-to="scrollIntoDocument(item)"
@remove="onDocumentRemove"
@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-05-31 23:19:20 +03:00
:template-id="template.id"
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">
<div class="px-3">
<Document
v-for="document in sortedDocuments"
:key="document.uuid"
:ref="setDocumentRefs"
:areas-index="fieldAreasIndex[document.uuid]"
:document="document"
:is-draw="!!drawField"
:is-drag="!!dragFieldType"
@draw="onDraw"
@drop-field="onDropfield"
/>
</div>
</div>
<div
class="relative w-72 flex-none"
:class="drawField ? 'overflow-hidden' : 'overflow-auto'"
>
<div
v-if="drawField"
class="sticky inset-0 bg-white h-full"
>
Draw {{ drawField.name }} field on the page
<button @click="drawField = false">
Cancel
</button>
</div>
<div>
FIelds
<Fields
ref="fields"
2023-05-31 23:19:20 +03:00
v-model:fields="template.fields"
2023-05-31 00:50:43 +03:00
@set-draw="drawField = $event"
@set-drag="dragFieldType = $event"
@drag-end="dragFieldType = null"
/>
</div>
2023-05-21 17:59:36 +03:00
</div>
</div>
</div>
</template>
<script>
import Upload from './upload'
import Fields from './fields'
import Document from './document'
2023-05-31 00:50:43 +03:00
import Contenteditable from './contenteditable'
import DocumentPreview from './preview'
import { IconUsersPlus, IconDeviceFloppy } from '@tabler/icons-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,
DocumentPreview,
Contenteditable,
IconUsersPlus,
IconDeviceFloppy
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
2023-05-21 17:59:36 +03:00
}
},
data () {
return {
2023-05-31 00:50:43 +03:00
documentRefs: [],
isSaving: false,
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: {
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
},
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
})
}
},
mounted () {
document.addEventListener('keyup', this.disableDrawOnEsc)
},
unmounted () {
document.removeEventListener('keyup', this.disableDrawOnEsc)
},
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-05-21 17:59:36 +03:00
disableDrawOnEsc (e) {
if (e.code === 'Escape') {
this.drawField = null
}
},
onDraw (area) {
this.drawField.areas ||= []
this.drawField.areas.push(area)
this.drawField = null
},
2023-05-23 00:03:26 +03:00
onDropfield (area) {
this.$refs.fields.addField(this.dragFieldType, area)
},
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-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(() => {
window.Turbo.visit('/')
}).finally(() => {
this.isSaving = false
})
},
2023-05-21 17:59:36 +03:00
save () {
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-05-31 23:19:20 +03:00
body: JSON.stringify({ template: this.template }),
2023-05-21 17:59:36 +03:00
headers: { 'Content-Type': 'application/json' }
}).then((resp) => {
console.log(resp)
})
}
}
}
</script>