Skip to content

Commit

Permalink
feat: add touch emulater
Browse files Browse the repository at this point in the history
  • Loading branch information
BeADre committed Dec 24, 2020
1 parent 2858e3e commit 6836953
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 8 deletions.
11 changes: 4 additions & 7 deletions packages/varlet-cli/site/mobile/main.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { createApp } from 'vue'
import { createRouter, createWebHashHistory } from 'vue-router'
import '@varlet/touch-emulator'
import App from './App.vue'
import AppType from './components/AppType.vue'

import routes from './routes'

const router = createRouter({
history: createWebHashHistory(),
routes
history: createWebHashHistory(),
routes,
})

const app = createApp(App as any)
app
.component(AppType.name, AppType)
.use(router)
.mount('#app')

app.component(AppType.name, AppType).use(router).mount('#app')
1 change: 0 additions & 1 deletion packages/varlet-cli/site/mobile/mobile.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<meta content="<%= htmlWebpackPlugin.options.description %>" name="description" />
<link href="<%= htmlWebpackPlugin.options.logo %>" rel="icon" type="image/png" />
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, viewport-fit=cover" name="viewport" />
<script src="https://cdn.jsdelivr.net/npm/@vant/touch-emulator"></script>
</head>
<body>
<div id="app"></div>
Expand Down
Empty file.
78 changes: 78 additions & 0 deletions packages/varlet-touch-emulator/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const supportTouch = 'ontouchstart' in window
let initiated = false
let eventTarget: EventTarget | null

const isMousedown = (eventType: string): boolean => eventType === 'mousedown'

const isMousemove = (eventType: string): boolean => eventType === 'mousemove'

const isMouseup = (eventType: string): boolean => eventType === 'mouseup'

const isUpdateTarget = (eventType: string): boolean =>
isMousedown(eventType) || !eventTarget || (eventTarget && !eventTarget.dispatchEvent)

function updateTouchList(mouseEvent: MouseEvent): Touch[] {
const { clientX, clientY, screenX, screenY, pageX, pageY } = mouseEvent
const touchList: Touch[] = []
const touchInitDict: TouchInit = {
identifier: 1,
target: eventTarget as EventTarget,
clientX,
clientY,
screenX,
screenY,
pageX,
pageY,
}
touchList.push(new Touch(touchInitDict))
return touchList
}

function getActiveTouches(mouseEvent: MouseEvent): Touch[] {
const { type } = mouseEvent
const touchList: Touch[] = []
if (isMouseup(type)) return touchList
return updateTouchList(mouseEvent)
}

function triggerTouch(touchType: string, mouseEvent: MouseEvent) {
const { altKey, ctrlKey, metaKey, shiftKey } = mouseEvent

const eventInitDict: TouchEventInit = {
touches: getActiveTouches(mouseEvent),
targetTouches: getActiveTouches(mouseEvent),
changedTouches: updateTouchList(mouseEvent),
altKey,
ctrlKey,
metaKey,
shiftKey,
bubbles: true,
cancelable: true,
}

const touchEvent = new TouchEvent(touchType, eventInitDict)

;(eventTarget as EventTarget).dispatchEvent(touchEvent)
}

function onMouse(mouseEvent: MouseEvent, touchType: string) {
const { type, target } = mouseEvent

initiated = isMousedown(type) ? true : isMouseup(type) ? false : initiated

if (isMousemove(type) && !initiated) return

if (isUpdateTarget(type)) eventTarget = target

triggerTouch(touchType, mouseEvent)

if (isMouseup(type)) eventTarget = null
}

function createTouchEmulator() {
window.addEventListener('mousedown', (event) => onMouse(event, 'touchstart'), true)
window.addEventListener('mousemove', (event) => onMouse(event, 'touchmove'), true)
window.addEventListener('mouseup', (event) => onMouse(event, 'touchend'), true)
}

if (!supportTouch) createTouchEmulator()
21 changes: 21 additions & 0 deletions packages/varlet-touch-emulator/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "@varlet/touch-emulator",
"version": "0.5.1",
"description": "touch-emulator",
"keywords": [
"emulator",
"varlet"
],
"author": "BeADre <[email protected]>",
"homepage": "https://github.com/haoziqaq/varlet#readme",
"license": "MIT",
"main": "index.ts",
"repository": {
"type": "git",
"url": "git+https://github.com/haoziqaq/varlet.git"
},
"bugs": {
"url": "https://github.com/haoziqaq/varlet/issues"
},
"gitHead": "5dabda353dc88340816ddc6f6ac8081c0680908f"
}

0 comments on commit 6836953

Please sign in to comment.