@fsegurai/codemirror-theme-nord
Version:
Nord theme for the CodeMirror editor
511 lines (505 loc) • 17.3 kB
JavaScript
import { EditorView } from '@codemirror/view';
import { HighlightStyle, syntaxHighlighting } from '@codemirror/language';
import { tags } from '@lezer/highlight';
// Helper module for styling options
const generalContent = {
fontSize: '14px',
fontFamily: 'JetBrains Mono, Consolas, monospace',
lineHeight: '1.6',
};
const generalCursor = {
borderLeftWidth: '2px',
};
const generalDiff = {
insertedTextDecoration: 'none',
deletedTextDecoration: 'line-through',
insertedLinePadding: '1px 3px',
borderRadius: '3px'};
const generalGutter = {
border: 'none',
paddingRight: '8px',
fontSize: '0.9em',
fontWeight: '500',
lineHeight: '1.78', // Adjusted to compensate for 0.9em fontSize (1.6 / 0.9 ≈ 1.78)
};
const generalPanel = {
border: 'none',
borderRadius: '4px',
padding: '2px 10px',
};
const generalLine = {
borderRadius: '2px',
};
const generalMatching = {
borderRadius: '2px',
};
const generalPlaceholder = {
borderRadius: '4px',
padding: '0 5px',
margin: '0 2px',
};
const generalScroller = {
width: '12px',
height: '12px',
borderRadius: '6px',
};
const generalSearchField = {
borderRadius: '4px',
padding: '2px 6px',
};
const generalTooltip = {
borderRadius: '4px',
borderRadiusSelected: '3px',
lineHeight: '1.3',
padding: '4px 8px',
paddingRight: '8px',
};
/**
* Function to apply merge revert styles for a theme
* @param styles Styles for the merge revert buttons
* @param styles.backgroundColor Background color of the revert area
* @param styles.borderColor Border color of the revert area
* @param styles.buttonColor Color of the revert buttons
* @param styles.buttonHoverColor Hover color of the revert buttons
*/
function applyMergeRevertStyles(styles) {
// Create a stylesheet
const styleEl = document.createElement('style');
styleEl.id = 'cm-merge-revert-styles';
// Define CSS with the theme-specific values
styleEl.textContent = `
.cm-merge-revert {
display: flex;
flex-direction: column;
align-items: center;
justify-content: flex-start;
padding: 4px;
background-color: ${styles.backgroundColor};
border-left: 1px solid ${styles.borderColor};
border-right: 1px solid ${styles.borderColor};
width: 32px;
}
.cm-merge-revert button {
width: 100%;
height: auto;
background-color: transparent;
border: none;
color: ${styles.buttonColor};
cursor: pointer;
margin: 0 auto;
font-size: 20px;
}
.cm-merge-revert button:hover {
background-color: ${styles.buttonHoverColor};
}
`;
// Remove any existing merge styles
const existingStyle = document.getElementById('cm-merge-revert-styles');
if (existingStyle)
existingStyle.remove();
// Add the new styles
document.head.appendChild(styleEl);
}
/**
* Enhanced Nord theme color definitions
* ------------------------------------
* Colors organized by function with visual color blocks
*/
// Polar Night
const base00 = '#2e3440', // Background - deep navy blue
base01 = '#3b4252', // Lighter background (popups, statuslines)
base02 = '#434c5e', // Selection background
base03 = '#4c566a', // Comments, invisibles
// Snow Storm
base04 = '#d8dee9', // Foreground - light blue-grey
base05 = '#eceff4', // Light background
// Frost
base06 = '#8fbcbb', // Moss green - function names
base07 = '#88c0d0', // Ice blue - classes, attributes
base08 = '#81a1c1', // Water blue - methods
base09 = '#5e81ac', // Deep blue - keywords
// Aurora
base0A = '#bf616a', // Red - errors, brackets
base0B = '#d08770', // Orange - numbers, constants
base0C = '#ebcb8b', // Yellow - types, classes
base0D = '#a3be8c', // Green - strings
base0E = '#b48ead'; // Purple - operators, special characters
// UI specific colors
const invalid = '#d30102', // Bright red for errors
darkBackground = '#252a33', // Darker background for panels
highlightBackground = '#3b425277', // Active line highlight with opacity
background = base00, // Main editor background
tooltipBackground = base01, // Tooltip background
selection = base02, // Selection background
selectionMatch = '#4c566a80', // Selection match with opacity
cursor = base04, // Cursor color
activeBracketBg = '#4c566a55', // Active bracket background with opacity
activeBracketBorder = base07, // Active bracket border - ice blue
diagnosticWarning = base0C, // Warning color - yellow
linkColor = base08, // Link color - water blue
visitedLinkColor = base0E; // Visited link color - purple
// Diff/merge specific colors
const addedBackground = '#3b4a3880', // Dark green with transparency for insertions
removedBackground = '#4a393a80', // Dark red with transparency for deletions
addedText = '#a3be8c', // Nord green for added text
removedText = '#bf616a'; // Nord red for removed text
/**
* Enhanced editor theme styles for Nord
*/
const nordTheme = /*@__PURE__*/EditorView.theme({
// Base editor styles
'&': {
color: base04,
backgroundColor: background,
fontSize: generalContent.fontSize,
fontFamily: generalContent.fontFamily,
},
// Content and cursor
'.cm-content': {
caretColor: cursor,
lineHeight: generalContent.lineHeight,
},
'.cm-cursor, .cm-dropCursor': {
borderLeftColor: cursor,
borderLeftWidth: generalCursor.borderLeftWidth,
},
'.cm-fat-cursor': {
backgroundColor: `${cursor}99`,
color: background,
},
// Selection
'&.cm-focused > .cm-scroller > .cm-selectionLayer .cm-selectionBackground, .cm-selectionBackground, .cm-content ::selection': {
backgroundColor: selection,
},
// Make sure selection appears above active line
'.cm-selectionLayer': {
zIndex: 100,
},
// Search functionality
'.cm-searchMatch': {
backgroundColor: '#5e81ac80',
outline: `1px solid ${base06}`,
color: base04,
borderRadius: generalSearchField.borderRadius,
'& span': {
color: base04,
},
},
'.cm-searchMatch.cm-searchMatch-selected': {
backgroundColor: base08,
color: base05,
padding: generalSearchField.padding,
'& span': {
color: base05,
},
},
'.cm-search.cm-panel.cm-textfield': {
color: base04,
borderRadius: generalSearchField.borderRadius,
padding: generalSearchField.padding,
},
// Panels
'.cm-panels': {
backgroundColor: darkBackground,
color: base04,
borderRadius: '4px',
},
'.cm-panels.cm-panels-top': {
borderBottom: `1px solid ${base03}`,
},
'.cm-panels.cm-panels-bottom': {
borderTop: `1px solid ${base03}`,
},
'.cm-panel button': {
backgroundColor: tooltipBackground,
color: base04,
border: generalPanel.border,
borderRadius: generalPanel.borderRadius,
padding: generalPanel.padding,
},
'.cm-panel button:hover': {
backgroundColor: base02,
},
// Line highlighting
'.cm-activeLine': {
backgroundColor: highlightBackground,
borderRadius: generalLine.borderRadius,
zIndex: 1,
},
// Gutters
'.cm-gutters': {
backgroundColor: darkBackground,
color: base03,
border: generalGutter.border,
borderRight: `1px solid ${base02}`,
paddingRight: generalGutter.paddingRight,
},
'.cm-activeLineGutter': {
backgroundColor: base01,
color: base04,
fontWeight: generalGutter.fontWeight,
},
'.cm-lineNumbers': {
fontSize: generalGutter.fontSize,
lineHeight: generalGutter.lineHeight,
},
'.cm-foldGutter': {
fontSize: generalGutter.fontSize,
lineHeight: generalGutter.lineHeight,
},
'.cm-foldGutter .cm-gutterElement': {
color: base03,
cursor: 'pointer',
},
'.cm-foldGutter .cm-gutterElement:hover': {
color: base04,
},
// Diff/Merge View Styles
// Inserted/Added Content
'.cm-insertedLine': {
textDecoration: generalDiff.insertedTextDecoration,
backgroundColor: addedBackground,
color: addedText,
padding: generalDiff.insertedLinePadding,
borderRadius: generalDiff.borderRadius,
},
'ins.cm-insertedLine, ins.cm-insertedLine:not(:has(.cm-changedText))': {
textDecoration: generalDiff.insertedTextDecoration,
backgroundColor: `${addedBackground} !important`,
color: addedText,
padding: generalDiff.insertedLinePadding,
borderRadius: generalDiff.borderRadius,
border: `1px solid ${addedText}40`,
},
'ins.cm-insertedLine .cm-changedText': {
background: 'transparent !important',
},
// Deleted/Removed Content
'.cm-deletedLine': {
textDecoration: generalDiff.deletedTextDecoration,
backgroundColor: removedBackground,
color: removedText,
padding: generalDiff.insertedLinePadding,
borderRadius: generalDiff.borderRadius,
},
'del.cm-deletedLine, del, del:not(:has(.cm-deletedText))': {
textDecoration: generalDiff.deletedTextDecoration,
backgroundColor: `${removedBackground} !important`,
color: removedText,
padding: generalDiff.insertedLinePadding,
borderRadius: generalDiff.borderRadius,
border: `1px solid ${removedText}40`,
},
'del .cm-deletedText, del .cm-changedText': {
background: 'transparent !important',
},
// Tooltips and autocomplete
'.cm-tooltip': {
backgroundColor: tooltipBackground,
border: `1px solid ${base03}`,
borderRadius: generalTooltip.borderRadius,
padding: generalTooltip.padding,
boxShadow: '0 2px 8px rgba(0, 0, 0, 0.3)',
},
'.cm-tooltip-autocomplete': {
'& > ul': {
backgroundColor: tooltipBackground,
border: 'none',
},
'& > ul > li': {
padding: generalTooltip.padding,
lineHeight: generalTooltip.lineHeight,
},
'& > ul > li[aria-selected]': {
backgroundColor: base02,
color: base05,
borderRadius: generalTooltip.borderRadiusSelected,
},
'& > ul > li > span.cm-completionIcon': {
color: base03,
paddingRight: generalTooltip.paddingRight,
},
'& > ul > li > span.cm-completionDetail': {
color: base03,
fontStyle: 'italic',
},
},
'.cm-tooltip .cm-tooltip-arrow:before': {
borderTopColor: 'transparent',
borderBottomColor: 'transparent',
},
'.cm-tooltip .cm-tooltip-arrow:after': {
borderTopColor: tooltipBackground,
borderBottomColor: tooltipBackground,
},
// Diagnostics styling
'.cm-diagnostic': {
'&-error': {
borderLeft: `3px solid ${base0A}`,
},
'&-warning': {
borderLeft: `3px solid ${diagnosticWarning}`,
},
'&-info': {
borderLeft: `3px solid ${linkColor}`,
},
},
'.cm-lintPoint-error': {
borderBottom: `2px wavy ${base0A}`,
},
'.cm-lintPoint-warning': {
borderBottom: `2px wavy ${diagnosticWarning}`,
},
// Matching brackets
'.cm-matchingBracket': {
backgroundColor: activeBracketBg,
outline: `1px solid ${activeBracketBorder}`,
borderRadius: generalMatching.borderRadius,
},
'.cm-nonmatchingBracket': {
backgroundColor: `${base0A}40`,
outline: `1px solid ${invalid}`,
borderRadius: generalMatching.borderRadius,
},
// Selection matches
'.cm-selectionMatch': {
backgroundColor: selectionMatch,
outline: `1px solid ${base02}50`,
borderRadius: generalMatching.borderRadius,
},
// Fold placeholder
'.cm-foldPlaceholder': {
backgroundColor: tooltipBackground,
color: base03,
border: `1px dotted ${base03}70`,
borderRadius: generalPlaceholder.borderRadius,
padding: generalPlaceholder.padding,
margin: generalPlaceholder.margin,
},
// Focus outline
'&.cm-focused': {
outline: 'none',
boxShadow: `0 0 0 2px ${background}, 0 0 0 3px ${base07}40`,
},
// Scrollbars
'& .cm-scroller::-webkit-scrollbar': {
width: generalScroller.width,
height: generalScroller.height,
},
'& .cm-scroller::-webkit-scrollbar-track': {
background: darkBackground,
},
'& .cm-scroller::-webkit-scrollbar-thumb': {
backgroundColor: base02,
borderRadius: generalScroller.borderRadius,
border: `3px solid ${darkBackground}`,
},
'& .cm-scroller::-webkit-scrollbar-thumb:hover': {
backgroundColor: base03,
},
// Ghost text
'.cm-ghostText': {
opacity: '0.5',
color: base03,
},
}, { dark: true });
/**
* Enhanced syntax highlighting for Nord theme
*/
const nordHighlightStyle = /*@__PURE__*/HighlightStyle.define([
// Keywords and control flow
{ tag: tags.keyword, color: base09, fontWeight: 'bold' },
{ tag: tags.controlKeyword, color: base09, fontWeight: 'bold' },
{ tag: tags.moduleKeyword, color: base09, fontWeight: 'bold' },
// Names and variables
{ tag: [tags.name, tags.deleted, tags.character, tags.macroName], color: base07 },
{ tag: [tags.variableName], color: base04 },
{ tag: [tags.propertyName], color: base07, fontStyle: 'normal' },
// Classes and types
{ tag: [tags.typeName], color: base0C },
{ tag: [tags.className], color: base0C, fontStyle: 'italic' },
{ tag: [tags.namespace], color: base08, fontStyle: 'italic' },
// Operators and punctuation
{ tag: [tags.operator, tags.operatorKeyword], color: base0E },
{ tag: [tags.bracket], color: base04 },
{ tag: [tags.brace], color: base06 },
{ tag: [tags.punctuation], color: base04 },
// Functions and parameters
{ tag: [/*@__PURE__*/tags.function(tags.variableName), tags.labelName], color: base06 },
{ tag: [/*@__PURE__*/tags.definition(/*@__PURE__*/tags.function(tags.variableName))], color: base06 },
{ tag: [/*@__PURE__*/tags.definition(tags.variableName)], color: base0B },
// Constants and literals
{ tag: tags.number, color: base0B },
{ tag: tags.changed, color: base0E },
{ tag: tags.annotation, color: base0A, fontStyle: 'italic' },
{ tag: tags.modifier, color: base0E, fontStyle: 'italic' },
{ tag: tags.self, color: base0E },
{ tag: [tags.color, /*@__PURE__*/tags.constant(tags.name), /*@__PURE__*/tags.standard(tags.name)], color: base0B },
{ tag: [tags.atom, tags.bool, /*@__PURE__*/tags.special(tags.variableName)], color: base0E },
// Strings and regex
{ tag: [tags.processingInstruction, tags.inserted], color: base06 },
{ tag: [/*@__PURE__*/tags.special(tags.string), tags.regexp], color: base0E },
{ tag: tags.string, color: base0D },
// Punctuation and structure
{ tag: /*@__PURE__*/tags.definition(tags.typeName), color: base0C, fontWeight: 'bold' },
{ tag: [/*@__PURE__*/tags.definition(tags.name), tags.separator], color: base0D },
// Comments and documentation
{ tag: tags.meta, color: base03 },
{ tag: tags.comment, fontStyle: 'italic', color: base03 },
{ tag: tags.docComment, fontStyle: 'italic', color: base03 },
// HTML/XML elements
{ tag: [tags.tagName], color: base0A },
{ tag: [tags.attributeName], color: base0C },
// Markdown and text formatting
{ tag: [tags.heading], fontWeight: 'bold', color: base08 },
{ tag: [tags.strong], fontWeight: 'bold', color: base07 },
{ tag: [tags.emphasis], fontStyle: 'italic', color: base0C },
// Links and URLs
{
tag: [tags.link],
color: visitedLinkColor,
fontWeight: '500',
textDecoration: 'underline',
textUnderlinePosition: 'under',
},
{
tag: [tags.url],
color: linkColor,
textDecoration: 'underline',
textUnderlineOffset: '2px',
},
// Special states
{
tag: [tags.invalid],
color: base04,
textDecoration: 'underline wavy',
borderBottom: `1px wavy ${base0A}`,
},
{ tag: [tags.strikethrough], color: base0A, textDecoration: 'line-through' },
// Enhanced syntax highlighting
{ tag: /*@__PURE__*/tags.constant(tags.name), color: base0B },
{ tag: tags.deleted, color: base0A },
{ tag: tags.squareBracket, color: base0A },
{ tag: tags.angleBracket, color: base0B },
// Additional specific styles
{ tag: tags.monospace, color: base04, fontStyle: 'italic' },
{ tag: [tags.contentSeparator], color: base08 },
{ tag: tags.quote, color: base0E },
]);
/**
* Combined Nord theme extension
*/
const nord = [
nordTheme,
/*@__PURE__*/syntaxHighlighting(nordHighlightStyle),
];
/**
* Nord merge revert styles configuration
*/
const nordMergeStyles = {
backgroundColor: darkBackground,
borderColor: base03,
buttonColor: base04,
buttonHoverColor: base02,
};
export { applyMergeRevertStyles, nord, nordMergeStyles };