-
Notifications
You must be signed in to change notification settings - Fork 22
Add tour infrastructure #966
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
037afbb
add tour infrastructure
srzeszut ef9b1cc
forbid settings change during tour
srzeszut 130cc3a
refactor api
srzeszut 82a3fdd
CR suggestions
srzeszut 16fe437
refactor
srzeszut 110c8f1
add pubsub options to api
srzeszut 92ace2c
fix
srzeszut a996fe5
remove radius
srzeszut 604421a
update assets
srzeszut b0ae664
update tour elements
srzeszut f61f09f
change elements from id to selectors
srzeszut 7e2a6ed
refactor api
srzeszut a32b770
remove outdated doc
srzeszut 216086f
add background
srzeszut 88f7dfa
add id
srzeszut bab11d2
build assets
srzeszut File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,212 @@ | ||
| const OVERLAY_ID = 'tour-overlay'; | ||
| const PENDING_ACTION_KEY = 'lvdbg-tour-pending'; | ||
|
|
||
| const classGuardians = new Set(); | ||
|
|
||
| function clearAll() { | ||
| const overlay = document.getElementById(OVERLAY_ID); | ||
| if (overlay) overlay.remove(); | ||
|
|
||
| classGuardians.forEach((guardian) => guardian.disconnect()); | ||
| classGuardians.clear(); | ||
|
|
||
| document | ||
| .querySelectorAll('.tour-highlight, .tour-spotlight-target') | ||
| .forEach((el) => { | ||
| el.classList.remove('tour-highlight', 'tour-spotlight-target'); | ||
| }); | ||
| } | ||
|
|
||
| function guardClass(target, className) { | ||
| const observer = new MutationObserver(() => { | ||
| if (!target.classList.contains(className)) { | ||
| target.classList.add(className); | ||
| } | ||
| }); | ||
|
|
||
| observer.observe(target, { | ||
| attributes: true, | ||
| attributeFilter: ['class'], | ||
| }); | ||
|
|
||
| classGuardians.add(observer); | ||
| } | ||
|
|
||
| function highlight(target) { | ||
| target.scrollIntoView({ | ||
| behavior: 'smooth', | ||
| block: 'center', | ||
| }); | ||
| target.classList.add('tour-highlight'); | ||
|
|
||
| guardClass(target, 'tour-highlight'); | ||
| } | ||
|
|
||
| function createOverlay() { | ||
| if (!document.getElementById(OVERLAY_ID)) { | ||
| const overlay = document.createElement('div'); | ||
| overlay.id = OVERLAY_ID; | ||
| overlay.className = 'tour-overlay'; | ||
| document.body.appendChild(overlay); | ||
| } | ||
| } | ||
|
|
||
| function spotlight(target) { | ||
| createOverlay(); | ||
|
|
||
| target.scrollIntoView({ | ||
| behavior: 'smooth', | ||
| block: 'center', | ||
| }); | ||
| target.classList.add('tour-spotlight-target'); | ||
|
|
||
| guardClass(target, 'tour-spotlight-target'); | ||
| } | ||
|
|
||
| const Tour = { | ||
| mounted() { | ||
| this._cleanups = new Set(); | ||
| const pending = sessionStorage.getItem(PENDING_ACTION_KEY); | ||
| if (pending) { | ||
| sessionStorage.removeItem(PENDING_ACTION_KEY); | ||
| this._applyAction(JSON.parse(pending)); | ||
| } | ||
|
|
||
| this.handleEvent('tour-action', (payload) => { | ||
| this._applyAction(payload); | ||
| }); | ||
| }, | ||
|
|
||
| _applyAction(payload) { | ||
| const { | ||
| action, | ||
| target: selector, | ||
| dismiss, | ||
| url, | ||
| then: nextAction, | ||
| clear = true, | ||
| } = payload; | ||
|
|
||
| if (clear || action === 'clear') { | ||
| this._cleanupListeners(); | ||
| clearAll(); | ||
| } | ||
|
|
||
| if (action === 'clear') return; | ||
|
|
||
| if (action === 'redirect') { | ||
| if (nextAction) { | ||
| sessionStorage.setItem(PENDING_ACTION_KEY, JSON.stringify(nextAction)); | ||
| } | ||
| this.pushEvent('tour-redirect', { url }); | ||
| return; | ||
| } | ||
|
|
||
| const target = document.querySelector(selector); | ||
| if (!target) { | ||
| this._waitForTarget(selector, () => | ||
| this._applyToTarget(selector, payload) | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| this._applyToTarget(selector, payload); | ||
| }, | ||
|
|
||
| _applyToTarget(selector, payload) { | ||
| const { action, dismiss } = payload; | ||
| const target = document.querySelector(selector); | ||
| if (!target) return; | ||
|
|
||
| switch (action) { | ||
| case 'highlight': | ||
| highlight(target); | ||
| break; | ||
| case 'spotlight': | ||
| spotlight(target); | ||
| break; | ||
| default: | ||
| console.warn(`[Tour] Unknown action: ${action}`); | ||
| return; | ||
| } | ||
|
|
||
| if (dismiss === 'click-anywhere') { | ||
| this._setupClickAnywhereDismiss(); | ||
| } else if (dismiss === 'click-target') { | ||
| this._setupClickTargetDismiss(target, selector); | ||
| } | ||
| }, | ||
|
|
||
| _waitForTarget(selector, callback) { | ||
| const observer = new MutationObserver(() => { | ||
| if (document.querySelector(selector)) { | ||
| observer.disconnect(); | ||
| this._cleanups.delete(cleanup); | ||
| callback(); | ||
| } | ||
| }); | ||
|
|
||
| observer.observe(document.body, { childList: true, subtree: true }); | ||
|
|
||
| const cleanup = () => observer.disconnect(); | ||
| this._cleanups.add(cleanup); | ||
| }, | ||
|
|
||
| _cleanupListeners() { | ||
| this._cleanups.forEach((cleanup) => cleanup()); | ||
| this._cleanups.clear(); | ||
| }, | ||
|
|
||
| _setupClickAnywhereDismiss() { | ||
| const controller = new AbortController(); | ||
|
|
||
| const handler = () => { | ||
| clearAll(); | ||
| this._cleanupListeners(); | ||
| this.pushEvent('step-completed', { target: 'anywhere' }); | ||
| }; | ||
|
|
||
| const cleanup = () => controller.abort(); | ||
| this._cleanups.add(cleanup); | ||
|
|
||
| setTimeout(() => { | ||
| if (!controller.signal.aborted) { | ||
| document.addEventListener('click', handler, { | ||
| once: true, | ||
| signal: controller.signal, | ||
| }); | ||
| } | ||
| }, 0); | ||
| }, | ||
|
|
||
| _setupClickTargetDismiss(target, selector) { | ||
| const overlay = document.getElementById(OVERLAY_ID); | ||
|
|
||
| const handler = () => { | ||
| clearAll(); | ||
| this._cleanupListeners(); | ||
| this.pushEvent('step-completed', { target: selector }); | ||
| }; | ||
|
|
||
| const overlayHandler = (e) => e.stopPropagation(); | ||
| if (overlay) { | ||
| overlay.addEventListener('click', overlayHandler); | ||
| } | ||
|
|
||
| target.addEventListener('click', handler, { once: true }); | ||
|
|
||
| const cleanup = () => { | ||
| target.removeEventListener('click', handler); | ||
| if (overlay) overlay.removeEventListener('click', overlayHandler); | ||
| }; | ||
|
|
||
| this._cleanups.add(cleanup); | ||
| }, | ||
|
|
||
| destroyed() { | ||
| this._cleanupListeners(); | ||
| clearAll(); | ||
| }, | ||
| }; | ||
|
|
||
| export default Tour; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.