[go: up one dir, main page]
More Web Proxy on the site http://driver.im/Jump to content

MediaWiki talk:Gadget-BiDiTools.js

Page contents not supported in other languages.
From Wikipedia!
/*
 * BiDiTools.js v0.1 -- BiDi directional control for MediaWiki
 * Copyright (C) 2008 Gadi Cohen <dragon@wastelands.net>
 * Released under GPL.
 *
 * More details on the Wiki page, but briefly: allows the use of Unicode
 * BiDi directional chars in the editing window to fine tune bidirectionality.
 * Allows switching between Unicode chars and their HTML representations both
 * during editing, and automatically when loading and saving the page (the page
 * is saved with HTML entities to prevent confusion).
 */

if(wgAction == 'edit' || wgAction == 'submit') {
  var textarea = 'wpTextbox1';
} else if(wgCanonicalSpecialPageName == 'Upload') {
  var textarea = 'wpUploadDescription';
}

if(window.textarea) addOnloadHook(biditools_start);
	  
function biditools_end() {
	// convert Unicode BiDI chars to HTML entities before save, etc.
	var txtarea = document.editform.wpTextbox1;
	txtarea.value = biditools_escape(txtarea.value, 1);
}
 
var BiDi_Chars = new Array(
	new Array('LRE', '‪', String.fromCharCode(8234)),
	new Array('RLE', '‫', String.fromCharCode(8235)),
	new Array('PDF', '‬', String.fromCharCode(8236)),
	new Array('LRO', '‭', String.fromCharCode(8237)),
	new Array('RLO', '‮', String.fromCharCode(8238))
);

function biditools_escape(str, unicode2html) {
	for (var i=0; i < BiDi_Chars.length; i++) {
		var pattern = new RegExp(BiDi_Chars[i][unicode2html?2:1], 'g');
		if (str.search(pattern)!=-1)
			str = str.replace(pattern, BiDi_Chars[i][unicode2html?1:2]);
	}
	return str;
}
 
function biditools_createbutton(editform, label, tagOpen, tagClose) {
	var button = document.createElement("input");
	button.type = "button";
	button.value = label;
	button. {
		biditools_insertTags(tagOpen, tagClose, '');
	}
	editform.parentNode.insertBefore(button, editform);
}

function biditools_switchescape(event) {
	var txtarea = document.editform.wpTextbox1;
	var button = document.getElementById('bidi-escape');
	var state = button.value == "BIDI->HTML" ? 1 : 0;
	button.value = state ? "HTML->BIDI" : "BIDI->HTML";
	
	if (document.selection  && document.selection.createRange) { // IE/Opera

		//save window scroll position
		if (document.documentElement && document.documentElement.scrollTop)
			var winScroll = document.documentElement.scrollTop
		else if (document.body)
			var winScroll = document.body.scrollTop;
		
		// do the switch
		txtarea.value = biditools_escape(txtarea.value, state);
		
		//restore window scroll position
		if (document.documentElement && document.documentElement.scrollTop)
			document.documentElement.scrollTop = winScroll
		else if (document.body)
			document.body.scrollTop = winScroll;

	} else if (txtarea.selectionStart || txtarea.selectionStart == '0') { // Mozilla

		//save textarea scroll position
		var textScroll = txtarea.scrollTop;
		
		// do the switch
		txtarea.value = biditools_escape(txtarea.value, state);

		//restore textarea scroll position
		txtarea.scrollTop = textScroll;
	} 
}

function biditools_start() {
	var editform = document.getElementById("editform");

	// Buttons for each Unicode BiDi char
	for (var i=0; i < BiDi_Chars.length; i++) {
		biditools_createbutton(editform, BiDi_Chars[i][0], 
			BiDi_Chars[i][2], BiDi_Chars[2][2]);
	}
	
	// the HTML/BIDI switch button
	var button = document.createElement("input");
	button.type = "button";
	button.value = "BIDI->HTML";
	button.id = 'bidi-escape';
	button.
	editform.parentNode.insertBefore(button, editform);
	
	// as we start, convert BiDi html entities to unicode
	var txtarea = document.editform.wpTextbox1;
	txtarea.value = biditools_escape(txtarea.value, 0);
	
	// convert Unicode to HTML entities on save/preview/upload
	var handlers = new Array('wpSave','wpPreview','wpDiff','wpUpload');
	for(var i=0; i < handlers.length; i++) {
		var btn = getElementByIdOrName(handlers[i]);
		if (btn) addClickHandler(btn, biditools_end);
	}
} 

// modified insertTags, only insert tagOpen on no selection
// quick hack, needs to be cleaned up.
function biditools_insertTags(tagOpen, tagClose, sampleText) {
	var txtarea;
	if (document.editform) {
		txtarea = document.editform.wpTextbox1;
	} else {
		// some alternate form? take the first one we can find
		var areas = document.getElementsByTagName('textarea');
		txtarea = areas[0];
	}
	var selText, isSample = false;

	if (document.selection  && document.selection.createRange) { // IE/Opera

		//save window scroll position
		if (document.documentElement && document.documentElement.scrollTop)
			var winScroll = document.documentElement.scrollTop
		else if (document.body)
			var winScroll = document.body.scrollTop;
		//get current selection  
		txtarea.focus();
		var range = document.selection.createRange();
		selText = range.text;
		//insert tags
		checkSelectedText();
		range.text = tagOpen + (isSample ? '' : selText + tagClose);
		//mark sample text as selected
		if (isSample && range.moveStart) {
			if (window.opera)
				tagClose = tagClose.replace(/\n/g,'');
			range.moveStart('character', - tagClose.length - selText.length); 
			range.moveEnd('character', - tagClose.length); 
		}
		range.select();   
		//restore window scroll position
		if (document.documentElement && document.documentElement.scrollTop)
			document.documentElement.scrollTop = winScroll
		else if (document.body)
			document.body.scrollTop = winScroll;

	} else if (txtarea.selectionStart || txtarea.selectionStart == '0') { // Mozilla

		//save textarea scroll position
		var textScroll = txtarea.scrollTop;
		//get current selection
		txtarea.focus();
		var startPos = txtarea.selectionStart;
		var endPos = txtarea.selectionEnd;
		selText = txtarea.value.substring(startPos, endPos);
		//insert tags
		checkSelectedText();
		txtarea.value = txtarea.value.substring(0, startPos)
			+ tagOpen + (isSample ? '' : selText + tagClose)
			+ txtarea.value.substring(endPos, txtarea.value.length);
		//set new selection
		if (isSample) {
			txtarea.selectionStart = startPos + tagOpen.length;
			txtarea.selectionEnd = startPos + tagOpen.length + selText.length;
		} else {
			txtarea.selectionStart = startPos + tagOpen.length + selText.length + tagClose.length;
			txtarea.selectionEnd = txtarea.selectionStart;
		}
		//restore textarea scroll position
		txtarea.scrollTop = textScroll;
	} 

	function checkSelectedText(){
		if (!selText) {
			selText = '';
			isSample = true;
		} else if (selText.charAt(selText.length - 1) == ' ') { //exclude ending space char
			selText = selText.substring(0, selText.length - 1);
			tagClose += ' '
		} 
	}

}

 
//Needed because the upload form submit has no ID (from EvilUnicodeConverter)
function getElementByIdOrName(idorname,par) {
  var parent = (!par) ? document : par
  if(parent.getElementById(idorname)) {
    return parent.getElementById(idorname);
  } else if(parent.getElementsByName(idorname)[0]) {
    return parent.getElementsByName(idorname)[0];
  } else {
    return false;
  }
}

Start a discussion about MediaWiki:Gadget-BiDiTools.js

Start a discussion