support native haptics

This commit is contained in:
SrGooglo 2023-06-22 19:42:34 +00:00
parent 5353780c67
commit 4634691b31

View File

@ -1,4 +1,5 @@
import Core from "evite/src/core" import Core from "evite/src/core"
import { Haptics } from "@capacitor/haptics"
const vibrationPatterns = { const vibrationPatterns = {
light: [10], light: [10],
@ -19,6 +20,10 @@ export default class HapticsCore extends Core {
} }
async onInitialize() { async onInitialize() {
if (window.navigator.userAgent === "capacitor") {
navigator.vibrate = this.nativeVibrate
}
document.addEventListener("click", this.handleClickEvent) document.addEventListener("click", this.handleClickEvent)
} }
@ -27,6 +32,18 @@ export default class HapticsCore extends Core {
vibrate: this.vibrate.bind(this), vibrate: this.vibrate.bind(this),
} }
nativeVibrate = (pattern) => {
if (!Array.isArray(pattern)) {
pattern = [pattern]
}
for (let i = 0; i < pattern.length; i++) {
Haptics.vibrate({
duration: pattern[i],
})
}
}
handleClickEvent = (event) => { handleClickEvent = (event) => {
const button = event.target.closest("button") || event.target.closest(".ant-btn") const button = event.target.closest("button") || event.target.closest(".ant-btn")