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

isActive() doesn't work with multiple attributes #666

Closed
ThomasKientz opened this issue Apr 13, 2020 · 1 comment
Closed

isActive() doesn't work with multiple attributes #666

ThomasKientz opened this issue Apr 13, 2020 · 1 comment
Labels
Type: Bug The issue or pullrequest is related to a bug

Comments

@ThomasKientz
Copy link

ThomasKientz commented Apr 13, 2020

Describe the bug
The isActive props on menuBar doesn't works if the node's schema contains more attributes than passed in the prop's function.

Steps to Reproduce / Codesandbox Example
Add a attribute to the "Heading" extension.

export default class Heading extends Node {
 // ...

  get schema() {
    return {
      attrs: {
        level: {
          default: 1,
        },
        align: { // <----  Added prop
          default: '',
        },
      },
      // ...
    }
  }

isActive.heading({ level: 1 }) doesn't return true anymore.

<editor-menu-bar v-slot="{ commands, isActive }" :editor="editor">
  <div class="menubar">
    <button
      class="menubar__button"
      :class="{ 'is-active': isActive.heading({ level: 1 }) }"
      @click="commands.heading({ level: 1 })"
    >
      H1
    </button>
  </div>
</editor-menu-bar>

Explanation

The bug is caused by the nodeIsActive() util function which compares the attributes passed in (eg isActive.heading({ level: 1 })) with the attributes of the node (eg {level: 1, align: null}). As the attributes are not exactly the same, the function will return false. One way of coping with that would be to only check if the attributes passed in ({ level: 1 }) are "included" in the node.

Solution

Replace https://github.com/scrumpy/tiptap/blob/master/packages/tiptap-utils/src/utils/nodeIsActive.js#L15
with return node.node.hasMarkup(type, {...node.node.attrs, ...attrs})

export default function nodeIsActive(state, type, attrs = {}) {
  const predicate = node => node.type === type
  const node = findSelectedNodeOfType(type)(state.selection)
    || findParentNode(predicate)(state.selection)

  if (!Object.keys(attrs).length || !node) {
    return !!node
  }

  return node.node.hasMarkup(type, {...node.node.attrs, ...attrs})
}
@ThomasKientz ThomasKientz added the Type: Bug The issue or pullrequest is related to a bug label Apr 13, 2020
@ThomasKientz
Copy link
Author

Related to #621

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug The issue or pullrequest is related to a bug
Projects
None yet
Development

No branches or pull requests

1 participant