// ==UserScript== // @author Phillip Berndt // @name TabArea // @namespace http://www.pberndt.com/ // @description Add tabstop, autoindention, resizing and template capabilities to textAreas // @include * // ==/UserScript== TabArea = { load: function() { // Create menu item for template editing TabArea.loadTemplates(); GM_registerMenuCommand("Edit textarea templates", TabArea.showTemplateEditor); // Bind forms for template usage if(TabArea.templates) { forms = document.getElementsByTagName("form"); for(n=0; nTextarea template editor

Template editor

TagValue

Remove tag to remove an entry. Characters ; and | are not allowed. You may use JS in values by prepending 'js:' to the value.

For using templates, put {{tag}} into any textarea and submit the form."); editorWindow.document.close(); table = editorWindow.document.getElementById("tpl"); // Create an editor form for(template in TabArea.templates) { tr = editorWindow.document.createElement("tr"); td = editorWindow.document.createElement("td"); te = editorWindow.document.createElement("input"); te.setAttribute("type", "text"); te.value = template; td.appendChild(te); tr.appendChild(td); td = editorWindow.document.createElement("td"); te = editorWindow.document.createElement("input"); te.setAttribute("type", "text"); te.value = TabArea.templates[template]; td.appendChild(te); tr.appendChild(td); table.appendChild(tr); } // Generate button functions editorWindow.document.getElementById("add").addEventListener("click", function() { tr = editorWindow.document.createElement("tr"); td = editorWindow.document.createElement("td"); te = editorWindow.document.createElement("input"); te.setAttribute("type", "text"); td.appendChild(te); tr.appendChild(td); tr.appendChild(td.cloneNode(true)); table.appendChild(tr); textArea.addEventListener("keydown", TabArea.keyPressCallback, false); }, false); editorWindow.document.getElementById("save").addEventListener("click", function() { TabArea.templates = new Object(); inputs = editorWindow.document.getElementsByTagName("input"); for(i=0; i begin + 1) element.value = element.value.substr(0, begin) + insertText + element.value.substr(end); else element.value = element.value.substr(0, begin) + insertText + element.value.substr(begin); element.selectionStart = begin + insertText.length; element.selectionEnd = begin + insertText.length; element.scrollTop = savedScrollTop; } else(element.value += insertText); element.focus(); }, keyPressCallback: function(e) { element = e.target; if(element.selectionStart == null) return; switch(e.keyCode) { // Tabstopp case 9: element.preventBlur = true; if(element.selectionEnd != element.selectionStart) { selectionText = element.value.substring(element.selectionStart, element.selectionEnd); if(e.shiftKey) selectionText = selectionText.replace(/\n\t/g, "\n").replace(/^\t/, ""); else selectionText = "\t" + selectionText.split("\n").join("\n\t"); begin = element.selectionStart; TabArea.insertText(element, selectionText); element.selectionStart = begin; } else TabArea.insertText(element, String.fromCharCode(9)); break; // Enter case 13: currPos = element.selectionStart; lastLine = ""; for(i=currPos-1;i>=0;i--) { if(e.target.value.substring(i, i + 1) == '\n') break; } lastLine = e.target.value.substring(i + 1, currPos); whiteSpace = ""; for(i=0;i