-
Notifications
You must be signed in to change notification settings - Fork 121
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
Implement the code formatting shortcut #2613
base: main
Are you sure you want to change the base?
Implement the code formatting shortcut #2613
Conversation
fbf699f
to
333617a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome! This looks fantastic. Thanks for your thoroughness, and for fitting it in with the other formatting commands.
With
document.queryCommandState
also being deprecated and non-standard, I also switched to usinggetCommandState
for selected text (rather than only an entire selected thought) for the other commands as well.
That sounds fine, thanks.
Fwiw, I did some research on queryCommandState
before adding it to the codebase. Despite its deprecation status, it is very well supported and unlikely to be dropped. But if we have reliable alternatives that are performant and offer the same behavior then I'm happy to use them.
The only issue I found is that it does work in Context View. While Context View is admittedly incomplete, other formatting commands like bold, italic, underline do work there, so I think it's appropriate to add support for format as code. (Notably, text color does not work in Context View.)
Steps to Reproduce
- a
- m
- x
- b
- m
- y
- Set the cursor on
a/m
- Activate Context View.
- Set the cursor on
a/m~/a
. - Format as code.
Current Behavior
- a
- <code>a</code>
- x
- b
- m
- y
Expected Behavior
- <code>a</code>
- m
- x
- b
- m
- y
Thanks for the review! It appears that One fix for this which worked was to change
to:
However, I am less familiar with this part of the code and I'm currently unable to determine if that is the correct fix or if it will cause other regressions. It does cause several unit tests related to the context view to fail, so I assume this is not the correct approach. Rather than diving deeper into Screen.Recording.2024-11-30.at.12.02.47.PM.mov |
…hin the formatWithTag action
Yes, that is confusing. The reason that Of course, this logic is wrong when formatting a thought, as we expect the visible thought The root cause is that the context view has ambiguous semantics due to it being the transitional space between two different parts of the tree. The particular SimplePath that a context view thought should be resolved to will depend on the command that is being executed. The design of the
That should be fine. Another way is |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The unit tests are stalling for some reason. I thought it was just a flaky CI run, but I re-ran it several times and got the same result, and then confirmed that they halt in the same place on my local machine. Also confirmed that it does not occur in main
.
Let me know if you're able to reproduce. Thanks!
…it tests which are timing out, while preserving functionality
7828503
to
0b73af9
Compare
I am able to reproduce it locally, and it is very strange. The tests appear successful but some stall indefinitely. I would expect there to be a relatively short timeout, but the tests stall until I forcefully quit. Quitting them produces these error messages, which don't make add much value for me:
Through some bisecting / trial-and-error, I found the apparent culprit to be What we're trying to determine is whether the selection is only part of a thought or the entire thought, for deciding whether the command buttons should be active. I'd be happy to consider an alternative if you're familiar with a better option, but this change is relatively simple, working as expected, and passing the tests: 0b73af9 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, that sounds like a reasonable solution. However, I think isOnThought
will come back to bite us if we leave it as is.
I was already thinking it was probably an infinite loop, because the tests could only be quit by killing the process. Thanks for narrowing it down to isOnThought
. Try this:
diff --git a/src/device/selection.ts b/src/device/selection.ts
index 485d3065665..f01c4371a8e 100644
--- a/src/device/selection.ts
+++ b/src/device/selection.ts
@@ -73,8 +73,7 @@ export const isThought = (): boolean => {
/** Returns true if the selection is on a thought. */
export const isOnThought = (): boolean => {
let focusNode = window.getSelection()?.focusNode
- if (!focusNode) return false
- while ((focusNode as HTMLElement)?.tagName !== 'DIV') {
+ while (focusNode && (focusNode as HTMLElement)?.tagName !== 'DIV') {
if (isEditable(focusNode)) return true
focusNode = focusNode?.parentNode
}
Overview
This pull request implements #1957 by adding a shortcut to format selected text as code.
It currently uses the Bold icon as a placeholder. When the designer creates a dedicated icon and animation (most likely a
</>
icon), we can replace the placeholder.Since the
c
key is already used for multiple shortcuts, this change usesk
for the keyboard shortcut.We are not able to use the same
document.queryCommandState
, so I updatedgetCommandState
to also work for selected text for determining whether the shortcut icon should be active or inactive. The only change needed to make it work was to consider when the cursor is within text but not selecting any, which returns selected HTML such as<b><code></code></b>
without any non-tag characters. Withdocument.queryCommandState
also being deprecated and non-standard, I also switched to usinggetCommandState
for selected text (rather than only an entire selected thought) for the other commands as well.We also cannot use
document.execCommand
within theformatSelection
action, so I added a newformatWithTag
action that manually adds/removes the provided tag. This is currently only used for<code>
, but it could be reused for other commands that are not supported bydocument.execCommand
.Demo
Screen.Recording.2024-11-22.at.10.50.57.PM.mov