Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persist and restore code folds #1815

Merged
merged 7 commits into from Dec 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -231,12 +231,21 @@ class CodeEditor extends React.Component {
if (!uniquenessKey || !this.codeMirror) {
return;
}
const marks = this.codeMirror
.getAllMarks()
.filter(c => c.__isFold)
.map(mark => {
const { from, to } = mark.find();

return { from, to };
});

editorStates[uniquenessKey] = {
scroll: this.codeMirror.getScrollInfo(),
selections: this.codeMirror.listSelections(),
cursor: this.codeMirror.getCursor(),
history: this.codeMirror.getHistory(),
marks,
};
}

Expand All @@ -246,13 +255,18 @@ class CodeEditor extends React.Component {
return;
}

const { scroll, selections, cursor, history } = editorStates[uniquenessKey];
const { scroll, selections, cursor, history, marks } = editorStates[uniquenessKey];
this.codeMirror.scrollTo(scroll.left, scroll.top);
this.codeMirror.setHistory(history);

// NOTE: These won't be visible unless the editor is focused
this.codeMirror.setCursor(cursor.line, cursor.ch, { scroll: false });
this.codeMirror.setSelections(selections, null, { scroll: false });

marks &&
marks.forEach(({ from, to }) => {
this.codeMirror.foldCode(from, to);
});
}

_setFilterInputRef(n) {
Expand Down Expand Up @@ -284,6 +298,8 @@ class CodeEditor extends React.Component {
this.codeMirror.on('blur', this._codemirrorBlur);
this.codeMirror.on('paste', this._codemirrorPaste);
this.codeMirror.on('scroll', this._codemirrorScroll);
this.codeMirror.on('fold', this._codemirrorToggleFold);
this.codeMirror.on('unfold', this._codemirrorToggleFold);
this.codeMirror.on('keyHandled', this._codemirrorKeyHandled);

// Prevent these things if we're type === "password"
Expand Down Expand Up @@ -695,6 +711,10 @@ class CodeEditor extends React.Component {
this._persistState();
}

_codemirrorToggleFold() {
this._persistState();
}

_codemirrorKeyHandled(codeMirror, keyName, event) {
const { keyMap } = this.props;
const { keyCode } = event;
Expand Down
3 changes: 3 additions & 0 deletions packages/insomnia-app/flow-typed/codemirror.js
Expand Up @@ -2,7 +2,9 @@

declare module 'codemirror' {
declare class TextMarker {
__isFold: boolean;
clear(): void;
find(): { from: Pos, to: Pos };
}
declare class Doc {
markText(
Expand All @@ -28,5 +30,6 @@ declare module 'codemirror' {
setValue(string): void;
hasFocus(): boolean;
indexFromPos(pos: Pos): number;
getAllMarks(): Array<TextMarker>;
}
}