Skip to content

Conversation

@liberaldev
Copy link
Contributor

@liberaldev liberaldev commented Dec 5, 2024

At the time I posted #183, I didn't have a Mac or other Apple device, so I couldn't validate the issue with Apple devices. I have now purchased a Mac and verified the issue that occurs on Apple devices, and this PR improves the run shortcut to work well on Apple devices. The detailed improvements are as follows:

  • Added cmd + return shortcut
  • Improved conflict in Safari by adding e.prevent.
  • iOS and iPadOS that still conflict now only work with cmd + return.

app/try_ruby.rb Outdated
#If hold down the control and the Enter key goes down, run
$document.on :keydown, '#editor' do |e|
if e.key == "Enter" && e.ctrl?
if e.key == "Enter" && ((e.ctrl? && !@navigator.user_agent&.match?(/\b(iPad|iPhone|iPod)\b/)) ||
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is kinda hard to read... maybe we could add helper functions like:

def ios?
  @navigator.user_agent&.match?(/\b(iPad|iPhone|iPod)\b/)
end

def macos?
  @navigator.user_agent&.match?(/\bMac\b/)
end

Then the modified code could be just:

    $document.on :keydown, '#editor' do |e|
      if e.key == "Enter" && ((!ios? && e.ctrl?) || ((ios? || macos?) && e.meta?))
        e.prevent
        do_run
      end
    end

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added the Helper class to make my code more reusable

@liberaldev liberaldev requested a review from hmdne December 6, 2024 04:36
app/try_ruby.rb Outdated
#If hold down the control and the Enter key goes down, run
$document.on :keydown, '#editor' do |e|
if e.key == "Enter" && e.ctrl?
if (e.key == "Enter" && (e.ctrl? && !Helper.ios?)) || ((Helper.macos? || Helper.ios?) && e.meta?)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Right-hand side of the || expression is missing check for Enter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for pointing this out. I've fixed the issue, please check it out.

@liberaldev liberaldev requested a review from hmdne December 6, 2024 06:03
@liberaldev liberaldev changed the title Improved behavior of the run shortcut key (ctrl+return(ctrl+enter)) on Apple devices Improved behavior of the run shortcut key (ctrl+return (ctrl+enter)) on Apple devices Dec 6, 2024
@hmdne
Copy link
Collaborator

hmdne commented Dec 6, 2024

Looks good to me :D Let's merge

@hmdne hmdne merged commit 85a532e into ruby:master Dec 6, 2024
1 check passed
@liberaldev liberaldev deleted the fix-apple-device branch December 6, 2024 11:12
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.

2 participants