Skip to content
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 42 additions & 23 deletions components/annotorious-annotator/line-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1084,41 +1084,54 @@ class AnnotoriousAnnotator extends HTMLElement {
let page = JSON.parse(JSON.stringify(this.#resolvedAnnotationPage))
page.items = allAnnotations
const pageID = page["@id"] ?? page.id
const mod = await fetch(`${TPEN.servicesURL}/project/${TPEN.activeProject._id}/page/${pageID.split("/").pop()}`, {
let mod
try {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is weird. The fetch.then.catch is replaced with a try{fetch.then}catch{} and it looks like nothing is really changed until after this block around line 1109.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It believed that the try/catch was more explicit and I also think it thought that was the most reliable way to stop code execution. In practice the old way works the same, so I reverted this change.

const res = await fetch(`${TPEN.servicesURL}/project/${TPEN.activeProject._id}/page/${pageID.split("/").pop()}`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${TPEN.getAuthorization()}`,
},
body: JSON.stringify({ "items": page.items })
})
.then(res => {
if(!res.ok) {
TPEN.eventDispatcher.dispatch("tpen-toast", {
message: "ERROR Annotations Not Saved",
status: "error"
})
throw new Error("Could not save annotations", { "cause": `\n${res.status} Error from TPEN Services. Check the Network response.` })
}
return res.json()
})
.catch(err => {
saveButton.textContent = "ERROR"
throw err
})
page.items = page.items.map(i => ({
...i,
...(mod.items?.find(a => a.target === i.target) ?? {})
}))
if (!res.ok) {
TPEN.eventDispatcher.dispatch("tpen-toast", {
message: "ERROR Annotations Not Saved",
status: "error"
})
throw new Error("Could not save annotations", { "cause": `\n${res.status} Error from TPEN Services. Check the Network response.` })
}
mod = await res.json()
} catch (err) {
saveButton.textContent = "ERROR"
throw err
}
page.items = page.items.map(i => {
const selectorValue = i.target?.selector?.value ?? i.target
// Prefer matching by ID for previously-saved annotations, fall back to selector for new ones
const match = mod.items?.find(a => a.id === i.id)
?? mod.items?.find(a => {
const aSelector = a.target?.selector?.value ?? a.target
return aSelector === selectorValue
})
return match ? { ...i, ...match } : i
})
this.#modifiedAnnotationPage = page
this.#resolvedAnnotationPage = JSON.parse(JSON.stringify(page))
// Sync server-assigned IDs back to Annotorious so subsequent saves use the correct IDs
let syncAnnotations = JSON.parse(JSON.stringify(page.items))
syncAnnotations = this.formatAnnotations(syncAnnotations)
syncAnnotations = this.convertSelectors(syncAnnotations, true)
this.#annotoriousInstance.clearAnnotations()
this.#annotoriousInstance.setAnnotations(syncAnnotations, false)
this.#resolvedAnnotationPage.$isDirty = false
TPEN.eventDispatcher.dispatch("tpen-page-committed", this.#modifiedAnnotationPage)
TPEN.eventDispatcher.dispatch("tpen-toast", {
message: "Annotations Saved",
status: "success"
})
saveButton.removeAttribute("disabled")
saveButton.textContent = "Save Annotations"
this.#resolvedAnnotationPage.$isDirty = false
return this.#modifiedAnnotationPage
}

Expand All @@ -1127,17 +1140,23 @@ class AnnotoriousAnnotator extends HTMLElement {
* https://annotorious.dev/api-reference/openseadragon-annotator/#clearannotations
*/
async deleteAllAnnotations() {
const deleteAllBtn = this.shadowRoot.getElementById("deleteAllBtn")
deleteAllBtn.setAttribute("disabled", "true")
deleteAllBtn.textContent = "deleting. please wait..."
this.#annotoriousInstance.clearAnnotations()
this.#resolvedAnnotationPage.$isDirty = true
await this.saveAnnotations()
try {
await this.saveAnnotations()
await this.clearColumnsServerSide()
} catch (err) {
console.error("Could not clear columns server side.", err)
console.error("Could not delete all annotations.", err)
TPEN.eventDispatcher.dispatch("tpen-toast", {
message: "Could not clear columns. Some column data may remain.",
message: "Could not delete all annotations.",
status: "error"
})
} finally {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this makes sense.

deleteAllBtn.removeAttribute("disabled")
deleteAllBtn.textContent = "Delete All Annotations"
}
}

Expand Down