Skip to content
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

feat: Keyboard shortcut to reopen the browser without restarting the dev command #1211

Open
wants to merge 25 commits into
base: main
Choose a base branch
from

Conversation

nishu-murmu
Copy link
Contributor

@nishu-murmu nishu-murmu commented Nov 26, 2024

Thanks for the insight @aklinker1, still getting used to the code structure and APIs.
First time actually contributing to open source project. 😓
Will look into it.

But I was also suggesting that, we can map few keys for various server related functionality.

Like the reference you gave
https://github.com/vitejs/vite/blob/bf1e9c2fd7b05f84d05e59f72b3fc26ca22807bb/packages/vite/src/node/shortcuts.ts

Had various key mapped to certain functionality.

We can do so?

I've added the code for removing and registering the listeners.
Can you check if it correct implementation?

Edit:
Sorry, Accidently closed the actual PR.

Copy link

netlify bot commented Nov 26, 2024

Deploy Preview for creative-fairy-df92c4 ready!

Name Link
🔨 Latest commit cda147b
🔍 Latest deploy log https://app.netlify.com/sites/creative-fairy-df92c4/deploys/67481c510ecda30008ed48de
😎 Deploy Preview https://deploy-preview-1211--creative-fairy-df92c4.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@aklinker1
Copy link
Collaborator

No worries, if you push code to the same branch, you don't need to open a new PR with every change. We'll just use this one now instead of #1206. I'll to go this later today, woke up and going to work now lol.

Copy link
Collaborator

@aklinker1 aklinker1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of recommendations:

  1. Let's move createKeyboardShortcuts into it's own file for better organization, src/core/keyboard-shortcuts.ts
  2. Instead of using a global variable, we should put the isWatching variable inside the createKeyboardShortcuts function so it's not global, but state for a single instance of the keyboardShortcuts object.
  3. Related to 2, we should only create 1 keyboard shortcut object outside the server object. You mentioned you weren't sure where to put it so it has access to the server? I'd recommend putting it here: https://github.com/nishu-murmu/wxt/blob/88d15a2b715c93e89b09923b70875bf46dcb3bd4/packages/wxt/src/core/create-server.ts#L153

sh.exe.stackdump Outdated Show resolved Hide resolved
packages/wxt/src/types.ts Outdated Show resolved Hide resolved
@aklinker1 aklinker1 changed the title Provide a way to reopen the browser like vite. feat: Keyboard shortcut to re-open the browser without restarting the dev command Nov 26, 2024
@aklinker1 aklinker1 changed the title feat: Keyboard shortcut to re-open the browser without restarting the dev command feat: Keyboard shortcut to reopen the browser without restarting the dev command Nov 26, 2024
Copy link
Collaborator

@aklinker1 aklinker1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, code looks good, other than the capitalization @Timeraa mentioned.

Tested it out on Mac, but I can no longer use ctrl+C to stop the server. It does nothing when I press those keys, when it should stop the process. Reloading works really well though!

I did some snooping around Vite's code, and they don't use raw input mode:

https://github.com/vitejs/vite/blob/51a5569e66bd7f0de79ac14b9e902d1382ccd0aa/packages/vite/src/node/shortcuts.ts#L90-L92

Should we also use readline.createInterface?

@nishu-murmu
Copy link
Contributor Author

Alright, code looks good, other than the capitalization @Timeraa mentioned.

Tested it out on Mac, but I can no longer use ctrl+C to stop the server. It does nothing when I press those keys, when it should stop the process. Reloading works really well though!

I did some snooping around Vite's code, and they don't use raw input mode:

https://github.com/vitejs/vite/blob/51a5569e66bd7f0de79ac14b9e902d1382ccd0aa/packages/vite/src/node/shortcuts.ts#L90-L92

Should we also use readline.createInterface?

The first thing I did I saw the vite's code.

Initially I tried that code itself
But we have to additionally press enter for the keypress to work

Then I tried the raw input mode which was working.

Do you any idea regarding this?

@aklinker1
Copy link
Collaborator

aklinker1 commented Nov 28, 2024

The first thing I did I saw the vite's code.

Initially I tried that code itself
But we have to additionally press enter for the keypress to work

Then I tried the raw input mode which was working.

Do you any idea regarding this?

My main concern here is there's more than just ctrl+C... We need to continue handling all other signals as well, ctrl+X, ctrl+Z, etc. I don't think adding if-statements is the right way to go... Vitest's keyboard shortcuts don't require pressing enter. Seems like they use if-statements, but it seems pretty complex...

https://github.com/vitest-dev/vitest/blob/1fa16f941a651c1af0c24124ec4533d432002236/packages/vitest/src/node/stdin.ts#L52-L75

Is there a library we can use for this?

@aklinker1
Copy link
Collaborator

aklinker1 commented Nov 28, 2024

Is there a library we can use for this?

Doesn't seem like there is. I prefer to use Vite's approach and require pressing enter over trying to implement a perfect raw input mode. Vite's help message makes it really easy to see that you need to press enter. We should add a log telling people to "Type o + enter to open the browser". Until we have a second keyboard shortcut, we don't need a full help menu.

@nishu-murmu
Copy link
Contributor Author

Yeah we can do that. I'm on with showing the log approach.
I'll revert last commit.
I need to first reach home.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants