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 11/**
22 * toolbar config
3- * @type {import('./public.d.ts').Config }
3+ * @type {import('./public.d.ts').ResolvedConfig }
44 */
55const config = {
66 tools : [ ]
@@ -12,24 +12,27 @@ const config = {
1212export 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/**
Original file line number Diff line number Diff 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+ }
Original file line number Diff line number Diff line change 11import ToolBar from './ToolBar.svelte' ;
22import { 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+
154export 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}
Original file line number Diff line number Diff 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 ;
You can’t perform that action at this time.
0 commit comments