From f6534e01bf90b4978e50d66753cf8ceb89f390c4 Mon Sep 17 00:00:00 2001 From: Kenox Date: Thu, 3 Nov 2022 18:22:53 +0100 Subject: [PATCH] Add line number links --- static/application.css | 21 +++++++++++++++++++++ static/application.js | 36 ++++++++++++++++++++++++++++++++---- static/application.min.js | 2 +- static/index.html | 2 -- 4 files changed, 54 insertions(+), 7 deletions(-) diff --git a/static/application.css b/static/application.css index db14a59..063e63a 100644 --- a/static/application.css +++ b/static/application.css @@ -36,6 +36,27 @@ textarea { user-select: none; } +.highlight { + background-color: rgba(187, 128, 9, 0.25); + position: absolute; + width: 100%; + height: 16px; +} + +.linenumber { + color: #3D3F41; + z-index: 1000; + text-decoration:none; + position: absolute; + top: 20px; + left: 0px; + width: 30px; /* 30 to get 20 away from box */ + font-size: 13px; + font-family: monospace; + text-align: right; + user-select: none; +} + /* code box when locked */ #box { diff --git a/static/application.js b/static/application.js index d4028ca..17d56c4 100644 --- a/static/application.js +++ b/static/application.js @@ -170,16 +170,44 @@ haste.prototype.lookupTypeByExtension = function(ext) { // Add line numbers to the document // For the specified number of lines haste.prototype.addLineNumbers = function(lineCount) { - var h = ''; + removeElementsByClass('linenumber'); for (var i = 0; i < lineCount; i++) { - h += (i + 1).toString() + '
'; + let div = document.createElement('a'); + div.classList.add('linenumber'); + div.style.marginTop = i * 16 + 'px'; + let line = i + 1; + div.href = '#' + line; + div.onclick = function() { + var box = document.getElementById("box"); + // create highlight div + removeElementsByClass('highlight'); + let highlight = document.createElement('div'); + highlight.classList.add('highlight'); + highlight.style.marginTop = ((line - 1) * 16) + 'px'; + box.insertBefore(highlight, box.firstChild); + } + let text = document.createTextNode(line.toString()); + div.appendChild(text); + document.body.appendChild(div) } - $('#linenos').html(h); }; +function removeElementsByClass(className){ + const elements = document.getElementsByClassName(className); + while(elements.length > 0){ + elements[0].parentNode.removeChild(elements[0]); + } +} + // Remove the line numbers haste.prototype.removeLineNumbers = function() { - $('#linenos').html('>'); + removeElementsByClass("highlight"); + removeElementsByClass('linenumber'); + let div = document.createElement('a'); + div.classList.add('linenumber'); + let text = document.createTextNode('>'); + div.appendChild(text); + document.body.appendChild(div) }; // Load a document and show it diff --git a/static/application.min.js b/static/application.min.js index b887887..052c5a9 100644 --- a/static/application.min.js +++ b/static/application.min.js @@ -1 +1 @@ -const workersUrl="https://api.knx.cool/";var haste_document=function(){this.locked=!1};haste_document.prototype.htmlEscape=function(t){return t.replace(/&/g,"&").replace(/>/g,">").replace(/'+t+"");$("#messages").prepend(o),setTimeout(function(){o.slideUp("fast",function(){$(this).remove()})},3e3)},haste.prototype.lightKey=function(){this.configureKey(["new","save"])},haste.prototype.fullKey=function(){this.configureKey(["new","duplicate","raw"])},haste.prototype.configureKey=function(t){var e,o=0;$("#box2 .function").each(function(){for(e=$(this),o=0;o";$("#linenos").html(e)},haste.prototype.removeLineNumbers=function(){$("#linenos").html(">")},haste.prototype.loadDocument=function(t){var e=t.split(".",2),o=this;o.doc=new haste_document,o.doc.load(e[0],function(t){t?(o.$code.html(t.value),o.setTitle(t.key),o.fullKey(),o.$textarea.val("").hide(),o.$box.show().focus(),o.addLineNumbers(t.lineCount)):o.newDocument()},this.lookupTypeByExtension(e[1]))},haste.prototype.duplicateDocument=function(){if(this.doc.locked){var t=this.doc.data;this.newDocument(),this.$textarea.val(t)}},haste.prototype.lockDocument=function(){var t=this;this.doc.save(this.$textarea.val(),function(e,o){if(e)t.showMessage(e.message,"error");else if(o){t.$code.html(o.value),t.setTitle(o.key);var n="/"+o.key;window.history.pushState(null,t.appName+"-"+o.key,n),t.fullKey(),t.$textarea.val("").hide(),t.$box.show().focus(),t.addLineNumbers(o.lineCount)}})},haste.prototype.configureButtons=function(){var t=this;this.buttons=[{$where:$("#box2 .save"),label:"Save",shortcutDescription:"control + s",shortcut:function(t){return t.ctrlKey&&83===t.keyCode},action:function(){""!==t.$textarea.val().replace(/^\s+|\s+$/g,"")&&t.lockDocument()}},{$where:$("#box2 .new"),label:"New",shortcut:function(t){return t.ctrlKey&&78===t.keyCode},shortcutDescription:"control + n",action:function(){t.newDocument(!t.doc.key)}},{$where:$("#box2 .duplicate"),label:"Duplicate & Edit",shortcut:function(e){return t.doc.locked&&e.ctrlKey&&68===e.keyCode},shortcutDescription:"control + d",action:function(){t.duplicateDocument()}},{$where:$("#box2 .raw"),label:"Just Text",shortcut:function(t){return t.ctrlKey&&t.shiftKey&&82===t.keyCode},shortcutDescription:"control + shift + r",action:function(){window.location.href=workersUrl+"raw/"+t.doc.key}}];for(var e=0;e/g,">").replace(/0;)t[0].parentNode.removeChild(t[0])}haste.prototype.setTitle=function(e){var t=e?this.appName+" - "+e:this.appName;document.title=t},haste.prototype.showMessage=function(e,t){var o=$('
  • '+e+"
  • ");$("#messages").prepend(o),setTimeout(function(){o.slideUp("fast",function(){$(this).remove()})},3e3)},haste.prototype.lightKey=function(){this.configureKey(["new","save"])},haste.prototype.fullKey=function(){this.configureKey(["new","duplicate","raw"])},haste.prototype.configureKey=function(e){var t,o=0;$("#box2 .function").each(function(){for(t=$(this),o=0;o");e.appendChild(t),document.body.appendChild(e)},haste.prototype.loadDocument=function(e){var t=e.split(".",2),o=this;o.doc=new haste_document,o.doc.load(t[0],function(e){e?(o.$code.html(e.value),o.setTitle(e.key),o.fullKey(),o.$textarea.val("").hide(),o.$box.show().focus(),o.addLineNumbers(e.lineCount)):o.newDocument()},this.lookupTypeByExtension(t[1]))},haste.prototype.duplicateDocument=function(){if(this.doc.locked){var e=this.doc.data;this.newDocument(),this.$textarea.val(e)}},haste.prototype.lockDocument=function(){var e=this;this.doc.save(this.$textarea.val(),function(t,o){if(t)e.showMessage(t.message,"error");else if(o){e.$code.html(o.value),e.setTitle(o.key);var n="/"+o.key;window.history.pushState(null,e.appName+"-"+o.key,n),e.fullKey(),e.$textarea.val("").hide(),e.$box.show().focus(),e.addLineNumbers(o.lineCount)}})},haste.prototype.configureButtons=function(){var e=this;this.buttons=[{$where:$("#box2 .save"),label:"Save",shortcutDescription:"control + s",shortcut:function(e){return e.ctrlKey&&83===e.keyCode},action:function(){""!==e.$textarea.val().replace(/^\s+|\s+$/g,"")&&e.lockDocument()}},{$where:$("#box2 .new"),label:"New",shortcut:function(e){return e.ctrlKey&&78===e.keyCode},shortcutDescription:"control + n",action:function(){e.newDocument(!e.doc.key)}},{$where:$("#box2 .duplicate"),label:"Duplicate & Edit",shortcut:function(t){return e.doc.locked&&t.ctrlKey&&68===t.keyCode},shortcutDescription:"control + d",action:function(){e.duplicateDocument()}},{$where:$("#box2 .raw"),label:"Just Text",shortcut:function(e){return e.ctrlKey&&e.shiftKey&&82===e.keyCode},shortcutDescription:"control + shift + r",action:function(){window.location.href=workersUrl+"raw/"+e.doc.key}}];for(var t=0;t -
    -