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: Required attributes #2640

Merged
merged 2 commits into from May 20, 2022
Merged

feat: Required attributes #2640

merged 2 commits into from May 20, 2022

Conversation

thatsjonsense
Copy link
Contributor

Problem: When using Tiptap, there's no way to mark a Node/Mark attribute as required. Tiptap sets default: null for every attribute, and there's no way to clear this if you want an attr to be required. This causes downstream issues - for example, ProseMirror's joinBackward command may try to create a wrapping with a node that has a required attribute, without knowing how to fill it in. ProseMirror tries to guard against this using the NodeType's hasRequiredAttrs method, but when using Tiptap it will always return false.

Solution: Add a new, optional, isRequired option on attrs. If an attr is set up with this property, and no default, then the default: null will be deleted and then ProseMirror's hasRequiredAttrs will return true.

Alternatively, I also considered allowing users to pass default: undefined, but this seemed more explicit.

Impact: Without this fix in @tiptap/core, I'm currently using an extension to hack this behavior. It's pretty hacky:

  onBeforeCreate() {
    Object.values(this.editor.schema.nodes).forEach((nodeType) => {
      // @ts-ignore - attrs is not a public method. But if it's ever changed, this won't crash.
      Object.values(nodeType.attrs || {}).forEach((attr: any) => {
        if (attr.default === undefined) {
          attr.hasDefault = false
        }
      })
    })
  },
})```

@netlify
Copy link

netlify bot commented Mar 23, 2022

Deploy Preview for tiptap-embed ready!

Name Link
🔨 Latest commit e173b3d
🔍 Latest deploy log https://app.netlify.com/sites/tiptap-embed/deploys/623a734f4fc64d00086c0111
😎 Deploy Preview https://deploy-preview-2640--tiptap-embed.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@bdbch bdbch self-requested a review April 29, 2022 10:45
@bdbch bdbch self-assigned this Apr 29, 2022
@bdbch bdbch merged commit 13a0342 into ueberdosis:main May 20, 2022
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.

None yet

2 participants