sh1k4ku commited on
Commit
7255c53
Β·
verified Β·
1 Parent(s): c61080a

Add force-save & live preview button to assignment3.html export bar

Browse files
Files changed (1) hide show
  1. assignment3.html +47 -10
assignment3.html CHANGED
@@ -223,12 +223,22 @@ InventoryService PaymentGateway</pre>
223
  </div><!-- /.layout -->
224
  </div>
225
 
 
 
 
 
 
 
 
 
 
226
  <!-- ============ sticky export bar ============ -->
227
  <div class="exportbar">
228
  <div class="sid">Student ID <input class="txt" id="studentId" placeholder="z1234567" style="width:130px"></div>
229
  <div class="spacer"></div>
230
  <span class="toast" id="toast"></span>
231
  <button class="ghost" id="clearBtn">Clear saved</button>
 
232
  <button class="ghost" id="copyBtn">Copy JSON</button>
233
  <button id="exportBtn">– Export JSON</button>
234
  </div>
@@ -501,28 +511,55 @@ document.getElementById("exportBtn").onclick = () => {
501
  const sid = document.getElementById("studentId").value.trim() || "submission";
502
  a.href = URL.createObjectURL(blob); a.download = `assignment3_${sid}.json`;
503
  a.click(); URL.revokeObjectURL(a.href); toast("Exported βœ“");
 
504
  };
505
  document.getElementById("copyBtn").onclick = async () => {
506
  try { await navigator.clipboard.writeText(JSON.stringify(buildJSON(), null, 2)); toast("Copied βœ“"); }
507
  catch(_){ toast("Copy failed"); }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
508
  };
509
 
510
  // ============ local session (auto-save / restore) ============
511
  const SESSION_KEY = "st_a3_session_v1";
512
  let __saveTimer = null;
 
 
 
 
 
 
 
 
 
 
 
 
 
513
  function saveSession(){
514
  clearTimeout(__saveTimer);
515
  __saveTimer = setTimeout(() => {
516
- try {
517
- const d = {
518
- ts: Date.now(),
519
- studentId: document.getElementById("studentId").value,
520
- q11: answers.q11, q12: answers.q12, q21: answers.q21, q22: answers.q22,
521
- q3: answers.q3, q41: answers.q41, q43: answers.q43, q44: answers.q44, q44assert: answers.q44assert,
522
- q42code: q42editor.getValue(),
523
- };
524
- localStorage.setItem(SESSION_KEY, JSON.stringify(d));
525
- } catch(_){}
526
  }, 250);
527
  }
528
  function restoreRadio(name, val){
 
223
  </div><!-- /.layout -->
224
  </div>
225
 
226
+ <!-- ============ live preview panel ============ -->
227
+ <div id="previewPanel" style="display:none;position:fixed;left:0;right:0;bottom:52px;max-height:45vh;overflow:auto;background:#181825;border-top:1px solid var(--border);padding:12px 28px;z-index:9">
228
+ <div style="display:flex;align-items:center;gap:10px;margin-bottom:6px">
229
+ <b style="font-size:12px;color:var(--muted)">Live preview β€” exactly what Copy/Export will send (updates as you type)</b>
230
+ <button class="ghost" id="previewCloseBtn" style="margin-left:auto;padding:2px 10px;font-size:11px">Close</button>
231
+ </div>
232
+ <pre id="previewJson" style="margin:0;font-size:11px;white-space:pre-wrap"></pre>
233
+ </div>
234
+
235
  <!-- ============ sticky export bar ============ -->
236
  <div class="exportbar">
237
  <div class="sid">Student ID <input class="txt" id="studentId" placeholder="z1234567" style="width:130px"></div>
238
  <div class="spacer"></div>
239
  <span class="toast" id="toast"></span>
240
  <button class="ghost" id="clearBtn">Clear saved</button>
241
+ <button class="ghost" id="previewBtn">πŸ” Force save &amp; preview</button>
242
  <button class="ghost" id="copyBtn">Copy JSON</button>
243
  <button id="exportBtn">– Export JSON</button>
244
  </div>
 
511
  const sid = document.getElementById("studentId").value.trim() || "submission";
512
  a.href = URL.createObjectURL(blob); a.download = `assignment3_${sid}.json`;
513
  a.click(); URL.revokeObjectURL(a.href); toast("Exported βœ“");
514
+ if (__previewOpen) renderPreview();
515
  };
516
  document.getElementById("copyBtn").onclick = async () => {
517
  try { await navigator.clipboard.writeText(JSON.stringify(buildJSON(), null, 2)); toast("Copied βœ“"); }
518
  catch(_){ toast("Copy failed"); }
519
+ if (__previewOpen) renderPreview();
520
+ };
521
+
522
+ // ============ live preview / force-save ============
523
+ // If you're ever unsure whether Copy/Export reflects your latest edit, use
524
+ // this: it immediately (no debounce) writes the session and shows exactly
525
+ // the JSON buildJSON() currently computes -- the same data Copy/Export send.
526
+ let __previewOpen = false;
527
+ function renderPreview(){
528
+ document.getElementById("previewJson").textContent = JSON.stringify(buildJSON(), null, 2);
529
+ }
530
+ document.getElementById("previewBtn").onclick = () => {
531
+ flushSession();
532
+ renderPreview();
533
+ __previewOpen = true;
534
+ document.getElementById("previewPanel").style.display = "block";
535
+ toast("Saved & previewed βœ“");
536
+ };
537
+ document.getElementById("previewCloseBtn").onclick = () => {
538
+ __previewOpen = false;
539
+ document.getElementById("previewPanel").style.display = "none";
540
  };
541
 
542
  // ============ local session (auto-save / restore) ============
543
  const SESSION_KEY = "st_a3_session_v1";
544
  let __saveTimer = null;
545
+ function flushSession(){
546
+ clearTimeout(__saveTimer);
547
+ try {
548
+ const d = {
549
+ ts: Date.now(),
550
+ studentId: document.getElementById("studentId").value,
551
+ q11: answers.q11, q12: answers.q12, q21: answers.q21, q22: answers.q22,
552
+ q3: answers.q3, q41: answers.q41, q43: answers.q43, q44: answers.q44, q44assert: answers.q44assert,
553
+ q42code: q42editor.getValue(),
554
+ };
555
+ localStorage.setItem(SESSION_KEY, JSON.stringify(d));
556
+ } catch(_){}
557
+ }
558
  function saveSession(){
559
  clearTimeout(__saveTimer);
560
  __saveTimer = setTimeout(() => {
561
+ flushSession();
562
+ if (__previewOpen) renderPreview();
 
 
 
 
 
 
 
 
563
  }, 250);
564
  }
565
  function restoreRadio(name, val){