Skip to content

Commit

Permalink
Improve search functionality
Browse files Browse the repository at this point in the history
* Addresses issues ArtifexSoftware#78 & ArtifexSoftware#98
  Fix the issue where the search results on the current page would be skipped when clicking the "Next" search button for the first time.
  Improve the search experience for the Chinese input method.

* Add an Enter key handler for the search input.
  Enter: search "Next"
  Shift + Enter: search "Prev"

* Display page number based on 1
  • Loading branch information
xybei committed Aug 30, 2024
1 parent 75ba992 commit 1aa2b78
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
4 changes: 2 additions & 2 deletions examples/simple-viewer/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@
size="40"
placeholder="Search..."
>
<button onclick="run_search(-1, 1)">Prev</button>
<button onclick="run_search(1, 1)">Next</button>
<button id="search-prev" onclick="run_search(-1, 1)">Prev</button>
<button id="search-next" onclick="run_search(1, 1)">Next</button>
<div id="search-status" style="flex-grow:1"></div>
<button onclick="hide_search_panel()">X</button>
</footer>
Expand Down
49 changes: 31 additions & 18 deletions examples/simple-viewer/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -582,10 +582,19 @@ let search_status = document.getElementById("search-status")
let search_input = document.getElementById("search-input")

var current_search_needle = ""
var current_search_page = 0
var last_search_page = -1

search_input.oninput = function (event) {
run_search(event.shiftKey ? -1 : 1, 0)
search_input.onchange = function (event) {
last_search_page = -1
}

search_input.onkeyup = function (event) {
if (event.key === 'Enter') {
if (event.shiftKey)
document.getElementById("search-prev").click()
else
document.getElementById("search-next").click()
}
}

function show_search_panel() {
Expand Down Expand Up @@ -619,34 +628,38 @@ async function run_search(direction, step) {
// start search from visible page
set_search_needle(search_input.value)

current_search_page = find_visible_page()

let next_page = current_search_page
if (step)
next_page += direction
let page = 0;
if (last_search_page === -1)
page = find_visible_page()
else {
page = last_search_page
if (step)
page += direction
}

while (next_page >= 0 && next_page < page_list.length) {
while (page >= 0 && page < page_list.length) {
// We run the check once per loop iteration,
// in case the search was cancel during the 'await' below.
if (current_search_needle === "") {
search_status.textContent = ""
return
}

search_status.textContent = `Searching page ${next_page}.`
search_status.textContent = `Searching page ${page + 1}.`

if (page_list[next_page].loadNeedle !== page_list[next_page].needle)
await page_list[next_page]._loadSearch()
if (page_list[page].loadNeedle !== page_list[page].needle)
await page_list[page]._loadSearch()

const hits = page_list[next_page].searchData
const hits = page_list[page].searchData
if (hits && hits.length > 0) {
page_list[next_page].rootNode.scrollIntoView()
current_search_page = next_page
search_status.textContent = `${hits.length} hits on page ${next_page}.`
page_list[page].rootNode.scrollIntoView()
last_search_page = page
const word = hits.length === 1 ? "hit" : "hits"
search_status.textContent = `${hits.length} ${word} on page ${page + 1}.`
return
}

next_page += direction
page += direction
}

search_status.textContent = "No more search hits."
Expand Down Expand Up @@ -735,7 +748,7 @@ async function init_document(title) {
clear_message()

current_search_needle = ""
current_search_page = 0
last_search_page = -1
}

async function open_document_from_buffer(buffer, magic, title) {
Expand Down

0 comments on commit 1aa2b78

Please sign in to comment.