Skip to content
This repository was archived by the owner on Jul 15, 2021. It is now read-only.

Commit b581b72

Browse files
committed
Add localStorage to interactive demo. Closes #10
1 parent f49f740 commit b581b72

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

src/demo/demo.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,26 @@
6868
}
6969
}
7070

71+
function saveLast(sql) {
72+
// Save the last stuff in the editor for next time
73+
if (root.window.localStorage) {
74+
root.window.localStorage.setItem('sqlite-parser-demo', JSON.stringify({
75+
sql: sql
76+
}));
77+
}
78+
}
79+
80+
function reloadLast(source) {
81+
if (root.window.localStorage) {
82+
try {
83+
var lastState = JSON.parse(root.window.localStorage.getItem('sqlite-parser-demo'));
84+
if (lastState && lastState['sql'] != null) {
85+
source.setValue(lastState['sql']);
86+
}
87+
} catch (e) {}
88+
}
89+
}
90+
7191
function editorFormat(e) {
7292
e.execCommand('selectAll');
7393
e.execCommand('indentAuto');
@@ -94,7 +114,14 @@
94114
}, cmDefaults)),
95115
update = debounce(updater(sql, ast), 250);
96116
sql.on('change', update);
117+
reloadLast(sql);
97118
update();
119+
root.window.onbeforeunload = function () {
120+
var lastValue = sql.getValue();
121+
if (lastValue.trim() !== '') {
122+
saveLast(lastValue);
123+
}
124+
};
98125
};
99126
root.onload = loadDemo;
100127
})(typeof self === 'object' ? self : global);

0 commit comments

Comments
 (0)