From 9d2f7550bd5ddef5047b3afb5cc56cf23f119fcb Mon Sep 17 00:00:00 2001 From: dominikg Date: Mon, 5 May 2025 14:40:31 +0200 Subject: [PATCH] oh types where art though --- packages/svelte/src/toolbar/configure.js | 33 +++++++++++++----------- packages/svelte/src/toolbar/public.d.ts | 4 +++ packages/svelte/src/toolbar/runtime.js | 26 +++++++++---------- packages/svelte/types/index.d.ts | 4 +++ 4 files changed, 38 insertions(+), 29 deletions(-) diff --git a/packages/svelte/src/toolbar/configure.js b/packages/svelte/src/toolbar/configure.js index 7ea6282945..ae96c37762 100644 --- a/packages/svelte/src/toolbar/configure.js +++ b/packages/svelte/src/toolbar/configure.js @@ -1,6 +1,6 @@ /** * toolbar config - * @type {import('./public.d.ts').Config} + * @type {import('./public.d.ts').ResolvedConfig} */ const config = { tools: [] @@ -12,24 +12,27 @@ const config = { export function configure(options) { for (const [key, value] of Object.entries(options)) { if (key === 'tools') { - for (let tool of /** @type {import('./public.d.ts').Config.tools[0][]}*/ value) { - 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); - } - } + continue } else { + // @ts-expect-error index access 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); + } + } + } } /** diff --git a/packages/svelte/src/toolbar/public.d.ts b/packages/svelte/src/toolbar/public.d.ts index aa2d16894e..bae54d44b5 100644 --- a/packages/svelte/src/toolbar/public.d.ts +++ b/packages/svelte/src/toolbar/public.d.ts @@ -14,3 +14,7 @@ export interface Config { position?: 'top' | 'bottom'; tools?: (Tool | ToolFn)[]; } + +export interface ResolvedConfig extends Config { + tools: Tool[] +} diff --git a/packages/svelte/src/toolbar/runtime.js b/packages/svelte/src/toolbar/runtime.js index b68e48d122..e6ce38f355 100644 --- a/packages/svelte/src/toolbar/runtime.js +++ b/packages/svelte/src/toolbar/runtime.js @@ -1,19 +1,17 @@ import ToolBar from './ToolBar.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() { - if (typeof window !== 'undefined') { - mount(ToolBar, { target: create_toolbar_host() }); + if(typeof window !== 'undefined') { + 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 }); } } diff --git a/packages/svelte/types/index.d.ts b/packages/svelte/types/index.d.ts index 880a3d7c9a..f4888afee5 100644 --- a/packages/svelte/types/index.d.ts +++ b/packages/svelte/types/index.d.ts @@ -2563,6 +2563,10 @@ declare module 'svelte/toolbar' { position?: 'top' | 'bottom'; tools?: (Tool | ToolFn)[]; } + + export interface ResolvedConfig extends Config { + tools: Tool[] + } export function configure(options: Partial): void; export function getConfig(): Config;