Skip to content

Commit c7eef3c

Browse files
committed
fix: improve support for sn-theme
also makes sn-theme the default theme. closes #2
1 parent 480d22b commit c7eef3c

File tree

2 files changed

+252
-17
lines changed

2 files changed

+252
-17
lines changed

src/components/Editor.tsx

Lines changed: 201 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ const initialState = {
6262
showSettings: true,
6363
tabSize: 2,
6464
text: '',
65-
theme: 'vs-dark',
65+
theme: 'sn-theme',
6666
wordWrap: 'on',
6767
};
6868

@@ -352,12 +352,18 @@ export default class Editor extends React.Component<{}, EditorInterface> {
352352
render() {
353353
return (
354354
<div
355-
className={HtmlElementId.snComponent + ' '}
355+
className={HtmlElementId.snComponent + ' ' + this.state.theme}
356356
id={HtmlElementId.snComponent}
357357
tabIndex={0}
358358
>
359359
{this.state.showEditor && (
360-
<div className={HtmlClassName.MonacoEditorContainerParentDiv}>
360+
<div
361+
className={
362+
HtmlClassName.MonacoEditorContainerParentDiv +
363+
' ' +
364+
this.state.theme
365+
}
366+
>
361367
<MonacoEditor
362368
fontSize={this.state.fontSize}
363369
language={this.state.language}
@@ -452,14 +458,203 @@ export const MonacoEditor: React.FC<MonacoEditorTypes> = ({
452458
let editor: monaco.editor.IStandaloneCodeEditor;
453459
useEffect(() => {
454460
if (divEl.current) {
455-
//let darkMode = window.matchMedia('(prefers-color-scheme: dark)').matches;
461+
const colorRegExp = /^#?([0-9A-Fa-f]{6})([0-9A-Fa-f]{2})?$/;
462+
const whiteSpaceRegExp = /\s+/g;
463+
464+
let backgroundColor: string;
465+
let tempBackgroundColor = getComputedStyle(document.documentElement)
466+
.getPropertyValue('--sn-stylekit-background-color')
467+
.replace(whiteSpaceRegExp, '');
468+
469+
if (!tempBackgroundColor.match(colorRegExp)) {
470+
console.error('Error parsing background color', tempBackgroundColor);
471+
backgroundColor = '#ffffff';
472+
} else {
473+
backgroundColor = tempBackgroundColor;
474+
}
475+
476+
let borderColor: string;
477+
let tempBorderColor = getComputedStyle(document.documentElement)
478+
.getPropertyValue('--sn-stylekit-border-color')
479+
.replace(whiteSpaceRegExp, '');
480+
if (!tempBorderColor.match(colorRegExp)) {
481+
console.error('Error parsing border color', tempBorderColor);
482+
borderColor = '#e3e3e3';
483+
} else {
484+
borderColor = tempBorderColor;
485+
}
486+
487+
let contrastBackgroundColor: string;
488+
let tempContrastBackgroundColor = getComputedStyle(
489+
document.documentElement
490+
)
491+
.getPropertyValue('--sn-stylekit-contrast-background-color')
492+
.replace(whiteSpaceRegExp, '');
493+
if (!tempContrastBackgroundColor.match(colorRegExp)) {
494+
console.error(
495+
'Error parsing contrast background color',
496+
tempContrastBackgroundColor
497+
);
498+
contrastBackgroundColor = '#F6F6F6';
499+
} else {
500+
contrastBackgroundColor = tempContrastBackgroundColor;
501+
}
502+
503+
let dangerColor: string;
504+
let tempDangerColor = getComputedStyle(document.documentElement)
505+
.getPropertyValue('--sn-stylekit-danger-color')
506+
.replace(whiteSpaceRegExp, '');
507+
if (!tempDangerColor.match(colorRegExp)) {
508+
console.error('Error parsing danger color', tempDangerColor);
509+
dangerColor = '#F80324'; // Red
510+
} else {
511+
dangerColor = tempDangerColor;
512+
}
513+
514+
let foregroundColor: string;
515+
let fadedForegroundColor: string;
516+
let fadedTwiceForegroundColor: string;
517+
let tempForegroundColor = getComputedStyle(document.documentElement)
518+
.getPropertyValue('--sn-stylekit-foreground-color')
519+
.replace(whiteSpaceRegExp, '');
520+
if (!tempForegroundColor.match(colorRegExp)) {
521+
console.error('Error parsing foreground color', tempForegroundColor);
522+
foregroundColor = '#000000';
523+
fadedForegroundColor = '#00000099'; /** 60 */
524+
fadedTwiceForegroundColor = '#0000001A'; /** 10 */
525+
} else {
526+
foregroundColor = tempForegroundColor;
527+
if (!foregroundColor.concat('99').match(colorRegExp)) {
528+
fadedForegroundColor = '#00000099'; /** 60 */
529+
fadedTwiceForegroundColor = '#0000001A'; /** 10 */
530+
} else {
531+
fadedForegroundColor = tempForegroundColor.concat('99'); /** 60% */
532+
fadedTwiceForegroundColor = tempForegroundColor.concat(
533+
'1A'
534+
); /** 10% */
535+
}
536+
}
537+
538+
let infoColor: string;
539+
let fadedInfoColor: string;
540+
let fadedTwiceInfoColor: string;
541+
let tempInfoColor = getComputedStyle(document.documentElement)
542+
.getPropertyValue('--sn-stylekit-info-color')
543+
.replace(whiteSpaceRegExp, '');
544+
if (!tempInfoColor.match(colorRegExp)) {
545+
console.error('Error parsing info color', tempInfoColor);
546+
infoColor = '#086dd6';
547+
fadedInfoColor = '#086dd666'; /** 40% */
548+
fadedTwiceInfoColor = '#086dd633'; /** 20% */
549+
} else {
550+
infoColor = tempInfoColor;
551+
/** You only need to test for one */
552+
if (!tempInfoColor.concat('66').match(colorRegExp)) {
553+
fadedInfoColor = '#086dd666';
554+
fadedTwiceInfoColor = '#086dd633';
555+
} else {
556+
fadedInfoColor = tempInfoColor.concat('66'); // This is 40% opacity
557+
fadedTwiceInfoColor = tempInfoColor.concat('33'); // This is 20% opacity
558+
}
559+
}
560+
561+
let shadowColor: string;
562+
let tempShadowColor = getComputedStyle(document.documentElement)
563+
.getPropertyValue('--sn-stylekit-shadow-color')
564+
.replace(whiteSpaceRegExp, '');
565+
if (!tempShadowColor.match(colorRegExp)) {
566+
console.error('Error parsing shadow color', tempShadowColor);
567+
shadowColor = '#C8C8C8'; // Gray shadow
568+
} else {
569+
shadowColor = tempShadowColor;
570+
}
571+
572+
let warningColor: string;
573+
let tempWarningColor = getComputedStyle(document.documentElement)
574+
.getPropertyValue('--sn-stylekit-warning-color')
575+
.replace(whiteSpaceRegExp, '');
576+
if (!tempWarningColor.match(colorRegExp)) {
577+
console.error('Error parsing warning color', tempWarningColor);
578+
warningColor = '#f6a200'; // Orange
579+
} else {
580+
warningColor = tempWarningColor;
581+
}
582+
583+
let darkMode = window.matchMedia('(prefers-color-scheme: dark)').matches;
584+
if (theme === 'sn-theme') {
585+
monaco.editor.defineTheme('sn-theme', {
586+
/** If sn-theme, then if not dark mode, use vs. Otherwise, use vs-dark (default) */
587+
base: !darkMode ? 'vs' : 'vs-dark',
588+
inherit: true,
589+
rules: [
590+
{
591+
token: '',
592+
background: backgroundColor,
593+
foreground: foregroundColor,
594+
},
595+
],
596+
colors: {
597+
/** Ordered in mostly alphabetical order */
598+
//foreground: foregroundColor /* Overall foreground color. This color is only used if not overridden by a component.
599+
600+
'button.background': backgroundColor, // Button background color.
601+
'button.foreground': foregroundColor, // Button foreground color.
602+
'button.hoverBackground': contrastBackgroundColor, // Button background color when hovering.
603+
604+
/** This is what you see when you right click */
605+
'dropdown.background': contrastBackgroundColor, // Dropdown background.
606+
'dropdown.border': borderColor, // Dropdown border.
607+
'dropdown.foreground': foregroundColor, // Dropdown foreground.,
608+
609+
'editor.background': backgroundColor, //
610+
'editor.foreground': foregroundColor, //
611+
'editor.inactiveSelectionBackground': fadedTwiceInfoColor, // Color of the selection in an inactive editor.
612+
'editor.lineHighlightBorder': fadedTwiceForegroundColor, // Background color for the border around the line at the cursor position.
613+
'editor.selectionBackground': fadedInfoColor, // Color of the editor selection.
614+
615+
'editorCursor.foreground': fadedForegroundColor, //Color of the editor cursor.
616+
'editorError.foreground': dangerColor, // Foreground color of error squigglies in the editor.
617+
618+
'editorLineNumber.foreground': fadedForegroundColor, // Color of editor line numbers.
619+
'editorLink.activeForeground': infoColor, // Color of active links. Such as when you press ctrl when hovering over a link
620+
621+
'editorWidget.background': contrastBackgroundColor, // Background color of editor widgets, such as find/replace.
622+
'editorWidget.border': borderColor, // Border color of editor widgets. The color is only used if the widget chooses to have a border and if the color is not overridden by a widget.
623+
'editorWarning.foreground': warningColor, // Foreground color of warning squigglies in the editor.
624+
625+
focusBorder: infoColor, // Overall border color for focused elements. This color is only used if not overridden by a component.
626+
627+
'input.background': backgroundColor, //,// Input box background.
628+
'input.border': borderColor, // Input box border.
629+
'input.foreground': foregroundColor, // Input box foreground.
630+
631+
'list.focusBackground': fadedInfoColor, // List/Tree background color for the focused item when the list/tree is active. An active list/tree has keyboard focus, an inactive does not.
632+
'list.focusForeground': foregroundColor, // List/Tree foreground color for the focused item when the list/tree is active. An active list/tree has keyboard focus, an inactive does not.
633+
'list.activeSelectionBackground': fadedInfoColor, // List/Tree background color for the selected item when the list/tree is active. An active list/tree has keyboard focus, an inactive does not.
634+
'list.activeSelectionForeground': foregroundColor, // List/Tree foreground color for the selected item when the list/tree is active. An active list/tree has keyboard focus, an inactive does not.
635+
'list.inactiveSelectionBackground': backgroundColor, // List/Tree background color for the selected item when the list/tree is inactive. An active list/tree has keyboard focus, an inactive does not.
636+
'list.inactiveSelectionForeground': foregroundColor, // List/Tree foreground color for the selected item when the list/tree is inactive. An active list/tree has keyboard focus, an inactive does not.
637+
'list.hoverBackground': backgroundColor, // List/Tree background when hovering over items using the mouse.
638+
'list.hoverForeground': foregroundColor, // List/Tree foreground when hovering over items using the mouse.
639+
'list.dropBackground': backgroundColor, // List/Tree drag and drop background when moving items around using the mouse.
640+
'list.highlightForeground': foregroundColor, // List/Tree foreground color of the match highlights when searching inside the list/tree.
641+
642+
'textLink.activeForeground': infoColor, // Foreground color for active links in text. // Not sure where this is used.
643+
'textLink.foreground': infoColor, // Foreground color for links in text (such as "Follow Link")
644+
645+
'widget.shadow': shadowColor, // Shadow color of widgets such as find/replace inside the editor.
646+
},
647+
});
648+
monaco.editor.setTheme(theme);
649+
}
650+
456651
editor = monaco.editor.create(divEl.current, {
457652
// These are variable: customizable by user or dependent on props
653+
//fontFamily: 'var(--sn-stylekit-monospace-font)',
458654
fontSize: parseInt(fontSize.replace('px', '')),
459655
language: language,
460656
tabSize: tabSize,
461-
/** if sn-theme, then if not dark mode, use vs. otherwise use vs-dark (default) */
462-
theme: theme === 'sn-theme' ? 'vs-dark' : theme,
657+
theme: theme,
463658
//@ts-ignore
464659
wordWrap: wordWrap,
465660

src/stylesheets/main.scss

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,27 @@ html {
3131
@media screen and (max-width: 420px) {
3232
min-height: -webkit-fill-available;
3333
}
34+
35+
&.vs {
36+
background-color: #fffffe;
37+
.settings {
38+
color: #000000;
39+
}
40+
}
41+
42+
&.vs-dark {
43+
background-color: #1e1e1e;
44+
.settings {
45+
color: #d4d4d4;
46+
}
47+
}
48+
49+
&.hc-black {
50+
background-color: #000000;
51+
.settings {
52+
color: #ffffff;
53+
}
54+
}
3455
}
3556

3657
/** Monaco */
@@ -135,30 +156,49 @@ html {
135156
}
136157
}
137158

138-
.sn-theme {
139-
/** This is the editing / input container */
140-
.monaco-editor,
141-
.monaco-editor-background,
142-
.monaco-editor .inputarea.ime-input {
143-
background-color: var(--sn-stylekit-background-color);
159+
.MonacoEditorContainer {
160+
/* This hides the shadow at the top of the editor */
161+
.monaco-editor .scroll-decoration {
162+
box-shadow: none;
144163
}
164+
}
145165

166+
.sn-theme {
146167
/* This is the line numbers and preview on the right */
147168
.monaco-editor .margin {
148169
background-color: var(--sn-stylekit-background-color);
149170
}
150171

172+
/* This is for the text in the list when you right click and press F1 */
173+
.monaco-editor .monaco-list-row,
174+
.monaco-editor .monaco-keybinding-key,
175+
.monaco-editor .find-widget,
151176
.mtk1 {
152177
color: var(--sn-stylekit-foreground-color);
153178
}
154179

155-
.monaco-editor .detected-link,
156-
.monaco-editor .detected-link-active,
157-
.monaco-hover .hover-contents a.code-link > span,
158-
.mtk5,
159-
.mtk20 {
180+
.monaco-editor .find-widget.no-results .matchesCount {
181+
color: var(--sn-stylekit-danger-color);
182+
}
183+
184+
/* This is for 'recently used' and 'other commands' */
185+
.monaco-editor .quick-input-list-separator {
160186
color: var(--sn-stylekit-info-color);
161187
}
188+
189+
@media (prefers-color-scheme: light) {
190+
/* This is for headers */
191+
.mtk6 {
192+
color: var(--sn-stylekit-success-color);
193+
}
194+
195+
/* This is for links */
196+
.mtk20,
197+
.mtk20.detected-link {
198+
color: var(--sn-stylekit-info-color);
199+
opacity: 0.9;
200+
}
201+
}
162202
}
163203
@import './print.scss';
164204
@import './dark.scss';

0 commit comments

Comments
 (0)