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: add optional merge method to decoration object #5776

Merged
merged 1 commit into from
Dec 7, 2024

Conversation

yf-yang
Copy link
Contributor

@yf-yang yf-yang commented Dec 7, 2024

Description
Currently, when decorations overlap with each other and they are merging with each other, Object.assign is used.
However, if two decorations with the same key has different values, one of the decoration will overwrite the other.

// text content: `them are happy`
// decoration 1:
{
  anchor: {
    path: [0],
    offset: 0,
  },
  focus: {
    path: [0],
    offset: 1,
  },
  message: '`t` should be upper case.'
}

// decoration 2:
{
  anchor: {
    path: [0],
    offset: 0,
  },
  focus: {
    path: [0],
    offset: 4,
  },
  message: 'The correct word is `they`.'
}

In this example, two messages cannot be shown together due to Object.assign overwriting behavior.

Example
A GIF or video showing the old and new behaviors after this pull request is merged. Or a code sample showing the usage of a new API. (If you don't include this, your pull request will not be reviewed as quickly, because it's much too hard to figure out exactly what is going wrong, and it makes maintenance much harder.)

After adding a function merge to the decoration, two messages can be merged together:

// text content: `them are happy`

const merge = (leaf, decoration) => {
  const { messages, ...rest } = decoration;
  leaf.messages = [...(leaf.messages ?? []), ...messages];
  Object.assign(leaf, rest);
}

// decoration 1:
{
  anchor: {
    path: [0],
    offset: 0,
  },
  focus: {
    path: [0],
    offset: 1,
  },
  merge,
  messages: ['`t` should be upper case.']
}

// decoration 2:
{
  anchor: {
    path: [0],
    offset: 0,
  },
  focus: {
    path: [0],
    offset: 4,
  },
  merge,
  messages: ['The correct word is `they`.']
}

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.)

Copy link

changeset-bot bot commented Dec 7, 2024

🦋 Changeset detected

Latest commit: b3e18e5

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

This PR includes changesets to release 1 package
Name Type
slate Minor

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

Copy link
Collaborator

@dylans dylans left a comment

Choose a reason for hiding this comment

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

Nice improvement, thank you!

@dylans dylans merged commit 5a1c728 into ianstormtaylor:main Dec 7, 2024
7 of 11 checks passed
@github-actions github-actions bot mentioned this pull request Nov 26, 2024
@yf-yang yf-yang deleted the decoration branch December 7, 2024 10:57
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