question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Editor.nodes function doesn't return the node.

See original GitHub issue
const isBlockActive = (editor, format) => {
  const [match] = Editor.nodes(editor, {
    match: n => n,
  })
  console.log(match)
  return !!match
}

I am getting undefined, I am supposed to get the node. :l I cloned it from richtext.js but the isBlockActive function doesn’t seem to work.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5

github_iconTop GitHub Comments

11reactions
neohotsaucecommented, Feb 3, 2020

its an issue with gatsby, fix it using:

const isBlockActive = (editor, format) => {
  let match = false
  for (const [node, paths] of Editor.nodes(editor, {
    match: n => n.type === format,
  })) {
    if (node.type === format) match = true
    break
  }
  return !!match
}
1reaction
JanStormcommented, Jul 23, 2021

In my opinion, this function has to use editor.selection, because we only want to mark the format buttons as active, when user has a element selected, which uses the format. (So he then could toggle it off) In my solution i only use only the cursor position, but it seems to work for me. In this workaround im searching in the editor.children for the format myself:

const isBlockActive = (editor: Editor, format: string) => {
    const cursorPosition = editor.selection?.focus;
    const elementPath = cursorPosition?.path;
    let match = false;
    if (elementPath && elementPath.length > 0 && editor.children) {
        let element: any = editor.children[elementPath[0]];
        for(let i = 1; i < elementPath.length; i += 1) {
            if (element?.type === format) {
                match = true;
                break;
            }
            element = element.children[elementPath[i]];
        }
    }

    return match;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Don't use Node.nodes to only iterate child nodes #3867 - GitHub
All this code is doing is using Node.nodes to iterate over all the child nodes in an array, but it ends up calling...
Read more >
Collapse to function creates a return node you can't delete
If you use the “Collapse to Function” menu item it creates a new function with a return node that can't be deleted.
Read more >
BUG: table.row(...).node(...).to$ is not a function - DataTables
Does not return an API object, so you can't call to$() on it. Instead, one must call row().nodes().to$() (note the plural on node)....
Read more >
Using the Node-Red Function Node- Beginners Guide
When a function node doesn't return anything the nodes connected to the function output aren't triggered and the flow stops for that path....
Read more >
Yet another question about function error returning a string
First off yes I have spent a lot of time reading the documentation on JSON and Node Red working with messages as well...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found