统一长按/右键指令

This commit is contained in:
kuaifan 2022-06-02 23:21:48 +08:00
parent e6aa7de922
commit e15f7f6377
5 changed files with 55 additions and 37 deletions

View File

@ -1,10 +1,7 @@
const isSupportTouch = "ontouchend" in document; const isSupportTouch = "ontouchend" in document;
// 长按指令 // 长按或右键指令
const longpress = { const longpress = {
bind: function (el, binding) { bind: function (el, binding) {
if (!isSupportTouch) {
return
}
let delay = 500, let delay = 500,
callback = binding.value; callback = binding.value;
if ($A.isJson(binding.value)) { if ($A.isJson(binding.value)) {
@ -15,6 +12,17 @@ const longpress = {
throw 'callback must be a function' throw 'callback must be a function'
} }
// 不支持touch时使用右键
if (!isSupportTouch) {
el.__longpressContextmenu__ = (e) => {
e.preventDefault()
e.stopPropagation()
callback(e, el)
}
el.addEventListener('contextmenu', el.__longpressContextmenu__);
return
}
// 定义变量 // 定义变量
let pressTimer = null let pressTimer = null
let isCall = false let isCall = false
@ -27,7 +35,7 @@ const longpress = {
if (pressTimer === null) { if (pressTimer === null) {
pressTimer = setTimeout(() => { pressTimer = setTimeout(() => {
isCall = true isCall = true
callback(e, el) callback(e.touches[0], el)
}, delay) }, delay)
} }
} }
@ -50,20 +58,25 @@ const longpress = {
el.addEventListener('touchstart', el.__longpressStart__) el.addEventListener('touchstart', el.__longpressStart__)
// 取消计时器 // 取消计时器
el.addEventListener('click', el.__longpressClick__) el.addEventListener('click', el.__longpressClick__)
el.addEventListener('touchmove', el.__longpressCancel__); el.addEventListener('touchmove', el.__longpressCancel__)
el.addEventListener('touchend', el.__longpressCancel__) el.addEventListener('touchend', el.__longpressCancel__)
el.addEventListener('touchcancel', el.__longpressCancel__) el.addEventListener('touchcancel', el.__longpressCancel__)
}, },
// 指令与元素解绑的时候,移除事件绑定 // 指令与元素解绑的时候,移除事件绑定
unbind(el) { unbind(el) {
if (!isSupportTouch) {
el.removeEventListener('contextmenu', el.__longpressContextmenu__)
delete el.__longpressContextmenu__
return
}
el.removeEventListener('touchstart', el.__longpressStart__) el.removeEventListener('touchstart', el.__longpressStart__)
el.removeEventListener('click', el.__longpressClick__) el.removeEventListener('click', el.__longpressClick__)
el.removeEventListener('touchmove', el.__longpressCancel__) el.removeEventListener('touchmove', el.__longpressCancel__)
el.removeEventListener('touchend', el.__longpressCancel__) el.removeEventListener('touchend', el.__longpressCancel__)
el.removeEventListener('touchcancel', el.__longpressCancel__) el.removeEventListener('touchcancel', el.__longpressCancel__)
delete el.__longpressStart__; delete el.__longpressStart__
delete el.__longpressClick__; delete el.__longpressClick__
delete el.__longpressCancel__; delete el.__longpressCancel__
} }
} }

View File

@ -163,8 +163,9 @@
:ref="`project_${item.id}`" :ref="`project_${item.id}`"
:key="key" :key="key"
:class="classNameProject(item)" :class="classNameProject(item)"
:data-id="item.id"
@click="toggleRoute('project', {projectId: item.id})" @click="toggleRoute('project', {projectId: item.id})"
@contextmenu.prevent.stop="handleContextmenu($event, item)"> v-longpress="handleLongpress">
<div class="project-h1"> <div class="project-h1">
<em @click.stop="toggleOpenMenu(item.id)"></em> <em @click.stop="toggleOpenMenu(item.id)"></em>
<div class="title">{{item.name}}</div> <div class="title">{{item.name}}</div>
@ -382,6 +383,7 @@ import MobileBack from "../components/Mobile/Back";
import TaskMenu from "./manage/components/TaskMenu"; import TaskMenu from "./manage/components/TaskMenu";
import MobileNotification from "../components/Mobile/Notification"; import MobileNotification from "../components/Mobile/Notification";
import MeetingManager from "./manage/components/MeetingManager"; import MeetingManager from "./manage/components/MeetingManager";
import longpress from "../directives/longpress";
export default { export default {
components: { components: {
@ -399,6 +401,7 @@ export default {
ProjectManagement, ProjectManagement,
TeamManagement, TeamManagement,
ProjectArchived}, ProjectArchived},
directives: {longpress},
data() { data() {
return { return {
loadIng: 0, loadIng: 0,
@ -995,16 +998,21 @@ export default {
}, typeof timeout === "number" ? timeout : 1000) }, typeof timeout === "number" ? timeout : 1000)
}, },
handleContextmenu(event, item) { handleLongpress(event, el) {
const projectId = $A.getAttr(el, 'data-id')
const projectItem = this.projectLists.find(item => item.id == projectId)
if (!projectItem) {
return
}
this.operateVisible = false; this.operateVisible = false;
this.operateItem = $A.isJson(item) ? item : {}; this.operateItem = $A.isJson(projectItem) ? projectItem : {};
this.$nextTick(() => { this.$nextTick(() => {
const dialogRect = this.$refs[`project_${item.id}`][0].getBoundingClientRect(); const projectRect = el.getBoundingClientRect();
const wrapRect = this.$refs.projectWrapper.getBoundingClientRect(); const wrapRect = this.$refs.projectWrapper.getBoundingClientRect();
this.operateStyles = { this.operateStyles = {
left: `${event.clientX - wrapRect.left}px`, left: `${event.clientX - wrapRect.left}px`,
top: `${dialogRect.top}px`, top: `${projectRect.top}px`,
height: dialogRect.height + 'px', height: projectRect.height + 'px',
} }
this.operateVisible = true; this.operateVisible = true;
}) })

View File

@ -171,13 +171,12 @@ export default {
}); });
}, },
handleLongpress(touchEvent, el) { handleLongpress(event, el) {
const projectId = $A.getAttr(el, 'data-id') const projectId = $A.getAttr(el, 'data-id')
const projectItem = this.projectLists.find(item => item.id == projectId) const projectItem = this.projectLists.find(item => item.id == projectId)
if (!projectItem) { if (!projectItem) {
return return
} }
const event = touchEvent.touches[0];
this.operateVisible = false; this.operateVisible = false;
this.operateItem = $A.isJson(projectItem) ? projectItem : {}; this.operateItem = $A.isJson(projectItem) ? projectItem : {};
this.$nextTick(() => { this.$nextTick(() => {

View File

@ -80,7 +80,8 @@
shear: shearIds.includes(item.id), shear: shearIds.includes(item.id),
highlight: selectIds.includes(item.id), highlight: selectIds.includes(item.id),
}" }"
@contextmenu.prevent.stop="handleRightClick($event, item)" :data-id="item.id"
v-longpress="handleLongpress"
@click="dropFile(item, 'openCheckMenu')"> @click="dropFile(item, 'openCheckMenu')">
<div class="file-check" :class="{'file-checked':selectIds.includes(item.id)}" @click.stop="dropFile(item, 'select')"> <div class="file-check" :class="{'file-checked':selectIds.includes(item.id)}" @click.stop="dropFile(item, 'select')">
<Checkbox :value="selectIds.includes(item.id)"/> <Checkbox :value="selectIds.includes(item.id)"/>
@ -369,12 +370,14 @@ import {sortBy} from "lodash";
import UserInput from "../../components/UserInput"; import UserInput from "../../components/UserInput";
import DrawerOverlay from "../../components/DrawerOverlay"; import DrawerOverlay from "../../components/DrawerOverlay";
import PreviewImage from "../../components/PreviewImage"; import PreviewImage from "../../components/PreviewImage";
import longpress from "../../directives/longpress";
const FilePreview = () => import('./components/FilePreview'); const FilePreview = () => import('./components/FilePreview');
const FileContent = () => import('./components/FileContent'); const FileContent = () => import('./components/FileContent');
export default { export default {
components: {PreviewImage, FilePreview, DrawerOverlay, UserInput, FileContent}, components: {PreviewImage, FilePreview, DrawerOverlay, UserInput, FileContent},
directives: {longpress},
data() { data() {
return { return {
loadIng: 0, loadIng: 0,
@ -894,6 +897,15 @@ export default {
this.autoBlur(id) this.autoBlur(id)
}, },
handleLongpress(event, el) {
const fileId = $A.getAttr(el, 'data-id')
const fileItem = this.fileList.find(item => item.id == fileId)
if (!fileItem) {
return
}
this.handleRightClick(event, fileItem)
},
handleRightClick(event, item, isAddButton) { handleRightClick(event, item, isAddButton) {
this.contextMenuItem = $A.isJson(item) ? item : {}; this.contextMenuItem = $A.isJson(item) ? item : {};
if (this.contextMenuVisible) { if (this.contextMenuVisible) {

View File

@ -50,8 +50,7 @@
}" }"
:data-id="dialog.id" :data-id="dialog.id"
@click="openDialog(dialog.id)" @click="openDialog(dialog.id)"
v-longpress="handleLongpress" v-longpress="handleLongpress">
@contextmenu.prevent.stop="handleContextmenu($event, dialog)">
<template v-if="dialog.type=='group'"> <template v-if="dialog.type=='group'">
<i v-if="dialog.group_type=='project'" class="taskfont icon-avatar project">&#xe6f9;</i> <i v-if="dialog.group_type=='project'" class="taskfont icon-avatar project">&#xe6f9;</i>
<i v-else-if="dialog.group_type=='task'" class="taskfont icon-avatar task">&#xe6f4;</i> <i v-else-if="dialog.group_type=='task'" class="taskfont icon-avatar task">&#xe6f4;</i>
@ -542,29 +541,16 @@ export default {
}) })
}, },
handleLongpress(touchEvent, el) { handleLongpress(event, el) {
if (this.$isDesktop) {
return
}
const dialogId = $A.getAttr(el, 'data-id') const dialogId = $A.getAttr(el, 'data-id')
const dialogItem = this.dialogList.find(item => item.id == dialogId) const dialogItem = this.dialogList.find(item => item.id == dialogId)
if (dialogItem) { if (!dialogItem) {
this.handleOperateShow(touchEvent.touches[0], dialogItem)
}
},
handleContextmenu(event, dialog) {
if (!this.$isDesktop) {
return return
} }
this.handleOperateShow(event, dialog);
},
handleOperateShow(event, dialog) {
this.operateVisible = false; this.operateVisible = false;
this.operateItem = $A.isJson(dialog) ? dialog : {}; this.operateItem = $A.isJson(dialogItem) ? dialogItem : {};
this.$nextTick(() => { this.$nextTick(() => {
const dialogRect = this.$refs[`dialog_${dialog.id}`][0].getBoundingClientRect(); const dialogRect = el.getBoundingClientRect();
const wrapRect = this.$refs.dialogWrapper.getBoundingClientRect(); const wrapRect = this.$refs.dialogWrapper.getBoundingClientRect();
this.operateStyles = { this.operateStyles = {
left: `${event.clientX - wrapRect.left}px`, left: `${event.clientX - wrapRect.left}px`,