From 4a4b3cf600c63f45385b9e6e12e2973d47743f45 Mon Sep 17 00:00:00 2001 From: George Stagg Date: Wed, 24 Jul 2024 09:52:37 +0100 Subject: [PATCH] Remove/restore focus as console is hidden/shown --- _extensions/drop/drop-runtime.js | 2 +- drop-runtime/src/drop-runtime.ts | 20 ++++++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/_extensions/drop/drop-runtime.js b/_extensions/drop/drop-runtime.js index 5230c93..75e9762 100644 --- a/_extensions/drop/drop-runtime.js +++ b/_extensions/drop/drop-runtime.js @@ -104,7 +104,7 @@ WARNING: This link could potentially be dangerous`)){let f=window.open();if(f){t matplotlib.use("module://matplotlib_pyodide.wasm_backend") import matplotlib.pyplot as plt plt.show() - `),Ut.write("\x1B[2J\x1B[2H\rq");;){let l=await Ut.read(">>> ");await a.writeConsole(l)}})()}window.RevealDrop=window.RevealDrop||{id:"RevealDrop",dropElement:document.createElement("div"),init:function(e){let r=e.getConfig().drop,i=window.RevealDrop.dropElement,n=document.createElement("div");if(n.className="drop-clip",i.className="drop",n.appendChild(i),document.querySelector(".reveal").appendChild(n),r.engine==="webr"?Rw(i,r.webr.packages):r.engine==="pyodide"&&Iw(i,r.pyodide.packages),r.button){let s=document.createElement("div"),a=document.createElement("a");s.className="drop-button",s.id="toggle-drop",a.href="#",a.title=`Toggle console (${r.shortcut})`,a.onclick=o=>{window.RevealDrop.toggleDrop(),o.preventDefault(),o.stopPropagation()},a.innerHTML=Qw(),s.appendChild(a),document.querySelector(".reveal").appendChild(s)}e.addKeyBinding({keyCode:(0,$w.default)(r.shortcut),key:r.shortcut,description:"Toggle console"},()=>{}),document.addEventListener("keydown",s=>{s.key==r.shortcut&&!s.altKey&&(window.RevealDrop.toggleDrop(),e.toggleHelp(!1),e.toggleOverview(!1),e.configure({keyboard:!window.RevealDrop.isActive()}),s.preventDefault(),s.stopPropagation())},{capture:!0})},toggleDrop(){window.RevealDrop.dropElement.classList.toggle("active")},isActive(){return window.RevealDrop.dropElement.classList.contains("active")}}; + `),Ut.write("\x1B[2J\x1B[2H\rq");;){let l=await Ut.read(">>> ");await a.writeConsole(l)}})()}window.RevealDrop=window.RevealDrop||{id:"RevealDrop",focusElement:null,dropElement:document.createElement("div"),init:function(e){let r=e.getConfig().drop,i=window.RevealDrop.dropElement,n=document.createElement("div");if(n.className="drop-clip",i.className="drop",n.appendChild(i),document.querySelector(".reveal").appendChild(n),r.engine==="webr"?Rw(i,r.webr.packages):r.engine==="pyodide"&&Iw(i,r.pyodide.packages),r.button){let s=document.createElement("div"),a=document.createElement("a");s.className="drop-button",s.id="toggle-drop",a.href="#",a.title=`Toggle console (${r.shortcut})`,a.onclick=o=>{window.RevealDrop.toggleDrop(),o.preventDefault(),o.stopPropagation()},a.innerHTML=Qw(),s.appendChild(a),document.querySelector(".reveal").appendChild(s)}e.addKeyBinding({keyCode:(0,$w.default)(r.shortcut),key:r.shortcut,description:"Toggle console"},()=>{}),document.addEventListener("keydown",s=>{s.key==r.shortcut&&!s.altKey&&(window.RevealDrop.toggleDrop(),e.toggleHelp(!1),e.toggleOverview(!1),window.RevealDrop.isActive()?(e.configure({keyboard:!1}),window.RevealDrop.focusElement instanceof HTMLElement&&window.RevealDrop.focusElement.focus()):(e.configure({keyboard:!0}),window.RevealDrop.focusElement=document.activeElement,document.activeElement instanceof HTMLElement&&document.activeElement.blur()),s.preventDefault(),s.stopPropagation())},{capture:!0})},toggleDrop(){window.RevealDrop.dropElement.classList.toggle("active")},isActive(){return window.RevealDrop.dropElement.classList.contains("active")}}; /*! Bundled license information: keycode/index.js: diff --git a/drop-runtime/src/drop-runtime.ts b/drop-runtime/src/drop-runtime.ts index 9629829..0b16516 100644 --- a/drop-runtime/src/drop-runtime.ts +++ b/drop-runtime/src/drop-runtime.ts @@ -8,6 +8,7 @@ declare global { RevealDrop?: { id: string; dropElement: HTMLDivElement; + focusElement: Element | null; init: (reveal: any) => void; toggleDrop: () => void; isActive: () => boolean; @@ -33,6 +34,7 @@ type DropConfig = { window.RevealDrop = window.RevealDrop || { id: 'RevealDrop', + focusElement: null, dropElement: document.createElement('div'), init: function (reveal) { const revealConfig = reveal.getConfig(); @@ -83,12 +85,26 @@ window.RevealDrop = window.RevealDrop || { window.RevealDrop.toggleDrop(); reveal.toggleHelp(false); reveal.toggleOverview(false); - reveal.configure({ keyboard: !window.RevealDrop.isActive() }); + + if (window.RevealDrop.isActive()) { + reveal.configure({ keyboard: false }); + // Restore focus as console is shown + if (window.RevealDrop.focusElement instanceof HTMLElement) { + window.RevealDrop.focusElement.focus(); + } + } else { + reveal.configure({ keyboard: true }); + // Remove focus as console is hidden + window.RevealDrop.focusElement = document.activeElement; + if (document.activeElement instanceof HTMLElement) { + document.activeElement.blur(); + } + } + event.preventDefault(); event.stopPropagation(); } }, { capture: true }); - }, toggleDrop() { window.RevealDrop.dropElement.classList.toggle("active");