oh types where art though

feat/devtool
dominikg 4 months ago
parent 2c90c27eb5
commit 9d2f7550bd

@ -1,6 +1,6 @@
/** /**
* toolbar config * toolbar config
* @type {import('./public.d.ts').Config} * @type {import('./public.d.ts').ResolvedConfig}
*/ */
const config = { const config = {
tools: [] tools: []
@ -12,24 +12,27 @@ const config = {
export function configure(options) { export function configure(options) {
for (const [key, value] of Object.entries(options)) { for (const [key, value] of Object.entries(options)) {
if (key === 'tools') { if (key === 'tools') {
for (let tool of /** @type {import('./public.d.ts').Config.tools[0][]}*/ value) { continue
if(typeof tool === 'function') {
tool = tool(); // TODO lazy init?
}
/** @type {import('./public.d.ts').Tool}*/
const existing = config.tools.find((t) => t.name === tool.name);
if (existing) {
for (const [k, v] of Object.entries(tool)) {
existing[k] = v;
}
} else {
config.tools.push(tool);
}
}
} else { } else {
// @ts-expect-error index access
config[key] = value; config[key] = value;
} }
} }
if(options.tools) {
for (let tool of options.tools) {
/** @type {import('./public.d.ts').Tool} */
const resolved_tool = typeof tool === 'function' ? tool() : tool;
const existing = config.tools.find((t) => t.name === resolved_tool.name);
if (existing) {
for (const [k, v] of Object.entries(tool)) {
// @ts-expect-error index access
existing[k] = v;
}
} else {
config.tools.push(resolved_tool);
}
}
}
} }
/** /**

@ -14,3 +14,7 @@ export interface Config {
position?: 'top' | 'bottom'; position?: 'top' | 'bottom';
tools?: (Tool | ToolFn)[]; tools?: (Tool | ToolFn)[];
} }
export interface ResolvedConfig extends Config {
tools: Tool[]
}

@ -1,19 +1,17 @@
import ToolBar from './ToolBar.svelte'; import ToolBar from './ToolBar.svelte';
import { mount } from 'svelte'; import { mount } from 'svelte';
function create_toolbar_host() {
const id = 'svelte-toolbar-host';
if (document.getElementById(id) != null) {
console.debug('svelte-toolbar-host already exists, skipping');
return;
}
const el = document.createElement('div');
el.setAttribute('id', id);
// appending to documentElement adds it outside of body
document.documentElement.appendChild(el);
return el;
}
export function mountUI() { export function mountUI() {
if (typeof window !== 'undefined') { if(typeof window !== 'undefined') {
mount(ToolBar, { target: create_toolbar_host() }); const id = 'svelte-toolbar-host';
if (document.getElementById(id) != null) {
console.debug('svelte-toolbar-host already exists, skipping');
return
}
const el = document.createElement('div');
el.setAttribute('id', id);
// appending to documentElement adds it outside of body
document.documentElement.appendChild(el);
mount(ToolBar, { target: el });
} }
} }

@ -2563,6 +2563,10 @@ declare module 'svelte/toolbar' {
position?: 'top' | 'bottom'; position?: 'top' | 'bottom';
tools?: (Tool | ToolFn)[]; tools?: (Tool | ToolFn)[];
} }
export interface ResolvedConfig extends Config {
tools: Tool[]
}
export function configure(options: Partial<Config>): void; export function configure(options: Partial<Config>): void;
export function getConfig(): Config; export function getConfig(): Config;

Loading…
Cancel
Save