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

add Nord editor theme #2887

Merged
merged 2 commits into from
May 6, 2019
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
31 changes: 14 additions & 17 deletions browser/components/MarkdownPreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -625,14 +625,14 @@ export default class MarkdownPreview extends React.Component {
)
}

GetCodeThemeLink (theme) {
theme = consts.THEMES.some(_theme => _theme === theme) &&
theme !== 'default'
? theme
: 'elegant'
return theme.startsWith('solarized')
? `${appPath}/node_modules/codemirror/theme/solarized.css`
: `${appPath}/node_modules/codemirror/theme/${theme}.css`
GetCodeThemeLink (name) {
const theme = consts.THEMES.find(theme => theme.name === name)

if (theme) {
return `${appPath}/${theme.path}`
} else {
return `${appPath}/node_modules/codemirror/theme/elegant.css`
}
}

rewriteIframe () {
Expand Down Expand Up @@ -690,9 +690,9 @@ export default class MarkdownPreview extends React.Component {
}
)

codeBlockTheme = consts.THEMES.some(_theme => _theme === codeBlockTheme)
? codeBlockTheme
: 'default'
codeBlockTheme = consts.THEMES.find(theme => theme.name === codeBlockTheme)

const codeBlockThemeClassName = codeBlockTheme ? codeBlockTheme.className : 'cm-s-default'

_.forEach(
this.refs.root.contentWindow.document.querySelectorAll('.code code'),
Expand All @@ -713,14 +713,11 @@ export default class MarkdownPreview extends React.Component {
})
}
}

el.parentNode.appendChild(copyIcon)
el.innerHTML = ''
if (codeBlockTheme.indexOf('solarized') === 0) {
const [refThema, color] = codeBlockTheme.split(' ')
el.parentNode.className += ` cm-s-${refThema} cm-s-${color}`
} else {
el.parentNode.className += ` cm-s-${codeBlockTheme}`
}
el.parentNode.className += ` ${codeBlockThemeClassName}`

CodeMirror.runMode(content, syntax.mime, el, {
tabSize: indentSize
})
Expand Down
47 changes: 38 additions & 9 deletions browser/lib/consts.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,43 @@ const fs = require('sander')
const { remote } = require('electron')
const { app } = remote

const themePath = process.env.NODE_ENV === 'production'
? path.join(app.getAppPath(), './node_modules/codemirror/theme')
: require('path').resolve('./node_modules/codemirror/theme')
const themes = fs.readdirSync(themePath)
.map((themePath) => {
return themePath.substring(0, themePath.lastIndexOf('.'))
})
themes.splice(themes.indexOf('solarized'), 1, 'solarized dark', 'solarized light')
const CODEMIRROR_THEME_PATH = 'node_modules/codemirror/theme'
const CODEMIRROR_EXTRA_THEME_PATH = 'extra_scripts/codemirror/theme'

const isProduction = process.env.NODE_ENV === 'production'
const paths = [
isProduction ? path.join(app.getAppPath(), CODEMIRROR_THEME_PATH) : path.resolve(CODEMIRROR_THEME_PATH),
isProduction ? path.join(app.getAppPath(), CODEMIRROR_EXTRA_THEME_PATH) : path.resolve(CODEMIRROR_EXTRA_THEME_PATH)
]

const themes = paths
.map(directory => fs.readdirSync(directory).map(file => {
const name = file.substring(0, file.lastIndexOf('.'))

return {
name,
path: path.join(directory.split(/\//g).slice(-3).join('/'), file),
className: `cm-s-${name}`
}
}))
.reduce((accumulator, value) => accumulator.concat(value), [])
.sort((a, b) => a.name.localeCompare(b.name))

themes.splice(themes.findIndex(({ name }) => name === 'solarized'), 1, {
name: 'solarized dark',
path: `${CODEMIRROR_THEME_PATH}/solarized.css`,
className: `cm-s-solarized cm-s-dark`
}, {
name: 'solarized light',
path: `${CODEMIRROR_THEME_PATH}/solarized.css`,
className: `cm-s-solarized cm-s-light`
})

themes.splice(0, 0, {
name: 'default',
path: `${CODEMIRROR_THEME_PATH}/elegant.css`,
className: `cm-s-default`
})

const snippetFile = process.env.NODE_ENV !== 'test'
? path.join(app.getPath('userData'), 'snippets.json')
Expand All @@ -35,7 +64,7 @@ const consts = {
'Dodger Blue',
'Violet Eggplant'
],
THEMES: ['default'].concat(themes),
THEMES: themes,
SNIPPET_FILE: snippetFile,
DEFAULT_EDITOR_FONT_FAMILY: [
'Monaco',
Expand Down
29 changes: 10 additions & 19 deletions browser/main/lib/ConfigManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,12 @@ function get () {
document.head.appendChild(editorTheme)
}

config.editor.theme = consts.THEMES.some((theme) => theme === config.editor.theme)
? config.editor.theme
: 'default'

if (config.editor.theme !== 'default') {
if (config.editor.theme.startsWith('solarized')) {
editorTheme.setAttribute('href', '../node_modules/codemirror/theme/solarized.css')
} else {
editorTheme.setAttribute('href', '../node_modules/codemirror/theme/' + config.editor.theme + '.css')
}
const theme = consts.THEMES.find(theme => theme.name === config.editor.theme)

if (theme) {
editorTheme.setAttribute('href', `../${theme.path}`)
} else {
config.editor.theme = 'default'
}
}

Expand Down Expand Up @@ -177,16 +173,11 @@ function set (updates) {
editorTheme.setAttribute('rel', 'stylesheet')
document.head.appendChild(editorTheme)
}
const newTheme = consts.THEMES.some((theme) => theme === newConfig.editor.theme)
? newConfig.editor.theme
: 'default'

if (newTheme !== 'default') {
if (newTheme.startsWith('solarized')) {
editorTheme.setAttribute('href', '../node_modules/codemirror/theme/solarized.css')
} else {
editorTheme.setAttribute('href', '../node_modules/codemirror/theme/' + newTheme + '.css')
}
const newTheme = consts.THEMES.find(theme => theme.name === newConfig.editor.theme)

if (newTheme) {
editorTheme.setAttribute('href', `../${newTheme.path}`)
}

ipcRenderer.send('config-renew', {
Expand Down
11 changes: 8 additions & 3 deletions browser/main/modals/PreferencesModal/UiTab.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,13 @@ class UiTab extends React.Component {
const newCodemirrorTheme = this.refs.editorTheme.value

if (newCodemirrorTheme !== codemirrorTheme) {
checkHighLight.setAttribute('href', `../node_modules/codemirror/theme/${newCodemirrorTheme.split(' ')[0]}.css`)
const theme = consts.THEMES.find(theme => theme.name === newCodemirrorTheme)

if (theme) {
checkHighLight.setAttribute('href', `../${theme.path}`)
}
}

this.setState({ config: newConfig, codemirrorTheme: newCodemirrorTheme }, () => {
const {ui, editor, preview} = this.props.config
this.currentConfig = {ui, editor, preview}
Expand Down Expand Up @@ -355,7 +360,7 @@ class UiTab extends React.Component {
>
{
themes.map((theme) => {
return (<option value={theme} key={theme}>{theme}</option>)
return (<option value={theme.name} key={theme.name}>{theme.name}</option>)
})
}
</select>
Expand Down Expand Up @@ -670,7 +675,7 @@ class UiTab extends React.Component {
>
{
themes.map((theme) => {
return (<option value={theme} key={theme}>{theme}</option>)
return (<option value={theme.name} key={theme.name}>{theme.name}</option>)
})
}
</select>
Expand Down
25 changes: 25 additions & 0 deletions extra_scripts/codemirror/theme/nord.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* Theme: nord */

.cm-s-nord.CodeMirror { color: #d8dee9; }
.cm-s-nord.CodeMirror { background: #2e3440; }
.cm-s-nord .CodeMirror-cursor { color: #d8dee9; border-color: #d8dee9; }
.cm-s-nord .CodeMirror-activeline-background { background: #434c5e52 !important; }
.cm-s-nord .CodeMirror-selected { background: undefined; }
.cm-s-nord .cm-comment { color: #4c566a; }
.cm-s-nord .cm-string { color: #a3be8c; }
.cm-s-nord .cm-string-2 { color: #8fbcbb; }
.cm-s-nord .cm-property { color: #8fbcbb; }
.cm-s-nord .cm-qualifier { color: #8fbcbb; }
.cm-s-nord .cm-tag { color: #81a1c1; }
.cm-s-nord .cm-attribute { color: #8fbcbb; }
.cm-s-nord .cm-number { color: #b48ead; }
.cm-s-nord .cm-keyword { color: #81a1c1; }
.cm-s-nord .cm-operator { color: #81a1c1; }
.cm-s-nord .cm-error { background: #bf616a; color: #d8dee9; }
.cm-s-nord .cm-invalidchar { background: #bf616a; color: #d8dee9; }
.cm-s-nord .cm-variable { color: #d8dee9; }
.cm-s-nord .cm-variable-2 { color: #8fbcbb; }
.cm-s-nord .CodeMirror-gutters {
background: #3b4252;
color: #d8dee9;
}