Folding a specific node


I try to make a plugin, and I want to add data without polluting the editor,
so I was thinking to write them in the file (to stay the plain text) and hide them.

I don’t see how I can do this with the current public API: I saw Editor#hideNode and Editor#collapseNode in the code, but not in the public API (and I’m note sure of the right semantic difference between them).

The code I found use Range instead of Node directly (because it uses the selection), but how can I create a Range from a specific Node? It is even a good idea? :slight_smile:

I created an example which creates a new item (so a new node) and tries to hide it. The problem is: when you call it from a list item, the following items will be collapsed as well.

Am I missing something?

Hi, glad you are trying out the API.

This is just an over site on my part. There’s tons of API and I figured better to just start with a little and expand out instead of include everything. But yes consider Editor#collapseNode public, I’ll document it soon. Basically collapse node should be pretty strait forward… works like in an outliner. It will “collapse” the node, hiding all of the nodes children in a fold at the end of the nodes line.

Editor#hideNodes is a bit different and not as well tested. I just wrote it for a specific case. But what it does is add folds to hide all nodes (it takes an array) passed into it. It hides them by adding folds (folds can be added anywhere, not just along hierarchy lines. Be happy make this public if/when someone needs it, but will probably leave private until then.

I’m not sure I follow. You mean the part of your code that’s doing forEachNodeInRange? If you just want to get a single node from the selection you can do range.startNode. That will return the node where the range starts. I don’t see your code trying to create a range… but if you want to create a range from a node you can do tree.createRangeFromNodes(node, 0).

I think the parts that’s tripping you up is that all of your changes are bracketed in beginUpdates and endUpdates (that’s a good thing) BUT also your call to Editor#collapseNode is in that update group. And that creates a problem because until endUpdates is called the Editor won’t know about the changes you’ve made.

So move Editor#collapseNode out of the updates block and it should work better, though I’m not certain it’s doing what you want yet.

Anyway… I’m definitely rambling now. I’ve created an updated version of your script here which “might” do what you were trying to do. Please ask again if I didn’t answer all of your questions.

Thanks for the explanations, it works like a charm :slight_smile: