mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-05-07 18:48:39 +00:00
restore folds when changing tab
This commit is contained in:
@@ -17,7 +17,7 @@ import {
|
|||||||
crosshairCursor,
|
crosshairCursor,
|
||||||
} from '@codemirror/view';
|
} from '@codemirror/view';
|
||||||
import { EditorState, Compartment, StateEffect, StateField } from '@codemirror/state';
|
import { EditorState, Compartment, StateEffect, StateField } from '@codemirror/state';
|
||||||
import { foldAll as foldAllCmd, unfoldAll as unfoldAllCmd, foldGutter, foldKeymap, foldEffect, syntaxHighlighting } from '@codemirror/language';
|
import { foldAll as foldAllCmd, unfoldAll as unfoldAllCmd, foldGutter, foldKeymap, foldEffect, foldState, syntaxHighlighting } from '@codemirror/language';
|
||||||
import { defaultKeymap, history, undo, redo, undoDepth, redoDepth } from '@codemirror/commands';
|
import { defaultKeymap, history, undo, redo, undoDepth, redoDepth } from '@codemirror/commands';
|
||||||
import { languages } from '@codemirror/language-data';
|
import { languages } from '@codemirror/language-data';
|
||||||
import { css } from '@codemirror/lang-css';
|
import { css } from '@codemirror/lang-css';
|
||||||
@@ -151,6 +151,7 @@ const CodeEditor = forwardRef(
|
|||||||
const tabRef = useRef(tab);
|
const tabRef = useRef(tab);
|
||||||
const prevTabRef = useRef(tab);
|
const prevTabRef = useRef(tab);
|
||||||
const scrollRef = useRef({});
|
const scrollRef = useRef({});
|
||||||
|
const foldsRef = useRef({});
|
||||||
const pageMap = useRef([]);
|
const pageMap = useRef([]);
|
||||||
|
|
||||||
const recomputePages = (doc)=>{
|
const recomputePages = (doc)=>{
|
||||||
@@ -180,6 +181,14 @@ const CodeEditor = forwardRef(
|
|||||||
return page;
|
return page;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const getFoldRanges = (state) => {
|
||||||
|
const folds = [];
|
||||||
|
state.field(foldState, false)?.between(0, state.doc.length, (from, to) => {
|
||||||
|
folds.push({ from, to });
|
||||||
|
});
|
||||||
|
return folds;
|
||||||
|
};
|
||||||
|
|
||||||
const createExtensions = ({ onChange, language, editorTheme })=>{
|
const createExtensions = ({ onChange, language, editorTheme })=>{
|
||||||
const setEventListeners = EditorView.updateListener.of((update)=>{
|
const setEventListeners = EditorView.updateListener.of((update)=>{
|
||||||
if(update.docChanged) {
|
if(update.docChanged) {
|
||||||
@@ -284,6 +293,14 @@ const CodeEditor = forwardRef(
|
|||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
const restoreFolds = (view, folds) => {
|
||||||
|
if (!folds?.length) return;
|
||||||
|
|
||||||
|
view.dispatch({
|
||||||
|
effects: folds.map(f => foldEffect.of(f))
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
useEffect(()=>{
|
useEffect(()=>{
|
||||||
const view = viewRef.current;
|
const view = viewRef.current;
|
||||||
if(!view) return;
|
if(!view) return;
|
||||||
@@ -291,6 +308,8 @@ const CodeEditor = forwardRef(
|
|||||||
tabRef.current = tab;
|
tabRef.current = tab;
|
||||||
const prevTab = prevTabRef.current;
|
const prevTab = prevTabRef.current;
|
||||||
|
|
||||||
|
foldsRef.current[prevTab] = getFoldRanges(view.state);
|
||||||
|
|
||||||
if(prevTab !== tab) {
|
if(prevTab !== tab) {
|
||||||
docsRef.current[prevTab] = view.state;
|
docsRef.current[prevTab] = view.state;
|
||||||
|
|
||||||
@@ -304,6 +323,7 @@ const CodeEditor = forwardRef(
|
|||||||
}
|
}
|
||||||
|
|
||||||
view.setState(nextState);
|
view.setState(nextState);
|
||||||
|
restoreFolds(view, foldsRef.current[tab]);
|
||||||
|
|
||||||
const savedScroll = scrollRef.current[tab];
|
const savedScroll = scrollRef.current[tab];
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user