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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce jitter in note editor #41

Merged
merged 2 commits into from
Mar 25, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions packages/marqus-desktop/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module.exports = {
env: {
browser: true,
es6: true,
jest: true,
},
rules: {
indent: ["error", 2, { SwitchCase: 1 }],
"no-console": ["warn", { allow: ["warn", "error"] }],
"react/prop-types": "off",
"@typescript-eslint/no-non-null-assertion": "off",
"no-prototype-builtins": "off",
},
extends: [
"eslint:recommended",
"plugin:react/recommended",
"plugin:@typescript-eslint/recommended",
"plugin:react-hooks/recommended",
],
ignorePatterns: ["jest.config.js", "webpack.*.js", ".eslintrc.js"],
parser: "@typescript-eslint/parser",
root: true,
plugins: ["react", "@typescript-eslint"],
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
settings: {
react: {
pragma: "React",
version: "detect",
},
},
};
35 changes: 0 additions & 35 deletions packages/marqus-desktop/.eslintrc.json

This file was deleted.

36 changes: 25 additions & 11 deletions packages/marqus-desktop/src/renderer/components/Monaco.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,18 @@ export function Monaco(props: MonacoProps): JSX.Element {

if (monacoEditor.current != null) {
monacoEditor.current.dispose();
monacoEditor.current = null;

// N.B. Disposing monaco editor will also dispose the active model so
// we remove it from the cache.
if (activeNoteId.current != null) {
props.removeCache(activeNoteId.current);
}
}

if (onChangeSub.current != null) {
onChangeSub.current.dispose();
onChangeSub.current = null;
}

if (el != null) {
Expand Down Expand Up @@ -251,8 +259,13 @@ export function Monaco(props: MonacoProps): JSX.Element {
}
}

// Load new tab
if (editor.activeTabNoteId != null) {
// Load new model when switching to a new tab (either no previous tab, or the
// new active tab is different).
if (
editor.activeTabNoteId != null &&
(lastActiveTabNoteId == null ||
lastActiveTabNoteId !== editor.activeTabNoteId)
) {
const newTab = editor.tabs.find(
t => t.note.id === editor.activeTabNoteId,
);
Expand All @@ -262,19 +275,20 @@ export function Monaco(props: MonacoProps): JSX.Element {

let cache = props.modelAndViewStateCache[newTab.note.id];
// First load, gotta create the model.
if (cache == null) {
if (cache == null || cache.model.isDisposed()) {
cache = {
model: createMarkdownModel(newTab.note.content),
}!;
}

monacoEditor.current.setModel(cache.model);
props.updateCache(newTab.note.id, cache);
monacoEditor.current.setModel(cache.model);

if (cache.viewState) {
monacoEditor.current.restoreViewState(cache.viewState);
}
if (state.focused[0] === Section.Editor) {
monacoEditor.current.focus();
if (cache.viewState) {
monacoEditor.current.restoreViewState(cache.viewState);
}
if (state.focused[0] === Section.Editor) {
monacoEditor.current.focus();
}
}

activeNoteId.current = newTab.note.id;
Expand Down Expand Up @@ -394,6 +408,6 @@ export function disableKeybinding(
_standaloneKeybindingService.addDynamicKeybinding(
`-${commandId}`,
undefined,
() => {},
() => void undefined,
);
}