משתמש:Guycn2/SaveSearchReplaceDialogPosition.js

הערה: לאחר הפרסום, ייתכן שיהיה צורך לנקות את זיכרון המטמון (cache) של הדפדפן כדי להבחין בשינויים.

  • פיירפוקס / ספארי: להחזיק את המקש Shift בעת לחיצה על טעינה מחדש (Reload) או ללחוץ על צירוף המקשים Ctrl-F5 או Ctrl-R (במחשב מק: ⌘-R).
  • גוגל כרום: ללחוץ על צירוף המקשים Ctrl-Shift-R (במחשב מק: ⌘-Shift-R).
  • אינטרנט אקספלורר / אדג': להחזיק את המקש Ctrl בעת לחיצה על רענן (Refresh) או ללחוץ על צירוף המקשים Ctrl-F5.
  • אופרה: ללחוץ על Ctrl-F5.
$( () => {
	
	'use strict';
	
	if ( ![ 'edit', 'submit' ].includes( mw.config.get( 'wgAction' ) ) ) {
		return;
	}
	
	const mutationObserver = new MutationObserver( mutationEntries => {
		
		for ( const mutationEntry of mutationEntries ) {
			
			if (
				mutationEntry &&
				mutationEntry.addedNodes &&
				mutationEntry.addedNodes[ 0 ] &&
				mutationEntry.addedNodes[ 0 ].id === 'wikieditor-toolbar-replace-dialog'
			) {
				
				mutationObserver.disconnect();
				
				const dialogSelector =
					'.wikiEditor-toolbar-dialog:has( #wikieditor-toolbar-replace-dialog )';
				
				const $outerDialog = $( dialogSelector );
				
				if ( $outerDialog.length ) {
					
					const $innerDialog =
						$outerDialog.children( '#wikieditor-toolbar-replace-dialog' );
					
					const intersectionObserver =
						new IntersectionObserver( intersectionEntries => {
							if ( intersectionEntries[ 0 ].isIntersecting ) {
								applyStyle( dialogSelector, $outerDialog, $innerDialog );
							}
						} );
					
					intersectionObserver.observe( $outerDialog[ 0 ] );
					
					const $buttonset = $outerDialog.find( '.ui-dialog-buttonset' );
					const $saveBtn = $buttonset.children( 'button' ).eq( 0 ).clone();
					
					$saveBtn
					.prependTo( $buttonset )
					.on( 'click', () => storeSize( $outerDialog, $innerDialog ) )
					.children( '.ui-button-text' ).text( 'שמירת גודל' );
					
				}
				
				break;
				
			}
			
		}
	
	} );
	
	mutationObserver.observe( document.body, { childList: true } );
	
	async function applyStyle( dialogSelector, $outerDialog, $innerDialog ) {
		
		await mw.loader.using( [ 'mediawiki.storage', 'mediawiki.util' ] );
		
		mw.util.addCSS( `${ dialogSelector } { position: fixed !important; }` );
		
		$outerDialog.css( { top: 0, left: 0 } );
		
		const storedValues = mw.storage.getObject( 'search-replace-dialog-size' );
			
		if ( storedValues ) {
			
			$outerDialog.css( {
				width: storedValues.outerDialogWidth || '500px',
				height: storedValues.outerDialogHeight || 'auto'
			} );
			
			$innerDialog.css( {
				width: storedValues.innerDialogWidth || 'auto',
				height: storedValues.innerDialogHeight || 'auto'
			} );
			
		}
		
	}
	
	function storeSize( $outerDialog, $innerDialog ) {
		
		const values = {
			outerDialogWidth: $outerDialog.css( 'width' ) || '500px',
			outerDialogHeight: $outerDialog.css( 'height' ) || 'auto',
			innerDialogWidth: $innerDialog.css( 'width' ) || 'auto',
			innerDialogHeight: $innerDialog.css( 'height' ) || 'auto'
		};
		
		if ( mw.storage.setObject( 'search-replace-dialog-size', values ) ) {
			mw.notify( 'נשמר' );
		}
		
	}
	
} );