Skip to content

Commit

Permalink
Merge pull request hyperaudio#151 from hyperaudio/150-fragment-selection
Browse files Browse the repository at this point in the history
more robust treatment of text selection
  • Loading branch information
maboa authored May 3, 2023
2 parents 5ffc267 + c133499 commit aaee07a
Showing 1 changed file with 39 additions and 32 deletions.
71 changes: 39 additions & 32 deletions js/hyperaudio-lite.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*! (C) The Hyperaudio Project. MIT @license: en.wikipedia.org/wiki/MIT_License. */
/*! Version 2.1.1 */
/*! Version 2.1.2 */

'use strict';

Expand Down Expand Up @@ -203,7 +203,7 @@ class HyperaudioLite {
() => {
const mediaFragment = this.getSelectionMediaFragment();

if (mediaFragment !== '') {
if (mediaFragment !== null) {
document.location.hash = mediaFragment;
}
},
Expand Down Expand Up @@ -321,7 +321,7 @@ class HyperaudioLite {
};

getSelectionMediaFragment = () => {
let fragment = '';
let fragment = null;
let selection = null;

if (window.getSelection) {
Expand All @@ -330,7 +330,8 @@ class HyperaudioLite {
selection = document.selection.createRange();
}

if (selection.toString() !== '') {
if (selection.toString() !== '' && selection.focusNode !== null && selection.anchorNode !== null) {

let fNode = selection.focusNode.parentNode;
let aNode = selection.anchorNode.parentNode;

Expand All @@ -355,43 +356,49 @@ class HyperaudioLite {
aNode = aNode.nextElementSibling;
}

let aNodeTime = parseInt(aNode.getAttribute('data-m'), 10);
let aNodeDuration = parseInt(aNode.getAttribute('data-d'), 10);
let fNodeTime;
let fNodeDuration;
if (aNode !== null) {
let aNodeTime = parseInt(aNode.getAttribute('data-m'), 10);
let aNodeDuration = parseInt(aNode.getAttribute('data-d'), 10);
let fNodeTime;
let fNodeDuration;

if (fNode !== null && fNode.getAttribute('data-m') !== null) {
// if the selection ends in a space we want the previous element if it exists
if(selection.toString().slice(-1) == " " && fNode.previousElementSibling !== null) {
fNode = fNode.previousElementSibling;
}
if (fNode !== null && fNode.getAttribute('data-m') !== null) {
// if the selection ends in a space we want the previous element if it exists
if(selection.toString().slice(-1) == " " && fNode.previousElementSibling !== null) {
fNode = fNode.previousElementSibling;
}

fNodeTime = parseInt(fNode.getAttribute('data-m'), 10);
fNodeDuration = parseInt(fNode.getAttribute('data-d'), 10);
fNodeTime = parseInt(fNode.getAttribute('data-m'), 10);
fNodeDuration = parseInt(fNode.getAttribute('data-d'), 10);

// if the selection starts with a space we want the next element
// if the selection starts with a space we want the next element

}
}

// 1 decimal place will do
aNodeTime = Math.round(aNodeTime / 100) / 10;
aNodeDuration = Math.round(aNodeDuration / 100) / 10;
fNodeTime = Math.round(fNodeTime / 100) / 10;
fNodeDuration = Math.round(fNodeDuration / 100) / 10;
// 1 decimal place will do
aNodeTime = Math.round(aNodeTime / 100) / 10;
aNodeDuration = Math.round(aNodeDuration / 100) / 10;
fNodeTime = Math.round(fNodeTime / 100) / 10;
fNodeDuration = Math.round(fNodeDuration / 100) / 10;

let nodeStart = aNodeTime;
let nodeDuration = Math.round((fNodeTime + fNodeDuration - aNodeTime) * 10) / 10;
let nodeStart = aNodeTime;
let nodeDuration = Math.round((fNodeTime + fNodeDuration - aNodeTime) * 10) / 10;

if (aNodeTime >= fNodeTime) {
nodeStart = fNodeTime;
nodeDuration = Math.round((aNodeTime + aNodeDuration - fNodeTime) * 10) / 10;
}
if (aNodeTime >= fNodeTime) {
nodeStart = fNodeTime;
nodeDuration = Math.round((aNodeTime + aNodeDuration - fNodeTime) * 10) / 10;
}

if (nodeDuration === 0 || nodeDuration === null || isNaN(nodeDuration)) {
nodeDuration = 10; // arbitary for now
}
if (nodeDuration === 0 || nodeDuration === null || isNaN(nodeDuration)) {
nodeDuration = 10; // arbitary for now
}

fragment = this.transcript.id + '=' + nodeStart + ',' + Math.round((nodeStart + nodeDuration) * 10) / 10;
if (isNaN(parseFloat(nodeStart))) {
fragment = null;
} else {
fragment = this.transcript.id + '=' + nodeStart + ',' + Math.round((nodeStart + nodeDuration) * 10) / 10;
}
}
}

return fragment;
Expand Down

0 comments on commit aaee07a

Please sign in to comment.