From 639b6d0bd6f52bb7aa6debd7a83486a6d0bafb74 Mon Sep 17 00:00:00 2001 From: antoine Date: Thu, 7 Aug 2025 16:32:31 +0200 Subject: [PATCH] POC form live preview --- demo/app/Sharp/Posts/PostForm.php | 6 ++ resources/js/Pages/Form/Form.vue | 90 +++++++++++-------- src/Form/Eloquent/EloquentModelUpdater.php | 6 +- .../Eloquent/WithSharpFormEloquentUpdater.php | 2 +- src/Form/SharpForm.php | 10 +++ .../Api/ApiFormPreviewController.php | 31 +++++++ src/routes/api.php | 4 + 7 files changed, 111 insertions(+), 38 deletions(-) create mode 100644 src/Http/Controllers/Api/ApiFormPreviewController.php diff --git a/demo/app/Sharp/Posts/PostForm.php b/demo/app/Sharp/Posts/PostForm.php index bd0c6538f..e7538daea 100644 --- a/demo/app/Sharp/Posts/PostForm.php +++ b/demo/app/Sharp/Posts/PostForm.php @@ -233,6 +233,12 @@ public function update($id, array $data) ->ignore(auth()->user()->isAdmin() ? [] : ['author_id']) ->save($post, $data); + if ($this->isPreview) { + return view('pages.post', [ + 'post' => $post, + ]); + } + if (sharp()->context()->isCreation()) { $this->notify('Your post was created, but not published yet.'); } diff --git a/resources/js/Pages/Form/Form.vue b/resources/js/Pages/Form/Form.vue index bb8e4c1eb..a08b868f3 100644 --- a/resources/js/Pages/Form/Form.vue +++ b/resources/js/Pages/Form/Form.vue @@ -12,6 +12,7 @@ import { Button } from '@/components/ui/button'; import { useResizeObserver } from "@vueuse/core"; import { slugify } from "@/utils"; + import { api } from "@/api/api"; const props = defineProps<{ form: FormData, @@ -71,6 +72,18 @@ }); } }, { immediate: true }); + + const previewHtml = ref(''); + let timeout: number; + watch(() => form.data, () => { + clearTimeout(timeout); + timeout = window.setTimeout(() => { + api.post(route('code16.sharp.api.form.preview', { entityKey, instanceId }), form.data) + .then(response => { + previewHtml.value = response.data.data.html; + }); + }, timeout ? 200 : 0); + }, { immediate: true, deep: true });