Enhance default field values and block type configuration

- Refactor field options to use default values from blocks_types.json
- Add default values for various form input types in blocks_types.json
- Improve default field initialization logic in FieldOptions and working_form store
- Add camera switching functionality for mobile devices in CameraUpload component
This commit is contained in:
Julien Nahum
2025-02-19 12:25:13 +01:00
parent 83ef18f453
commit efd31133cc
4 changed files with 178 additions and 90 deletions

View File

@@ -1,4 +1,3 @@
<template>
<div class="relative border">
<video
@@ -44,6 +43,16 @@
class="w-8 h-8"
/>
</span>
<span
v-if="isMobileDevice"
class="text-white cursor-pointer"
@click="switchCamera"
>
<Icon
name="heroicons:arrow-path"
class="w-8 h-8"
/>
</span>
</div>
</div>
<div
@@ -134,7 +143,9 @@ export default {
isCapturing: false,
capturedImage: null,
cameraPermissionStatus: "loading",
quaggaInitialized: false
quaggaInitialized: false,
currentFacingMode: 'user',
mediaStream: null
}),
computed: {
videoDisplay() {
@@ -143,6 +154,9 @@ export default {
canvasDisplay() {
return !this.isCapturing && this.capturedImage ? "" : "hidden"
},
isMobileDevice() {
return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)
}
},
mounted() {
const webcamElement = document.getElementById("webcam")
@@ -152,39 +166,74 @@ export default {
},
methods: {
async cleanupCurrentStream() {
if (this.quaggaInitialized) {
Quagga.stop()
this.quaggaInitialized = false
}
if (this.mediaStream) {
this.mediaStream.getTracks().forEach(track => track.stop())
this.mediaStream = null
}
if (this.webcam) {
this.webcam.stop()
}
const webcamElement = document.getElementById("webcam")
if (webcamElement && webcamElement.srcObject) {
webcamElement.srcObject = null
}
},
async switchCamera() {
try {
// Stop current camera
if (this.quaggaInitialized) {
Quagga.stop()
this.quaggaInitialized = false
}
this.webcam.stop()
// Toggle facing mode considering barcode mode
this.currentFacingMode = this.isBarcodeMode ? 'environment' :
(this.currentFacingMode === 'user' ? 'environment' : 'user')
// Restart camera
await this.openCameraUpload()
} catch (error) {
console.error('Error switching camera:', error)
this.cameraPermissionStatus = "unknown"
}
},
async openCameraUpload() {
this.isCapturing = true
this.capturedImage = null
try {
// Get video element
const webcamElement = document.getElementById("webcam")
const canvasElement = document.getElementById("canvas")
// iOS specific constraints
const constraints = {
audio: false,
video: {
facingMode: this.isBarcodeMode ? 'environment' : 'user',
facingMode: this.isBarcodeMode ? 'environment' : this.currentFacingMode,
width: { ideal: 1280 },
height: { ideal: 720 }
}
}
// Try getting the stream directly first
const stream = await navigator.mediaDevices.getUserMedia(constraints)
// Attach stream to video element
webcamElement.srcObject = stream
// Create webcam instance with the stream
this.webcam = new Webcam(
webcamElement,
this.isBarcodeMode ? 'environment' : 'user',
this.isBarcodeMode ? 'environment' : this.currentFacingMode,
canvasElement
)
// Wait for video to be ready
await new Promise((resolve) => {
webcamElement.onloadedmetadata = () => {
webcamElement.play()
@@ -266,10 +315,8 @@ export default {
byteArrays.push(byteArray)
}
// Create Blob from binary data
const blob = new Blob(byteArrays, { type: "image/png" })
const filename = Date.now()
// Create a File object from the Blob
const file = new File([blob], `${filename}.png`, { type: "image/png" })
this.$emit("uploadImage", file)
},

View File

@@ -609,6 +609,7 @@ import HiddenRequiredDisabled from './HiddenRequiredDisabled.vue'
import EditorSectionHeader from '~/components/open/forms/components/form-components/EditorSectionHeader.vue'
import { format } from 'date-fns'
import { default as _has } from 'lodash/has'
import blocksTypes from '~/data/blocks_types.json'
export default {
name: 'FieldOptions',
@@ -821,57 +822,43 @@ export default {
},
setDefaultFieldValues() {
const defaultFieldValues = {
slider: {
slider_min_value: 0,
slider_max_value: 100,
slider_step_value: 1
},
scale: {
scale_min_value: 1,
scale_max_value: 5,
scale_step_value: 1
},
rating: {
rating_max_value: 5
},
files: {
max_file_size: Math.min((this.field.max_file_size ?? this.mbLimit), this.mbLimit)
},
text: {
multi_lines: false,
max_char_limit: 2000
},
rich_text: {
max_char_limit: 2000
},
email: {
max_char_limit: 2000
},
url: {
max_char_limit: 2000
},
date: {
date_format: this.dateFormatOptions[0].value,
time_format: this.timeFormatOptions[0].value
},
matrix: {
rows:['Row 1'],
columns: [1 ,2 ,3],
selection_data:{
'Row 1': null
}
},
barcode: {
decoders: ['ean_reader', 'upc_reader']
}
}
// Apply type-specific defaults from blocks_types.json if available
if (this.field.type in blocksTypes && blocksTypes[this.field.type]?.default_values) {
Object.assign(this.field, blocksTypes[this.field.type].default_values)
}
// Apply additional defaults from defaultFieldValues if needed
if (this.field.type in defaultFieldValues) {
Object.keys(defaultFieldValues[this.field.type]).forEach(key => {
if (!_has(this.field,key)) {
if (!_has(this.field, key)) {
this.field[key] = defaultFieldValues[this.field.type][key]
}
})
}
// Ensure critical defaults for specific types
if (this.field.type === "rating" && !this.field.rating_max_value) {
this.field.rating_max_value = 5
} else if (this.field.type === "scale" && (!this.field.scale_min_value || !this.field.scale_max_value || !this.field.scale_step_value)) {
this.field.scale_min_value = 1
this.field.scale_max_value = 5
this.field.scale_step_value = 1
} else if (this.field.type === "slider" && (!this.field.slider_min_value || !this.field.slider_max_value || !this.field.slider_step_value)) {
this.field.slider_min_value = 0
this.field.slider_max_value = 50
this.field.slider_step_value = 1
} else if (["select", "multi_select"].includes(this.field.type) && !this.field[this.field.type]?.options) {
this.field[this.field.type] = { options: [] }
}
},
updateMatrixField(newField) {
this.field = newField