golang.org/x/playground@v0.0.0-20230418134305-14ebe15bcd59/static/jquery-linedtextarea.js (about) 1 /** 2 * Adapted from jQuery Lined Textarea Plugin 3 * http://alan.blog-city.com/jquerylinedtextarea.htm 4 * 5 * Released under the MIT License: 6 * http://www.opensource.org/licenses/mit-license.php 7 */ 8 (function($) { 9 $.fn.linedtextarea = function() { 10 /* 11 * Helper function to make sure the line numbers are always kept up to 12 * the current system 13 */ 14 var fillOutLines = function(linesDiv, h, lineNo) { 15 while (linesDiv.height() < h) { 16 linesDiv.append("<div>" + lineNo + "</div>"); 17 lineNo++; 18 } 19 return lineNo; 20 }; 21 22 return this.each(function() { 23 var lineNo = 1; 24 var textarea = $(this); 25 26 /* Wrap the text area in the elements we need */ 27 textarea.wrap("<div class='linedtextarea' style='height:100%; overflow:hidden'></div>"); 28 textarea.width("97%"); 29 textarea.parent().prepend("<div class='lines' style='width:3%'></div>"); 30 var linesDiv = textarea.parent().find(".lines"); 31 32 var scroll = function(tn) { 33 var domTextArea = $(this)[0]; 34 var scrollTop = domTextArea.scrollTop; 35 var clientHeight = domTextArea.clientHeight; 36 linesDiv.css({ 37 'margin-top' : (-scrollTop) + "px" 38 }); 39 lineNo = fillOutLines(linesDiv, scrollTop + clientHeight, 40 lineNo); 41 }; 42 /* React to the scroll event */ 43 textarea.scroll(scroll); 44 $(window).resize(function() { textarea.scroll(); }); 45 /* We call scroll once to add the line numbers */ 46 textarea.scroll(); 47 }); 48 }; 49 50 })(jQuery);