-
Notifications
You must be signed in to change notification settings - Fork 224
Create a slight delay in on shortcut keys so you can't spam theme in theme dev #6711
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@shopify/theme': patch | ||
| --- | ||
|
|
||
| Create a slight delay when keypressing theme dev shortcut keys to stop accidental copy pasting and opening up a ton of tabs |
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 |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ import {Theme} from '@shopify/cli-kit/node/themes/types' | |
| import {checkPortAvailability, getAvailableTCPPort} from '@shopify/cli-kit/node/tcp' | ||
| import {AbortError} from '@shopify/cli-kit/node/error' | ||
| import {openURL} from '@shopify/cli-kit/node/system' | ||
| import {debounce} from '@shopify/cli-kit/common/function' | ||
| import chalk from '@shopify/cli-kit/node/colors' | ||
| import readline from 'readline' | ||
|
|
||
|
|
@@ -126,43 +127,55 @@ export async function dev(options: DevOptions) { | |
| process.stdin.setRawMode(true) | ||
| } | ||
|
|
||
| process.stdin.on('keypress', (_str, key) => { | ||
| const keypressHandler = createKeypressHandler(urls, ctx) | ||
| process.stdin.on('keypress', keypressHandler) | ||
|
|
||
| await Promise.all([ | ||
| backgroundJobPromise, | ||
| renderDevSetupProgress() | ||
| .then(serverStart) | ||
| .then(() => { | ||
| renderLinks(urls) | ||
| if (options.open) { | ||
| openURLSafely(urls.local, 'development server') | ||
| } | ||
| }), | ||
| ]) | ||
| } | ||
|
|
||
| export function createKeypressHandler( | ||
| urls: {local: string; giftCard: string; themeEditor: string; preview: string}, | ||
| ctx: {lastRequestedPath: string}, | ||
| ) { | ||
| const debouncedOpenURL = debounce(openURLSafely, 100, {leading: true, trailing: false}) | ||
|
|
||
| return (_str: string, key: {ctrl?: boolean; name?: string}) => { | ||
| if (key.ctrl && key.name === 'c') { | ||
| process.exit() | ||
| } | ||
|
|
||
| switch (key.name) { | ||
| case 't': | ||
| openURLSafely(urls.local, 'localhost') | ||
| debouncedOpenURL(urls.local, 'localhost') | ||
|
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. I might've debounced the click handler instead of the URL opening itself but this works, too! |
||
| break | ||
| case 'p': | ||
| openURLSafely(urls.preview, 'theme preview') | ||
| debouncedOpenURL(urls.preview, 'theme preview') | ||
| break | ||
| case 'e': | ||
| openURLSafely( | ||
| debouncedOpenURL( | ||
| ctx.lastRequestedPath === '/' | ||
| ? urls.themeEditor | ||
| : `${urls.themeEditor}&previewPath=${encodeURIComponent(ctx.lastRequestedPath)}`, | ||
| 'theme editor', | ||
| ) | ||
| break | ||
| case 'g': | ||
| openURLSafely(urls.giftCard, 'gift card preview') | ||
| debouncedOpenURL(urls.giftCard, 'gift card preview') | ||
| break | ||
| default: | ||
| break | ||
| } | ||
| }) | ||
|
|
||
| await Promise.all([ | ||
| backgroundJobPromise, | ||
| renderDevSetupProgress() | ||
| .then(serverStart) | ||
| .then(() => { | ||
| renderLinks(urls) | ||
| if (options.open) { | ||
| openURLSafely(urls.local, 'development server') | ||
| } | ||
| }), | ||
| ]) | ||
| } | ||
| } | ||
|
|
||
| export function openURLSafely(url: string, label: string) { | ||
|
|
||
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.