|
| 1 | +import { nextTick, reactive, ref } from 'vue' |
| 2 | + |
| 3 | +import { getServiceFactory } from '../services/service.factory.js' |
| 4 | + |
| 5 | +export function useDrawerHelper () { |
| 6 | + const serviceFactory = getServiceFactory() |
| 7 | + const $services = serviceFactory.$serviceInstances |
| 8 | + |
| 9 | + const drawer = reactive({ open: false }) |
| 10 | + |
| 11 | + const isMouseInDrawer = ref(false) |
| 12 | + let teaseCloseTimeout = null |
| 13 | + const isInitialTease = ref(false) |
| 14 | + |
| 15 | + let containerEl = null |
| 16 | + let getInstance = null |
| 17 | + let setEditorWidth = null |
| 18 | + let defaultWidth = null |
| 19 | + |
| 20 | + function bindDrawer ({ |
| 21 | + containerEl: getEl, |
| 22 | + getInstance: getInst, |
| 23 | + setEditorWidth: setWidth, |
| 24 | + defaultWidth: width |
| 25 | + } = {}) { |
| 26 | + containerEl = getEl |
| 27 | + getInstance = getInst |
| 28 | + setEditorWidth = setWidth |
| 29 | + defaultWidth = width |
| 30 | + } |
| 31 | + |
| 32 | + function notifyDrawerState () { |
| 33 | + const instance = getInstance() |
| 34 | + |
| 35 | + if (!containerEl || !instance) return |
| 36 | + |
| 37 | + const iframe = window.frames['immersive-editor-iframe'] |
| 38 | + |
| 39 | + if (iframe) { |
| 40 | + const targetOrigin = instance.url || window.location.origin |
| 41 | + |
| 42 | + $services.messaging.sendMessage({ |
| 43 | + message: { |
| 44 | + type: 'drawer-state', |
| 45 | + payload: { open: drawer.open } |
| 46 | + }, |
| 47 | + target: iframe, |
| 48 | + targetOrigin |
| 49 | + }) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + function toggleDrawer () { |
| 54 | + if (drawer.open) { |
| 55 | + drawer.open = false |
| 56 | + } else { |
| 57 | + drawer.open = true |
| 58 | + if (setEditorWidth && defaultWidth != null) { |
| 59 | + setEditorWidth(defaultWidth) |
| 60 | + } |
| 61 | + } |
| 62 | + nextTick(() => { |
| 63 | + notifyDrawerState() |
| 64 | + }) |
| 65 | + } |
| 66 | + |
| 67 | + function handleDrawerMouseEnter () { |
| 68 | + if (isInitialTease.value) { |
| 69 | + isMouseInDrawer.value = true |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + function handleDrawerMouseLeave () { |
| 74 | + if (isInitialTease.value) { |
| 75 | + isMouseInDrawer.value = false |
| 76 | + if (teaseCloseTimeout) { |
| 77 | + clearTimeout(teaseCloseTimeout) |
| 78 | + teaseCloseTimeout = setTimeout(() => { |
| 79 | + if (!isMouseInDrawer.value && drawer.open) { |
| 80 | + toggleDrawer() |
| 81 | + } |
| 82 | + isInitialTease.value = false |
| 83 | + teaseCloseTimeout = null |
| 84 | + }, 2000) |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + function runInitialTease () { |
| 90 | + setTimeout(() => { |
| 91 | + isInitialTease.value = true |
| 92 | + toggleDrawer() |
| 93 | + teaseCloseTimeout = setTimeout(() => { |
| 94 | + if (!isMouseInDrawer.value) { |
| 95 | + toggleDrawer() |
| 96 | + } |
| 97 | + isInitialTease.value = false |
| 98 | + teaseCloseTimeout = null |
| 99 | + }, 2000) |
| 100 | + }, 1200) |
| 101 | + } |
| 102 | + |
| 103 | + function cleanup () { |
| 104 | + if (teaseCloseTimeout) { |
| 105 | + clearTimeout(teaseCloseTimeout) |
| 106 | + teaseCloseTimeout = null |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + return { |
| 111 | + drawer, |
| 112 | + toggleDrawer, |
| 113 | + notifyDrawerState, |
| 114 | + handleDrawerMouseEnter, |
| 115 | + handleDrawerMouseLeave, |
| 116 | + runInitialTease, |
| 117 | + bindDrawer, |
| 118 | + cleanup |
| 119 | + } |
| 120 | +} |
0 commit comments