Merge pull request #5138 from naturalcrit/fix-codemirror

fix shortcuts in foldall and unfoldall functions
This commit is contained in:
Trevor Buckner
2026-10-22 17:17:22 -04:00
committed by GitHub
2 changed files with 31 additions and 25 deletions
+28 -24
View File
@@ -156,6 +156,31 @@ const CodeEditor = forwardRef(
return folds; return folds;
}; };
const foldAll = (view = viewRef.current)=>{
if(!view) return false;
const doc = view.state.doc;
const pages = pageMap.current;
const effects = pages.map((start, i)=>{
const next = pages[i + 1] || doc.length;
const from = i ? doc.line(doc.lineAt(start).number + 1).from : 0;
const to = doc.line(doc.lineAt(next).number).from - 1;
return to > from ? foldEffect.of({ from, to }) : null;
}).filter(Boolean);
view.dispatch({ effects });
return true;
};
const unfoldAll = (view = viewRef.current)=>{
if(!view) return false;
view.dispatch(unfoldAllCmd(view));
return true;
};
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) {
@@ -199,7 +224,7 @@ const CodeEditor = forwardRef(
//keyboard shortcut //keyboard shortcut
keymap.of([...defaultKeymap, foldKeymap, ...searchKeymap]), keymap.of([...defaultKeymap, foldKeymap, ...searchKeymap]),
generalKeymap, generalKeymap(foldAll, unfoldAll),
...(tab === 'brewStyles' ? [cssKeymap] : [markdownKeymap]), ...(tab === 'brewStyles' ? [cssKeymap] : [markdownKeymap]),
//multiple cursors and selections //multiple cursors and selections
@@ -404,29 +429,8 @@ const CodeEditor = forwardRef(
undone : redoDepth(view.state), undone : redoDepth(view.state),
}; };
}, },
foldAll : ()=>foldAll(),
foldAll : ()=>{ unfoldAll : ()=>unfoldAll(),
const view = viewRef.current;
if(!view) return;
const doc = view.state.doc;
const pages = pageMap.current;
const effects = pages.map((start, i)=>{
const next = pages[i + 1] || doc.length;
const from = i ? doc.line(doc.lineAt(start).number + 1).from : 0;
const to = doc.line(doc.lineAt(next).number).from - 1;
return to > from ? foldEffect.of({ from, to }) : null;
}).filter(Boolean);
view.dispatch({ effects });
},
unfoldAll : ()=>{
const view = viewRef.current;
if(!view) return;
view.dispatch(unfoldAllCmd(view));
},
focus : ()=>viewRef.current.focus(), focus : ()=>viewRef.current.focus(),
})); }));
@@ -215,13 +215,15 @@ const newBreak = (type)=>(view)=>forEachSelection(view, (state, from, to, select
}; };
}); });
export const generalKeymap = Prec.high(keymap.of([ export const generalKeymap = (foldAll, unfoldAll) => Prec.high(keymap.of([
{ key: 'Tab', run: insertTab }, { key: 'Tab', run: insertTab },
{ key: 'Shift-Tab', run: indentLess }, { key: 'Shift-Tab', run: indentLess },
{ key: 'Mod-z', run: undo }, //it may be unnecessary { key: 'Mod-z', run: undo }, //it may be unnecessary
{ key: 'Mod-Shift-z', run: redo }, { key: 'Mod-Shift-z', run: redo },
{ key: 'Mod-y', run: redo }, //user asked, so double keybind { key: 'Mod-y', run: redo }, //user asked, so double keybind
{ key: 'Mod-d', run: deleteLine }, //annoyingly overrides "selectNextOccurrence" because users asked { key: 'Mod-d', run: deleteLine }, //annoyingly overrides "selectNextOccurrence" because users asked
{ key: 'Mod-Shift-[', run: foldAll },
{ key: 'Mod-Shift-]', run: unfoldAll },
])); ]));
export const cssKeymap = Prec.highest(keymap.of([ export const cssKeymap = Prec.highest(keymap.of([