diff --git a/src/renderer/src/directive/drag.js b/src/renderer/src/directive/drag.js new file mode 100644 index 0000000..9eddc29 --- /dev/null +++ b/src/renderer/src/directive/drag.js @@ -0,0 +1,81 @@ +/** + * @description: v-drag + * @author zdg + * @date 2023-06-07 + */ +// 工具包 +const utils = { + // Creates an event handler that can be used in Vue code + // setup start moving end + vueDragEvent: (el, action) => { + el.dispatchEvent(new Event(`drag-${action}`)) + }, + dragStart: (el, target, axis, snap, e) => { + // el.style.cursor = 'move' + // el.onmousedown = function (e) { + // const disX = e.clientX - el.offsetLeft + // const disY = e.clientY - el.offsetTop + // document.onmousemove = function (e) { + // const left = e.clientX - disX + // const top= e.clientY - disY + // } + // } + }, +} +// 首次向元素添加可拖动配置 | Add draggable configuration to element for the first time +const mountedHook = (el, binding) => { + console.log(el, binding) + const value = binding.value || {} + const handleSelector = value instanceof Object ? value.el : value // 获取元素 + const isOpen = value instanceof Object ? value.open || true : true // 是否开启拖拽 默认:开启 + const handleArray = [] // 拖拽元素 + if (!isOpen) return false // 没有开启不加载后面的代码 + let axis + // Store all the DOM elements that will be used as handles. + // They can be declared using a string with a CSS tag, class or id, or using Vue refs. + if (!!handleSelector) { + if (handleSelector instanceof HTMLElement) { + handleArray.push(handleSelector); + } else { + // handleArray.push(document.querySelectorAll(handleSelector)); + document.querySelectorAll(handleSelector).forEach((child) => { + handleArray.push(child); + }); + } + } + if (handleArray.length !== 0) { + // Define move element and apply CSS class + // el.classList.add(window.data.class.usesHandle); + + handleArray.forEach((grabElement) => { + // Apply CSS class to each grab element + // grabElement.classList.add(window.data.class.handle); + + // Add events to start drag with handle + grabElement.onmousedown = (e) => utils.dragStart(grabElement, el, axis, e); + grabElement.ontouchstart = (e) => utils.dragStart(grabElement, el, axis, e); + }); + } else { + // Add events to start drag without handle + el.onmousedown = (e) => utils.dragStart(el, el, axis, e); + el.ontouchstart = (e) => utils.dragStart(el, el, axis, e); + } + // Vue event on setup + utils.vueDragEvent(el, 'setup') +} +export default { + // Hooks for Vue3 + mounted(el, binding) { + mountedHook(el, binding) + }, + // Hooks for Vue2 + inserted(el, binding) { + mountedHook(el, binding) + }, + + update(el, binding){ + }, + updated(el, binding){ + + }, +} \ No newline at end of file diff --git a/src/renderer/src/utils/tool.js b/src/renderer/src/utils/tool.js index e87c0d0..a0dd8d4 100644 --- a/src/renderer/src/utils/tool.js +++ b/src/renderer/src/utils/tool.js @@ -175,9 +175,9 @@ const eventHandles = (type, win) => { const setIgnore = (_, ignore) => {win.setIgnoreMouseEvents(ignore, {forward: true})} Remote.ipcMain.on('tool-sphere:set:ignore', setIgnore) // 关闭窗口 - Remote.ipcMain.once('tool-sphere:close', () => { win.destroy() }) + Remote.ipcMain.once('tool-sphere:close', () => { win&&win.destroy() }) // 放大监听-测试 - Remote.ipcMain.once('maximize-window', () => {win.destroy()}) + Remote.ipcMain.once('maximize-window', () => {win&&win.destroy()}) const on = { onClosed: () => {Remote.ipcMain.off('tool-sphere:set:ignore', setIgnore)} } @@ -185,8 +185,7 @@ const eventHandles = (type, win) => { break} case 'open-PDF': { // 最小化窗口 minimize() - Remote.ipcMain.once('open-PDF:minimize', () => {win.destroy()}) - win.webContents.openDevTools() + Remote.ipcMain.once('open-PDF:minimize', () => {win&&win.destroy()}) publicMethods() // 加载公共方法 break} default: diff --git a/src/renderer/src/views/tool/sphere.vue b/src/renderer/src/views/tool/sphere.vue index 6c9ad30..8f84c87 100644 --- a/src/renderer/src/views/tool/sphere.vue +++ b/src/renderer/src/views/tool/sphere.vue @@ -2,11 +2,16 @@