Skip to content

Commit 9d2f755

Browse files
committed
oh types where art though
1 parent 2c90c27 commit 9d2f755

File tree

4 files changed

+38
-29
lines changed

4 files changed

+38
-29
lines changed

packages/svelte/src/toolbar/configure.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* toolbar config
3-
* @type {import('./public.d.ts').Config}
3+
* @type {import('./public.d.ts').ResolvedConfig}
44
*/
55
const config = {
66
tools: []
@@ -12,24 +12,27 @@ const config = {
1212
export function configure(options) {
1313
for (const [key, value] of Object.entries(options)) {
1414
if (key === 'tools') {
15-
for (let tool of /** @type {import('./public.d.ts').Config.tools[0][]}*/ value) {
16-
if(typeof tool === 'function') {
17-
tool = tool(); // TODO lazy init?
18-
}
19-
/** @type {import('./public.d.ts').Tool}*/
20-
const existing = config.tools.find((t) => t.name === tool.name);
21-
if (existing) {
22-
for (const [k, v] of Object.entries(tool)) {
23-
existing[k] = v;
24-
}
25-
} else {
26-
config.tools.push(tool);
27-
}
28-
}
15+
continue
2916
} else {
17+
// @ts-expect-error index access
3018
config[key] = value;
3119
}
3220
}
21+
if(options.tools) {
22+
for (let tool of options.tools) {
23+
/** @type {import('./public.d.ts').Tool} */
24+
const resolved_tool = typeof tool === 'function' ? tool() : tool;
25+
const existing = config.tools.find((t) => t.name === resolved_tool.name);
26+
if (existing) {
27+
for (const [k, v] of Object.entries(tool)) {
28+
// @ts-expect-error index access
29+
existing[k] = v;
30+
}
31+
} else {
32+
config.tools.push(resolved_tool);
33+
}
34+
}
35+
}
3336
}
3437

3538
/**

packages/svelte/src/toolbar/public.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@ export interface Config {
1414
position?: 'top' | 'bottom';
1515
tools?: (Tool | ToolFn)[];
1616
}
17+
18+
export interface ResolvedConfig extends Config {
19+
tools: Tool[]
20+
}
Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
import ToolBar from './ToolBar.svelte';
22
import { mount } from 'svelte';
3-
function create_toolbar_host() {
4-
const id = 'svelte-toolbar-host';
5-
if (document.getElementById(id) != null) {
6-
console.debug('svelte-toolbar-host already exists, skipping');
7-
return;
8-
}
9-
const el = document.createElement('div');
10-
el.setAttribute('id', id);
11-
// appending to documentElement adds it outside of body
12-
document.documentElement.appendChild(el);
13-
return el;
14-
}
3+
154
export function mountUI() {
16-
if (typeof window !== 'undefined') {
17-
mount(ToolBar, { target: create_toolbar_host() });
5+
if(typeof window !== 'undefined') {
6+
const id = 'svelte-toolbar-host';
7+
if (document.getElementById(id) != null) {
8+
console.debug('svelte-toolbar-host already exists, skipping');
9+
return
10+
}
11+
const el = document.createElement('div');
12+
el.setAttribute('id', id);
13+
// appending to documentElement adds it outside of body
14+
document.documentElement.appendChild(el);
15+
mount(ToolBar, { target: el });
1816
}
1917
}

packages/svelte/types/index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2563,6 +2563,10 @@ declare module 'svelte/toolbar' {
25632563
position?: 'top' | 'bottom';
25642564
tools?: (Tool | ToolFn)[];
25652565
}
2566+
2567+
export interface ResolvedConfig extends Config {
2568+
tools: Tool[]
2569+
}
25662570
export function configure(options: Partial<Config>): void;
25672571

25682572
export function getConfig(): Config;

0 commit comments

Comments
 (0)