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(slate-react): use Text.equals to compare leaf #4175

Merged
merged 5 commits into from
Aug 5, 2021

Conversation

gyh9457
Copy link
Contributor

@gyh9457 gyh9457 commented Apr 7, 2021

Description
fix MenoizedLeaf function

Issue

Example

  prev: { text: 'a' }
  next: { text: 'a', highlight: true }

  matches(text: Text, props: Partial<Text>): boolean {
    for (const key in props) {
      if (key === 'text') {
        continue
      }

      if (!text.hasOwnProperty(key) || text[key] !== props[key]) {
        return false
      }
    }

    return true
  },

  Text.matches(next.leaf, prev.leaf) // compare

  // in the matches function ,props do not include `highlight`. so it return true. but in fact ,it should return false

Checks

  • The new code matches the existing patterns and styles.
  • The tests pass with yarn test.
  • The linter passes with yarn lint. (Fix errors with yarn fix.)
  • The relevant examples still work. (Run examples with yarn start.)
  • You've added a changeset if changing functionality. (Add one with yarn changeset add.)

@changeset-bot
Copy link

changeset-bot bot commented Apr 7, 2021

🦋 Changeset detected

Latest commit: 9338c95

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
slate-react Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@@ -92,7 +92,7 @@ const MemoizedLeaf = React.memo(Leaf, (prev, next) => {
next.renderLeaf === prev.renderLeaf &&
next.text === prev.text &&
next.leaf.text === prev.leaf.text &&
Text.matches(next.leaf, prev.leaf) &&
Text.equals(prev.leaf, next.leaf, { loose: true }) &&
Copy link
Owner

Choose a reason for hiding this comment

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

Great catch @gyh9457! Would it be possible to remove the previous line now (and the loose option) to simplify the logic even further. Instead of:

     next.text === prev.text &&
     next.leaf.text === prev.leaf.text &&
     Text.equals(prev.leaf, next.leaf, { loose: true }) &&

We'd have:

     next.text === prev.text &&
     Text.equals(prev.leaf, next.leaf) &&

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@clauderic clauderic requested a review from ianstormtaylor June 1, 2021 18:55
@dylans
Copy link
Collaborator

dylans commented Aug 4, 2021

Part of this was handled in #4347.

Need to decide if we can safely also remove next.leaf.text === prev.leaf.text &&

@dylans dylans merged commit bde6e80 into ianstormtaylor:main Aug 5, 2021
dylans added a commit to dylans/slate that referenced this pull request Sep 13, 2021
* fix(slate-react): use Text.equals to compare leaf

* refactor(slate-react): simplify MenoizedLeaf logic

* Create breezy-lizards-travel.md

* Update packages/slate-react/src/components/leaf.tsx

Co-authored-by: Ian Storm Taylor <ian@ianstormtaylor.com>
Co-authored-by: Dylan Schiemann <dylan@dojotoolkit.org>
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.

3 participants