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

Refactor: Extract editor create options #26

Merged
merged 1 commit into from
Aug 31, 2021
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
51 changes: 3 additions & 48 deletions src/components/editor/create/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as monaco from "monaco-editor";
import { ensureEditorEnv } from "./env";
import { Editor } from "../state/state";
import { initVimMode, EditorVimMode } from "monaco-vim";
import { getEditorOptions } from "./options";

interface Options {
editor: HTMLDivElement;
Expand All @@ -14,57 +15,11 @@ interface Result {
editor: Editor;
}

export const getLeftPadding = (container: HTMLDivElement) => {
return Math.max((container.clientWidth - 1000) / 2, 24);
};

export const createEditor = (options: Options): Result => {
ensureEditorEnv();

const editor = monaco.editor.create(options.editor, {
value: "",
language: "markdown",

ariaLabel: "Main markdown editor",
codeLens: false,
contextmenu: false,
copyWithSyntaxHighlighting: false,
cursorBlinking: "smooth",
cursorSmoothCaretAnimation: true,
cursorSurroundingLines: 3,
cursorWidth: 3,
fontFamily: "iA Writer Duo",
fontLigatures: true,
fontWeight: "450",
glyphMargin: false,
lineDecorationsWidth: getLeftPadding(options.editor),
disableMonospaceOptimizations: true,
fontSize: 20,
lineHeight: 40,
lineNumbers: "off",
minimap: { enabled: false },
padding: { top: 150, bottom: 150 },
quickSuggestions: false,
roundedSelection: false,
selectionHighlight: false,
smoothScrolling: true,
snippetSuggestions: "none",
suggestOnTriggerCharacters: false,
wordBasedSuggestions: false,
wordWrap: "bounded",
scrollBeyondLastLine: false,
wordWrapColumn: 80,
folding: false,
occurrencesHighlight: false,
renderLineHighlight: "none",
hideCursorInOverviewRuler: true,
overviewRulerBorder: false,
scrollbar: {
useShadows: false,
horizontal: "hidden",
verticalSliderSize: 5,
},
});
const editorOptions = getEditorOptions(options.editor);
const editor = monaco.editor.create(options.editor, editorOptions);

const vimMode = options.vim ? initVimMode(editor, options.status) : null;

Expand Down
58 changes: 58 additions & 0 deletions src/components/editor/create/options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import * as monaco from "monaco-editor";

type EditorOptions = monaco.editor.IStandaloneEditorConstructionOptions;

export const getLeftPadding = (container: HTMLDivElement) => {
const freePadding = container.clientWidth - 1000;
const expected = Math.round(freePadding / 2);
return Math.max(expected, 24);
};

const EDITOR_STATIC_OPTIONS: EditorOptions = {
value: "",
language: "markdown",

ariaLabel: "Main markdown editor",
codeLens: false,
contextmenu: false,
copyWithSyntaxHighlighting: false,
cursorBlinking: "smooth",
cursorSmoothCaretAnimation: true,
cursorSurroundingLines: 3,
cursorWidth: 3,
fontFamily: "iA Writer Duo",
fontLigatures: true,
fontWeight: "450",
glyphMargin: false,
disableMonospaceOptimizations: true,
fontSize: 20,
lineHeight: 40,
lineNumbers: "off",
minimap: { enabled: false },
padding: { top: 150, bottom: 150 },
quickSuggestions: false,
roundedSelection: false,
selectionHighlight: false,
smoothScrolling: true,
snippetSuggestions: "none",
suggestOnTriggerCharacters: false,
wordBasedSuggestions: false,
wordWrap: "bounded",
scrollBeyondLastLine: false,
wordWrapColumn: 80,
folding: false,
occurrencesHighlight: false,
renderLineHighlight: "none",
hideCursorInOverviewRuler: true,
overviewRulerBorder: false,
scrollbar: {
useShadows: false,
horizontal: "hidden",
verticalSliderSize: 5,
},
};

export const getEditorOptions = (container: HTMLDivElement): EditorOptions => ({
...EDITOR_STATIC_OPTIONS,
lineDecorationsWidth: getLeftPadding(container),
});
3 changes: 2 additions & 1 deletion src/components/editor/editor.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as monaco from "monaco-editor";
import { RefObject, useEffect, useRef } from "react";
import { getRef } from "~/src/utils/ref";
import { createEditor, getLeftPadding } from "./create/create";
import { createEditor } from "./create/create";
import { getLeftPadding } from "./create/options";
import "./editor.css";
import s from "./editor.module.css";
import "./font/font.css";
Expand Down