Skip to content

Commit 46810b9

Browse files
committed
Update the component code and add the saving function
1 parent fba5d86 commit 46810b9

File tree

1 file changed

+51
-20
lines changed

1 file changed

+51
-20
lines changed
Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<!DOCTYPE html>
22
<html lang="en">
3-
43
<head>
54
<meta charset="utf-8" />
65
<meta name="viewport" content="width=device-width, initial-scale=1" />
@@ -9,33 +8,65 @@
98
<script src="./survey.core.js"></script>
109
<script src="./survey.i18n.js"></script>
1110
<script src="./survey-js-ui.js"></script>
12-
1311
<script src="./themes/index.js"></script>
1412
<script src="./creator-themes/index.js"></script>
15-
1613
<script src="./survey-creator-core.js"></script>
1714
<script src="./survey-creator-core.i18n.js"></script>
1815
<script src="./survey-creator-js.js"></script>
1916

20-
21-
<link rel="stylesheet" type="text/css" href="./survey-core.css" />
22-
<link rel="stylesheet" type="text/css" href="./survey-creator-core.css" />
23-
17+
<link rel="stylesheet" href="./survey-core.css" />
18+
<link rel="stylesheet" href="./survey-creator-core.css" />
2419
</head>
2520

2621
<body>
2722
<div id="surveyCreatorContainer" style="position: fixed; top: 0; left: 0; right: 0; bottom: 0;"></div>
23+
24+
<script>
25+
const LOCAL_STORAGE_KEY = "localSurveyJSON";
26+
const SYNCED_FLAG_KEY = "localSurveySynced";
27+
28+
SurveyCreatorCore.registerCreatorTheme(SurveyCreatorTheme);
29+
30+
const creator = new SurveyCreator.SurveyCreator();
31+
32+
creator.autoSaveEnabled = true;
33+
34+
const savedJSON = localStorage.getItem(LOCAL_STORAGE_KEY);
35+
if (savedJSON) {
36+
try {
37+
creator.JSON = JSON.parse(savedJSON);
38+
} catch (e) {
39+
console.warn("Failed to parse saved JSON from localStorage:", e);
40+
}
41+
}
42+
43+
creator.saveSurveyFunc = (saveNo, callback) => {
44+
const currentJSON = creator.JSON;
45+
localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(currentJSON));
46+
localStorage.setItem(SYNCED_FLAG_KEY, "false");
47+
console.log("Survey auto-saved locally.");
48+
callback(saveNo, true);
49+
};
50+
51+
window.addEventListener("online", () => {
52+
const json = localStorage.getItem(LOCAL_STORAGE_KEY);
53+
const isSynced = localStorage.getItem(SYNCED_FLAG_KEY);
54+
55+
if (json && isSynced !== "true") {
56+
sendToServer(JSON.parse(json));
57+
}
58+
});
59+
60+
function sendToServer(data) {
61+
console.log("Syncing with server...", data);
62+
63+
setTimeout(() => {
64+
console.log("Sync complete.");
65+
localStorage.setItem(SYNCED_FLAG_KEY, "true");
66+
}, 1000);
67+
}
68+
69+
creator.render("surveyCreatorContainer");
70+
</script>
2871
</body>
29-
<script>
30-
SurveyCreatorCore.registerCreatorTheme(SurveyCreatorTheme); // Add predefined Survey Creator UI themes
31-
var surveyJSON = {};
32-
var options = {
33-
showThemeTab: true,
34-
showTranslationTab: true
35-
};
36-
var creator = new SurveyCreator.SurveyCreator(options);
37-
creator.JSON = surveyJSON;
38-
creator.render("surveyCreatorContainer");
39-
</script>
40-
41-
</html>
72+
</html>

0 commit comments

Comments
 (0)