verbb / vizy

A flexible visual editor for Craft CMS
Other
44 stars 8 forks source link

Node methods and attributes return NULL #56

Closed dGilli closed 3 years ago

dGilli commented 3 years ago

Description I am trying to add the text of headings as their Id. node.tagName and node.text both return NULL.

Steps to reproduce

  1. Manually render heading nodes.
  2. Node attributes node.tagName and node.text return NULL.

Additional info

Additional context I am able to get the tag through node.getTag()[0].tag but node.getText() also returns NULL. When I try to get at the text via node.getContent() it cleary returns an array but then errors with Key "0" does not exist as the array is empty. when trying to get node.getContent()[0].text:

array(1) {
  [0]=>
  object(verbb\vizy\nodes\Text)#2816 (14) {
    ["tagName"]=>
    NULL
    ["content"]=>
    array(0) {
    }
    ["attrs"]=>
    array(0) {
    }
    ["marks"]=>
    array(0) {
    }
    ["text"]=>
    string(28) "..."
    ...
{% for node in entry.vizy.all() if node.type != 'vizyBlock' or node.enabled %}

    {% if node.type == 'heading' %}
        <pre>{{ dump(node.getContent()[0].text) }}</pre>
    {% endif %}

{% endfor %}
dGilli commented 3 years ago

{{ dump(node.rawNode) }}:

array(3) {
  ["type"]=>
  string(7) "heading"
  ["attrs"]=>
  array(2) {
    ["textAlign"]=>
    string(5) "start"
    ["level"]=>
    int(1)
  }
  ["content"]=>
  array(1) {
    [0]=>
    array(2) {
      ["type"]=>
      string(4) "text"
      ["text"]=>
      string(28) "..."
    }
  }
}

{{ dump(node.rawNode.content) }}:

Key "content" for array with keys "type, attrs" does not exist.

engram-design commented 3 years ago

I think the issue here is your use of node.getContent()[0] because this returns a Text node, which indeed doesn't have a tagName.

To get the tagName of the heading node, it's node.tagName. Not the inner content (which is a Text node).

{{ dump(node.tagName) }} would output h1 or the respective heading you've picked.

Using node.text on a heading node is also technically correctly null. I'm pretty sure this comes down to how Prosemirror handles a heading node's content, because a heading's child content can be marks or text, so it needs to be structured like that.

You could do {{ node.content[0].text }} to get the heading text - but be warned that as I've mentioned above, if there are nested marks (say, a portion of the heading has bold/italic, or nested nodes like Link nodes, this won't be a great way to get content, because it'll just be getting the first node of content.

I hope that makes sense - ProseMirror's schema is a little to get your head around. We'll be looking to make this a bit more developer friendly as Vizy grows.