-
Hi, I'm trying to save and restore the code folding state like this // Save all folds in local storage
localStorage.setItem(id, JSON.stringify(getFolds(editor.session.getAllFolds())));
function getFolds(folds) {
if (folds.length === 0) return [];
let res = [];
folds.forEach(function (fold) {
res.push({
start: fold.start,
end: fold.end,
placeholder: fold.placeholder,
subFolds: getFolds(fold.subFolds)
})
});
return res;
}
// Then restore all folds from local storage
editor.session.addFolds(setFolds(JSON.parse(localStorage.getItem(id))));
function setFolds(folds) {
let res = [];
folds.forEach(function (fold) {
let f = new Fold(new ace.Range(fold.start.row, fold.start.column, fold.end.row, fold.end.column), fold.placeholder);
if (fold.subFolds.length > 0) {
setFolds(fold.subFolds).forEach(function (subfold) {
f.addSubFold(subfold);
});
}
res.push(f);
});
return res;
} The issue that I have is that the sub folds are not restored at the right location. Any idea about what I'm doing wrong? |
Beta Was this translation helpful? Give feedback.
Answered by
Syone
Mar 6, 2024
Replies: 1 comment 1 reply
-
Looking at the source code, it looks like subfolds are calculated based on previous folds in the document. Does it work to only to do |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No it doesn't work but I found a solution, when I retrieve folds using
editor.session.getAllFolds()
subfolds start/end coordinates are relative to their parent fold. And when we add a subfold into another fold usingf.addSubFold(subfold)
we have to use "absolute coordinates".So what I have to do is to recalculate each subfold coordinates before storing it: