@@ -52,13 +52,15 @@ const nameToTitle: Record<Comp, string> = {
5252} ;
5353
5454const repairPlayerSettings = ( settings : Partial < Settings > ) => {
55- const keys = Object . keys ( settings ) . toSorted ( ( a , b ) => ( settings [ a ] ?. position ?? defaultPositions [ a ] ) - ( settings [ b ] ?. position ?? defaultPositions [ b ] ) ) ;
55+ const keys = Object . keys ( defaultPositions ) . toSorted ( ( a , b ) => ( settings [ a ] ?. position ?? defaultPositions [ a ] ) - ( settings [ b ] ?. position ?? defaultPositions [ b ] ) ) ;
5656 const left = keys . filter ( i => ( settings [ i ] ?. position ?? defaultPositions [ i ] ) >= 0 ) ;
5757 const right = keys . filter ( i => ( settings [ i ] ?. position ?? defaultPositions [ i ] ) < 0 ) ;
5858 for ( let i = 0 ; i < left . length ; ++ i ) {
59+ if ( settings [ left [ i ] ] === undefined ) settings [ left [ i ] ] = { } ;
5960 settings [ left [ i ] ] . position = i ;
6061 }
6162 for ( let i = 0 ; i < right . length ; ++ i ) {
63+ if ( settings [ right [ i ] ] === undefined ) settings [ right [ i ] ] = { } ;
6264 settings [ right [ i ] ] . position = - right . length + i ;
6365 }
6466} ;
@@ -115,22 +117,35 @@ const render = (settings: Partial<Settings>, selected: Comp = undefined) => {
115117 } ) ;
116118 const updateWithPos = async ( oldPos : number , newPos : number ) => {
117119 if ( oldPos === newPos ) return ;
118- for ( const other of sorted ) {
119- if ( ( settings [ other ] ?. position ?? defaultPositions [ other ] ) !== newPos ) continue ;
120+ const toReplace = ( newPos < oldPos ? sorted . toReversed ( ) . filter ( other => ( settings [ other ] ?. position ?? defaultPositions [ other ] ) <= newPos ) : sorted . filter ( other => ( settings [ other ] ?. position ?? defaultPositions [ other ] ) >= newPos ) ) . shift ( ) ;
121+ if ( toReplace !== undefined ) {
122+ const other = toReplace ;
120123 if ( ! ( other in settings ) ) settings [ other ] = { } ;
121124 settings [ other ] . position = oldPos ;
122125 if ( ! ( selected in settings ) ) settings [ selected ] = { } ;
123126 settings [ selected ] . position = newPos ;
127+ repairPlayerSettings ( settings ) ;
124128 wrapper . replaceWith ( render ( settings , selected ) ) ;
125129 await setToStorage ( { playerSettings : settings } ) ;
126130 return ;
131+ } else if ( Math . sign ( oldPos ) === Math . sign ( newPos ) ) {
132+ // most common is for hidden elements to cause this
133+ // when they are pushed towards the edge
134+ // e.g. time ... chromecast pip subtitles
135+ // move subtitles left, tries to swap with chromecast
136+ // and pip instead of switching sides
137+ const { min, max } = minMaxPos ( settings , true ) ;
138+ newPos = curPos < 0 ? max + 1 : min - 1 ;
139+ }
140+ const { min, max } = minMaxPos ( settings , true ) ;
141+ if ( newPos < min || newPos > max ) {
142+ // moved to other side, new position
143+ if ( ! ( selected in settings ) ) settings [ selected ] = { } ;
144+ settings [ selected ] . position = newPos ;
145+ repairPlayerSettings ( settings ) ;
146+ wrapper . replaceWith ( render ( settings , selected ) ) ;
147+ await setToStorage ( { playerSettings : settings } ) ;
127148 }
128- if ( Math . sign ( oldPos ) === Math . sign ( newPos ) ) return ; // edge, don't move
129- // otherwise moved to other side, new position
130- if ( ! ( selected in settings ) ) settings [ selected ] = { } ;
131- settings [ selected ] . position = newPos ;
132- wrapper . replaceWith ( render ( settings , selected ) ) ;
133- await setToStorage ( { playerSettings : settings } ) ;
134149 } ;
135150 const left = wrapper . appendChild ( document . createElement ( 'button' ) ) ;
136151 left . className = 'enhancer-button' ;
0 commit comments