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

Add IDs to headings and make "isActive" state work #621

Closed
git-mischa opened this issue Mar 2, 2020 · 4 comments
Closed

Add IDs to headings and make "isActive" state work #621

git-mischa opened this issue Mar 2, 2020 · 4 comments
Labels
Type: Bug The issue or pullrequest is related to a bug

Comments

@git-mischa
Copy link

git-mischa commented Mar 2, 2020

Hey guys,

so I'm trying to make my own heading node, which adds IDs to the headings, which I in return use to create jump marks in my editor environment. I can make it work as far as it correctly writes the HTML-tag and adds the ID. Also if the editor is loaded, already existing ID don't get overwritten but parsed correctly. Only problem is, that I somehow can't make it work, that the "isActive" state is computed within the button I use, which means that I can't toggle the heading on and off.

Here is the code, where I used the existing heading component as a basis and tried to add the capability for IDs:

import { Node } from 'tiptap'
import { setBlockType, textblockTypeInputRule, toggleBlockType } from 'tiptap-commands'

export default class Heading extends Node {

  get name () {
    return 'heading'
  }

  get defaultOptions () {
    return {
      levels: [1, 2, 3, 4, 5, 6]
    }
  }

  get schema () {
    return {
      attrs: {
        level: {
          default: 1,
        },
        id: {
          default: ''
        }
      },
      content: 'inline*',
      group: 'block',
      defining: true,
      draggable: false,
      parseDOM: this.options.levels
        .map(level => (
          {
            tag: `h${level}`,
            getAttrs: dom => ({
              level: level,
              id: dom.getAttribute('id')
            })
          })),
      toDOM: node => [`h${node.attrs.level}`,
        {
          class: 'anchor-heading',
          id: node.attrs.id || this.createId()
        }, 0]
    }
  }

  commands ({ type, schema }) {
    return attrs => toggleBlockType(type, schema.nodes.paragraph, attrs)
  }

  keys ({ type }) {
    return this.options.levels.reduce((items, level) => ({
      ...items,
      ...{
        [`Shift-Ctrl-${level}`]: setBlockType(type, { level })
      }
    }), {})
  }

  inputRules ({ type }) {
    return this.options.levels.map(level => {
      textblockTypeInputRule(
        new RegExp(`^(#{1,${level}})\\s$`),
        type,
        () => ({ level })
      )
    })
  }

  createId (length = 5) {
    return Math.round((Math.pow(36, length + 1) - Math.random() * Math.pow(36, length))).toString(36).slice(1)
  }
}

I am a 100% sure that I am missing some clue as how the active state is created and I tried to follow the steps in the library, but I can't make out what I'm missing.
Maybe someone can take a look at my code and guide me to the solution.

@git-mischa git-mischa added the Type: Bug The issue or pullrequest is related to a bug label Mar 2, 2020
@mekery
Copy link
Contributor

mekery commented Mar 3, 2020

same problems

@git-mischa
Copy link
Author

Does anyone have an idea on how to solve this? I tried a few different approaches, but nothing really worked.

@philippkuehn
Copy link
Contributor

This should be fixed with the latest update. (see #666)

@wiwat-p
Copy link

wiwat-p commented Jul 22, 2021

How to use this ?

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

4 participants