The proper way of checking mark activity

Hello guys, Sorry I have another question to ask.

From the sample here, https://prosemirror.net/examples/menu/

let active = command(this.editorView.state, null, this.editorView)

It uses toggleMark to check the mark activity, but from I learnt from the doc, it seems much proper to get the selection to check if a mark is applied to any node in it.

Which is the best practice to check mark activity, I’m a bit confused by the sample code, so if anyone could help me figure it out, that would be excellent, cheer!!!

That’s pretty much what the command does. If you’re testing if a given button should be enabled, or similar, running the corresponding command with null for the dispatch argument is the recommended way. If you’re just looking for a way to scan the selection for a mark, take a look how toggleMark implements that internally.

Really appreciate your help Marjin, I ended up making it work by using selection, but I will try to do the way you suggested since it’s the best practice.

Not sure if this is the best place to put this, but hopefully it helps the next guy who tries to figure this out.

By “mark activity”, I think a lot of devs think of this like when you click on the Prosemirror editor Atlassian has setup for Jira, and the buttons are highlighted depending on what marks are currently active.

For example, if I have a bullet-list item that has bolded text and the caret cursor is currently there, then the menu-bar would look like this:

jiraMenubar

At the same time, notice how the text type can only be set to “Normal text”, because you can’t have a heading inside a bullet list. This is what the:

let active = command(this.editorView.state, null, this.editorView);

line does. If you’re not allowed to use that mark (or its already been added to the selection), then it will return false. Indeed this is what @marijn refers to when he says to look at how toggleMarks is implemented internally:

/// ...
/// Will return `false` when the current selection
/// doesn't support that mark. This will remove the mark if any marks
/// of that type exist in the selection, or add it otherwise.
/// ...

I think that’s the distinction that’s confusing people (it confused me, but thankfully I’ve become aware).

You can confirm this to be the case as well for the editor menu example, as when you bold some text and move the cursor in-between the bolded text, nothing happens to the menu bar:

https://prosemirror.net/examples/menu/

Hope this helps someone. Liberty speed your step.