fix new area order

master^2
Pete Matsyburka 6 days ago
parent 7305637b8c
commit 9c9dc27537

@ -1012,6 +1012,13 @@ export default {
fieldsDragFieldRef: () => ref(), fieldsDragFieldRef: () => ref(),
customDragFieldRef: () => ref(), customDragFieldRef: () => ref(),
selectedAreasRef: () => ref([]), selectedAreasRef: () => ref([]),
attachmentUuidsIndex () {
return this.template.schema.reduce((acc, e, index) => {
acc[e.attachment_uuid] = index
return acc
}, {})
},
language () { language () {
return this.locale.split('-')[0].toLowerCase() return this.locale.split('-')[0].toLowerCase()
}, },
@ -1663,32 +1670,25 @@ export default {
this.save() this.save()
} }
}, },
findFieldInsertIndex (field) { compareAreas (a, b) {
if (!field.areas?.length) return -1 const aAttIdx = this.attachmentUuidsIndex[a.attachment_uuid]
const bAttIdx = this.attachmentUuidsIndex[b.attachment_uuid]
const area = field.areas[0]
const attachmentUuidsIndex = this.template.schema.reduce((acc, e, index) => {
acc[e.attachment_uuid] = index
return acc
}, {})
const compareAreas = (a, b) => { if (aAttIdx !== bAttIdx) return aAttIdx - bAttIdx
const aAttIdx = attachmentUuidsIndex[a.attachment_uuid] if (a.page !== b.page) return a.page - b.page
const bAttIdx = attachmentUuidsIndex[b.attachment_uuid]
if (aAttIdx !== bAttIdx) return aAttIdx - bAttIdx const aY = a.y + a.h
if (a.page !== b.page) return a.page - b.page const bY = b.y + b.h
const aY = a.y + a.h if (Math.abs(aY - bY) < 0.01) return a.x - b.x
const bY = b.y + b.h if (a.h < b.h ? a.y >= b.y && aY <= bY : b.y >= a.y && bY <= aY) return a.x - b.x
if (Math.abs(aY - bY) < 0.01) return a.x - b.x return aY - bY
if (a.h < b.h ? a.y >= b.y && aY <= bY : b.y >= a.y && bY <= aY) return a.x - b.x },
findFieldInsertIndex (field) {
if (!field.areas?.length) return -1
return aY - bY const area = field.areas[0]
}
let closestBeforeIndex = -1 let closestBeforeIndex = -1
let closestBeforeArea = null let closestBeforeArea = null
@ -1698,15 +1698,15 @@ export default {
this.template.fields.forEach((f, index) => { this.template.fields.forEach((f, index) => {
if (f.submitter_uuid === field.submitter_uuid) { if (f.submitter_uuid === field.submitter_uuid) {
(f.areas || []).forEach((a) => { (f.areas || []).forEach((a) => {
const cmp = compareAreas(a, area) const cmp = this.compareAreas(a, area)
if (cmp < 0) { if (cmp < 0) {
if (!closestBeforeArea || (compareAreas(a, closestBeforeArea) > 0 && closestBeforeIndex < index)) { if (!closestBeforeArea || (this.compareAreas(a, closestBeforeArea) > 0 && closestBeforeIndex < index)) {
closestBeforeIndex = index closestBeforeIndex = index
closestBeforeArea = a closestBeforeArea = a
} }
} else { } else {
if (!closestAfterArea || (compareAreas(a, closestAfterArea) < 0 && closestAfterIndex > index)) { if (!closestAfterArea || (this.compareAreas(a, closestAfterArea) < 0 && closestAfterIndex > index)) {
closestAfterIndex = index closestAfterIndex = index
closestAfterArea = a closestAfterArea = a
} }
@ -1729,6 +1729,17 @@ export default {
this.template.fields.push(field) this.template.fields.push(field)
} }
}, },
insertArea (field, area) {
field.areas ||= []
const insertIndex = field.areas.findIndex((a) => this.compareAreas(a, area) > 0)
if (insertIndex === -1) {
field.areas.push(area)
} else {
field.areas.splice(insertIndex, 0, area)
}
},
insertDetectedField (field) { insertDetectedField (field) {
if (!this.withDetectExistingFields || !field.name) { if (!this.withDetectExistingFields || !field.name) {
this.insertField(field) this.insertField(field)
@ -1744,7 +1755,7 @@ export default {
if (existingField) { if (existingField) {
existingField.areas = existingField.areas || [] existingField.areas = existingField.areas || []
existingField.areas.push(...(field.areas || [])) field.areas.forEach((area) => this.insertArea(existingField, area))
} else { } else {
const customField = this.detectCustomFieldsIndex[indexKey] || this.detectCustomFieldsIndex[nameKey] const customField = this.detectCustomFieldsIndex[indexKey] || this.detectCustomFieldsIndex[nameKey]
@ -2249,7 +2260,7 @@ export default {
fieldUuidIndex[field.uuid] = newField fieldUuidIndex[field.uuid] = newField
newField.areas.push(newArea) this.insertArea(newField, newArea)
newAreas.push(newArea) newAreas.push(newArea)
if (['radio', 'multiple'].includes(field.type) && field.options?.length) { if (['radio', 'multiple'].includes(field.type) && field.options?.length) {
@ -2362,17 +2373,7 @@ export default {
area.y -= area.h / 2 area.y -= area.h / 2
} }
this.drawField.areas ||= [] this.insertArea(this.drawField, area)
const insertBeforeAreaIndex = this.drawField.areas.findIndex((a) => {
return a.attachment_uuid === area.attachment_uuid && a.page > area.page
})
if (insertBeforeAreaIndex !== -1) {
this.drawField.areas.splice(insertBeforeAreaIndex, 0, area)
} else {
this.drawField.areas.push(area)
}
if (this.template.fields.indexOf(this.drawField) === -1) { if (this.template.fields.indexOf(this.drawField) === -1) {
this.insertField(this.drawField) this.insertField(this.drawField)
@ -2513,9 +2514,7 @@ export default {
delete field.height delete field.height
} }
field.areas ||= [] this.insertArea(field, fieldArea)
field.areas.push(fieldArea)
if (this.selectedAreasRef.value.length < 2) { if (this.selectedAreasRef.value.length < 2) {
this.selectedAreasRef.value = [fieldArea] this.selectedAreasRef.value = [fieldArea]
@ -2585,7 +2584,7 @@ export default {
} }
} }
field.areas.push(fieldArea) this.insertArea(field, fieldArea)
}) })
} else { } else {
const fieldArea = { const fieldArea = {

@ -786,17 +786,27 @@ export default {
}, },
copyToAllPages (field) { copyToAllPages (field) {
const areaString = JSON.stringify(field.areas[0]) const areaString = JSON.stringify(field.areas[0])
const newAreas = []
const existingAreasIndex = field.areas.reduce((acc, area) => {
acc[`${area.attachment_uuid}-${area.page}`] = area
return acc
}, {})
this.template.schema.forEach((item) => {
const attachment = this.template.documents.find((d) => d.uuid === item.attachment_uuid)
this.template.documents.forEach((attachment) => {
const numberOfPages = attachment.metadata?.pdf?.number_of_pages || attachment.preview_images.length const numberOfPages = attachment.metadata?.pdf?.number_of_pages || attachment.preview_images.length
for (let page = 0; page <= numberOfPages - 1; page++) { for (let page = 0; page <= numberOfPages - 1; page++) {
if (!field.areas.find((area) => area.attachment_uuid === attachment.uuid && area.page === page)) { const existing = existingAreasIndex[`${attachment.uuid}-${page}`]
field.areas.push({ ...JSON.parse(areaString), attachment_uuid: attachment.uuid, page })
} newAreas.push(existing || { ...JSON.parse(areaString), attachment_uuid: attachment.uuid, page })
} }
}) })
field.areas = newAreas
this.$emit('scroll-to', this.field.areas[this.field.areas.length - 1]) this.$emit('scroll-to', this.field.areas[this.field.areas.length - 1])
this.$emit('save') this.$emit('save')

Loading…
Cancel
Save