Skip to content

Commit 3be1a6e

Browse files
authored
refactor manage columns to use vault (#450)
* refactor manage columns to use vault * refactor manage columns to use vault
1 parent f6cad0d commit 3be1a6e

2 files changed

Lines changed: 26 additions & 44 deletions

File tree

components/create-column/index.js

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import TPEN from "../../api/TPEN.js"
22
import CheckPermissions from "../check-permissions/checkPermissions.js"
33
import { onProjectReady } from "../../utilities/projectReady.js"
44
import { CleanupRegistry } from "../../utilities/CleanupRegistry.js"
5+
import vault from '../../js/vault.js'
56

67
/**
78
* TpenCreateColumn - Interface for creating and managing columns on annotation pages.
@@ -563,27 +564,14 @@ class TpenCreateColumn extends HTMLElement {
563564
return { x: xywh[0], y: xywh[1], w: xywh[2], h: xywh[3] }
564565
}
565566

566-
isValidUrl(str) {
567-
try {
568-
new URL(str)
569-
return true
570-
} catch {
571-
return false
572-
}
573-
}
574-
575-
async getSpecificTypeData(type) {
576-
if (!type) throw new Error("No IIIF resource provided")
577-
if (typeof type === "string" && this.isValidUrl(type)) {
578-
const res = await fetch(type, { cache: "no-store" })
579-
if (!res.ok) throw new Error(`Fetch failed: ${res.status}`)
580-
return await res.json()
581-
}
582-
}
583-
584567
async fetchPageViewerData(pageID = null) {
585-
const annotationPageData = pageID ? await this.getSpecificTypeData(pageID) : null
586-
const canvasData = await this.getSpecificTypeData(annotationPageData.target)
568+
if (!pageID) throw new Error("No page ID provided")
569+
const annotationPageData = await vault.getWithFallback(pageID, 'annotationpage', TPEN.activeProject?.manifest, true)
570+
if (!annotationPageData) throw new Error("Failed to load annotation page")
571+
const canvasData = await vault.getWithFallback(
572+
annotationPageData.target, 'canvas', TPEN.activeProject?.manifest
573+
)
574+
if (!canvasData) throw new Error("Failed to load canvas data")
587575
return await this.processDirectCanvasData(canvasData, annotationPageData)
588576
}
589577

@@ -597,8 +585,11 @@ class TpenCreateColumn extends HTMLElement {
597585
if (!annotationPageData?.items) return []
598586
const results = await Promise.all(annotationPageData.items.map(async anno => {
599587
try {
600-
const res = await fetch(anno.id, { cache: "no-store" })
601-
const data = await res.json()
588+
let data = anno
589+
if (!data?.target) {
590+
data = await vault.get(anno.id ?? anno, 'annotation', true)
591+
}
592+
if (!data) return null
602593
return { target: data?.target?.selector?.value ?? data?.target, lineId: data?.id }
603594
} catch { return null }
604595
}))

interfaces/manage-columns/index.js

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import CheckPermissions from "../../components/check-permissions/checkPermission
33
import { renderPermissionError } from "../../utilities/renderPermissionError.js"
44
import { onProjectReady } from "../../utilities/projectReady.js"
55
import { CleanupRegistry } from '../../utilities/CleanupRegistry.js'
6+
import vault from '../../js/vault.js'
67

78
/**
89
* TpenManageColumns - Interface for managing column assignments on annotation pages.
@@ -633,27 +634,14 @@ class TpenManageColumns extends HTMLElement {
633634
return { x: xywh[0], y: xywh[1], w: xywh[2], h: xywh[3] }
634635
}
635636

636-
isValidUrl(str) {
637-
try {
638-
new URL(str)
639-
return true
640-
} catch {
641-
return false
642-
}
643-
}
644-
645-
async getSpecificTypeData(type) {
646-
if (!type) throw new Error("No IIIF resource provided")
647-
if (typeof type === "string" && this.isValidUrl(type)) {
648-
const res = await fetch(type, { cache: "no-store" })
649-
if (!res.ok) throw new Error(`Fetch failed: ${res.status}`)
650-
return await res.json()
651-
}
652-
}
653-
654637
async fetchPageViewerData(pageID = null) {
655-
const annotationPageData = pageID ? await this.getSpecificTypeData(pageID) : null
656-
const canvasData = await this.getSpecificTypeData(annotationPageData.target)
638+
if (!pageID) throw new Error("No page ID provided")
639+
const annotationPageData = await vault.getWithFallback(pageID, 'annotationpage', TPEN.activeProject?.manifest, true)
640+
if (!annotationPageData) throw new Error("Failed to load annotation page")
641+
const canvasData = await vault.getWithFallback(
642+
annotationPageData.target, 'canvas', TPEN.activeProject?.manifest
643+
)
644+
if (!canvasData) throw new Error("Failed to load canvas data")
657645
return await this.processDirectCanvasData(canvasData, annotationPageData)
658646
}
659647

@@ -667,8 +655,11 @@ class TpenManageColumns extends HTMLElement {
667655
if (!annotationPageData?.items) return []
668656
const results = await Promise.all(annotationPageData.items.map(async anno => {
669657
try {
670-
const res = await fetch(anno.id, { cache: "no-store" })
671-
const data = await res.json()
658+
let data = anno
659+
if (!data?.target) {
660+
data = await vault.get(anno.id ?? anno, 'annotation', true)
661+
}
662+
if (!data) return null
672663
return { target: data?.target?.selector?.value ?? data?.target, lineId: data?.id }
673664
} catch { return null }
674665
}))

0 commit comments

Comments
 (0)