File tree Expand file tree Collapse file tree 4 files changed +38
-29
lines changed Expand file tree Collapse file tree 4 files changed +38
-29
lines changed Original file line number Diff line number Diff line change 1
1
/**
2
2
* toolbar config
3
- * @type {import('./public.d.ts').Config }
3
+ * @type {import('./public.d.ts').ResolvedConfig }
4
4
*/
5
5
const config = {
6
6
tools : [ ]
@@ -12,24 +12,27 @@ const config = {
12
12
export function configure ( options ) {
13
13
for ( const [ key , value ] of Object . entries ( options ) ) {
14
14
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
29
16
} else {
17
+ // @ts -expect-error index access
30
18
config [ key ] = value ;
31
19
}
32
20
}
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
+ }
33
36
}
34
37
35
38
/**
Original file line number Diff line number Diff line change @@ -14,3 +14,7 @@ export interface Config {
14
14
position ?: 'top' | 'bottom' ;
15
15
tools ?: ( Tool | ToolFn ) [ ] ;
16
16
}
17
+
18
+ export interface ResolvedConfig extends Config {
19
+ tools : Tool [ ]
20
+ }
Original file line number Diff line number Diff line change 1
1
import ToolBar from './ToolBar.svelte' ;
2
2
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
+
15
4
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 } ) ;
18
16
}
19
17
}
Original file line number Diff line number Diff line change @@ -2563,6 +2563,10 @@ declare module 'svelte/toolbar' {
2563
2563
position ?: 'top' | 'bottom' ;
2564
2564
tools ?: ( Tool | ToolFn ) [ ] ;
2565
2565
}
2566
+
2567
+ export interface ResolvedConfig extends Config {
2568
+ tools : Tool [ ]
2569
+ }
2566
2570
export function configure ( options : Partial < Config > ) : void ;
2567
2571
2568
2572
export function getConfig ( ) : Config ;
You can’t perform that action at this time.
0 commit comments