Hello, I’m using the javascript mode to display json. At present, the hierarchical indentation of json has been implemented. I want to collapse the specified level of json when loading, such as folding all second levels:
Original json:
{
"a": {
"b":1
},
"c": {
"d":2
}
}
Json after folding:
{
"a": {...},
"c": {...}
}
I checked the documentation and it seems that this feature is not currently available. Can you provide some ideas?Thanks
If you have a way to find the start lines you want to fold, you can call editor.foldCode({line: N, ch: 0}) to fold the range starting at a specific line. The editor doesn’t have functionality for finding those lines though (unless you’re using CodeMirror 6, in which case the syntax tree might help).
Yes, you are right. I checked other built-in plugins and found that foldOptions has a rangeFinder method to return the current fold line, which is this:
const foldLines = []
foldOptions: {
rangeFinder: (cm, start) => {
let helpers = cm.getHelpers(start, “fold”);
for (let i = 0; i < helpers.length; i++) {
let cur = helpers[i](cm, start);
if (cur) {
foldLines.push(cur.from.line);
return cur;
}
}
}
}
But I can only use it to find the fold line of the current visible area, not the entire document; in addition, if you use this method and foldCode, because foldCode can only be executed after the codemirror instance is loaded, users can see To the obvious folding action. Is there a way to complete the folding before the instance is loaded?