1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
function insertTag(startTag, endTag, textareaId, tagType) {
var field = document.getElementById(textareaId);
var scroll = field.scrollTop;
field.focus();
if (window.ActiveXObject) {
var textRange = document.selection.createRange();
var currentSelection = textRange.text;
} else {
var startSelection = field.value.substring(0, field.selectionStart);
var currentSelection = field.value.substring(field.selectionStart, field.selectionEnd);
var endSelection = field.value.substring(field.selectionEnd);
}
if (tagType) {
switch (tagType) {
case "lien":
endTag = "</lien>";
if (currentSelection) {
if (currentSelection.indexOf("http://") == 0 || currentSelection.indexOf("https://") == 0 || currentSelection.indexOf("ftp://") == 0 || currentSelection.indexOf("www.") == 0) {
var label = prompt("Quel est le libellé du lien ?") || "";
startTag = "<lien url=\"" + currentSelection + "\">";
currentSelection = label;
} else {
var URL = prompt("Quelle est l'url ?");
startTag = "<lien url=\"" + URL + "\">";
}
} else {
var URL = prompt("Quelle est l'url ?") || "";
var label = prompt("Quel est le libellé du lien ?") || "";
startTag = "<lien url=\"" + URL + "\">";
currentSelection = label;
}
break;
case "citation":
endTag = "</citation>";
if (currentSelection) {
if (currentSelection.length > 30) {
var auteur = prompt("Quel est l'auteur de la citation ?") || "";
startTag = "<citation nom=\"" + auteur + "\">";
} else {
var citation = prompt("Quelle est la citation ?") || "";
startTag = "<citation nom=\"" + currentSelection + "\">";
currentSelection = citation;
}
} else {
var auteur = prompt("Quel est l'auteur de la citation ?") || "";
var citation = prompt("Quelle est la citation ?") || "";
startTag = "<citation nom=\"" + auteur + "\">";
currentSelection = citation;
}
break;
}
}
if (window.ActiveXObject) {
textRange.text = startTag + currentSelection + endTag;
textRange.moveStart('character', -endTag.length-currentSelection.length);
textRange.moveEnd('character', -endTag.length);
textRange.select();
} else { // Ce n'est pas IE
field.value = startSelection + startTag + currentSelection + endTag + endSelection;
field.focus();
field.setSelectionRange(startSelection.length + startTag.length, startSelection.length + startTag.length + currentSelection.length);
}
field.scrollTop = scroll;
} |
Partager