From 831dd82c6967fea86b2f1c707e855f4d23f30a79 Mon Sep 17 00:00:00 2001 From: Cristian Necula Date: Mon, 11 Dec 2023 03:14:57 +0200 Subject: [PATCH] feat: async images --- cosmoz-image-viewer.style.js | 7 ++- lib/hooks/use-cosmoz-image-viewer.js | 66 ++++++++++++++++++++-------- 2 files changed, 53 insertions(+), 20 deletions(-) diff --git a/cosmoz-image-viewer.style.js b/cosmoz-image-viewer.style.js index f801c3d..352e6a2 100644 --- a/cosmoz-image-viewer.style.js +++ b/cosmoz-image-viewer.style.js @@ -115,8 +115,13 @@ button.nav:active:not([disabled]) { cosmoz-slider { min-height: 150px; + overflow-y: auto !important; height:100%; - overflow-y: hidden; } +cz-spinner { + position: absolute; + top: 50%; + left: 50%; +} `; diff --git a/lib/hooks/use-cosmoz-image-viewer.js b/lib/hooks/use-cosmoz-image-viewer.js index 5069a7c..a1c3d1e 100644 --- a/lib/hooks/use-cosmoz-image-viewer.js +++ b/lib/hooks/use-cosmoz-image-viewer.js @@ -1,11 +1,17 @@ +import '@neovici/cosmoz-utils/elements/cz-spinner'; + import { useSlideList } from '@neovici/cosmoz-slider'; import { useImperativeApi } from '@neovici/cosmoz-utils/hooks/use-imperative-api'; import { useNotifyProperty } from '@neovici/cosmoz-utils/hooks/use-notify-property'; -import { useCallback, useEffect, useState } from 'haunted'; -import { html } from 'lit-html'; +import { useCallback, useEffect, useMemo, useState } from 'haunted'; +import { html, nothing } from 'lit-html'; import { download } from '../pdf'; import { popout, hasWindow } from '../popout'; import { print } from '../print'; +import { guard } from 'lit-html/directives/guard.js'; +import { until } from 'lit-html/directives/until.js'; +import { invoke } from '@neovici/cosmoz-utils/function'; +import { memoize } from '@neovici/cosmoz-utils/memoize'; const onImageError = (e) => { if (!e.currentTarget.parentElement) { @@ -18,21 +24,32 @@ const onImageError = (e) => { e.currentTarget.setAttribute('hidden', true); }, onStatusChanged = (ev) => ev.detail.value === 'error' && onImageError(ev), - renderImage = ({ showZoom, image, isZoomed, _onZoomChanged }) => - showZoom - ? html`` - : html``, + renderImage = ({ src$, showZoom, isZoomed, _onZoomChanged }) => { + const src = guard(src$, () => until(src$)); + + return [ + showZoom + ? html`` + : html``, + guard(src$, () => + until( + src$.then(() => nothing), + html``, + ), + ), + ]; + }, render = (opts) => html`