MediaWiki:Gadget-visual-editor-novedelete.js

De Wikimedica

Note : après avoir enregistré vos modifications, il se peut que vous deviez forcer le rechargement complet du cache de votre navigateur pour voir les changements.

/* This gadget prevents deletion of elements in the visual editor that include the __NOVEDELETE__ behavior switch. */

mw.libs.ve.targetLoader.addPlugin(function(target) {
	
	console.log("Loading visual-editor-novedelete");
	
	mw.hook( 've.activationComplete' ).add( function () {
	    ve.init.target.getSurface().getModel().on('undoStackChange', function() { 
	
		    var undoStack = ve.init.target.getSurface().getModel().undoStack;
		    var change = undoStack[undoStack.length - 1 - ve.init.target.getSurface().getModel().undoIndex];
		    
		    if (typeof change === 'undefined') { return; } // Change is undefined.
			
			// If there were two transactions, this means the transaction was a move (replace and insert), which means no element was deleted.
			if(change.transactions.length > 1) { return; }
			
		    var operations = change.transactions[0].operations;
	
		    operations.forEach(function(operation){
		    	
		        if (operation.type !== "replace"){ return; }
	            if(!operation.remove.length) { return; } // If no text was removed.
	            
                // Searching undeletable content within removed items
                operation.remove.forEach(function(removedItem) {
                	
					// Only check for deleted templates
                    if (removedItem.hasOwnProperty('type') && removedItem.type == 'mwTransclusionBlock'){
                    	
                        var templateName = Object.values(removedItem.attributes.mw.parts).find(function(removedItemPart){return typeof removedItemPart.template !== 'undefined'}).template.target.wt; // Get the name of the deleted template.
                        var model = ve.init.target.getSurface().getModel();
                        var dom = $(model.getDocument().getStore().value(removedItem.originalDomElementsHash)); // Get the DOM item from the hash store.
                        
                        // Parse for __NOVEDELETE__ pattern here using normal jQuery syntax.
                        if($(dom).filter(':contains(__NOVEDELETE__)').length) {
							
							OO.ui.alert( '', {
								  title: 'Un modèle ne peut pas être supprimé',
								  message: 'Vous avez probablement tenter de supprimé le modèle ' + templateName + ' par erreur. Ce modèle ne peut pas être supprimé avec l\'éditeur visuel. Pour le faire, il faut passer par le wikicode. N\'hésitez pas à demander de l\'aide sur le forum si vous ne savez pas pourquoi vous recevez ce message d\'erreur.',
								  actions: [
									    {
									      action: 'accept',
									      label: 'Ok',
									      flags: 'destructive'
									    }
								  ]
							});
							
		                    model.undo(); // Undo the delete.
		                    model.truncateUndoStack(); // Do not let users redo the delete.
                        }
                    }
                });
		    });
		});
	} );
});