This commit is contained in:
Colin 2024-07-24 14:59:14 -07:00
commit bc62766bef
3 changed files with 567 additions and 564 deletions

View file

@ -36,14 +36,6 @@ textarea {
user-select: none; user-select: none;
} }
.line {
/* display: block;*/
}
/*.line:hover {
background-color: #cbd387;
}*/
.lineHighlight { .lineHighlight {
background-color: yellow; background-color: yellow;
} }

View file

@ -28,7 +28,7 @@ haste_document.prototype.load = function(key, callback, lang) {
_this.key = key; _this.key = key;
_this.data = res.data; _this.data = res.data;
try { try {
var high = { value: "" }; var high = { value: "", language: null};
var lines = res.data.split("\n"); var lines = res.data.split("\n");
for (var i = 0; i < lines.length; i++) { for (var i = 0; i < lines.length; i++) {
if (lang === "txt") { if (lang === "txt") {
@ -56,7 +56,7 @@ haste_document.prototype.load = function(key, callback, lang) {
setTimeout(function() { setTimeout(function() {
// show current line and the one before it // show current line and the one before it
if (selectedLines.startLine >= 3) { if (selectedLines.startLine >= 3) {
document.body.scrollTo(0, $("#line-" + (selectedLines.startLine - 2)).offset().top) document.body.scrollTo(0, $("#line-" + (selectedLines.startLine - 2)).offset().top);
} else { } else {
// if lines 1-2, go to top of file // if lines 1-2, go to top of file
document.body.scrollTo(0, 0); document.body.scrollTo(0, 0);
@ -218,11 +218,26 @@ haste.prototype.lookupTypeByExtension = function(ext) {
}; };
// Add line numbers to the document // Add line numbers to the document
// For the specified number of lines, each with a class and id // For the specified number of lines
haste.prototype.addLineNumbers = function(lineCount) { haste.prototype.addLineNumbers = function(lineCount) {
var h = ''; var h = '';
for (var i = 0; i < lineCount; i++) { for (var i = 0; i < lineCount; i++) {
h += '<span onclick="handleLineClick(' + i.toString() + ')" onmouseenter="handleMouseEnter(' + i.toString() + ')" onmouseleave="handleMouseLeave(' + i.toString() + ')" onmousedown="handleMouseDown(' + i.toString() + ')" onmouseup="handleMouseUp(' + i.toString() + ')" id="line-number-' + (i + 1) + '">' + (i + 1) + '</span><br/>'; h +=
'<span onclick="handleLineClick(' +
i.toString() +
')" onmouseenter="handleMouseEnter(' +
i.toString() +
')" onmouseleave="handleMouseLeave(' +
i.toString() +
')" onmousedown="handleMouseDown(' +
i.toString() +
')" onmouseup="handleMouseUp(' +
i.toString() +
')" id="line-number-' +
(i + 1) +
'">' +
(i + 1) +
"</span><br/>";
} }
$('#linenos').html(h); $('#linenos').html(h);
}; };
@ -284,7 +299,6 @@ haste.prototype.lockDocument = function() {
_this.addLineNumbers(ret.lineCount); _this.addLineNumbers(ret.lineCount);
// Load Document Again // Load Document Again
var path = window.location.href; var path = window.location.href;
console.log(path);
_this.loadDocument(path.split('#')[0].split('/').slice(-1)[0]); _this.loadDocument(path.split('#')[0].split('/').slice(-1)[0]);
} }
}); });
@ -427,4 +441,5 @@ $(function() {
} }
} }
}); });
}); });

View file

@ -38,7 +38,6 @@
}; };
}, 1000); }, 1000);
/** /**
* Function to parse the URL fragment and extract line numbers * Function to parse the URL fragment and extract line numbers
* @returns {Object} Object with parsed startLine, endLine * @returns {Object} Object with parsed startLine, endLine
@ -68,27 +67,21 @@
$(function() { $(function() {
var baseUrl = window.location.href.split('#')[0].split('/'); var baseUrl = window.location.href.split('#')[0].split('/');
baseUrl = baseUrl.slice(0, baseUrl.length - 1).join('/') + '/'; baseUrl = baseUrl.slice(0, baseUrl.length - 1).join('/') + '/';
console.log(baseUrl);
const selectedLines = getSelectedLinesFromURL(); const selectedLines = getSelectedLinesFromURL();
console.log(selectedLines); app = new haste('hastebin', { twitter: true, baseUrl: baseUrl, selectedLines: selectedLines });
app = new haste('hastebin', { twitter: false, baseUrl: baseUrl, selectedLines: selectedLines });
handlePop({ target: window }); handlePop({ target: window });
}); });
$(window).on('hashchange', function() { // Update the window hash with the selected lines
isHashChange = true;
});
function updateWindowLineHash(lineId){ function updateWindowLineHash(lineId){
console.log('updateWindowLineHash', lineId, app.selectedLines.startLine, app.selectedLines.endLine);
app.selectedLines.startLine = Math.min(lineId,app.selectedLines.startLine); app.selectedLines.startLine = Math.min(lineId,app.selectedLines.startLine);
app.selectedLines.endLine = Math.max(lineId,app.selectedLines.endLine); app.selectedLines.endLine = Math.max(lineId,app.selectedLines.endLine);
window.location.hash = "#L" + (app.selectedLines.startLine) + "-L" + (app.selectedLines.endLine); window.location.hash = "#L" + (app.selectedLines.startLine) + "-L" + (app.selectedLines.endLine);
} }
// Handle mouse enter
function handleMouseEnter(lineId) { function handleMouseEnter(lineId) {
lineId = lineId + 1; lineId = lineId + 1;
console.log('lineId', lineId);
$('#line-number-' + lineId).css('background-color', '#cbd387'); $('#line-number-' + lineId).css('background-color', '#cbd387');
$('#line-' + lineId).css('background-color', '#cbd387'); $('#line-' + lineId).css('background-color', '#cbd387');
if(isDragging){ if(isDragging){
@ -97,12 +90,14 @@
} }
} }
// Handle mouse leave
function handleMouseLeave(lineId) { function handleMouseLeave(lineId) {
lineId = lineId + 1; lineId = lineId + 1;
$('#line-number-' + lineId).css('background-color', ''); $('#line-number-' + lineId).css('background-color', '');
$('#line-' + lineId).css('background-color', ''); $('#line-' + lineId).css('background-color', '');
} }
// Handle mouse down, unhighlight current lines first
function unHighlightCurrent(){ function unHighlightCurrent(){
startLine = app.selectedLines.startLine; startLine = app.selectedLines.startLine;
endLine = app.selectedLines.endLine; endLine = app.selectedLines.endLine;
@ -120,6 +115,7 @@
updateWindowLineHash(lineId); updateWindowLineHash(lineId);
} }
// Handle mouse up
function handleMouseUp(lineId) { function handleMouseUp(lineId) {
if(!isDragging){ if(!isDragging){
return; return;