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: Make it possible to close inline reply with the escape key. #2273

Merged
merged 2 commits into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/routes/_components/status/StatusToolbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
{#if enableShortcuts}
<Shortcut scope={shortcutScope} key="f" on:pressed="toggleFavorite(true)"/>
<Shortcut scope={shortcutScope} key="r" on:pressed="reply()"/>
<Shortcut scope={shortcutScope} key="escape" on:pressed="dismiss()"/>
<Shortcut scope={shortcutScope} key="b" on:pressed="reblog(true)"/>
{/if}
<style>
Expand Down Expand Up @@ -146,6 +147,16 @@
this.fire('recalculateHeight')
})
},
dismiss () {
const { replyShown } = this.get()
if (replyShown) {
this.reply()
try {
// return focus to the article.
this.refs.node.closest('article').focus({ preventScroll: true })
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

  1. There's some probably not great coupling here, but I'm not sure how else to get at the article.
  2. This is potentially controversial. I think most users who value this will be using keyboard shortcuts, in which case focusing the article makes sense, since they probably opened the reply by focusing the article and pressing r. On the other hand, a decision was made to focus the Reply button after a reply is posted. Personally, I think the article should be focused in that case too, but I can understand the other perspective here too; the reply may well have been opened by tabbing to the reply button and pressing it.

Copy link
Owner

Choose a reason for hiding this comment

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

There's some probably not great coupling here, but I'm not sure how else to get at the article.

One common way to do this in Svelte is to fire an event and have the parent handle it. I can refactor to do that.

I don't think this is super controversial. I'm not sure what else Esc could be used for. Also, it is possible to disable keyboard shortcuts entirely.

BTW I also have a utility called tryToFocusElement that does the try/catch.

Copy link
Owner

Choose a reason for hiding this comment

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

Done! ccdd54f

} catch (e) { /* ignore */ }
}
},
async onOptionsClick () {
const { originalStatus, originalAccountId } = this.get()
const updateRelationshipPromise = updateProfileAndRelationship(originalAccountId)
Expand Down
19 changes: 13 additions & 6 deletions src/routes/_utils/shortcuts.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,22 @@ function unmapKeys (keyMap, keys, component) {

function acceptShortcutEvent (event) {
const { target } = event
return !(
if (
event.altKey ||
event.metaKey ||
event.ctrlKey ||
(event.shiftKey && event.key !== '?') || // '?' is a special case - it is allowed
(target && (
target.isContentEditable ||
(event.shiftKey && event.key !== '?') // '?' is a special case - it is allowed
) {
return false
}
if (event.key === 'Escape') {
// Allow escape everywhere.
return true
}
// Don't allow other keys in text boxes.
return !(target && (
target.isContentEditable ||
['TEXTAREA', 'SELECT'].includes(target.tagName) ||
(target.tagName === 'INPUT' && !['radio', 'checkbox'].includes(target.getAttribute('type')))
))
)
))
}