change hash

This commit is contained in:
Alysa Meng 2024-07-23 10:00:01 -07:00
commit 02c82acf8c
3 changed files with 105 additions and 90 deletions

View file

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

View file

@ -47,20 +47,19 @@ haste_document.prototype.load = function(key, callback, lang) {
currentLine <= selectedLines.endLine
) {
highlighted =
'<span style="background-color: yellow;">' + highlighted + "</span>";
"<span class='lineHighlight'>" + highlighted + "</span>";
}
highlighted = "<span id='line-" + i + "'>" + highlighted + "</span>";
highlighted = "<span onclick='handleLineClick(" + i + ")' class='line' id='line-" + i + "'>" + highlighted + "</span>";
high.value += highlighted + "\n";
}
// scroll to position in document after ensuring components have had time to render
// scroll to position in document after ensuring components h"ve had time to render
setTimeout(function() {
// show current line and the one before it
if (selectedLines.startLine >= 3) {
document.body.scrollTo(0, $("#line-" + (selectedLines.startLine - 2)).offset().top)
} else {
// if lines 1-2, go to top of file
document.body.scrollTop(0);
document.body.scrollTo(0, 0);
}
}, 0);
} catch (err) {
@ -423,5 +422,4 @@ $(function() {
}
}
});
});
});

View file

@ -1,100 +1,105 @@
<html>
<head>
<head>
<title>hastebin</title>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="solarized_dark.css"/>
<link rel="stylesheet" type="text/css" href="application.css"/>
<title>hastebin</title>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="solarized_dark.css" />
<link rel="stylesheet" type="text/css" href="application.css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="highlight.min.js"></script>
<script type="text/javascript" src="application.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="highlight.min.js"></script>
<script type="text/javascript" src="application.min.js"></script>
<meta name="robots" content="noindex,nofollow"/>
<meta name="robots" content="noindex,nofollow" />
<script type="text/javascript">
var app = null;
// Handle pops
var handlePop = function(evt) {
var path = evt.target.location.href;
if (path === app.baseUrl) { app.newDocument(true); }
else { app.loadDocument(path.split('#')[0].split('/').slice(-1)[0]); }
<script type="text/javascript">
var app = null;
// Handle pops
var handlePop = function (evt) {
var path = evt.target.location.href;
if (path === app.baseUrl) { app.newDocument(true); }
else { app.loadDocument(path.split('#')[0].split('/').slice(-1)[0]); }
};
// Set up the pop state to handle loads, skipping the first load
// to make chrome behave like others:
// http://code.google.com/p/chromium/issues/detail?id=63040
setTimeout(function () {
window.onpopstate = function (evt) {
try { handlePop(evt); } catch (err) { /* not loaded yet */ }
};
// Set up the pop state to handle loads, skipping the first load
// to make chrome behave like others:
// http://code.google.com/p/chromium/issues/detail?id=63040
setTimeout(function() {
window.onpopstate = function(evt) {
try { handlePop(evt); } catch(err) { /* not loaded yet */ }
};
}, 1000);
/**
* Function to parse the URL fragment and extract line numbers
* @returns {Object} Object with parsed startLine, endLine
*/
function getSelectedLinesFromURL() {
const urlHash = window.location.hash.substring(1); // Remove the '#' from the hash
if (!urlHash) return {}; // Return an empty object if there's no hash
}, 1000);
// Parse the hash into start and end parts
const range = urlHash.split("L");
let start, end;
if (range.length > 2) {
start = parseInt(range[1], 10);
end = parseInt(range[2], 10);
} else {
start = parseInt(range[1], 10);
end = start;
}
return {
startLine: start,
endLine: end,
};
/**
* Function to parse the URL fragment and extract line numbers
* @returns {Object} Object with parsed startLine, endLine
*/
function getSelectedLinesFromURL() {
const urlHash = window.location.hash.substring(1); // Remove the '#' from the hash
if (!urlHash) return {}; // Return an empty object if there's no hash
// Parse the hash into start and end parts
const range = urlHash.split("L");
let start, end;
if (range.length > 2) {
start = parseInt(range[1], 10);
end = parseInt(range[2], 10);
} else {
start = parseInt(range[1], 10);
end = start;
}
// Construct app and load initial path
$(function() {
var baseUrl = window.location.href.split('#')[0].split('/');
baseUrl = baseUrl.slice(0, baseUrl.length - 1).join('/') + '/';
console.log(baseUrl);
const selectedLines = getSelectedLinesFromURL();
console.log(selectedLines);
app = new haste('hastebin', { twitter: true, baseUrl: baseUrl, selectedLines: selectedLines });
handlePop({ target: window });
});
</script>
return {
startLine: start,
endLine: end,
};
}
</head>
// Construct app and load initial path
$(function () {
var baseUrl = window.location.href.split('#')[0].split('/');
baseUrl = baseUrl.slice(0, baseUrl.length - 1).join('/') + '/';
console.log(baseUrl);
const selectedLines = getSelectedLinesFromURL();
console.log(selectedLines);
app = new haste('hastebin', { twitter: false, baseUrl: baseUrl, selectedLines: selectedLines });
handlePop({ target: window });
});
<body>
<ul id="messages"></ul>
function handleLineClick(lineId) {
/* TODO */
window.location.hash = "#L" + (lineId + 1);
}
</script>
<div id="key">
<div id="pointer" style="display:none;"></div>
<div id="box1">
<a href="about.md" class="logo"></a>
</div>
<div id="box2">
<button class="save function button-picture">Save</button>
<button class="new function button-picture">New</button>
<button class="duplicate function button-picture">Duplicate & Edit</button>
<button class="raw function button-picture">Just Text</button>
<button class="twitter function button-picture">Twitter</button>
</div>
<div id="box3" style="display:none;">
<div class="label"></div>
<div class="shortcut"></div>
</div>
</head>
<body>
<ul id="messages"></ul>
<div id="key">
<div id="pointer" style="display:none;"></div>
<div id="box1">
<a href="about.md" class="logo"></a>
</div>
<div id="box2">
<button class="save function button-picture">Save</button>
<button class="new function button-picture">New</button>
<button class="duplicate function button-picture">Duplicate & Edit</button>
<button class="raw function button-picture">Just Text</button>
<button class="twitter function button-picture">Twitter</button>
</div>
<div id="box3" style="display:none;">
<div class="label"></div>
<div class="shortcut"></div>
</div>
</div>
<div id="linenos"></div>
<pre id="box" style="display:none;" class="hljs" tabindex="0"><code></code></pre>
<textarea spellcheck="false" style="display:none;"></textarea>
<div id="linenos"></div>
<pre id="box" style="display:none;" class="hljs" tabindex="0"><code></code></pre>
<textarea spellcheck="false" style="display:none;"></textarea>
</body>
</body>
</html>
</html>