Skip to content

Commit

Permalink
Optimize moveAnchor
Browse files Browse the repository at this point in the history
- Also fix support for offset > 1.
  • Loading branch information
danny0838 committed Apr 21, 2024
1 parent 9c4779c commit 59105a9
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 50 deletions.
66 changes: 41 additions & 25 deletions sts/data/htmlpage.tpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,36 +41,52 @@
.popup a:not([tabindex="0"])::before { content: attr(tabindex) "."; }
</style>
<script>
function moveAnchor(anchor, offset, filter) {
const viewer = document.querySelector('#viewer');
const anchors = viewer.querySelectorAll('a');
const len = anchors.length;

// no anchor, it's impossible to move
if (len === 0) {
return false;
function* walkThroughAnchors(anchor, direction) {
let a = anchor;
if (direction > 0) {
while (a = a.nextElementSibling) {
if (a === anchor) { return; }
yield a;
}
if (a = anchor.parentNode.firstElementChild) {
if (a === anchor) { return; }
yield a;
while (a = a.nextElementSibling) {
if (a === anchor) { return; }
yield a;
}
}
return;
}

let idx = Array.prototype.indexOf.call(anchors, anchor);

if (idx === -1) {
throw new Error(`specified anchor is invalid`);
if (direction < 0) {
while (a = a.previousElementSibling) {
if (a === anchor) { return; }
yield a;
}
if (a = anchor.parentNode.lastElementChild) {
if (a === anchor) { return; }
yield a;
while (a = a.previousElementSibling) {
if (a === anchor) { return; }
yield a;
}
}
return;
}
}

let anchorNew;
while (true) {
idx += offset;
while (idx >= len) { idx -= len; }
while (idx < 0) { idx += len; }
anchorNew = anchors[idx];
if (anchorNew === anchor) {
return false;
}
if (typeof filter !== "function" || filter(anchorNew)) {
break;
function moveAnchor(anchor, offset, filter) {
let i = Math.abs(offset);
for (const a of walkThroughAnchors(anchor, offset)) {
if (typeof filter !== "function" || filter(a)) {
if (i > 1) {
i--;
continue;
}
return a;
}
}
return anchorNew;
return null;
}

function moveCandidate(anchor, offset, filter) {
Expand Down
66 changes: 41 additions & 25 deletions sts/data/htmlpage/index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,49 @@
function moveAnchor(anchor, offset, filter) {
const viewer = document.querySelector('#viewer');
const anchors = viewer.querySelectorAll('a');
const len = anchors.length;

// no anchor, it's impossible to move
if (len === 0) {
return false;
function* walkThroughAnchors(anchor, direction) {
let a = anchor;
if (direction > 0) {
while (a = a.nextElementSibling) {
if (a === anchor) { return; }
yield a;
}
if (a = anchor.parentNode.firstElementChild) {
if (a === anchor) { return; }
yield a;
while (a = a.nextElementSibling) {
if (a === anchor) { return; }
yield a;
}
}
return;
}

let idx = Array.prototype.indexOf.call(anchors, anchor);

if (idx === -1) {
throw new Error(`specified anchor is invalid`);
if (direction < 0) {
while (a = a.previousElementSibling) {
if (a === anchor) { return; }
yield a;
}
if (a = anchor.parentNode.lastElementChild) {
if (a === anchor) { return; }
yield a;
while (a = a.previousElementSibling) {
if (a === anchor) { return; }
yield a;
}
}
return;
}
}

let anchorNew;
while (true) {
idx += offset;
while (idx >= len) { idx -= len; }
while (idx < 0) { idx += len; }
anchorNew = anchors[idx];
if (anchorNew === anchor) {
return false;
}
if (typeof filter !== "function" || filter(anchorNew)) {
break;
function moveAnchor(anchor, offset, filter) {
let i = Math.abs(offset);
for (const a of walkThroughAnchors(anchor, offset)) {
if (typeof filter !== "function" || filter(a)) {
if (i > 1) {
i--;
continue;
}
return a;
}
}
return anchorNew;
return null;
}

function moveCandidate(anchor, offset, filter) {
Expand Down

0 comments on commit 59105a9

Please sign in to comment.