From ee5708e0babd08e3ed4fffbf9a24d7cd5755a782 Mon Sep 17 00:00:00 2001 From: lironh Date: Sat, 21 Mar 2020 21:47:31 +0200 Subject: feat(demo): saving user history --- .gitignore | 1 + demo/src/index.html | 1 + demo/src/index.ts | 21 ++++++++++++++++++++- demo/src/utils/editor-history.util.ts | 17 +++++++++++++++++ 4 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 demo/src/utils/editor-history.util.ts diff --git a/.gitignore b/.gitignore index 92cd86e..3a05d54 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ node_modules dist *.log +.idea diff --git a/demo/src/index.html b/demo/src/index.html index 99b1c84..9b69306 100644 --- a/demo/src/index.html +++ b/demo/src/index.html @@ -17,6 +17,7 @@
diff --git a/demo/src/index.ts b/demo/src/index.ts index c886bba..4246547 100644 --- a/demo/src/index.ts +++ b/demo/src/index.ts @@ -5,6 +5,7 @@ import { formatTime } from './format-time'; import './index.css'; import { CPUPerformance } from './cpu-performance'; import { LEDElement } from '@wokwi/elements'; +import { EditorHistoryUtil } from './utils/editor-history.util'; let editor: any; // eslint-disable-line @typescript-eslint/no-explicit-any const BLINK_CODE = ` @@ -32,7 +33,7 @@ window.require.config({ }); window.require(['vs/editor/editor.main'], () => { editor = monaco.editor.create(document.querySelector('.code-editor'), { - value: BLINK_CODE, + value: EditorHistoryUtil.getValue() || BLINK_CODE, language: 'cpp', minimap: { enabled: false } }); @@ -50,6 +51,8 @@ const runButton = document.querySelector('#run-button'); runButton.addEventListener('click', compileAndRun); const stopButton = document.querySelector('#stop-button'); stopButton.addEventListener('click', stopCode); +const revertButton = document.querySelector('#revert-button'); +revertButton.addEventListener('click', setBlinkSnippet); const statusLabel = document.querySelector('#status-label'); const compilerOutputText = document.querySelector('#compiler-output-text'); const serialOutputText = document.querySelector('#serial-output-text'); @@ -80,7 +83,11 @@ async function compileAndRun() { led12.value = false; led13.value = false; + storeUserSnippet(); + runButton.setAttribute('disabled', '1'); + revertButton.setAttribute('disabled', '1'); + serialOutputText.textContent = ''; try { statusLabel.textContent = 'Compiling...'; @@ -97,15 +104,27 @@ async function compileAndRun() { runButton.removeAttribute('disabled'); alert('Failed: ' + err); } finally { + revertButton.removeAttribute('disabled'); statusLabel.textContent = ''; } } +function storeUserSnippet() { + EditorHistoryUtil.clearSnippet(); + EditorHistoryUtil.storeSnippet(editor.getValue()); +} + function stopCode() { stopButton.setAttribute('disabled', '1'); runButton.removeAttribute('disabled'); + revertButton.removeAttribute('disabled'); if (runner) { runner.stop(); runner = null; } } + +function setBlinkSnippet() { + editor.setValue(BLINK_CODE); + EditorHistoryUtil.storeSnippet(editor.getValue()); +} diff --git a/demo/src/utils/editor-history.util.ts b/demo/src/utils/editor-history.util.ts new file mode 100644 index 0000000..33d1916 --- /dev/null +++ b/demo/src/utils/editor-history.util.ts @@ -0,0 +1,17 @@ +const AVRJS8_EDITOR_HISTORY = 'AVRJS8_EDITOR_HISTORY'; + +export class EditorHistoryUtil { + static storeSnippet(codeSnippet: string) { + if (window.localStorage) { + window.localStorage.setItem(AVRJS8_EDITOR_HISTORY, codeSnippet); + } else throw new Error('no localStorage support'); + } + + static clearSnippet() { + localStorage.removeItem(AVRJS8_EDITOR_HISTORY); + } + + static getValue() { + return localStorage.getItem(AVRJS8_EDITOR_HISTORY); + } +} -- cgit v1.2.3 From 9ece5bd28144f34fc336a37f4688a23371778603 Mon Sep 17 00:00:00 2001 From: lironh Date: Sun, 22 Mar 2020 08:42:02 +0200 Subject: feat(demo): saving user history --- demo/src/index.html | 2 +- demo/src/index.ts | 2 +- demo/src/utils/editor-history.util.ts | 9 ++++++--- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/demo/src/index.html b/demo/src/index.html index 9b69306..332ccb3 100644 --- a/demo/src/index.html +++ b/demo/src/index.html @@ -17,7 +17,7 @@ diff --git a/demo/src/index.ts b/demo/src/index.ts index 4246547..e3f9a67 100644 --- a/demo/src/index.ts +++ b/demo/src/index.ts @@ -102,9 +102,9 @@ async function compileAndRun() { } } catch (err) { runButton.removeAttribute('disabled'); + revertButton.removeAttribute('disabled'); alert('Failed: ' + err); } finally { - revertButton.removeAttribute('disabled'); statusLabel.textContent = ''; } } diff --git a/demo/src/utils/editor-history.util.ts b/demo/src/utils/editor-history.util.ts index 33d1916..65ca38a 100644 --- a/demo/src/utils/editor-history.util.ts +++ b/demo/src/utils/editor-history.util.ts @@ -1,17 +1,20 @@ const AVRJS8_EDITOR_HISTORY = 'AVRJS8_EDITOR_HISTORY'; export class EditorHistoryUtil { + static hasLocalStorage: boolean = !!window.localStorage; + static storeSnippet(codeSnippet: string) { - if (window.localStorage) { - window.localStorage.setItem(AVRJS8_EDITOR_HISTORY, codeSnippet); - } else throw new Error('no localStorage support'); + if (!EditorHistoryUtil.hasLocalStorage) return; + window.localStorage.setItem(AVRJS8_EDITOR_HISTORY, codeSnippet); } static clearSnippet() { + if (!EditorHistoryUtil.hasLocalStorage) return; localStorage.removeItem(AVRJS8_EDITOR_HISTORY); } static getValue() { + if (!EditorHistoryUtil.hasLocalStorage) return; return localStorage.getItem(AVRJS8_EDITOR_HISTORY); } } -- cgit v1.2.3