mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 01:28:55 +00:00
feat(plugins): add session storage api for plugin bridge
This commit is contained in:
@@ -76,6 +76,27 @@ const data = await trek.invoke('/status') // calls your own route, host-proxie
|
||||
trek.notify('success', 'Saved')
|
||||
```
|
||||
|
||||
### Keep UI state for this browser tab
|
||||
|
||||
The opaque frame cannot use browser storage directly. The bridge provides
|
||||
host-managed, JSON-only session state instead. It survives an iframe remount and
|
||||
page reload in the same browser tab, but is not durable data. Do not use it to
|
||||
store secrets.
|
||||
|
||||
```js
|
||||
// Default: this user + this plugin + this browser tab.
|
||||
await trek.session.set('dismissed-onboarding', true)
|
||||
const dismissed = await trek.session.get('dismissed-onboarding')
|
||||
|
||||
// Explicitly partition state by the trip in view (fails without a trip context).
|
||||
await trek.session.set('filters', ['flight', 'hotel'], { scope: 'trip' })
|
||||
```
|
||||
|
||||
`get()` returns `undefined` for a missing key. `set`, `get`, `remove`, and
|
||||
`clear` accept `{ scope: 'plugin' | 'trip' }`; `plugin` is the default. Use your
|
||||
plugin's own logical key (for example `reservation:88:expanded`) for any finer
|
||||
partitioning.
|
||||
|
||||
The kit applies the theme, mirrors the appearance flags (reduced-motion,
|
||||
no-transparency) and auto-reports your height. It also upgrades any native
|
||||
`<select>` into a host-styled, keyboard-accessible dropdown that matches TREK —
|
||||
|
||||
@@ -664,6 +664,22 @@ function ctx(){
|
||||
}
|
||||
function postCtx(){ if(f.contentWindow) f.contentWindow.postMessage(ctx(),"*"); }
|
||||
var tt; function toast(msg){var el=document.getElementById("toast");el.textContent=msg;el.classList.add("on");clearTimeout(tt);tt=setTimeout(function(){el.classList.remove("on");},2200);}
|
||||
var SESSION_PREFIX="trek:plugin-session:1:"+encodeURIComponent(${JSON.stringify(id)})+":";
|
||||
function sessionMessage(m){
|
||||
var trip=ctx().tripId, scope=m.scope==="trip"?"trip":"plugin";
|
||||
if(scope==="trip"&&!trip){f.contentWindow.postMessage({type:"trek:error",requestId:m.requestId,code:"NO_TRIP_CONTEXT",message:"trip session storage requires a trip context"},"*");return;}
|
||||
var prefix=SESSION_PREFIX+scope+(scope==="trip"?":"+encodeURIComponent(String(trip)):"")+":";
|
||||
var data;
|
||||
if(m.type==="trek:session:clear"){
|
||||
var doomed=[];for(var i=0;i<sessionStorage.length;i++){var stored=sessionStorage.key(i);if(stored&&stored.indexOf(prefix)===0)doomed.push(stored);}doomed.forEach(function(stored){sessionStorage.removeItem(stored);});
|
||||
} else {
|
||||
var key=prefix+encodeURIComponent(m.key);
|
||||
if(m.type==="trek:session:get"){var raw=sessionStorage.getItem(key);data=raw===null?undefined:JSON.parse(raw);}
|
||||
else if(m.type==="trek:session:remove"){sessionStorage.removeItem(key);}
|
||||
else {sessionStorage.setItem(key,JSON.stringify(m.value));}
|
||||
}
|
||||
f.contentWindow.postMessage({type:"trek:response",requestId:m.requestId,data:data},"*");
|
||||
}
|
||||
window.addEventListener("message", function(ev){
|
||||
if(ev.source!==f.contentWindow) return;
|
||||
var m=ev.data; if(!m||typeof m!=="object") return;
|
||||
@@ -697,6 +713,9 @@ window.addEventListener("message", function(ev){
|
||||
}
|
||||
else { f.contentWindow.postMessage({type:"trek:geolocation:result",requestId:m.requestId,position:pos},"*"); }
|
||||
}
|
||||
else if(m.type==="trek:session:get"||m.type==="trek:session:set"||m.type==="trek:session:remove"||m.type==="trek:session:clear"){
|
||||
sessionMessage(m);
|
||||
}
|
||||
});
|
||||
var geoTick=null;
|
||||
["theme","accent","rm","nt","blur","trip"].forEach(function(id){ val(id).addEventListener("change",postCtx); });
|
||||
|
||||
@@ -457,9 +457,41 @@ export const TREK_THEME_JS = `(function () {
|
||||
mount: function (node, target) { (target || document.body).appendChild(node); return node; }
|
||||
};
|
||||
|
||||
var session = {
|
||||
get: function (key, opts) {
|
||||
var id = 's' + (++seq);
|
||||
return new Promise(function (resolve, reject) {
|
||||
pending[id] = { resolve: resolve, reject: reject };
|
||||
send({ type: 'trek:session:get', requestId: id, key: key, scope: opts && opts.scope });
|
||||
});
|
||||
},
|
||||
set: function (key, value, opts) {
|
||||
var id = 's' + (++seq);
|
||||
return new Promise(function (resolve, reject) {
|
||||
pending[id] = { resolve: resolve, reject: reject };
|
||||
send({ type: 'trek:session:set', requestId: id, key: key, value: value, scope: opts && opts.scope });
|
||||
});
|
||||
},
|
||||
remove: function (key, opts) {
|
||||
var id = 's' + (++seq);
|
||||
return new Promise(function (resolve, reject) {
|
||||
pending[id] = { resolve: resolve, reject: reject };
|
||||
send({ type: 'trek:session:remove', requestId: id, key: key, scope: opts && opts.scope });
|
||||
});
|
||||
},
|
||||
clear: function (opts) {
|
||||
var id = 's' + (++seq);
|
||||
return new Promise(function (resolve, reject) {
|
||||
pending[id] = { resolve: resolve, reject: reject };
|
||||
send({ type: 'trek:session:clear', requestId: id, scope: opts && opts.scope });
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
var api = {
|
||||
context: null,
|
||||
ui: ui,
|
||||
session: session,
|
||||
ready: function () { send({ type: 'trek:ready' }); },
|
||||
requestContext: function () { send({ type: 'trek:context:request' }); },
|
||||
onContext: function (cb) {
|
||||
|
||||
@@ -27,6 +27,9 @@ describe('design kit', () => {
|
||||
expect(TREK_THEME_JS).toContain('trek:resize');
|
||||
// Trusts only the real parent window (opaque frame has a 'null' origin).
|
||||
expect(TREK_THEME_JS).toContain('ev.source !== window.parent');
|
||||
// Host-managed tab state uses the same request/response channel.
|
||||
expect(TREK_THEME_JS).toContain("trek:session:get");
|
||||
expect(TREK_THEME_JS).toContain('session: session');
|
||||
});
|
||||
|
||||
it('never contains a closing tag that would break <style>/<script> inlining', () => {
|
||||
|
||||
Reference in New Issue
Block a user