-
Notifications
You must be signed in to change notification settings - Fork 5k
feat: update API/SFC preference using shortcuts #1449
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
Closed
edimitchel
wants to merge
27
commits into
vuejs:main
from
vuejs-translations:feat/update-preference-from-keyboard
Closed
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
5f9500a
fix typo on composition api faq
edimitchel da692e2
Merge branch 'vuejs:main' into next
edimitchel d214beb
feat: update API/SFC from keyboard shortcuts
edimitchel dc19a96
fix: update condition
edimitchel 3758364
fix: navigator from window
edimitchel dc38995
fix: globalThis instead of window
edimitchel a613c3e
fix: hide after using shortcut when preference was closed
edimitchel 8b80e24
fix: add shortcut in docs
edimitchel ccee93d
fix: avoid unnecessary toRefs and remove return type
edimitchel 9113684
fix: use keycode instead of key
edimitchel 3c38b7c
feat: separate into composables files
edimitchel b8bc156
fix: use not deprecated api
edimitchel df537b2
fix: export as default composable
edimitchel 1b60c85
fix: use keyCode
edimitchel d8389f7
Merge remote-tracking branch 'upstream/main' into feat/update-prefere…
edimitchel 358626e
fix: bad import default
edimitchel 0908ce8
fix: conflict
edimitchel f5d5385
Merge remote-tracking branch 'upstream/main' into feat/update-prefere…
edimitchel 3860f10
feat: improve opening by keyboard
edimitchel 945a815
fix: use key instead of keyCode (deprecrated
edimitchel c77f053
fix: revert to keyCode...
edimitchel 8640227
Merge remote-tracking branch 'upstream/main' into feat/update-prefere…
95a80f9
Merge branch 'main' into feat/update-preference-from-keyboard
edimitchel c26cbc8
Merge remote-tracking branch 'upstream/main' into feat/update-prefere…
edimitchel 47c7f9e
fix: reuse AugmentedHeader
edimitchel 7e22d00
fix: apply comment
edimitchel 8ff55a7
fix: remove SFC toggle shortcut
edimitchel 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
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 was deleted.
Oops, something went wrong.
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,9 @@ | ||||||
| export default function usePlatform() { | ||||||
| const isMac = /(Mac OS X)/i.test(globalThis.navigator?.userAgent) | ||||||
| const altKey = isMac ? 'Option' : 'Alt'; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| return { | ||||||
| isMac, | ||||||
| altKey | ||||||
| } | ||||||
| } | ||||||
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,115 @@ | ||
| import { AugmentedHeader } from '.vitepress/headerMdPlugin' | ||
| import { useRoute } from 'vitepress' | ||
| import { computed, onMounted, onUnmounted, Ref, ref } from 'vue' | ||
|
|
||
| import usePlatform from './usePlatform' | ||
|
|
||
| const hasStorage = typeof localStorage !== 'undefined' | ||
| const get = (key: string, defaultValue = false): boolean => | ||
| hasStorage | ||
| ? JSON.parse(localStorage.getItem(key) || String(defaultValue)) | ||
| : defaultValue | ||
|
|
||
| const preferCompositionKey = 'vue-docs-prefer-composition' | ||
| export const preferComposition = ref(get(preferCompositionKey)) | ||
|
|
||
| const preferSFCKey = 'vue-docs-prefer-sfc' | ||
| export const preferSFC = ref(get(preferSFCKey, true)) | ||
|
|
||
| export function filterHeadersByPreference(h: AugmentedHeader) { | ||
| return preferComposition.value ? !h.optionsOnly : !h.compositionOnly | ||
| } | ||
|
|
||
| export default function usePreferences() { | ||
| const route = useRoute() | ||
| const showPreference = computed(() => | ||
| /^\/(guide|tutorial|examples)\//.test(route.path) | ||
| ) | ||
| const { altKey } = usePlatform() | ||
|
|
||
| const shortcutInfo = computed(() => `Ctrl+${altKey}+A: toggle API preference`) | ||
|
|
||
| const isOpen = ref( | ||
| typeof localStorage !== 'undefined' && | ||
| !localStorage.getItem(preferCompositionKey) | ||
| ) | ||
|
|
||
| const toggleOpen = () => { | ||
| isOpen.value = !isOpen.value | ||
| } | ||
|
|
||
| const toggleCompositionAPI = useToggleFn( | ||
| preferCompositionKey, | ||
| preferComposition, | ||
| 'prefer-composition' | ||
| ) | ||
| const toggleSFC = useToggleFn(preferSFCKey, preferSFC, 'prefer-sfc') | ||
|
|
||
| function useToggleFn( | ||
| storageKey: string, | ||
| state: Ref<boolean>, | ||
| className: string | ||
| ) { | ||
| if (typeof localStorage === 'undefined') { | ||
| return () => {} | ||
| } | ||
| const classList = document.documentElement.classList | ||
| return (value = !state.value) => { | ||
| if ((state.value = value)) { | ||
| classList.add(className) | ||
| } else { | ||
| classList.remove(className) | ||
| } | ||
| localStorage.setItem(storageKey, String(state.value)) | ||
| } | ||
| } | ||
|
|
||
| let closeTimeout: any | ||
| let hasInitiallyBeenOpenByKeyboard = false | ||
| const onPreferenceKeyupChange = (callback: Function) => { | ||
| clearTimeout(closeTimeout) | ||
| if (!isOpen.value) { | ||
| isOpen.value = true // Open preference to see what is changed. | ||
| hasInitiallyBeenOpenByKeyboard = true | ||
| setTimeout(() => { | ||
| // Defer the toggle a bit to be able to see the change | ||
| callback() | ||
| }, 100) | ||
| } else { | ||
| callback() | ||
| } | ||
| if (hasInitiallyBeenOpenByKeyboard) { | ||
| // Close automatically when it was initially opened by shortcut | ||
| closeTimeout = setTimeout(() => { | ||
| // Close after 5 seconds | ||
| isOpen.value = false | ||
| hasInitiallyBeenOpenByKeyboard = false | ||
| }, 5000) | ||
| } | ||
| } | ||
|
|
||
| const preferenceKeyupHandler = (e: KeyboardEvent) => { | ||
| if (e.altKey && e.ctrlKey && showPreference.value) { | ||
| if (e.keyCode === 65) { | ||
| // Ctrl+Alt+A + preference switch available | ||
| onPreferenceKeyupChange(toggleCompositionAPI) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| onMounted(() => { | ||
| document.addEventListener('keyup', preferenceKeyupHandler) | ||
| }) | ||
| onUnmounted(() => { | ||
| document.removeEventListener('keyup', preferenceKeyupHandler) | ||
| }) | ||
|
|
||
| return { | ||
| isOpen, | ||
| shortcutInfo, | ||
| showPreference, | ||
| toggleOpen, | ||
| toggleCompositionAPI, | ||
| toggleSFC | ||
| } | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that
showSFCis no longer being returned, so the second switch is currently missing on the Tutorial and Examples pages.