Skip to content

Instantly share code, notes, and snippets.

@joegaffey
Last active July 10, 2023 19:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joegaffey/643ab30187028cb5bba9ae9478daca9b to your computer and use it in GitHub Desktop.
Save joegaffey/643ab30187028cb5bba9ae9478daca9b to your computer and use it in GitHub Desktop.
Basic annotation UI// source https://jsbin.com/xogegejowe
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Basic annotation UI">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<p>
I will arise and go now, and go to Innisfree,
And a small cabin build there, of clay and wattles made;
Nine bean-rows will I have there, a hive for the honey-bee,
And live alone in the bee-loud glade.
</p>
<p>
And I shall have some peace there, for peace comes dropping slow,
Dropping from the veils of the morning to where the cricket sings;
There midnight’s all a glimmer, and noon a purple glow,
And evening full of the linnet’s wings.
</p>
<p>
I will arise and go now, for always night and day
I hear lake water lapping with low sounds by the shore;
While I stand on the roadway, or on the pavements grey,
I hear it in the deep heart’s core.
</p>
<style id="jsbin-css">
.selected {
background-color: orange;
}
.tooltip {
display: none;
background-color: black;
color: white;
padding: 4px;
position: absolute;
margin-left: 10;
margin-top: 15px;
}
.selected:hover .tooltip {
display: inline-block;
}
p {
/* position: relative; */
font-family: helvetica;
}
</style>
</head>
<body>
<script id="jsbin-javascript">
let timer;
function debounce(callback, delay) {
timer = setTimeout(() => {
callback()
}, delay);
}
function handleSelection(){
let selection = document.getSelection();
let selectedText = selection.toString();
if(selectedText.trim().length > 1 && selection.baseNode == selection.extentNode) {
let range = selection.getRangeAt(0);
selection.empty();
let comment = prompt('Enter comment');
if(comment.length < 1) {
return;
}
makeComment(range, comment);
}
}
function makeComment(range, comment) {
let span = document.createElement('span');
span.className = 'selected';
span.appendChild(range.extractContents());
range.insertNode(span);
let tooltip = document.createElement('span');
tooltip.className = 'tooltip';
tooltip.innerText = comment;
span.prepend(tooltip);
}
document.onselectionchange = () => {
debounce(handleSelection, 1000);
};
</script>
<script id="jsbin-source-css" type="text/css">.selected {
background-color: orange;
}
.tooltip {
display: none;
background-color: black;
color: white;
padding: 4px;
position: absolute;
margin-left: 10;
margin-top: 15px;
}
.selected:hover .tooltip {
display: inline-block;
}
p {
/* position: relative; */
font-family: helvetica;
}</script>
<script id="jsbin-source-javascript" type="text/javascript">
let timer;
function debounce(callback, delay) {
timer = setTimeout(() => {
callback()
}, delay);
}
function handleSelection(){
let selection = document.getSelection();
let selectedText = selection.toString();
if(selectedText.trim().length > 1 && selection.baseNode == selection.extentNode) {
let range = selection.getRangeAt(0);
selection.empty();
let comment = prompt('Enter comment');
if(comment.length < 1) {
return;
}
makeComment(range, comment);
}
}
function makeComment(range, comment) {
let span = document.createElement('span');
span.className = 'selected';
span.appendChild(range.extractContents());
range.insertNode(span);
let tooltip = document.createElement('span');
tooltip.className = 'tooltip';
tooltip.innerText = comment;
span.prepend(tooltip);
}
document.onselectionchange = () => {
debounce(handleSelection, 1000);
};</script></body>
</html>
.selected {
background-color: orange;
}
.tooltip {
display: none;
background-color: black;
color: white;
padding: 4px;
position: absolute;
margin-left: 10px;
margin-top: 20px;
}
.selected:hover .tooltip {
display: inline-block;
}
p {
font-family: helvetica;
}
let timer;
function debounce(callback, delay) {
timer = setTimeout(() => {
callback()
}, delay);
}
function handleSelection(){
let selection = document.getSelection();
let selectedText = selection.toString();
if(selectedText.trim().length > 1 && selection.baseNode == selection.extentNode) {
let range = selection.getRangeAt(0);
selection.empty();
let comment = prompt('Enter comment');
if(comment.length < 1) {
return;
}
makeComment(range, comment);
}
}
function makeComment(range, comment) {
let span = document.createElement('span');
span.className = 'selected';
span.appendChild(range.extractContents());
range.insertNode(span);
let tooltip = document.createElement('span');
tooltip.className = 'tooltip';
tooltip.innerText = comment;
span.prepend(tooltip);
}
document.onselectionchange = () => {
debounce(handleSelection, 1000);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment