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 @@
+
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={() => {}}
+ /> */}
+ {/*
{}} /> */}