first commit
This commit is contained in:
15
packages/capacitor/package.json
Normal file
15
packages/capacitor/package.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "@native-vue-router/capacitor",
|
||||
"version": "0.1.0",
|
||||
"type": "module",
|
||||
"license": "MIT",
|
||||
"files": ["dist"],
|
||||
"scripts": { "build": "vite build --config vite.config.ts && vue-tsc -p tsconfig.json --emitDeclarationOnly" },
|
||||
"exports": { ".": { "types": "./dist/index.d.ts", "import": "./dist/index.js" } },
|
||||
"peerDependencies": {
|
||||
"@capacitor/app": "^8.0.0",
|
||||
"@capacitor/core": "^8.0.0",
|
||||
"@capacitor/haptics": "^8.0.0",
|
||||
"@native-vue-router/core": "^0.1.0"
|
||||
}
|
||||
}
|
||||
45
packages/capacitor/src/index.ts
Normal file
45
packages/capacitor/src/index.ts
Normal file
@@ -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())) }
|
||||
},
|
||||
}
|
||||
}
|
||||
15
packages/capacitor/tsconfig.json
Normal file
15
packages/capacitor/tsconfig.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"extends": "../../tsconfig.app.json",
|
||||
"compilerOptions": {
|
||||
"declaration": true,
|
||||
"declarationMap": true,
|
||||
"emitDeclarationOnly": true,
|
||||
"noEmit": false,
|
||||
"rootDir": "src",
|
||||
"outDir": "dist",
|
||||
"tsBuildInfoFile": "../../node_modules/.tmp/capacitor.tsbuildinfo",
|
||||
"baseUrl": ".",
|
||||
"paths": { "@native-vue-router/core": ["../core/dist/index.d.ts"] }
|
||||
},
|
||||
"include": ["src/**/*.ts"]
|
||||
}
|
||||
10
packages/capacitor/vite.config.ts
Normal file
10
packages/capacitor/vite.config.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { defineConfig } from 'vite'
|
||||
import { resolve } from 'node:path'
|
||||
|
||||
export default defineConfig({
|
||||
build: {
|
||||
outDir: 'dist', emptyOutDir: true,
|
||||
lib: { entry: resolve(__dirname, 'src/index.ts'), formats: ['es'], fileName: 'index' },
|
||||
rollupOptions: { external: ['@capacitor/app', '@capacitor/core', '@capacitor/haptics', '@native-vue-router/core'] },
|
||||
},
|
||||
})
|
||||
Reference in New Issue
Block a user