mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2026-12-06 00:37:23 +00:00
Provides the cross-platform primitives needed to wrap the mkstack web app as a native iOS and Android binary using Capacitor, modeled on how Ditto integrates the framework. Each helper works unconditionally on every platform \u2014 native builds use the real Capacitor plugin, web falls back to an equivalent browser API: - haptics: impactLight/Medium/Heavy, notificationSuccess/Warning/Error, selectionChanged \u2014 taptic engine on native, navigator.vibrate() on web, silent no-op where neither exists - downloadTextFile / openUrl \u2014 @capacitor/filesystem and native share sheet on native, <a download> and window.open on web - secureStorage + useSecureLocalStorage \u2014 iOS Keychain / Android KeyStore adapter with automatic migration of existing plaintext values from localStorage on first native read, plus an async-aware React hook that exposes a 'ready' flag - DeepLinkHandler \u2014 listens for Capacitor appUrlOpen and forwards into React Router navigation - bootstrapNative() \u2014 hides the iOS keyboard accessory bar and keeps the system-bar icon style in sync with the active CSS theme via MutationObserver on both the <html> class and the theme-vars style element; detects dark background from theme class or computed body luminance - safe-area CSS utilities \u2014 .safe-area-top/-bottom/-left/-right padding classes plus .safe-area-inset-top/-bottom positional variants, preferring --safe-area-inset-* CSS vars (injected by the SystemBars plugin on Android) over env(safe-area-inset-*) so Chromium <140 on Android Just Works - capacitor.config.ts template \u2014 preconfigured with androidScheme https, ios contentInset never, SystemBars insetsHandling css; placeholder appId / appName / scheme ready to be customized - patch-cap-config.mjs \u2014 post-cap-sync hook that restores packageClassList entries for local (non-SPM) native plugin classes so they get registered at startup SKILL.md documents the full setup sequence: install the nine @capacitor/* + capacitor-secure-storage-plugin packages, copy files into src/, paste the CSS snippet into index.css, edit capacitor.config.ts identity fields, add viewport-fit=cover, wire bootstrapNative() into main.tsx and DeepLinkHandler into AppRouter, npx cap add android / ios, build, sync, run. Also covers common follow-ups (custom plugins, icons, splash, push, deep-link verification, credential manager integration) and troubleshooting (safe-area zero on Android, navigation no-op, keychain orphaning, keyboard accessory bar timing, custom theme detection). Validated: all files staged into src/ + deps installed \u2014 npm run test passes with only the 6 pre-existing baseline errors (template FIXMEs + missing index.html manifest/meta), then cleanly reverted.
61 lines
2.1 KiB
TypeScript
61 lines
2.1 KiB
TypeScript
import { Capacitor } from '@capacitor/core';
|
|
|
|
/**
|
|
* Download a text file to the user's device.
|
|
*
|
|
* On the web this uses the classic `<a download>` trick.
|
|
* On native (Android & iOS) the file is saved to the app's Documents
|
|
* directory, which is visible in the iOS Files app and Android's
|
|
* app-scoped documents. No permissions are required.
|
|
*
|
|
* @example
|
|
* await downloadTextFile('backup.json', JSON.stringify(data));
|
|
*/
|
|
export async function downloadTextFile(filename: string, content: string): Promise<void> {
|
|
if (Capacitor.isNativePlatform()) {
|
|
const { Filesystem, Directory, Encoding } = await import('@capacitor/filesystem');
|
|
|
|
// Write straight to Documents — visible in the iOS Files app and
|
|
// Android's app-scoped documents. No storage permissions needed.
|
|
// NOTE: encoding is required — without it Capacitor expects base64 data
|
|
// and will throw for plain-text strings.
|
|
await Filesystem.writeFile({
|
|
path: filename,
|
|
data: content,
|
|
directory: Directory.Documents,
|
|
encoding: Encoding.UTF8,
|
|
});
|
|
} else {
|
|
// Web: use the anchor-click download pattern
|
|
const blob = new Blob([content], { type: 'text/plain; charset=utf-8' });
|
|
const url = globalThis.URL.createObjectURL(blob);
|
|
const a = document.createElement('a');
|
|
a.href = url;
|
|
a.download = filename;
|
|
a.style.display = 'none';
|
|
document.body.appendChild(a);
|
|
a.click();
|
|
globalThis.URL.revokeObjectURL(url);
|
|
document.body.removeChild(a);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Open a URL in a new browser tab, or present the native share sheet on Capacitor.
|
|
*
|
|
* The programmatic `<a target="_blank">` click pattern doesn't work inside
|
|
* WKWebView on iOS. On native platforms this presents the share sheet instead,
|
|
* letting the user open, save, or share the resource.
|
|
*
|
|
* @example
|
|
* <Button onClick={() => openUrl('https://example.com')}>Visit site</Button>
|
|
*/
|
|
export async function openUrl(url: string): Promise<void> {
|
|
if (Capacitor.isNativePlatform()) {
|
|
const { Share } = await import('@capacitor/share');
|
|
await Share.share({ url });
|
|
} else {
|
|
window.open(url, '_blank', 'noopener,noreferrer');
|
|
}
|
|
}
|