first commit
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
import { App } from '@capacitor/app'
|
||||
import { Capacitor } from '@capacitor/core'
|
||||
import { Haptics, ImpactStyle } from '@capacitor/haptics'
|
||||
import type { NativePlatformAdapter, NativeRouterRuntime } from '@native-vue-router/core'
|
||||
|
||||
export interface CapacitorAdapterOptions {
|
||||
exitAtRoot?: boolean
|
||||
haptics?: boolean
|
||||
deepLinkPath?: (url: URL) => string
|
||||
}
|
||||
|
||||
export function createCapacitorAdapter(options: CapacitorAdapterOptions = {}): NativePlatformAdapter {
|
||||
const enabled = Capacitor.isNativePlatform()
|
||||
return {
|
||||
name: enabled ? `capacitor-${Capacitor.getPlatform()}` : 'capacitor-web',
|
||||
async haptic(event) {
|
||||
if (!enabled || options.haptics === false) return
|
||||
if (event === 'selection') await Haptics.selectionChanged()
|
||||
else await Haptics.impact({ style: event === 'commit' ? ImpactStyle.Light : ImpactStyle.Medium })
|
||||
},
|
||||
async install(runtime: NativeRouterRuntime) {
|
||||
if (!enabled) return
|
||||
const handles = await Promise.all([
|
||||
App.addListener('backButton', async () => {
|
||||
if (runtime.transaction.value) return await runtime.cancelInteractive()
|
||||
if (runtime.canGoBack.value) return void await runtime.pop()
|
||||
if (options.exitAtRoot !== false) await App.exitApp()
|
||||
}),
|
||||
App.addListener('appUrlOpen', ({ url }) => {
|
||||
const parsed = new URL(url)
|
||||
const path = options.deepLinkPath?.(parsed) ?? `${parsed.pathname}${parsed.search}${parsed.hash}`
|
||||
void runtime.push(path || '/')
|
||||
}),
|
||||
App.addListener('pause', () => void runtime.cancelInteractive()),
|
||||
])
|
||||
const launch = await App.getLaunchUrl()
|
||||
if (launch?.url) {
|
||||
const parsed = new URL(launch.url)
|
||||
const path = options.deepLinkPath?.(parsed) ?? `${parsed.pathname}${parsed.search}${parsed.hash}`
|
||||
if (path) void runtime.replace(path, { presentation: 'none' })
|
||||
}
|
||||
return () => { void Promise.all(handles.map((handle) => handle.remove())) }
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user