mirror of
https://github.com/livebook-dev/livebook.git
synced 2025-12-19 14:40:41 +08:00
High-contrast option for the code editor (#868)
* High-contrast option for the code editor * Small refactor * Creates a high-contrast theme from the custom theme * Refactor to store the theme as a string * Fix prettier
This commit is contained in:
parent
d5c6dfadcb
commit
67fa155f3d
7 changed files with 46 additions and 6 deletions
|
|
@ -4,6 +4,8 @@ import Markdown from "./markdown";
|
||||||
import { globalPubSub } from "../lib/pub_sub";
|
import { globalPubSub } from "../lib/pub_sub";
|
||||||
import { md5Base64, smoothlyScrollToElement } from "../lib/utils";
|
import { md5Base64, smoothlyScrollToElement } from "../lib/utils";
|
||||||
import scrollIntoView from "scroll-into-view-if-needed";
|
import scrollIntoView from "scroll-into-view-if-needed";
|
||||||
|
import { loadLocalSettings } from "../lib/settings";
|
||||||
|
import { THEME_BACKGROUND_COLOR } from "./live_editor/theme";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A hook managing a single cell.
|
* A hook managing a single cell.
|
||||||
|
|
@ -41,6 +43,10 @@ const Cell = {
|
||||||
// Create an empty container for the editor to be mounted in.
|
// Create an empty container for the editor to be mounted in.
|
||||||
const editorElement = document.createElement("div");
|
const editorElement = document.createElement("div");
|
||||||
editorContainer.appendChild(editorElement);
|
editorContainer.appendChild(editorElement);
|
||||||
|
// Adjust the background color based on local settings
|
||||||
|
const settings = loadLocalSettings();
|
||||||
|
editorContainer.parentElement.style.backgroundColor =
|
||||||
|
THEME_BACKGROUND_COLOR[settings.editor_theme];
|
||||||
// Setup the editor instance.
|
// Setup the editor instance.
|
||||||
this.state.liveEditor = new LiveEditor(
|
this.state.liveEditor = new LiveEditor(
|
||||||
this,
|
this,
|
||||||
|
|
|
||||||
|
|
@ -160,7 +160,7 @@ class LiveEditor {
|
||||||
},
|
},
|
||||||
occurrencesHighlight: false,
|
occurrencesHighlight: false,
|
||||||
renderLineHighlight: "none",
|
renderLineHighlight: "none",
|
||||||
theme: "custom",
|
theme: settings.editor_theme,
|
||||||
fontFamily: "JetBrains Mono, Droid Sans Mono, monospace",
|
fontFamily: "JetBrains Mono, Droid Sans Mono, monospace",
|
||||||
fontSize: settings.editor_font_size,
|
fontSize: settings.editor_font_size,
|
||||||
tabIndex: -1,
|
tabIndex: -1,
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import * as monaco from "monaco-editor/esm/vs/editor/editor.api";
|
import * as monaco from "monaco-editor/esm/vs/editor/editor.api";
|
||||||
import { CommandsRegistry } from "monaco-editor/esm/vs/platform/commands/common/commands";
|
import { CommandsRegistry } from "monaco-editor/esm/vs/platform/commands/common/commands";
|
||||||
import ElixirOnTypeFormattingEditProvider from "./elixir/on_type_formatting_edit_provider";
|
import ElixirOnTypeFormattingEditProvider from "./elixir/on_type_formatting_edit_provider";
|
||||||
import theme from "./theme";
|
import { theme, highContrast } from "./theme";
|
||||||
|
|
||||||
monaco.languages.registerOnTypeFormattingEditProvider(
|
monaco.languages.registerOnTypeFormattingEditProvider(
|
||||||
"elixir",
|
"elixir",
|
||||||
|
|
@ -9,7 +9,8 @@ monaco.languages.registerOnTypeFormattingEditProvider(
|
||||||
);
|
);
|
||||||
|
|
||||||
// Define custom theme
|
// Define custom theme
|
||||||
monaco.editor.defineTheme("custom", theme);
|
monaco.editor.defineTheme("default", theme);
|
||||||
|
monaco.editor.defineTheme("highContrast", highContrast);
|
||||||
|
|
||||||
// See https://github.com/microsoft/monaco-editor/issues/648#issuecomment-564978560
|
// See https://github.com/microsoft/monaco-editor/issues/648#issuecomment-564978560
|
||||||
// Without this selecting text with whitespace shrinks the whitespace.
|
// Without this selecting text with whitespace shrinks the whitespace.
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
// This is a port of the One Dark theme to the Monaco editor.
|
// This is a port of the One Dark theme to the Monaco editor.
|
||||||
|
|
||||||
const colors = {
|
const colors = {
|
||||||
default: "#abb2bf",
|
default: "#abb2bf",
|
||||||
lightRed: "#e06c75",
|
lightRed: "#e06c75",
|
||||||
|
|
@ -12,6 +11,8 @@ const colors = {
|
||||||
peach: "#d19a66",
|
peach: "#d19a66",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const THEME_BACKGROUND_COLOR = { default: "#282c34", highContrast: "#060708" };
|
||||||
|
|
||||||
const theme = {
|
const theme = {
|
||||||
base: "vs-dark",
|
base: "vs-dark",
|
||||||
inherit: false,
|
inherit: false,
|
||||||
|
|
@ -54,7 +55,7 @@ const theme = {
|
||||||
],
|
],
|
||||||
|
|
||||||
colors: {
|
colors: {
|
||||||
"editor.background": "#282c34",
|
"editor.background": THEME_BACKGROUND_COLOR.default,
|
||||||
"editor.foreground": colors.default,
|
"editor.foreground": colors.default,
|
||||||
"editorLineNumber.foreground": "#636d83",
|
"editorLineNumber.foreground": "#636d83",
|
||||||
"editorCursor.foreground": "#636d83",
|
"editorCursor.foreground": "#636d83",
|
||||||
|
|
@ -70,4 +71,12 @@ const theme = {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export default theme;
|
const highContrast = {
|
||||||
|
...theme,
|
||||||
|
colors: {
|
||||||
|
...theme.colors,
|
||||||
|
"editor.background": THEME_BACKGROUND_COLOR.highContrast,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export { theme, highContrast, THEME_BACKGROUND_COLOR };
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ import {
|
||||||
loadLocalSettings,
|
loadLocalSettings,
|
||||||
storeLocalSettings,
|
storeLocalSettings,
|
||||||
EDITOR_FONT_SIZE,
|
EDITOR_FONT_SIZE,
|
||||||
|
EDITOR_THEME,
|
||||||
} from "../lib/settings";
|
} from "../lib/settings";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -24,11 +25,16 @@ const EditorSettings = {
|
||||||
const editorFontSizeCheckbox = this.el.querySelector(
|
const editorFontSizeCheckbox = this.el.querySelector(
|
||||||
`[name="editor_font_size"][value="true"]`
|
`[name="editor_font_size"][value="true"]`
|
||||||
);
|
);
|
||||||
|
const editorHighContrastCheckbox = this.el.querySelector(
|
||||||
|
`[name="editor_high_contrast"][value="true"]`
|
||||||
|
);
|
||||||
|
|
||||||
editorAutoCompletionCheckbox.checked = settings.editor_auto_completion;
|
editorAutoCompletionCheckbox.checked = settings.editor_auto_completion;
|
||||||
editorAutoSignatureCheckbox.checked = settings.editor_auto_signature;
|
editorAutoSignatureCheckbox.checked = settings.editor_auto_signature;
|
||||||
editorFontSizeCheckbox.checked =
|
editorFontSizeCheckbox.checked =
|
||||||
settings.editor_font_size === EDITOR_FONT_SIZE.large ? true : false;
|
settings.editor_font_size === EDITOR_FONT_SIZE.large ? true : false;
|
||||||
|
editorHighContrastCheckbox.checked =
|
||||||
|
settings.editor_theme === EDITOR_THEME.highContrast ? true : false;
|
||||||
|
|
||||||
editorAutoCompletionCheckbox.addEventListener("change", (event) => {
|
editorAutoCompletionCheckbox.addEventListener("change", (event) => {
|
||||||
storeLocalSettings({ editor_auto_completion: event.target.checked });
|
storeLocalSettings({ editor_auto_completion: event.target.checked });
|
||||||
|
|
@ -45,6 +51,14 @@ const EditorSettings = {
|
||||||
: EDITOR_FONT_SIZE.normal,
|
: EDITOR_FONT_SIZE.normal,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
editorHighContrastCheckbox.addEventListener("change", (event) => {
|
||||||
|
storeLocalSettings({
|
||||||
|
editor_theme: event.target.checked
|
||||||
|
? EDITOR_THEME.highContrast
|
||||||
|
: EDITOR_THEME.default,
|
||||||
|
});
|
||||||
|
});
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,10 +5,16 @@ export const EDITOR_FONT_SIZE = {
|
||||||
large: 16,
|
large: 16,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const EDITOR_THEME = {
|
||||||
|
default: "default",
|
||||||
|
highContrast: "highContrast",
|
||||||
|
};
|
||||||
|
|
||||||
const DEFAULT_SETTINGS = {
|
const DEFAULT_SETTINGS = {
|
||||||
editor_auto_completion: true,
|
editor_auto_completion: true,
|
||||||
editor_auto_signature: true,
|
editor_auto_signature: true,
|
||||||
editor_font_size: EDITOR_FONT_SIZE.normal,
|
editor_font_size: EDITOR_FONT_SIZE.normal,
|
||||||
|
editor_theme: EDITOR_THEME.default,
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -115,6 +115,10 @@ defmodule LivebookWeb.SettingsLive do
|
||||||
name="editor_font_size"
|
name="editor_font_size"
|
||||||
label="Increase font size"
|
label="Increase font size"
|
||||||
checked={false} />
|
checked={false} />
|
||||||
|
<.switch_checkbox
|
||||||
|
name="editor_high_contrast"
|
||||||
|
label="High contrast editor"
|
||||||
|
checked={false} />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue