|
| 1 | +const OVERLAY_ID = 'tour-overlay'; |
| 2 | +const PENDING_ACTION_KEY = 'lvdbg-tour-pending'; |
| 3 | + |
| 4 | +const classGuardians = new Set(); |
| 5 | + |
| 6 | +function clearAll() { |
| 7 | + const overlay = document.getElementById(OVERLAY_ID); |
| 8 | + if (overlay) overlay.remove(); |
| 9 | + |
| 10 | + classGuardians.forEach((guardian) => guardian.disconnect()); |
| 11 | + classGuardians.clear(); |
| 12 | + |
| 13 | + document |
| 14 | + .querySelectorAll('.tour-highlight, .tour-spotlight-target') |
| 15 | + .forEach((el) => { |
| 16 | + el.classList.remove('tour-highlight', 'tour-spotlight-target'); |
| 17 | + }); |
| 18 | +} |
| 19 | + |
| 20 | +function guardClass(target, className) { |
| 21 | + const observer = new MutationObserver(() => { |
| 22 | + if (!target.classList.contains(className)) { |
| 23 | + target.classList.add(className); |
| 24 | + } |
| 25 | + }); |
| 26 | + |
| 27 | + observer.observe(target, { |
| 28 | + attributes: true, |
| 29 | + attributeFilter: ['class'], |
| 30 | + }); |
| 31 | + |
| 32 | + classGuardians.add(observer); |
| 33 | +} |
| 34 | + |
| 35 | +function highlight(target) { |
| 36 | + target.scrollIntoView({ |
| 37 | + behavior: 'smooth', |
| 38 | + block: 'center', |
| 39 | + }); |
| 40 | + target.classList.add('tour-highlight'); |
| 41 | + |
| 42 | + guardClass(target, 'tour-highlight'); |
| 43 | +} |
| 44 | + |
| 45 | +function createOverlay() { |
| 46 | + if (!document.getElementById(OVERLAY_ID)) { |
| 47 | + const overlay = document.createElement('div'); |
| 48 | + overlay.id = OVERLAY_ID; |
| 49 | + overlay.className = 'tour-overlay'; |
| 50 | + document.body.appendChild(overlay); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +function spotlight(target) { |
| 55 | + createOverlay(); |
| 56 | + |
| 57 | + target.scrollIntoView({ |
| 58 | + behavior: 'smooth', |
| 59 | + block: 'center', |
| 60 | + }); |
| 61 | + target.classList.add('tour-spotlight-target'); |
| 62 | + |
| 63 | + guardClass(target, 'tour-spotlight-target'); |
| 64 | +} |
| 65 | + |
| 66 | +const Tour = { |
| 67 | + mounted() { |
| 68 | + this._cleanups = new Set(); |
| 69 | + const pending = sessionStorage.getItem(PENDING_ACTION_KEY); |
| 70 | + if (pending) { |
| 71 | + sessionStorage.removeItem(PENDING_ACTION_KEY); |
| 72 | + this._applyAction(JSON.parse(pending)); |
| 73 | + } |
| 74 | + |
| 75 | + this.handleEvent('tour-action', (payload) => { |
| 76 | + this._applyAction(payload); |
| 77 | + }); |
| 78 | + }, |
| 79 | + |
| 80 | + _applyAction(payload) { |
| 81 | + const { |
| 82 | + action, |
| 83 | + target: selector, |
| 84 | + dismiss, |
| 85 | + url, |
| 86 | + then: nextAction, |
| 87 | + clear = true, |
| 88 | + } = payload; |
| 89 | + |
| 90 | + if (clear || action === 'clear') { |
| 91 | + this._cleanupListeners(); |
| 92 | + clearAll(); |
| 93 | + } |
| 94 | + |
| 95 | + if (action === 'clear') return; |
| 96 | + |
| 97 | + if (action === 'redirect') { |
| 98 | + if (nextAction) { |
| 99 | + sessionStorage.setItem(PENDING_ACTION_KEY, JSON.stringify(nextAction)); |
| 100 | + } |
| 101 | + this.pushEvent('tour-redirect', { url }); |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + const target = document.querySelector(selector); |
| 106 | + if (!target) { |
| 107 | + this._waitForTarget(selector, () => |
| 108 | + this._applyToTarget(selector, payload) |
| 109 | + ); |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + this._applyToTarget(selector, payload); |
| 114 | + }, |
| 115 | + |
| 116 | + _applyToTarget(selector, payload) { |
| 117 | + const { action, dismiss } = payload; |
| 118 | + const target = document.querySelector(selector); |
| 119 | + if (!target) return; |
| 120 | + |
| 121 | + switch (action) { |
| 122 | + case 'highlight': |
| 123 | + highlight(target); |
| 124 | + break; |
| 125 | + case 'spotlight': |
| 126 | + spotlight(target); |
| 127 | + break; |
| 128 | + default: |
| 129 | + console.warn(`[Tour] Unknown action: ${action}`); |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + if (dismiss === 'click-anywhere') { |
| 134 | + this._setupClickAnywhereDismiss(); |
| 135 | + } else if (dismiss === 'click-target') { |
| 136 | + this._setupClickTargetDismiss(target, selector); |
| 137 | + } |
| 138 | + }, |
| 139 | + |
| 140 | + _waitForTarget(selector, callback) { |
| 141 | + const observer = new MutationObserver(() => { |
| 142 | + if (document.querySelector(selector)) { |
| 143 | + observer.disconnect(); |
| 144 | + this._cleanups.delete(cleanup); |
| 145 | + callback(); |
| 146 | + } |
| 147 | + }); |
| 148 | + |
| 149 | + observer.observe(document.body, { childList: true, subtree: true }); |
| 150 | + |
| 151 | + const cleanup = () => observer.disconnect(); |
| 152 | + this._cleanups.add(cleanup); |
| 153 | + }, |
| 154 | + |
| 155 | + _cleanupListeners() { |
| 156 | + this._cleanups.forEach((cleanup) => cleanup()); |
| 157 | + this._cleanups.clear(); |
| 158 | + }, |
| 159 | + |
| 160 | + _setupClickAnywhereDismiss() { |
| 161 | + const controller = new AbortController(); |
| 162 | + |
| 163 | + const handler = () => { |
| 164 | + clearAll(); |
| 165 | + this._cleanupListeners(); |
| 166 | + this.pushEvent('step-completed', { target: 'anywhere' }); |
| 167 | + }; |
| 168 | + |
| 169 | + const cleanup = () => controller.abort(); |
| 170 | + this._cleanups.add(cleanup); |
| 171 | + |
| 172 | + setTimeout(() => { |
| 173 | + if (!controller.signal.aborted) { |
| 174 | + document.addEventListener('click', handler, { |
| 175 | + once: true, |
| 176 | + signal: controller.signal, |
| 177 | + }); |
| 178 | + } |
| 179 | + }, 0); |
| 180 | + }, |
| 181 | + |
| 182 | + _setupClickTargetDismiss(target, selector) { |
| 183 | + const overlay = document.getElementById(OVERLAY_ID); |
| 184 | + |
| 185 | + const handler = () => { |
| 186 | + clearAll(); |
| 187 | + this._cleanupListeners(); |
| 188 | + this.pushEvent('step-completed', { target: selector }); |
| 189 | + }; |
| 190 | + |
| 191 | + const overlayHandler = (e) => e.stopPropagation(); |
| 192 | + if (overlay) { |
| 193 | + overlay.addEventListener('click', overlayHandler); |
| 194 | + } |
| 195 | + |
| 196 | + target.addEventListener('click', handler, { once: true }); |
| 197 | + |
| 198 | + const cleanup = () => { |
| 199 | + target.removeEventListener('click', handler); |
| 200 | + if (overlay) overlay.removeEventListener('click', overlayHandler); |
| 201 | + }; |
| 202 | + |
| 203 | + this._cleanups.add(cleanup); |
| 204 | + }, |
| 205 | + |
| 206 | + destroyed() { |
| 207 | + this._cleanupListeners(); |
| 208 | + clearAll(); |
| 209 | + }, |
| 210 | +}; |
| 211 | + |
| 212 | +export default Tour; |
0 commit comments