From 1ae0420656a776de163a5c292e9ac062bd4b6496 Mon Sep 17 00:00:00 2001 From: Favour Olayinka Date: Mon, 13 May 2024 13:47:59 +0100 Subject: [PATCH] drag and drop to add block (#404) * drag and drop to add block * Change styling for drag & drop * Improve dragging/reordering fields * fix drag dropped bug * Fix spacing between form elements * fix sorting bug * fix: move field * fix page break bugs * fix move and add logic implementation * Changed cursor to grab --------- Co-authored-by: Julien Nahum --- client/components/open/forms/OpenForm.vue | 105 ++++---- .../components/open/forms/OpenFormField.vue | 116 +++------ .../form-components/AddFormBlock.vue | 243 ++++++------------ client/stores/working_form.js | 98 +++++++ 4 files changed, 280 insertions(+), 282 deletions(-) diff --git a/client/components/open/forms/OpenForm.vue b/client/components/open/forms/OpenForm.vue index c2692c07..9e5372a7 100644 --- a/client/components/open/forms/OpenForm.vue +++ b/client/components/open/forms/OpenForm.vue @@ -43,15 +43,16 @@ class="form-group flex flex-wrap w-full" > + + + diff --git a/client/stores/working_form.js b/client/stores/working_form.js index 496cdfdc..eab5c4d6 100644 --- a/client/stores/working_form.js +++ b/client/stores/working_form.js @@ -1,4 +1,28 @@ import { defineStore } from "pinia" +import clonedeep from "clone-deep" +import { generateUUID } from "~/lib/utils.js" + +const defaultBlockNames = { + text: "Your name", + date: "Date", + url: "Link", + phone_number: "Phone Number", + number: "Number", + rating: "Rating", + scale: "Scale", + slider: "Slider", + email: "Email", + checkbox: "Checkbox", + select: "Select", + multi_select: "Multi Select", + files: "Files", + signature: "Signature", + "nf-text": "Text Block", + "nf-page-break": "Page Break", + "nf-divider": "Divider", + "nf-image": "Image", + "nf-code": "Code Block", +} export const useWorkingFormStore = defineStore("working_form", { state: () => ({ @@ -8,6 +32,8 @@ export const useWorkingFormStore = defineStore("working_form", { selectedFieldIndex: null, showEditFieldSidebar: null, showAddFieldSidebar: null, + blockForm: null, + draggingNewBlock: false, }), actions: { set(form) { @@ -54,5 +80,77 @@ export const useWorkingFormStore = defineStore("working_form", { this.showEditFieldSidebar = null this.showAddFieldSidebar = null }, + + resetBlockForm() { + this.blockForm = useForm({ + type: null, + name: null, + }) + + }, + + prefillDefault(data) { + 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 = + '
This is a code block.
' + } else if (data.type === "signature") { + data.help = "Draw your signature above" + } + return data + }, + + addBlock(type, index = null) { + this.blockForm.type = type + this.blockForm.name = defaultBlockNames[type] + const newBlock = this.prefillDefault(this.blockForm.data()) + newBlock.id = generateUUID() + newBlock.hidden = false + if (["select", "multi_select"].includes(this.blockForm.type)) { + newBlock[this.blockForm.type] = { options: [] } + } + if (this.blockForm.type === "rating") { + newBlock.rating_max_value = 5 + } + if (this.blockForm.type === "scale") { + newBlock.scale_min_value = 1 + newBlock.scale_max_value = 5 + newBlock.scale_step_value = 1 + } + if (this.blockForm.type === "slider") { + newBlock.slider_min_value = 0 + newBlock.slider_max_value = 50 + newBlock.slider_step_value = 1 + } + newBlock.help_position = "below_input" + if ( + this.selectedFieldIndex === null || + this.selectedFieldIndex === undefined + ) { + const newFields = clonedeep(this.content.properties) + newFields.push(newBlock) + this.content.properties = newFields + this.openSettingsForField( + this.form.properties.length - 1, + ) + } else { + const fieldIndex = typeof index === "number" ? index : this.selectedFieldIndex + 1 + const newFields = clonedeep(this.content.properties) + newFields.splice(fieldIndex, 0, newBlock) + this.content.properties = newFields + this.openSettingsForField(fieldIndex) + } + }, + + moveField(oldIndex, newIndex) { + const newFields = clonedeep(this.content.properties) + const field = newFields.splice(oldIndex, 1)[0]; + newFields.splice(newIndex, 0, field); + this.content.properties = newFields + } }, })