feat(focus): link mode
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
// Deep-focus pane on the library viewer. Protocol picker, selection
|
||||
// toolbar, and the annotation list. No localStorage: the URL carries
|
||||
// protocol and ann. Label text is always visible; color is extra.
|
||||
// toolbar, annotation list, and link mode. 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");
|
||||
@@ -18,6 +19,8 @@ export async function mountFocus(host) {
|
||||
let annotations = [];
|
||||
let canonical = "";
|
||||
let pending = null;
|
||||
let linkFrom = null;
|
||||
let linkTargets = [];
|
||||
|
||||
try {
|
||||
const r = await fetch("/api/focus/protocols");
|
||||
@@ -39,6 +42,8 @@ export async function mountFocus(host) {
|
||||
picker.addEventListener("change", () => {
|
||||
protocolName = picker.value;
|
||||
currentId = "";
|
||||
linkFrom = null;
|
||||
linkTargets = [];
|
||||
writeUrl();
|
||||
buildToolbar();
|
||||
refresh();
|
||||
@@ -97,6 +102,7 @@ export async function mountFocus(host) {
|
||||
}
|
||||
|
||||
function showSelection() {
|
||||
if (linkFrom) return;
|
||||
const sel = window.getSelection();
|
||||
if (!sel || sel.isCollapsed || !sel.rangeCount) {
|
||||
hideBar();
|
||||
@@ -370,6 +376,8 @@ export async function mountFocus(host) {
|
||||
status.className = "focus-status";
|
||||
panel.appendChild(status);
|
||||
|
||||
if (linkFrom) renderLinkMode();
|
||||
|
||||
if (!annotations.length) {
|
||||
const empty = document.createElement("p");
|
||||
empty.className = "focus-empty";
|
||||
@@ -392,7 +400,13 @@ export async function mountFocus(host) {
|
||||
jump.type = "button";
|
||||
jump.className = "jump";
|
||||
jump.textContent = labelText(ann.label) + ": " + clip(ann.quote);
|
||||
jump.addEventListener("click", () => choose(ann));
|
||||
jump.addEventListener("click", () => {
|
||||
if (linkFrom && protocolName === "comment-stance") {
|
||||
makeLink(ann.id);
|
||||
return;
|
||||
}
|
||||
choose(ann);
|
||||
});
|
||||
li.appendChild(jump);
|
||||
if (ann._orphaned) {
|
||||
const badge = document.createElement("span");
|
||||
@@ -413,10 +427,114 @@ export async function mountFocus(host) {
|
||||
del.textContent = "Delete";
|
||||
del.addEventListener("click", () => remove(ann));
|
||||
actions.appendChild(del);
|
||||
if (protocolName === "cms-disposition") {
|
||||
const link = document.createElement("button");
|
||||
link.type = "button";
|
||||
link.textContent = "link to…";
|
||||
link.addEventListener("click", () => startLink(ann));
|
||||
actions.appendChild(link);
|
||||
}
|
||||
li.appendChild(actions);
|
||||
return li;
|
||||
}
|
||||
|
||||
function renderLinkMode() {
|
||||
const banner = document.createElement("p");
|
||||
banner.className = "focus-status";
|
||||
banner.textContent = "Choose a comment-stance highlight to link. The disposition responds to it.";
|
||||
panel.appendChild(banner);
|
||||
const cancel = document.createElement("button");
|
||||
cancel.type = "button";
|
||||
cancel.textContent = "Cancel link";
|
||||
cancel.addEventListener("click", () => {
|
||||
linkFrom = null;
|
||||
linkTargets = [];
|
||||
renderPanel();
|
||||
});
|
||||
panel.appendChild(cancel);
|
||||
|
||||
const form = document.createElement("form");
|
||||
form.className = "focus-link-form";
|
||||
const input = document.createElement("input");
|
||||
input.type = "text";
|
||||
input.placeholder = "other item key";
|
||||
input.setAttribute("aria-label", "Other item key");
|
||||
input.autocomplete = "off";
|
||||
const load = document.createElement("button");
|
||||
load.type = "submit";
|
||||
load.textContent = "Load";
|
||||
form.append(input, load);
|
||||
form.addEventListener("submit", (e) => {
|
||||
e.preventDefault();
|
||||
loadOther(input.value.trim());
|
||||
});
|
||||
panel.appendChild(form);
|
||||
|
||||
if (!linkTargets.length) return;
|
||||
const list = document.createElement("ul");
|
||||
list.className = "focus-list";
|
||||
for (const ann of linkTargets) {
|
||||
if (ann.id === linkFrom) continue;
|
||||
const li = document.createElement("li");
|
||||
const button = document.createElement("button");
|
||||
button.type = "button";
|
||||
button.className = "jump";
|
||||
button.textContent = (ann.item_key || "") + " — " + clip(ann.quote);
|
||||
button.addEventListener("click", () => makeLink(ann.id));
|
||||
li.appendChild(button);
|
||||
list.appendChild(li);
|
||||
}
|
||||
panel.appendChild(list);
|
||||
}
|
||||
|
||||
async function startLink(ann) {
|
||||
linkFrom = ann.id;
|
||||
linkTargets = [];
|
||||
const r = await fetch(
|
||||
"/api/focus/" + encodeURIComponent(host.key) + "/annotations?protocol=comment-stance",
|
||||
);
|
||||
if (r.ok) linkTargets = await r.json();
|
||||
renderPanel();
|
||||
}
|
||||
|
||||
async function loadOther(key) {
|
||||
if (!KEY_RE.test(key)) {
|
||||
setStatus("Item keys are 6 to 12 letters or digits.");
|
||||
return;
|
||||
}
|
||||
const r = await fetch(
|
||||
"/api/focus/" + encodeURIComponent(key) + "/annotations?protocol=comment-stance",
|
||||
);
|
||||
if (!r.ok) {
|
||||
setStatus("Could not load " + key + " (" + r.status + ").");
|
||||
return;
|
||||
}
|
||||
const rows = await r.json();
|
||||
const seen = new Set(linkTargets.map((a) => a.id));
|
||||
for (const row of rows) {
|
||||
if (!seen.has(row.id)) linkTargets.push(row);
|
||||
}
|
||||
renderPanel();
|
||||
if (!rows.length) setStatus("No comment-stance highlights on " + key + ".");
|
||||
}
|
||||
|
||||
async function makeLink(toId) {
|
||||
const r = await fetch("/api/focus/links", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ from_id: linkFrom, to_id: toId, kind: "responds_to" }),
|
||||
});
|
||||
if (!r.ok) {
|
||||
setStatus("Could not link (" + r.status + ").");
|
||||
return;
|
||||
}
|
||||
linkFrom = null;
|
||||
linkTargets = [];
|
||||
setStatus("Linked as responds-to.");
|
||||
renderPanel();
|
||||
setStatus("Linked as responds-to.");
|
||||
}
|
||||
|
||||
function choose(ann) {
|
||||
currentId = ann.id;
|
||||
writeUrl();
|
||||
|
||||
Reference in New Issue
Block a user