implement signature preview

This commit is contained in:
Alex Turchyn
2023-05-24 01:22:17 +03:00
parent f1da37d1ec
commit b5b4f286cf
6 changed files with 73 additions and 19 deletions
+14 -1
View File
@@ -1,5 +1,18 @@
export default class extends HTMLElement {
setValue (value) {
this.innerHTML = value
const { fieldType } = this.dataset
if (fieldType === 'signature') {
[...this.children].forEach(e => e.remove())
const img = document.createElement('img')
img.classList.add('w-full', 'h-full', 'object-contain')
img.src = value.url
this.append(img)
} else {
this.innerHTML = value
}
}
}
+13 -5
View File
@@ -14,9 +14,13 @@ export default actionable(targetable(class extends HTMLElement {
]
passValueToArea (e) {
this.areasSetValue(e.target.id, e.target.value)
}
areasSetValue (fieldUuid, value) {
return (this.areas || []).forEach((area) => {
if (area.dataset.fieldUuid === e.target.id) {
area.setValue(e.target.value)
if (area.dataset.fieldUuid === fieldUuid) {
area.setValue(value)
}
})
}
@@ -42,6 +46,8 @@ export default actionable(targetable(class extends HTMLElement {
console.error('Error submitting form:', error)
}).finally(() => {
e.target.okButton.disabled = false
this.areasSetValue(e.target.closest('disable-hidden').dataset.fieldUuid, e.detail)
})
}
@@ -64,7 +70,7 @@ export default actionable(targetable(class extends HTMLElement {
}
moveStepBack (e) {
e.preventDefault()
e?.preventDefault()
const currentStepIndex = this.steps.findIndex((el) => !el.classList.contains('hidden'))
@@ -75,7 +81,9 @@ export default actionable(targetable(class extends HTMLElement {
}
}
moveNextStep () {
moveNextStep (e) {
e?.preventDefault()
const currentStepIndex = this.steps.findIndex((el) => !el.classList.contains('hidden'))
const nextStep = this.steps[currentStepIndex + 1]
@@ -89,7 +97,7 @@ export default actionable(targetable(class extends HTMLElement {
}
focusField ({ target }) {
this.setVisibleStep(target.dataset.fieldUuid)
this.setVisibleStep(target.closest('flow-area').dataset.fieldUuid)
}
focusArea ({ target }) {
+20 -4
View File
@@ -9,7 +9,10 @@ export default actionable(targetable(class extends HTMLElement {
'canvas',
'input',
'okButton',
'clearButton'
'image',
'clearButton',
'redrawButton',
'nextButton'
]
connectedCallback () {
@@ -22,7 +25,7 @@ export default actionable(targetable(class extends HTMLElement {
this.okButton.disabled = true
this.canvas.toBlob((blob) => {
const file = new File([blob], 'signature.jpg', { type: 'image/jpg' })
const file = new File([blob], 'signature.png', { type: 'image/png' })
new DirectUpload(
file,
@@ -38,10 +41,23 @@ export default actionable(targetable(class extends HTMLElement {
headers: { 'Content-Type': 'application/json' }
}).then((resp) => resp.json()).then((attachment) => {
this.input.value = attachment.uuid
this.dispatchEvent(new CustomEvent('upload', { details: attachment }))
this.dispatchEvent(new CustomEvent('upload', { detail: attachment }))
})
})
}, 'image/jpeg', 0.95)
}, 'image/png')
}
redraw (e) {
e?.preventDefault()
this.input.value = ''
this.canvas.classList.remove('hidden')
this.clearButton.classList.remove('hidden')
this.okButton.classList.remove('hidden')
this.image.remove()
this.redrawButton.remove()
this.nextButton.remove()
}
clear (e) {