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

fix Set text color of selection #2289

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

Conversation

MOONSTAR1220
Copy link

@MOONSTAR1220 MOONSTAR1220 commented Aug 15, 2024

This pull request implements #2267.
Fix the setting color to current selection of thought.
Screen

Copy link
Contributor

@raineorshine raineorshine left a comment

Choose a reason for hiding this comment

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

Thanks! Please fix lint errors and tests, and I'll do a full review. It should automatically lint on prepush so I'm not sure how the lint errors got pushed.

We will need to add unit tests to cover the new functionality. An RTL test should allow you to set a browser selection and then set the text color.

If that doesn't work, we can create a Puppeteer test, but RTL is preferred.

@MOONSTAR1220
Copy link
Author

@raineorshine , now, I'm fixing the lint and test issues.
I'll let you know when it's done.
And for about unit test, I will check about that.
Thanks.

@MOONSTAR1220
Copy link
Author

MOONSTAR1220 commented Aug 17, 2024

Hello @raineorshine . I tried to implement Unit test using RTL.
I have fixed lint and test issues.
Please check and let me know the result.
Thanks.

src/util/getCommandState.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@raineorshine raineorshine left a comment

Choose a reason for hiding this comment

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

Thank you! Below you'll find a complete review.

You did a good job fitting into the existing structure, modifying stripStyleAttribute and using the execCommand infrastructure that was already in place. I didn't know about foreColor and backColor, so that's great that we can implement this with close to the same logic already used for bold, italic, underline, and strikethrough.

We will need to add puppeteer tests since neither jsdom nor happy-dom supports document.execCommand.

It works well, but for one aspect of the behavior: foreground and background colors should not be mixed and matched. Take a look at closer look at the existing functionality in main. Each swatch is a foreground+background combo. If you select blue text, the background color is automatically removed. If you select a blue background, the foreground color is automatically removed. This limits the combinations and ensures proper color contrast. Let me know if that needs any clarification.

Otherwise, they were mostly small suggestions, either technical or stylistic. Watch out for code duplication and avoid mutations if at all possible.

Thanks, and let me know if you have any questions!

src/actions/formatSelection.ts Outdated Show resolved Hide resolved
src/actions/textColor.ts Outdated Show resolved Hide resolved
src/actions/textColor.ts Outdated Show resolved Hide resolved
src/actions/textColor.ts Show resolved Hide resolved
src/components/ColorPicker.tsx Outdated Show resolved Hide resolved
src/util/formattingNodeToHtml.ts Show resolved Hide resolved
src/util/formattingNodeToHtml.ts Outdated Show resolved Hide resolved
src/util/formattingNodeToHtml.ts Outdated Show resolved Hide resolved
src/util/getCommandState.ts Outdated Show resolved Hide resolved
src/util/getCommandState.ts Outdated Show resolved Hide resolved
@MOONSTAR1220
Copy link
Author

Thanks for your clear explanation. I will fix this and let you know.

@MOONSTAR1220

This comment was marked as resolved.

@MOONSTAR1220

This comment was marked as resolved.

Copy link
Contributor

@raineorshine raineorshine left a comment

Choose a reason for hiding this comment

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

Thanks! It's coming along well.

The selection should be preserved when the color is set (just like when using bold, italic, underline, and strikethrough). You may need to save and restore the selection in the action-creator.

(command: 'bold' | 'italic' | 'strikethrough' | 'underline'): Thunk =>
(
command: 'bold' | 'italic' | 'strikethrough' | 'underline' | 'foreColor' | 'backColor',
color: string = '#ffffff',
Copy link
Contributor

Choose a reason for hiding this comment

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

We can't default to #ffffff since it depends on the theme.

src/actions/textColor.ts Show resolved Hide resolved
const thought = pathToThought(state, state.cursor)
const thoughtText = thought.value.replace(/<[^>]*>/g, '')
// set bullet to text color when the entire thought selected
if ((selection.text()?.length === 0 && thoughtText.length !== 0) || selection.text()?.length === thoughtText.length) {
Copy link
Contributor

Choose a reason for hiding this comment

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

You need to move the selection logic into the action-creator. Reducers must be pure functions and not rely on any global or window context. state1 + args → state2

src/components/ColorPicker.tsx Show resolved Hide resolved
src/components/ColorPicker.tsx Show resolved Hide resolved
src/util/formattingNodeToHtml.ts Show resolved Hide resolved
src/util/formattingNodeToHtml.ts Show resolved Hide resolved
src/util/formattingNodeToHtml.ts Show resolved Hide resolved
src/util/getCommandState.ts Show resolved Hide resolved
@@ -0,0 +1,35 @@
import helpers from '../helpers'
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks good.

There are few other tests we need to port over from the old RTL test:

  • Clear the text color when selecting white
  • Clear background color when selecting text color
  • Change color to black when setting background color (this is broken)

@raineorshine
Copy link
Contributor

Note that there are some linting errors. (Is your pre-push hook working?)

$ ls .hooks
post-commit* pre-push*
$ cat .hooks/pre-push
npm run lint

@MOONSTAR1220
Copy link
Author

@raineorshine
Thanks for your review.
I'm traveling right now due to urgent personal matters.
Is it ok to fix above issues next week? Really sorry for that.

@raineorshine
Copy link
Contributor

Of course! No problem.

@MOONSTAR1220
Copy link
Author

Hello @raineorshine .
I'm back.
I will fix the feedbacks and let you know when it's done.
Thanks.

@raineorshine
Copy link
Contributor

Thanks, let me know if any questions come up :).

@MOONSTAR1220
Copy link
Author

I got sick during my long trip.
But I tried to fix these problems first, but I couldn't and now I'm in the hospital.
The doctor said I'll recover in 5-10 days.
Is it okay if I fix the problems after that?
I'll solve them as soon as I get out of the hospital.
Really sorry for that.

@raineorshine
Copy link
Contributor

Of course! So sorry to hear you got sick. Hope you get well soon.

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