|
| 1 | + |
| 2 | + let textWidth = function (elem, text) { |
| 3 | + $body = $('body'); |
| 4 | + $this = elem; |
| 5 | + $text = text; |
| 6 | + var calc = '<div style="clear:both;display:block;visibility:hidden;"><span style="width;inherit;margin:0;font-family:' + $this.css('font-family') + ';font-size:' + $this.css('font-size') + ';font-weight:' + $this.css('font-weight') + '">' + $text + '</span></div>'; |
| 7 | + $body.append(calc); |
| 8 | + var width = $('body').find('span:last').width(); |
| 9 | + $body.find('span:last').parent().remove(); |
| 10 | + return width; |
| 11 | + }; |
| 12 | +(function($){ |
| 13 | + $.fn.linenumbers = function(inopts){ |
| 14 | + let opts = $.extend({ |
| 15 | + width: 32, |
| 16 | + padding: 8, |
| 17 | + start: 1, |
| 18 | + digits: 4 |
| 19 | + }, inopts); |
| 20 | + $('[data-name="linenumbers"]').remove(); |
| 21 | + |
| 22 | + return this.each(function(){ |
| 23 | + let self = this; |
| 24 | + let width = $(this).width(); |
| 25 | + let height = $(this).height(); |
| 26 | + let newwidth = width - opts.width; |
| 27 | + |
| 28 | + $(this).before('<textarea \ |
| 29 | + data-name="linenumbers" \ |
| 30 | + style=" \ |
| 31 | + width: ' + newwidth + 'px; \ |
| 32 | + height: ' + height + 'px; \ |
| 33 | + float: left; \ |
| 34 | + margin-right: -' + newwidth + 'px; \ |
| 35 | + white-space: pre; overflow: hidden;\ |
| 36 | + " disabled></textarea>'); |
| 37 | + $(this).css({'width': newwidth + 'px', 'height': height + 'px', |
| 38 | + 'padding': opts.padding + 'px', 'float': 'right'}); |
| 39 | + $(this).after('<div style="clear: both;"></div>'); |
| 40 | + |
| 41 | + let lnbox = $(this).parent().find('textarea[data-name="linenumbers"]'); |
| 42 | + |
| 43 | + // Determine textarea len with chars |
| 44 | + let charlen = -1; |
| 45 | + for (let i = 0; charlen == -1; i++) |
| 46 | + if (textWidth($(this), "*".repeat(i)) >= newwidth - opts.padding * 2) |
| 47 | + charlen = i - 2; |
| 48 | + $(this).bind('blur focus change keyup keydown', function() { |
| 49 | + // Break apart and regex the lines, everything to spaces sans linebreaks |
| 50 | + let lines = $(this).val().split('\n'); |
| 51 | + |
| 52 | + // declare output var |
| 53 | + let output=''; |
| 54 | + // declare spacers and max_spacers vars, and set defaults |
| 55 | + let max_spacers = ''; |
| 56 | + for(i=0; i < opts.digits; i++) |
| 57 | + max_spacers += ' '; |
| 58 | + // Loop through and process each line |
| 59 | + $.each(lines,function(k, v) { |
| 60 | + // Add a line if not blank |
| 61 | + if (k != 0) |
| 62 | + output += '\n'; |
| 63 | + // Determine the appropriate number of leading spaces |
| 64 | + let lencheck = k + opts.start + '!'; |
| 65 | + let spacers = max_spacers.substr(lencheck.length - 1); |
| 66 | + // Add the line with out line number, to the output variable |
| 67 | + if (k >= Math.pow(10, opts.digits) - 1) |
| 68 | + output += "\n" |
| 69 | + else { |
| 70 | + output += spacers + (k + opts.start) + ':'; |
| 71 | + output += "\n---".repeat((v.length - 1) / charlen); |
| 72 | + } |
| 73 | + }); |
| 74 | + // Give the text area out modified content. |
| 75 | + $(lnbox).val(output); |
| 76 | + // Change scroll position as they type, makes sure they stay in sync |
| 77 | + $(lnbox).scrollTop($(this).scrollTop()); |
| 78 | + }); |
| 79 | + // Lock scrolling together, for mouse-wheel scrolling |
| 80 | + $(this).scroll(function(){ |
| 81 | + $(lnbox).scrollTop($(this).scrollTop()); |
| 82 | + }); |
| 83 | + // Fire it off once to get things started |
| 84 | + $(this).trigger('keydown'); |
| 85 | + }); |
| 86 | + }; |
| 87 | +})(jQuery); |
0 commit comments