import { defineStore } from "pinia" import clonedeep from "clone-deep" import { generateUUID } from "~/lib/utils.js" import blocksTypes from "~/data/blocks_types.json" import { useAlert } from '~/composables/useAlert' export const useWorkingFormStore = defineStore("working_form", { state: () => ({ content: null, activeTab: 0, formPageIndex: 0, // Field being edited selectedFieldIndex: null, showEditFieldSidebar: null, showAddFieldSidebar: null, blockForm: null, draggingNewBlock: false, }), actions: { set(form) { this.content = form }, setProperties(properties) { this.content.properties = [...properties] }, objectToIndex(field) { if (typeof field === 'object') { return this.content.properties.findIndex( prop => prop.id === field.id ) } return field }, setEditingField(field) { this.selectedFieldIndex = this.objectToIndex(field) }, openSettingsForField(field) { this.setEditingField(field) this.showEditFieldSidebar = true this.showAddFieldSidebar = false }, closeEditFieldSidebar() { this.selectedFieldIndex = null this.showEditFieldSidebar = false this.showAddFieldSidebar = false }, openAddFieldSidebar(field) { if (field !== null) { this.setEditingField(field) } this.showAddFieldSidebar = true this.showEditFieldSidebar = false }, closeAddFieldSidebar() { this.selectedFieldIndex = null this.showAddFieldSidebar = false this.showEditFieldSidebar = false }, reset() { this.content = null this.selectedFieldIndex = null this.showEditFieldSidebar = null this.showAddFieldSidebar = null }, resetBlockForm() { this.blockForm = useForm({ type: null, name: null, }) }, prefillDefault(data) { // If a field already has this name, we need to make it unique with a number at the end let baseName = data.name let counter = 1 while (this.content.properties.some(prop => prop.name === data.name)) { counter++ data.name = `${baseName} ${counter}` } if (data.type === "nf-text") { data.content = "
This is a text block.
" } else if (data.type === "nf-page-break") { data.next_btn_text = "Next" data.previous_btn_text = "Previous" } else if (data.type === "nf-code") { data.content = '