feat(site-builder): block registry, notices, local prefs, project extras, overlay slot
Add an exported block registry (keywords, search, insertBlock) consumed by BlocksPanel, a NoticeProvider/NoticeHost toast stack, a guarded localStorage prefs helper, a ProjectExtras store persisted as project.json 'extras', and a CanvasOverlays mount point. Refs #38 Refs #41 Refs #49 Refs #48 Refs #57 Refs #58 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
97ccbee461
commit
21c2776c88
16 files changed
+869
-215
No files matched your search
@@ -0,0 +1,24 @@
|
||||
import { describe, test, expect, afterEach, vi } from 'vitest';
|
||||
import { getPref, setPref } from './local-prefs';
|
||||
|
||||
afterEach(() => { vi.restoreAllMocks(); window.localStorage.clear(); });
|
||||
|
||||
describe('local-prefs', () => {
|
||||
test('round-trips JSON under the wsb: namespace', () => {
|
||||
setPref('tab', { a: [1, 2] });
|
||||
expect(getPref('tab', null)).toEqual({ a: [1, 2] });
|
||||
expect(window.localStorage.getItem('wsb:tab')).toBe('{"a":[1,2]}');
|
||||
});
|
||||
|
||||
test('missing key and corrupt JSON return the fallback', () => {
|
||||
expect(getPref('nope', 'fb')).toBe('fb');
|
||||
window.localStorage.setItem('wsb:bad', '{not json');
|
||||
expect(getPref('bad', 7)).toBe(7);
|
||||
});
|
||||
|
||||
test('a throwing localStorage returns the fallback and setPref does not throw', () => {
|
||||
vi.spyOn(window, 'localStorage', 'get').mockImplementation(() => { throw new Error('blocked'); });
|
||||
expect(getPref('x', 'fb')).toBe('fb');
|
||||
expect(() => setPref('x', 1)).not.toThrow();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* Per-browser UI preferences (collapse state, remembered tabs). Namespaced
|
||||
* `wsb:`, JSON-encoded. Storage can be missing, blocked or throw (private
|
||||
* mode, sandboxed iframe), so every access is guarded: reads fall back,
|
||||
* writes are silently dropped. Never use this for state that must persist.
|
||||
*/
|
||||
const NS = 'wsb:';
|
||||
|
||||
export function getPref<T>(key: string, fallback: T): T {
|
||||
try {
|
||||
const raw = window.localStorage.getItem(NS + key);
|
||||
if (raw === null) return fallback;
|
||||
return JSON.parse(raw) as T;
|
||||
} catch {
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
|
||||
export function setPref(key: string, value: unknown): void {
|
||||
try {
|
||||
window.localStorage.setItem(NS + key, JSON.stringify(value));
|
||||
} catch {
|
||||
// Storage unavailable or full: the preference simply isn't remembered.
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user