@knaw-huc/osd-iiif-viewer 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1796,7 +1796,15 @@ const defaultState$1 = {
1796
1796
  setIsFullPage: (isFullPage) => set({ isFullPage }),
1797
1797
  setRotation: (rotation) => set({ rotation }),
1798
1798
  reset: () => set(defaultState)
1799
- }), defaultManifest = {
1799
+ });
1800
+ function findCanvasIndex(vault, manifestId, canvasId) {
1801
+ return vault.get({ id: manifestId, type: "Manifest" }).items.findIndex((item) => vault.get(item).id === canvasId);
1802
+ }
1803
+ function orThrow(msg) {
1804
+ throw new Error(msg);
1805
+ }
1806
+ const defaultManifest = {
1807
+ id: null,
1800
1808
  url: null,
1801
1809
  isLoading: !1,
1802
1810
  isReady: !1,
@@ -1804,10 +1812,20 @@ const defaultState$1 = {
1804
1812
  }, createManifestSlice = (vault) => (set) => ({
1805
1813
  vault,
1806
1814
  ...defaultManifest,
1807
- loadManifest: async (url) => {
1815
+ loadManifest: async (url, initialCanvasId) => {
1808
1816
  set({ url, isLoading: !0, error: null });
1809
1817
  try {
1810
- await vault.loadManifest(url), set({ isLoading: !1, error: null });
1818
+ const id = (await vault.loadManifest(url))?.id || orThrow("No manifest"), updates = {
1819
+ id,
1820
+ url,
1821
+ isLoading: !1,
1822
+ error: null
1823
+ };
1824
+ if (initialCanvasId) {
1825
+ const index = findCanvasIndex(vault, id, initialCanvasId);
1826
+ index !== -1 && (updates.currentIndex = index);
1827
+ }
1828
+ set(updates);
1811
1829
  } catch (e2) {
1812
1830
  const error = e2 instanceof Error ? e2.message : "Unknown error";
1813
1831
  set({ isLoading: !1, error });
@@ -1819,12 +1837,23 @@ const defaultState$1 = {
1819
1837
  }, createCanvasSlice = (set, get) => ({
1820
1838
  ...defaultCanvas,
1821
1839
  goToCanvas: (index) => {
1822
- const { vault, url } = get();
1823
- if (!url)
1840
+ const { vault, id: manifestId } = get();
1841
+ if (!manifestId)
1824
1842
  return;
1825
- const manifest = vault.get({ id: url, type: "Manifest" }), clamped = Math.max(0, Math.min(index, manifest.items.length - 1));
1843
+ const manifest = vault.get({ id: manifestId, type: "Manifest" }), clamped = Math.max(0, Math.min(index, manifest.items.length - 1));
1826
1844
  set({ currentIndex: clamped });
1827
1845
  },
1846
+ goToCanvasById: (id) => {
1847
+ const { vault, id: manifestId, goToCanvas } = get();
1848
+ if (!manifestId)
1849
+ return;
1850
+ const index = findCanvasIndex(vault, manifestId, id);
1851
+ if (index === -1) {
1852
+ console.warn(`Canvas not found: ${id}`);
1853
+ return;
1854
+ }
1855
+ goToCanvas(index);
1856
+ },
1828
1857
  nextCanvas: () => {
1829
1858
  const { currentIndex, goToCanvas } = get();
1830
1859
  goToCanvas(currentIndex + 1);
@@ -1850,9 +1879,6 @@ const defaultState$1 = {
1850
1879
  };
1851
1880
  });
1852
1881
  }, ViewerContext = createContext(null);
1853
- function orThrow(msg) {
1854
- throw new Error(msg);
1855
- }
1856
1882
  function useViewerStore() {
1857
1883
  return useContext(ViewerContext) ?? orThrow("viewer hook must be used within viewer provider");
1858
1884
  }
@@ -1926,7 +1952,14 @@ function getImageServiceId(vault, canvas) {
1926
1952
  if (!isExternalWebResource(resource))
1927
1953
  return null;
1928
1954
  const services = de(resource);
1929
- return services.length ? H(services[0]) ?? null : null;
1955
+ return services.length ? H(services[0]) ?? null : getImageServiceIdWithoutProfileCheck(resource);
1956
+ }
1957
+ function getImageServiceIdWithoutProfileCheck(resource) {
1958
+ const service = resource.service;
1959
+ if (!Array.isArray(service) || !service.length)
1960
+ return null;
1961
+ const first = service[0];
1962
+ return typeof first == "object" && first && "id" in first ? first.id : null;
1930
1963
  }
1931
1964
  function isExternalWebResource(resource) {
1932
1965
  return resource.type !== "SpecificResource";
@@ -1940,17 +1973,26 @@ function useViewerStoreSelector(selector) {
1940
1973
  return useStore(store, selector);
1941
1974
  }
1942
1975
  function useCanvas() {
1943
- const vault = useViewerStoreSelector((s2) => s2.vault), url = useViewerStoreSelector((s2) => s2.url), isLoading = useViewerStoreSelector((s2) => s2.isLoading), currentIndex = useViewerStoreSelector((s2) => s2.currentIndex), goTo = useViewerStoreSelector((s2) => s2.goToCanvas), next = useViewerStoreSelector((s2) => s2.nextCanvas), prev = useViewerStoreSelector((s2) => s2.prevCanvas), { current, total } = useMemo(() => {
1944
- if (!url || isLoading)
1976
+ const vault = useViewerStoreSelector((s2) => s2.vault), id = useViewerStoreSelector((s2) => s2.id), isLoading = useViewerStoreSelector((s2) => s2.isLoading), currentIndex = useViewerStoreSelector((s2) => s2.currentIndex), goTo = useViewerStoreSelector((s2) => s2.goToCanvas), goToById = useViewerStoreSelector((s2) => s2.goToCanvasById), next = useViewerStoreSelector((s2) => s2.nextCanvas), prev = useViewerStoreSelector((s2) => s2.prevCanvas), { current, total } = useMemo(() => {
1977
+ if (!id || isLoading)
1945
1978
  return { current: null, total: 0 };
1946
- const manifest = vault.get({ id: url, type: "Manifest" }), canvasRef = manifest.items[currentIndex] ?? orThrow("No canvas ref for " + currentIndex), current2 = vault.get(canvasRef), total2 = manifest.items.length;
1979
+ const manifest = vault.get({ id, type: "Manifest" }), canvasRef = manifest.items[currentIndex] ?? orThrow("No canvas ref for " + currentIndex), current2 = vault.get(canvasRef), total2 = manifest.items.length;
1947
1980
  return { current: current2, total: total2 };
1948
- }, [vault, url, isLoading, currentIndex]);
1949
- return { currentIndex, current, total, goTo, next, prev };
1981
+ }, [vault, id, isLoading, currentIndex]), currentCanvasId = current?.id ?? null;
1982
+ return {
1983
+ currentIndex,
1984
+ currentCanvasId,
1985
+ current,
1986
+ total,
1987
+ goTo,
1988
+ goToById,
1989
+ next,
1990
+ prev
1991
+ };
1950
1992
  }
1951
1993
  function useManifest() {
1952
- const vault = useViewerStoreSelector((s2) => s2.vault), url = useViewerStoreSelector((s2) => s2.url), isLoading = useViewerStoreSelector((s2) => s2.isLoading), error = useViewerStoreSelector((s2) => s2.error), loadManifest = useViewerStoreSelector((s2) => s2.loadManifest);
1953
- return { vault, url, isLoading, error, isReady: !!url && !isLoading && !error, loadManifest };
1994
+ const vault = useViewerStoreSelector((s2) => s2.vault), id = useViewerStoreSelector((s2) => s2.id), url = useViewerStoreSelector((s2) => s2.url), isLoading = useViewerStoreSelector((s2) => s2.isLoading), error = useViewerStoreSelector((s2) => s2.error), loadManifest = useViewerStoreSelector((s2) => s2.loadManifest);
1995
+ return { vault, id, url, isLoading, error, isReady: !!url && !isLoading && !error, loadManifest };
1954
1996
  }
1955
1997
  function useLoadManifest() {
1956
1998
  return useViewerStoreSelector((s2) => s2.loadManifest);
@@ -2048,23 +2090,23 @@ function useImageInfo() {
2048
2090
  }, [viewer, ready]);
2049
2091
  }
2050
2092
  function Overlay({ location, children }) {
2051
- const viewer = useViewer(), [container] = useState(() => {
2052
- const el = document.createElement("div");
2053
- return el.style.position = "absolute", el.style.pointerEvents = "none", el;
2093
+ const viewer = useViewer(), [overlay] = useState(() => {
2094
+ const div = document.createElement("div");
2095
+ return div.style.position = "absolute", div.style.pointerEvents = "none", div;
2054
2096
  });
2055
2097
  return useEffect(() => {
2056
- function stopPropagation(e2) {
2057
- e2.target !== container && e2.stopPropagation();
2098
+ function stopPropagation(event) {
2099
+ event.target !== overlay && event.stopPropagation();
2058
2100
  }
2059
- return container.addEventListener("pointerdown", stopPropagation), () => {
2060
- container.removeEventListener("pointerdown", stopPropagation);
2101
+ return overlay.addEventListener("pointerdown", stopPropagation), () => {
2102
+ overlay.removeEventListener("pointerdown", stopPropagation);
2061
2103
  };
2062
- }, [container]), useEffect(() => {
2104
+ }, [overlay]), useEffect(() => {
2063
2105
  if (viewer)
2064
- return viewer.addOverlay({ element: container, location }), () => {
2065
- viewer.isOpen() && viewer.removeOverlay(container);
2106
+ return viewer.addOverlay({ element: overlay, location }), () => {
2107
+ viewer.isOpen() && viewer.removeOverlay(overlay);
2066
2108
  };
2067
- }, [viewer, location, container]), viewer ? createPortal(children, container) : null;
2109
+ }, [viewer, location, overlay]), viewer ? createPortal(children, overlay) : null;
2068
2110
  }
2069
2111
  function ViewerProvider({ children }) {
2070
2112
  const [store] = useState(() => createViewerStore());
@@ -6,6 +6,7 @@ export type CanvasState = {
6
6
  };
7
7
  export type CanvasSlice = Resettable & CanvasState & {
8
8
  goToCanvas: (index: number) => void;
9
+ goToCanvasById: (id: string) => void;
9
10
  nextCanvas: () => void;
10
11
  prevCanvas: () => void;
11
12
  };
@@ -3,6 +3,7 @@ import { Vault } from '@iiif/helpers/vault';
3
3
  import { ViewerStore } from '../ViewerStore.ts';
4
4
  import { Resettable } from '../Resettable.ts';
5
5
  export type ManifestState = {
6
+ id: string | null;
6
7
  url: string | null;
7
8
  isLoading: boolean;
8
9
  error: string | null;
@@ -10,6 +11,6 @@ export type ManifestState = {
10
11
  };
11
12
  export type ManifestSlice = Resettable & ManifestState & {
12
13
  vault: Vault;
13
- loadManifest: (url: string) => Promise<void>;
14
+ loadManifest: (url: string, initialCanvasId?: string) => Promise<void>;
14
15
  };
15
16
  export declare const createManifestSlice: (vault: Vault) => StateCreator<ViewerStore, [], [], ManifestSlice>;
@@ -0,0 +1,2 @@
1
+ import { Vault } from '@iiif/helpers/vault';
2
+ export declare function findCanvasIndex(vault: Vault, manifestId: string, canvasId: string): number;
@@ -1,9 +1,11 @@
1
1
  import { CanvasNormalized } from '@iiif/presentation-3-normalized';
2
2
  export declare function useCanvas(): {
3
3
  currentIndex: number;
4
+ currentCanvasId: string | null;
4
5
  current: CanvasNormalized | null;
5
6
  total: number;
6
7
  goTo: (index: number) => void;
8
+ goToById: (id: string) => void;
7
9
  next: () => void;
8
10
  prev: () => void;
9
11
  };
@@ -1,9 +1,10 @@
1
1
  export declare function useManifest(): {
2
2
  vault: import('@iiif/helpers/vault').Vault;
3
+ id: string | null;
3
4
  url: string | null;
4
5
  isLoading: boolean;
5
6
  error: string | null;
6
7
  isReady: boolean;
7
- loadManifest: (url: string) => Promise<void>;
8
+ loadManifest: (url: string, initialCanvasId?: string) => Promise<void>;
8
9
  };
9
- export declare function useLoadManifest(): (url: string) => Promise<void>;
10
+ export declare function useLoadManifest(): (url: string, initialCanvasId?: string) => Promise<void>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knaw-huc/osd-iiif-viewer",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/knaw-huc/osd-iiif-viewer.git"