Skip to content

Commit

Permalink
fix: include img tags on mobile pwa (#375)
Browse files Browse the repository at this point in the history
  • Loading branch information
Renji-XD authored Oct 7, 2024
1 parent 77c4e7f commit c54bcd8
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 46 deletions.
1 change: 1 addition & 0 deletions apps/web/src/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ declare global {
}
interface Navigator {
msMaxTouchPoints: number;
standalone: boolean | undefined;
}
}

Expand Down
9 changes: 8 additions & 1 deletion apps/web/src/lib/components/book-reader/book-reader.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import { iffBrowser } from '$lib/functions/rxjs/iff-browser';
import { reduceToEmptyString } from '$lib/functions/rxjs/reduce-to-empty-string';
import { writableSubject } from '$lib/functions/svelte/store';
import { isMobile$ } from '$lib/functions/utils';
import { logger } from '$lib/data/logger';
import { imageLoadingState } from './image-loading-state';
import { reactiveElements } from './reactive-elements';
Expand Down Expand Up @@ -158,7 +159,13 @@
const reactiveElements$ = iffBrowser(() => of(document)).pipe(
mergeMap((document) => {
const reactiveElementsFn = reactiveElements(document, furiganaStyle, hideSpoilerImage);
const reactiveElementsFn = reactiveElements(
document,
furiganaStyle,
hideSpoilerImage,
$isMobile$ &&
(navigator.standalone || window.matchMedia('(display-mode: standalone)').matches)
);
return contentEl$.pipe(mergeMap((contentEl) => reactiveElementsFn(contentEl)));
}),
reduceToEmptyString()
Expand Down
101 changes: 56 additions & 45 deletions apps/web/src/lib/components/book-reader/reactive-elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import { toggleImageGalleryPictureSpoiler$ } from '$lib/components/book-reader/b
export function reactiveElements(
document: Document,
furiganaStyle: FuriganaStyle,
hideSpoilerImage: boolean
hideSpoilerImage: boolean,
includeImgTag: boolean
) {
const anchorTagDocumentListener = anchorTagListener(document);
const spoilerImageDocumentListener = spoilerImageListener(document);
Expand All @@ -36,7 +37,7 @@ export function reactiveElements(
anchorTagDocumentListener(contentEl),
rubyTagListener(contentEl, furiganaStyle),
spoilerImageDocumentListener(contentEl),
openImageInNewTab(contentEl, hideSpoilerImage)
openImageInNewTab(contentEl, hideSpoilerImage, includeImgTag)
);
}

Expand Down Expand Up @@ -109,51 +110,61 @@ function spoilerImageListener(document: Document) {
};
}

function openImageInNewTab(contentEl: HTMLElement, hideSpoilerImage: boolean) {
function openImageInNewTab(
contentEl: HTMLElement,
hideSpoilerImage: boolean,
includeImgTag: boolean
) {
return merge(
...[...contentEl.querySelectorAll<HTMLElement>('image')].map((elm) =>
fromEvent(elm, 'pointerdown').pipe(
switchMap((event) => {
const { clientX, clientY } = event as PointerEvent;

return timer(1000).pipe(
takeUntil(
race(
fromEvent(elm, 'pointermove').pipe(
throttleTime(200, undefined, { trailing: true }),
filter((event2) => {
const { clientX: newX, clientY: newY } = event2 as PointerEvent;

return Math.abs(clientX - newX) > 5 || Math.abs(clientY - newY) > 5;
})
),
fromEvent(elm, 'pointerup'),
fromEvent(elm, 'pointercancel')
...[...contentEl.querySelectorAll<HTMLElement>(`${includeImgTag ? 'img,' : ''}image`)].map(
(elm) =>
fromEvent(elm, 'pointerdown').pipe(
switchMap((event) => {
const { clientX, clientY } = event as PointerEvent;

return timer(1000).pipe(
takeUntil(
race(
fromEvent(elm, 'pointermove').pipe(
throttleTime(200, undefined, { trailing: true }),
filter((event2) => {
const { clientX: newX, clientY: newY } = event2 as PointerEvent;

return Math.abs(clientX - newX) > 5 || Math.abs(clientY - newY) > 5;
})
),
fromEvent(elm, 'pointerup'),
fromEvent(elm, 'pointercancel')
)
)
)
);
}),
filter(
() =>
!hideSpoilerImage ||
elm.classList.contains('ttu-unspoilered') ||
!elm.closest('span[data-ttu-spoiler-img]')
),
switchMap(() => {
pulseElement(elm.parentElement ? elm.parentElement : elm, 'add', 0.5, 500);

return merge(fromEvent(elm, 'pointerup'), fromEvent(elm, 'pointercancel')).pipe(
take(1),
tap(() => {
const src = elm.getAttribute('src') || elm.getAttribute('href');

if (src) {
window.open(src, '_blank');
}
})
);
})
)
);
}),
filter(
() =>
!hideSpoilerImage ||
elm.classList.contains('ttu-unspoilered') ||
!elm.closest('span[data-ttu-spoiler-img]')
),
switchMap(() => {
pulseElement(
elm.parentElement && elm.tagName.toLowerCase() === 'image' ? elm.parentElement : elm,
'add',
0.5,
500
);

return merge(fromEvent(elm, 'pointerup'), fromEvent(elm, 'pointercancel')).pipe(
take(1),
tap(() => {
const src = elm.getAttribute('src') || elm.getAttribute('href');

if (src) {
window.open(src, '_blank');
}
})
);
})
)
)
);
}
Expand Down

0 comments on commit c54bcd8

Please sign in to comment.