From 448a26e75827f3754b4e4fa2341f917e6b1f02a5 Mon Sep 17 00:00:00 2001 From: Jonatan Heyman Date: Fri, 13 Jun 2025 12:58:17 +0200 Subject: [PATCH] Store and restore folded ranges in the buffer file on save/load --- src/common/note-format.js | 7 +++++++ src/editor/editor.js | 15 +++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/common/note-format.js b/src/common/note-format.js index cb5ceb2..3653174 100644 --- a/src/common/note-format.js +++ b/src/common/note-format.js @@ -42,4 +42,11 @@ export class NoteFormat { get cursors() { return this.metadata.cursors } + + set foldedRanges(foldState) { + this.metadata.foldedRanges = foldState + } + get foldedRanges() { + return this.metadata?.foldedRanges || [] + } } diff --git a/src/editor/editor.js b/src/editor/editor.js index e5bde35..700cbaa 100644 --- a/src/editor/editor.js +++ b/src/editor/editor.js @@ -1,6 +1,6 @@ -import { Annotation, EditorState, Compartment, Facet, EditorSelection, Transaction, Prec } from "@codemirror/state" +import { Annotation, EditorState, Compartment, Facet, EditorSelection, Transaction, Prec, RangeSet } from "@codemirror/state" import { EditorView, keymap as cmKeymap, drawSelection, ViewPlugin, lineNumbers } from "@codemirror/view" -import { ensureSyntaxTree } from "@codemirror/language" +import { ensureSyntaxTree, foldState, foldEffect } from "@codemirror/language" import { markdown, markdownKeymap } from "@codemirror/lang-markdown" import { undo, redo } from "@codemirror/commands" @@ -165,6 +165,13 @@ export class HeynoteEditor { getContent() { this.note.content = this.view.state.sliceDoc() this.note.cursors = this.view.state.selection.toJSON() + + // fold state + const foldedRanges = [] + this.view.state.field(foldState, false)?.between(0, this.view.state.doc.length, (from, to) => { + foldedRanges.push({from, to}) + }) + this.note.foldedRanges = foldedRanges const ranges = this.note.cursors.ranges if (ranges.length == 1 && ranges[0].anchor == 0 && ranges[0].head == 0) { @@ -231,6 +238,10 @@ export class HeynoteEditor { scrollIntoView: true, }) } + // set folded ranges + this.view.dispatch({ + effects: this.note.foldedRanges.map(range => foldEffect.of(range)), + }) resolve() }) })