Skip to content

Commit

Permalink
Reduce jitter in note editor (#41)
Browse files Browse the repository at this point in the history
* Fix eslint

* Reduce number of re-renders and set models on monaco
  • Loading branch information
EddieAbbondanzio authored Mar 25, 2023
1 parent db8efb9 commit 16a89df
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 46 deletions.
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,
);
}

0 comments on commit 16a89df

Please sign in to comment.