feat(focus): annotation panel
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
// Deep-focus pane on the library viewer. Protocol picker, selection
|
||||
// toolbar, annotation list, and link mode. No localStorage: the URL
|
||||
// carries protocol and ann. Label text is always visible; color is extra.
|
||||
// toolbar, and the annotation list. No localStorage: the URL carries
|
||||
// protocol and ann. Label text is always visible; color is extra.
|
||||
|
||||
import { reanchor, selectionSelectors } from "./focus_anchor.mjs";
|
||||
|
||||
const KEY_RE = /^[A-Za-z0-9]{6,12}$/;
|
||||
|
||||
export async function mountFocus(host) {
|
||||
const picker = document.getElementById("protocol");
|
||||
@@ -366,14 +365,95 @@ export async function mountFocus(host) {
|
||||
const heading = document.createElement("h2");
|
||||
heading.textContent = "Annotations";
|
||||
panel.appendChild(heading);
|
||||
const line = document.createElement("p");
|
||||
line.id = "focus-status";
|
||||
line.className = "focus-empty";
|
||||
const n = annotations.length;
|
||||
line.textContent = n
|
||||
? n + " highlight" + (n === 1 ? "" : "s") + " on this protocol."
|
||||
: "No highlights for this protocol.";
|
||||
panel.appendChild(line);
|
||||
const status = document.createElement("p");
|
||||
status.id = "focus-status";
|
||||
status.className = "focus-status";
|
||||
panel.appendChild(status);
|
||||
|
||||
if (!annotations.length) {
|
||||
const empty = document.createElement("p");
|
||||
empty.className = "focus-empty";
|
||||
empty.textContent = "No highlights for this protocol.";
|
||||
panel.appendChild(empty);
|
||||
return;
|
||||
}
|
||||
const list = document.createElement("ul");
|
||||
list.className = "focus-list";
|
||||
for (const ann of annotations) {
|
||||
list.appendChild(rowFor(ann));
|
||||
}
|
||||
panel.appendChild(list);
|
||||
}
|
||||
|
||||
function rowFor(ann) {
|
||||
const li = document.createElement("li");
|
||||
if (ann.id === currentId) li.classList.add("current");
|
||||
const jump = document.createElement("button");
|
||||
jump.type = "button";
|
||||
jump.className = "jump";
|
||||
jump.textContent = labelText(ann.label) + ": " + clip(ann.quote);
|
||||
jump.addEventListener("click", () => choose(ann));
|
||||
li.appendChild(jump);
|
||||
if (ann._orphaned) {
|
||||
const badge = document.createElement("span");
|
||||
badge.className = "badge";
|
||||
badge.textContent = "orphaned";
|
||||
li.appendChild(badge);
|
||||
}
|
||||
const note = document.createElement("textarea");
|
||||
note.rows = 2;
|
||||
note.value = ann.note || "";
|
||||
note.setAttribute("aria-label", "Note");
|
||||
note.addEventListener("change", () => saveNote(ann, note.value));
|
||||
li.appendChild(note);
|
||||
const actions = document.createElement("div");
|
||||
actions.className = "focus-actions";
|
||||
const del = document.createElement("button");
|
||||
del.type = "button";
|
||||
del.textContent = "Delete";
|
||||
del.addEventListener("click", () => remove(ann));
|
||||
actions.appendChild(del);
|
||||
li.appendChild(actions);
|
||||
return li;
|
||||
}
|
||||
|
||||
function choose(ann) {
|
||||
currentId = ann.id;
|
||||
writeUrl();
|
||||
reveal(ann.id, true);
|
||||
renderPanel();
|
||||
}
|
||||
|
||||
function reveal(id, smooth) {
|
||||
const ann = annotations.find((a) => a.id === id);
|
||||
if (ann && ann.page) host.goToPage(ann.page, smooth);
|
||||
const el = host.viewer.querySelector('[data-ann="' + cssEscape(id) + '"]');
|
||||
if (el) el.scrollIntoView({ block: "center", behavior: smooth ? "smooth" : "auto" });
|
||||
}
|
||||
|
||||
async function saveNote(ann, note) {
|
||||
const r = await fetch("/api/focus/annotations/" + encodeURIComponent(ann.id), {
|
||||
method: "PATCH",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ note }),
|
||||
});
|
||||
if (!r.ok) setStatus("Could not save the note (" + r.status + ").");
|
||||
else ann.note = note;
|
||||
}
|
||||
|
||||
async function remove(ann) {
|
||||
const r = await fetch("/api/focus/annotations/" + encodeURIComponent(ann.id), {
|
||||
method: "DELETE",
|
||||
});
|
||||
if (!r.ok) {
|
||||
setStatus("Could not delete (" + r.status + ").");
|
||||
return;
|
||||
}
|
||||
if (currentId === ann.id) {
|
||||
currentId = "";
|
||||
writeUrl();
|
||||
}
|
||||
await refresh();
|
||||
}
|
||||
|
||||
function setStatus(text) {
|
||||
@@ -382,6 +462,16 @@ export async function mountFocus(host) {
|
||||
}
|
||||
}
|
||||
|
||||
function clip(text) {
|
||||
const s = (text || "").replace(/\s+/g, " ").trim();
|
||||
return s.length > 90 ? s.slice(0, 89) + "…" : s;
|
||||
}
|
||||
|
||||
function cssId(id) {
|
||||
return String(id || "").replace(/[^a-z0-9-]+/gi, "-");
|
||||
}
|
||||
|
||||
function cssEscape(value) {
|
||||
if (window.CSS && CSS.escape) return CSS.escape(value);
|
||||
return String(value).replace(/[^a-zA-Z0-9_-]/g, "\\$&");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user