@@ -6,16 +6,26 @@ import RegistryLookup from '../loader/registry/index.js'
66import CustomEmitter from './custom.js'
77import { ClearableEmitter } from './index.js'
88
9- type AddOptions =
10- | {
11- after : IdInput < ItemId >
12- }
13- | {
14- before : IdInput < ItemId >
15- }
9+ interface TabOptions {
10+ file ?: IdInput
11+ }
12+
13+ interface TabOptionsWithAfter extends TabOptions {
14+ after : IdInput < ItemId >
15+ }
16+
17+ interface TabOptionsWithBefore extends TabOptions {
18+ before : IdInput < ItemId >
19+ }
20+
21+ type AddOptions = TabOptionsWithAfter | TabOptionsWithBefore | TabOptions
1622
1723export interface PolytoneTabs {
18- remove ( tab : IdInput < CreativeModeTabId > | IdInput < CreativeModeTabId > [ ] , items : IdInput < ItemId > [ ] ) : void
24+ remove (
25+ tab : IdInput < CreativeModeTabId > | IdInput < CreativeModeTabId > [ ] ,
26+ items : IdInput < ItemId > [ ] ,
27+ options ?: TabOptions
28+ ) : void
1929 add (
2030 tab : IdInput < CreativeModeTabId > | IdInput < CreativeModeTabId > [ ] ,
2131 items : IdInput < ItemId > [ ] ,
@@ -54,16 +64,25 @@ function mergeModifiers(a: PolytoneTabModifier, b: PolytoneTabModifier): Polyton
5464 }
5565}
5666
57- function translateOptions ( options ?: AddOptions ) : Partial < AdditionEntry > {
58- if ( ! options ) return { }
59- const before = 'before' in options
60- const item = before ? options . before : options . after
61- return {
62- before,
63- predicate : {
64- items : [ encodeId ( item ) ] ,
65- type : 'items_match' ,
66- } ,
67+ function translateOptions ( options : AddOptions = { } ) : Partial < AdditionEntry > {
68+ if ( 'before' in options ) {
69+ return {
70+ before : true ,
71+ predicate : {
72+ items : [ encodeId ( options . before ) ] ,
73+ type : 'items_match' ,
74+ } ,
75+ }
76+ } else if ( 'after' in options ) {
77+ return {
78+ before : false ,
79+ predicate : {
80+ items : [ encodeId ( options . after ) ] ,
81+ type : 'items_match' ,
82+ } ,
83+ }
84+ } else {
85+ return { }
6786 }
6887}
6988
@@ -88,16 +107,32 @@ export default class PolytoneTabsEmitter implements PolytoneTabs, ClearableEmitt
88107 await Promise . all ( [ this . entries . emit ( acceptor ) , this . tabs . emit ( acceptor ) ] )
89108 }
90109
91- remove ( tab : IdInput < CreativeModeTabId > | IdInput < CreativeModeTabId > [ ] , items : IdInput < ItemId > [ ] ) {
92- this . forEach ( tab , {
93- removals : [ { type : 'items_match' , items : items . map ( encodeId ) } ] ,
94- } )
110+ remove (
111+ tab : IdInput < CreativeModeTabId > | IdInput < CreativeModeTabId > [ ] ,
112+ items : IdInput < ItemId > [ ] ,
113+ options : TabOptions = { }
114+ ) {
115+ this . forEach (
116+ tab ,
117+ {
118+ removals : [ { type : 'items_match' , items : items . map ( encodeId ) } ] ,
119+ } ,
120+ options
121+ )
95122 }
96123
97- add ( tab : IdInput < CreativeModeTabId > | IdInput < CreativeModeTabId > [ ] , items : IdInput < ItemId > [ ] , options ?: AddOptions ) {
98- this . forEach ( tab , {
99- additions : [ { items : items . map ( encodeId ) , ...translateOptions ( options ) } ] ,
100- } )
124+ add (
125+ tab : IdInput < CreativeModeTabId > | IdInput < CreativeModeTabId > [ ] ,
126+ items : IdInput < ItemId > [ ] ,
127+ options : AddOptions = { }
128+ ) {
129+ this . forEach (
130+ tab ,
131+ {
132+ additions : [ { items : items . map ( encodeId ) , ...translateOptions ( options ) } ] ,
133+ } ,
134+ options
135+ )
101136 }
102137
103138 create ( id : IdInput ) {
@@ -109,14 +144,16 @@ export default class PolytoneTabsEmitter implements PolytoneTabs, ClearableEmitt
109144
110145 private forEach (
111146 tab : IdInput < CreativeModeTabId > | IdInput < CreativeModeTabId > [ ] ,
112- modifier : Omit < PolytoneTabModifier , 'targets' >
147+ modifier : Omit < PolytoneTabModifier , 'targets' > ,
148+ options : TabOptions
113149 ) {
114150 arrayOrSelf ( tab ) . forEach ( target => {
151+ const file = options . file ?? target
115152 const entry : PolytoneTabModifier = {
116153 targets : [ encodeId ( target ) ] ,
117154 ...modifier ,
118155 }
119- this . entries . merge ( target , entry , mergeModifiers )
156+ this . entries . merge ( file , entry , mergeModifiers )
120157 } )
121158 }
122159}
0 commit comments