esc-to-cancel - Restore feature on PRs#9280
Conversation
esc-to-cancel on new PR header
|
|
||
| function findCancelButton(field: HTMLInputElement): HTMLButtonElement | HTMLAnchorElement | undefined { | ||
| for (const titleContainerSelector of titleContainerSelectors) { | ||
| const titleContainer = field.closest(titleContainerSelector); |
There was a problem hiding this comment.
No loop needed, just pass the whole array to .closest()
| function findCancelButton(field: HTMLInputElement): HTMLButtonElement | HTMLAnchorElement | undefined { | ||
| for (const titleContainerSelector of titleContainerSelectors) { | ||
| const titleContainer = field.closest(titleContainerSelector); | ||
| if (!titleContainer) { |
There was a problem hiding this comment.
closest() must/will find the element, so no condition needed here
| continue; | ||
| } | ||
|
|
||
| const localCancelButton = [...titleContainer.querySelectorAll<HTMLButtonElement>('button:not([disabled])')] |
There was a problem hiding this comment.
$$() already selects, returns an array, and is properly typed:
| const localCancelButton = [...titleContainer.querySelectorAll<HTMLButtonElement>('button:not([disabled])')] | |
| const localCancelButton = $$('button:not([disabled])') |
| } | ||
| } | ||
|
|
||
| return $optional<HTMLButtonElement | HTMLAnchorElement>('.js-cancel-issue-edit'); |
There was a problem hiding this comment.
This cannot be optional. The button must be found. If it's not found, the selectors are wrong and/or GitHub was updated. In both cases we want an error to be thrown ($() will throw, $optional() doesn't)
| || field.hasAttribute('aria-autocomplete') | ||
| // Classic autocomplete dropdown | ||
| || elementExists('.suggester', field.form!) | ||
| || elementExists('.suggester', field.form ?? undefined) |
There was a problem hiding this comment.
Don't make unnecessary changes
| } | ||
|
|
||
| const cancelButton = findCancelButton(event.delegateTarget); | ||
| if (!cancelButton) { |
There was a problem hiding this comment.
As mentioned, the cancel button must be found
|
Note that untested AI PRs will result in a block. So make sure it works and ideally record a screencast or gif (with LICEcap if you use macos for example) |
fix_tested.mp4 |
fregante
left a comment
There was a problem hiding this comment.
Thank you! Looks reasonable already
|
|
||
| function findCancelButton(field: HTMLInputElement): HTMLButtonElement | HTMLAnchorElement { | ||
| const titleContainer = field.closest(titleContainerSelectors.join(','))!; | ||
| return $$('button:not([disabled])', titleContainer) |
There was a problem hiding this comment.
Without changing the order of operations, we have to temporarily use $$optional here or else it might throw on the old version.
esc-to-cancel on new PR headeresc-to-cancel - Restore feature on PRs
|
|
||
| function findCancelButton(field: HTMLInputElement): HTMLButtonElement | HTMLAnchorElement { | ||
| const titleContainer = field.closest(titleContainerSelectors); | ||
| return $$optional('button:not([disabled])', titleContainer ?? undefined) |
There was a problem hiding this comment.
@fregante allow baseElements in select-dom to be null 🙏
There was a problem hiding this comment.
I follow sindresorhus/meta#7
I guess an exception can be made since the DOM has a lot of nulls. PR welcome to explore it further
| // Old view -- TODO: Remove after legacy PR files view is removed | ||
| '.js-cancel-issue-edit', | ||
| ]); | ||
| if (cancelButton.textContent.trim() !== 'Cancel') { |
There was a problem hiding this comment.
Which works only with text nodes or elements containing only them
There was a problem hiding this comment.
Therefore the code can be adjusted to use it, what's the issue? cancelButton.firstChild probably.
There was a problem hiding this comment.
The issue is that it would be cancelButton.firstChild.firstChild
There was a problem hiding this comment.
Or cancelButton.querySelector('span[data-component="text"]'). I like neither
| } | ||
|
|
||
| // TODO: Drop in March 2025, implemented by GitHub | ||
| // https://github.com/refined-github/refined-github/pull/7892 |
There was a problem hiding this comment.
We still want a TODO comment to check if GitHub is already handling the keypress or not. September 2026 would be good, then every 6 months or so.
Co-authored-by: fregante <[email protected]>
Co-authored-by: Grigory <[email protected]> Co-authored-by: fregante <[email protected]>
Fixes #9250
Test URLs
Demo gif
(
fix_tested.mp4