Skip to content

Commit fe41109

Browse files
Nurrllowlevl
authored andcommitted
Added line number to the input textarea.
1 parent 689f603 commit fe41109

File tree

4 files changed

+104
-3
lines changed

4 files changed

+104
-3
lines changed

assets/css/style.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ textarea {
3636
border: none; border-radius: 4px;
3737
padding: 8px;
3838

39+
word-wrap: break-word;
40+
word-break: break-all;
3941
box-sizing: border-box;
4042
resize: none;
4143
}
@@ -151,6 +153,7 @@ button:disabled {
151153
width: 100%;
152154

153155
display: none;
156+
z-index: 2;
154157
}
155158
.tooltip > span {
156159
/* Size */
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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);

assets/js/main.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
$(function() { /* Wait for jQuery */
22

3+
4+
/* Textarea */
5+
$(".input > #input").linenumbers();
6+
37
/* Init lets */
48
let isCodeCompiled = false;
59
let Duck = new Duckuino();
@@ -10,7 +14,13 @@ $(function() { /* Wait for jQuery */
1014
} catch (e) {}
1115

1216
/* Compile button enable/disable */
13-
$(".input > textarea").keyup(function() {
17+
$(".input > #input").keyup(function() {
18+
// Disable export when input is changed
19+
$(".copy-but").prop("disabled", true);
20+
$(".export .copy-but").text("Copy !");
21+
$(".dl-but button").prop("disabled", true);
22+
$(".dl-but select").prop("disabled", true);
23+
1424
if($(this).val() !== "") {
1525
$(".process-but button").prop("disabled", false);
1626
$(".process-but select").prop("disabled", false);
@@ -28,7 +38,7 @@ $(function() { /* Wait for jQuery */
2838

2939
/* Compile button click */
3040
$(".process-but button").click(function() {
31-
let duckyScript = $(".input > textarea").val();
41+
let duckyScript = $(".input > #input").val();
3242
let selectedModule = mods[$(".process-but select").find(":selected").val()];
3343

3444
/* Load Duckuino & Compile */

index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ <h6>This is where you put the DuckyScript</h6>
2727
<br>
2828

2929
<!-- Input textarea -->
30-
<textarea placeholder="Enter some DuckyScript to begin :D"></textarea>
30+
<textarea id="input" placeholder="Enter some DuckyScript to begin :D"></textarea>
3131
</div>
3232
<div class="part process">
3333
<div class="combined-but process-but">
@@ -62,6 +62,7 @@ <h4>Option 2: Copy/Paste</h4>
6262
<script src="assets/js/lib/jquery-3.1.0.min.js"></script>
6363
<script src="assets/js/lib/jszip-3.1.2.min.js"></script>
6464
<script src="assets/js/lib/FileSaver-1.3.2.min.js"></script>
65+
<script src="assets/js/lib/jquery-linenumbers.js"></script>
6566

6667
<!-- Handmade js <3 -->
6768
<script src="Duckuino.js"></script>

0 commit comments

Comments
 (0)