From bcf73660bd4c8dc680105e5dbea8cb5ba14c49a8 Mon Sep 17 00:00:00 2001 From: Andreas Tzionis Date: Sat, 4 Nov 2023 22:05:15 +0200 Subject: [PATCH] fix load and save page --- dev/nextjs-project/data/default.html | 40 +++++++++++++++++++ lib/client/vanilla/index.jsx | 60 ++++++++++++++++------------ lib/server/api/handle.ts | 32 +++++---------- lib/server/utils/index.ts | 11 ++--- 4 files changed, 92 insertions(+), 51 deletions(-) create mode 100644 dev/nextjs-project/data/default.html diff --git a/dev/nextjs-project/data/default.html b/dev/nextjs-project/data/default.html new file mode 100644 index 00000000..b807b210 --- /dev/null +++ b/dev/nextjs-project/data/default.html @@ -0,0 +1,40 @@ +
+
+ +
+
+

+ Let us find your + + Forever Home. +

+ +

+ Lorem ipsum dolor sit amet consectetur, adipisicing elit. Nesciunt illo tenetur fuga ducimus + numquam ea! +

+ + +
+
+
diff --git a/lib/client/vanilla/index.jsx b/lib/client/vanilla/index.jsx index 3dea73e0..4083c4df 100644 --- a/lib/client/vanilla/index.jsx +++ b/lib/client/vanilla/index.jsx @@ -21,6 +21,15 @@ const themes = [ const placeholderImageUrl = 'https://placehold.co/400x200' +function debounce(callback, timeout = 1000) { + let timeoutFn + return (...args) => { + const context = this + clearTimeout(timeoutFn) + timeoutFn = setTimeout(() => callback.apply(context, args), timeout) + } +} + const getBaseUrl = (standaloneServer) => { return standaloneServer ? `http://localhost:${standaloneServerPort}` : '' } @@ -37,30 +46,44 @@ function ContentProvider() { const [hoveredComponent, setHoveredComponent] = useState() const [components, setComponents] = useState([]) - const loadData = async () => { + const loadComponents = async () => { const baseUrl = getBaseUrl(false) const url = `${baseUrl}/api/builder/handle?type=theme&name=${themes[0].folder}` const _components = await fetch(url).then((r) => r.json()) setComponents(_components) + } + + const loadPage = async () => { + const baseUrl = getBaseUrl(false) + const url2 = `${baseUrl}/api/builder/handle?type=data&path=${location.pathname}` + const data = await fetch(url2).then((r) => r.text()) + canvasRef.current.innerHTML = data + } + + const savePage = async () => { + console.log('dom changed') + + const baseUrl = getBaseUrl(false) + const url = `${baseUrl}/api/builder/handle?type=data&path=${location.pathname}` - // TODO load page from served + await fetch(url, { method: 'post', body: canvasRef.current.innerHTML }) } const onDomChange = () => { canvasRef.current const config = { attributes: true, childList: true, subtree: true } - const observer = new MutationObserver(() => { - console.log('dom changed') - // TODO save page to server - }) + const observer = new MutationObserver( + debounce(() => { + savePage() + }), + ) observer.observe(canvasRef.current, config) return observer } useEffect(() => { - loadData() - + loadPage() loadComponents() const observer = onDomChange() @@ -68,19 +91,6 @@ function ContentProvider() { return () => observer.disconnect() }, []) - const loadComponents = () => { - const html = localStorage.getItem('page') - if (html) { - canvasRef.current.innerHTML = html - } else { - alert('Save not found') - } - } - - const saveComponents = () => { - localStorage.setItem('page', canvasRef.current.innerHTML) - } - const clearComponents = async () => { canvasRef.current.innerHTML = '' } @@ -231,11 +241,11 @@ function ContentProvider() {
- - + onClick={() => {}} + /> */} + {/* {}} /> */}