User:Ravid ziv/veReplace.js

function extractText{ var nodes = []; var model = ve.instances[0].model; function getTextNodes( obj ) { var i;		for ( i = 0; i < obj.children.length; i++ ) { if ( obj.children[i].type == 'text'){ nodes.push(obj.children[i]); }

if ( obj.children[i].children ) { getTextNodes( obj.children[i] ); }		}	}	getTextNodes(ve.instances[0].model.documentModel.getDocumentNode); return nodes; }

function seacrhAndReplace(fromText,toText ) { var textNodes = extractText; var model = ve.instances[0].model; var firstIndex = 0; for (var nodeI=0;nodeI<textNodes.length; nodeI++) { var node = textNodes[nodeI]; var nodeRange = node.getRange; var nodeData = model.getFragment(nodeRange).getData; var nodeText = $.map(nodeData, function(e){ 				if ( e instanceof Array){return e[0]} else {return e} 			}).join('');

var fromIndex = nodeText.indexOf( fromText, firstIndex ); if ( fromIndex == -1 ) { firstIndex = 0; continue; }		var start = nodeRange.from+fromIndex; var end = start+fromText.length; var removeRange = new ve.Range( start, end ); var transaction = ve.dm.Transaction.newFromReplacement(			ve.instances[0].view.documentView.model, 			removeRange, 			toText		); var newSelection = new ve.Range(0,0); ve.instances[0].view.changeModel(transaction, newSelection); firstIndex = fromIndex + toText.length; nodeI = nodeI -1; } }

ve.ui.SearchAndReplaceDialog = function( surface, config ) { // Configuration initialization config = ve.extendObject( { 'size': 'medium' }, config );

// Parent constructor ve.ui.Dialog.call( this, surface, config );

// Properties this.sanityCheckVerified = false; this.editSummaryByteLimit = 255; this.restoring = false; this.messages = {}; this.setupDeferred = $.Deferred; };

ve.ui.SearchAndReplaceDialog.prototype.initialize = function { ve.ui.Dialog.prototype.initialize.call( this ); this.panel = new OO.ui.PanelLayout( { '$': this.$, 'scrollable': true } ); // input from this.fromInput = new OO.ui.TextInputWidget(		{ '$': this.$, 'multiline': false, 'placeholder': 'From text' }	); //input to	this.toInput = new OO.ui.TextInputWidget(		{ '$': this.$, 'multiline': false, 'placeholder': 'To text' }	); // do	this.replaceButton = new OO.ui.ButtonWidget( {		'label': 'Do replace!',		'flags': ['constructive']	} ); this.replaceButton.connect( this, { 'click': function{		seacrhAndReplace('Hebrew', 'English');	} } ); this.panel.$element.append(		this.fromInput.$element,		this.toInput.$element	); this.$body.append( this.panel.$element ); this.$foot.append(		this.replaceButton.$element	);

} /* Inheritance */

OO.inheritClass( ve.ui.SearchAndReplaceDialog, ve.ui.Dialog );

/* Static Properties */

ve.ui.SearchAndReplaceDialog.static.name = 'search'; ve.ui.SearchAndReplaceDialog.static.title = 'Search and replace';

ve.ui.dialogFactory.register( ve.ui.SearchAndReplaceDialog );

//-- replace tool -- function ReplaceTool( toolGroup, config ) { OO.ui.Tool.call( this, toolGroup, config ); } OO.inheritClass( ReplaceTool, OO.ui.Tool ); ReplaceTool.static.name = 'ReplaceTool'; ReplaceTool.static.title = 'Replace' ReplaceTool.prototype.onSelect = function { this.toolbar.getSurface.execute('dialog', 'open', 'search', null); }; ReplaceTool.prototype.onUpdateState = function { this.setActive( false ); }; ve.ui.toolFactory.register( ReplaceTool );