Merge pull request 'fix(ui): copy GCG to clipboard on Android in-app WebViews' (#151) from feature/gcg-export-android-fix into development
This commit was merged in pull request #151.
This commit is contained in:
@@ -310,6 +310,7 @@ export const en = {
|
||||
|
||||
'game.exportGcg': 'Export GCG',
|
||||
'game.gcgActiveOnly': 'Available once the game is finished.',
|
||||
'game.gcgCopied': 'GCG copied to the clipboard.',
|
||||
'game.addFriendShort': 'Add friend?',
|
||||
'game.blockShort': 'Block?',
|
||||
|
||||
|
||||
@@ -310,6 +310,7 @@ export const ru: Record<MessageKey, string> = {
|
||||
|
||||
'game.exportGcg': 'Экспорт GCG',
|
||||
'game.gcgActiveOnly': 'Доступно после завершения игры.',
|
||||
'game.gcgCopied': 'GCG скопирован в буфер обмена.',
|
||||
'game.addFriendShort': 'В друзья?',
|
||||
'game.blockShort': 'В бан?',
|
||||
|
||||
|
||||
+35
-13
@@ -7,26 +7,31 @@ const file = {} as File;
|
||||
const gcg: GcgExport = { gameId: 'g1', filename: 'game.gcg', content: '#title game' };
|
||||
|
||||
describe('pickGcgDelivery', () => {
|
||||
it('shares when the platform can share files', () => {
|
||||
expect(pickGcgDelivery({ canShare: () => true, share: async () => {} }, file)).toBe('share');
|
||||
const canShareNav = { canShare: () => true, share: async () => {} };
|
||||
const noShareNav = { canShare: () => false, share: async () => {} };
|
||||
|
||||
it('shares when the platform can share files, even in an in-app webview (iOS)', () => {
|
||||
expect(pickGcgDelivery(canShareNav, file, false)).toBe('share');
|
||||
expect(pickGcgDelivery(canShareNav, file, true)).toBe('share');
|
||||
});
|
||||
|
||||
it('downloads when the platform cannot share files', () => {
|
||||
expect(pickGcgDelivery({ canShare: () => false, share: async () => {} }, file)).toBe('download');
|
||||
it('copies in an in-app webview that cannot share files (Android Telegram/VK: no share, dead download)', () => {
|
||||
expect(pickGcgDelivery(noShareNav, file, true)).toBe('copy');
|
||||
expect(pickGcgDelivery(undefined, file, true)).toBe('copy');
|
||||
expect(pickGcgDelivery({ canShare: () => true } as never, file, true)).toBe('copy');
|
||||
});
|
||||
|
||||
it('downloads when there is no navigator', () => {
|
||||
expect(pickGcgDelivery(undefined, file)).toBe('download');
|
||||
});
|
||||
|
||||
it('downloads when the Web Share API is incomplete', () => {
|
||||
expect(pickGcgDelivery({ canShare: () => true } as never, file)).toBe('download');
|
||||
it('downloads on a plain browser without Web Share (desktop)', () => {
|
||||
expect(pickGcgDelivery(noShareNav, file, false)).toBe('download');
|
||||
expect(pickGcgDelivery(undefined, file, false)).toBe('download');
|
||||
});
|
||||
});
|
||||
|
||||
describe('shareOrDownloadGcg', () => {
|
||||
afterEach(() => vi.unstubAllGlobals());
|
||||
|
||||
const noCopy = async () => false;
|
||||
|
||||
function stubDownloadEnv(canShare: boolean, share: () => Promise<void>) {
|
||||
const anchor = { href: '', download: '', click: vi.fn(), remove: vi.fn() };
|
||||
const createElement = vi.fn(() => anchor);
|
||||
@@ -45,20 +50,37 @@ describe('shareOrDownloadGcg', () => {
|
||||
const share = vi.fn().mockRejectedValue(new DOMException('cancelled', 'AbortError'));
|
||||
const { createElement } = stubDownloadEnv(true, share);
|
||||
|
||||
await shareOrDownloadGcg(gcg);
|
||||
expect(await shareOrDownloadGcg(gcg, false, noCopy)).toBe('shared');
|
||||
|
||||
expect(share).toHaveBeenCalledOnce();
|
||||
expect(createElement).not.toHaveBeenCalled(); // no download anchor → no webview navigation
|
||||
});
|
||||
|
||||
it('downloads via an anchor when the platform cannot share files', async () => {
|
||||
it('downloads via an anchor on a desktop browser that cannot share files', async () => {
|
||||
const { anchor, createElement } = stubDownloadEnv(false, vi.fn());
|
||||
|
||||
await shareOrDownloadGcg(gcg);
|
||||
expect(await shareOrDownloadGcg(gcg, false, noCopy)).toBe('downloaded');
|
||||
|
||||
expect(createElement).toHaveBeenCalledWith('a');
|
||||
expect(anchor.click).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it('copies to the clipboard in an in-app webview that cannot share files (no dead Blob download)', async () => {
|
||||
// Android Telegram/VK expose no Web Share AND ignore <a download>, so the export must copy the
|
||||
// GCG text instead of silently issuing an anchor click that does nothing.
|
||||
const copy = vi.fn().mockResolvedValue(true);
|
||||
const { createElement } = stubDownloadEnv(false, vi.fn());
|
||||
|
||||
expect(await shareOrDownloadGcg(gcg, true, copy)).toBe('copied');
|
||||
|
||||
expect(copy).toHaveBeenCalledWith(gcg.content);
|
||||
expect(createElement).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('reports failure when the in-app clipboard copy fails', async () => {
|
||||
stubDownloadEnv(false, vi.fn());
|
||||
expect(await shareOrDownloadGcg(gcg, true, async () => false)).toBe('failed');
|
||||
});
|
||||
});
|
||||
|
||||
describe('pickTextShare', () => {
|
||||
|
||||
+42
-15
@@ -1,14 +1,26 @@
|
||||
// GCG export delivery: share on mobile (Web Share API with a file) where supported,
|
||||
// otherwise download via a Blob + <a download> on desktop. The Capacitor-native file
|
||||
// save lands with the native wrapper; the Web Share path already covers mobile
|
||||
// browsers. pickGcgDelivery is the pure decision, unit-tested with a mock navigator.
|
||||
// GCG export delivery: Web Share (with the file) on mobile where supported — including the iOS
|
||||
// Telegram Mini App. An Android in-app WebView (Telegram / VK) has NO Web Share and silently ignores
|
||||
// an <a download>, so there the tiny GCG text is copied to the clipboard instead; a plain desktop
|
||||
// browser, where the anchor download works, still downloads a Blob. The Capacitor-native file save
|
||||
// lands with the native wrapper. pickGcgDelivery is the pure decision, unit-tested with a mock
|
||||
// navigator; the caller supplies the platform-aware clipboard copy.
|
||||
|
||||
import type { GcgExport } from './model';
|
||||
|
||||
type ShareNav = Pick<Navigator, 'canShare' | 'share'>;
|
||||
|
||||
/** pickGcgDelivery decides between the Web Share API and a Blob download for a file. */
|
||||
export function pickGcgDelivery(nav: ShareNav | undefined, file: File): 'share' | 'download' {
|
||||
/**
|
||||
* pickGcgDelivery decides how to deliver the GCG file. Web Share (with the file) wins wherever it is
|
||||
* available — the iOS Telegram Mini App and mobile browsers. Failing that, an in-app WebView (Android
|
||||
* Telegram / VK) has no Web Share and silently ignores an <a download>, so the tiny GCG text is copied
|
||||
* to the clipboard ('copy'); only a plain desktop browser, where the anchor download works, falls
|
||||
* through to 'download'. Pure, so it is unit-tested with a mock navigator.
|
||||
*/
|
||||
export function pickGcgDelivery(
|
||||
nav: ShareNav | undefined,
|
||||
file: File,
|
||||
inAppWebView: boolean,
|
||||
): 'share' | 'copy' | 'download' {
|
||||
if (
|
||||
nav &&
|
||||
typeof nav.canShare === 'function' &&
|
||||
@@ -17,27 +29,42 @@ export function pickGcgDelivery(nav: ShareNav | undefined, file: File): 'share'
|
||||
) {
|
||||
return 'share';
|
||||
}
|
||||
return 'download';
|
||||
return inAppWebView ? 'copy' : 'download';
|
||||
}
|
||||
|
||||
/** shareOrDownloadGcg shares the GCG file where supported, else triggers a download. */
|
||||
export async function shareOrDownloadGcg(gcg: GcgExport): Promise<void> {
|
||||
/**
|
||||
* shareOrDownloadGcg delivers the GCG export by the best available route and reports which it took —
|
||||
* 'shared', 'copied', 'downloaded' or 'failed' — so the caller can confirm a silent copy to the user.
|
||||
* inAppWebView marks an Android Telegram/VK WebView (no Web Share, a dead <a download>); copyText is
|
||||
* the platform-aware clipboard write (VKWebAppCopyText inside VK, navigator.clipboard otherwise).
|
||||
*/
|
||||
export async function shareOrDownloadGcg(
|
||||
gcg: GcgExport,
|
||||
inAppWebView: boolean,
|
||||
copyText: (text: string) => Promise<boolean>,
|
||||
): Promise<'shared' | 'copied' | 'downloaded' | 'failed'> {
|
||||
const file = new File([gcg.content], gcg.filename, { type: 'application/x-gcg' });
|
||||
const nav = typeof navigator !== 'undefined' ? navigator : undefined;
|
||||
if (pickGcgDelivery(nav, file) === 'share' && nav) {
|
||||
const decision = pickGcgDelivery(nav, file, inAppWebView);
|
||||
if (decision === 'share' && nav) {
|
||||
// Web Share is available (mobile, including the iOS Telegram Mini App): use it and stop here
|
||||
// whatever the outcome. Do NOT fall back to the Blob download — on iOS WKWebView an
|
||||
// <a download> navigates the webview to the blob: URL, replacing the SPA with the raw file
|
||||
// and stranding the app, so a cancelled or failed share must simply do nothing (the user can
|
||||
// retry). The download path is only for desktop browsers without Web Share.
|
||||
// whatever the outcome. Do NOT fall back to the Blob download — on iOS WKWebView an <a download>
|
||||
// navigates the webview to the blob: URL, replacing the SPA with the raw file and stranding the
|
||||
// app, so a cancelled or failed share must simply do nothing (the user can retry).
|
||||
try {
|
||||
await nav.share({ files: [file], title: gcg.filename });
|
||||
} catch {
|
||||
/* cancelled or failed — intentionally a no-op (see above) */
|
||||
}
|
||||
return;
|
||||
return 'shared';
|
||||
}
|
||||
if (decision === 'copy') {
|
||||
// Android Telegram/VK: no Web Share and a dead <a download>, so copy the GCG text instead of
|
||||
// silently issuing an anchor click that saves nothing.
|
||||
return (await copyText(gcg.content)) ? 'copied' : 'failed';
|
||||
}
|
||||
downloadFile(gcg.content, gcg.filename);
|
||||
return 'downloaded';
|
||||
}
|
||||
|
||||
function downloadFile(content: string, filename: string): void {
|
||||
|
||||
Reference in New Issue
Block a user