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

How to style the editor outside the createTheme properties in CodeMirror? #627

Closed
lancejpollard opened this issue Feb 2, 2024 · 3 comments

Comments

@lancejpollard
Copy link

I want to change some structural things of the editor as defined here https://codemirror.net/examples/styling/ , how do I get access to the EditorView to create a theme like:

let myTheme = EditorView.theme({
  "&": {
    color: "white",
    backgroundColor: "#034"
  },
  ".cm-content": {
    caretColor: "#0e9"
  },
  "&.cm-focused .cm-cursor": {
    borderLeftColor: "#0e9"
  },
  "&.cm-focused .cm-selectionBackground, ::selection": {
    backgroundColor: "#074"
  },
  ".cm-gutters": {
    backgroundColor: "#045",
    color: "#ddd",
    border: "none"
  }
}, {dark: true})

Yet at the same time create a theme using your system:

import { tags as t } from '@lezer/highlight'
import { createTheme, CreateThemeOptions } from '@uiw/codemirror-themes'

export const defaultSettingsDracula: CreateThemeOptions['settings'] = {
  background: '#282a36',
  foreground: '#f8f8f2',
  caret: '#f8f8f0',
  selection: 'rgba(255, 255, 255, 0.1)',
  selectionMatch: 'rgba(255, 255, 255, 0.2)',
  gutterBackground: '#282a36',
  gutterForeground: '#6D8A88',
  gutterBorder: 'transparent',
  lineHighlight: 'rgba(255, 255, 255, 0.1)',
}

export const draculaInit = (options?: Partial<CreateThemeOptions>) => {
  const { theme = 'dark', settings = {}, styles = [] } = options || {}
  return createTheme({
    theme: theme,
    settings: {
      ...defaultSettingsDracula,
      ...settings,
    },
    styles: [
      { tag: t.comment, color: '#6272a4' },
      { tag: t.string, color: '#f1fa8c' },
      { tag: t.atom, color: '#bd93f9' },
      { tag: t.meta, color: '#f8f8f2' },
      { tag: [t.keyword, t.operator, t.tagName], color: '#ff79c6' },
      {
        tag: [t.function(t.propertyName), t.propertyName],
        color: '#66d9ef',
      },
      {
        tag: [
          t.definition(t.variableName),
          t.function(t.variableName),
          t.className,
          t.attributeName,
        ],
        color: '#50fa7b',
      },
      { tag: t.atom, color: '#bd93f9' },
      ...styles,
    ],
  })
}

export const purpleTheme = draculaInit()

How do you combine these two?

@lancejpollard
Copy link
Author

const themeExtension = EditorView.theme(themeOptions, {

@lancejpollard
Copy link
Author

Now the syntax highlighting doesn't appear to work.

Here I have JSON specified:

import { json } from '@codemirror/lang-json'

    <CodeMirror
      value={value}
      height={height}
      basicSetup={{
        lineNumbers: false,
        defaultKeymap: false,
        searchKeymap: false,
        historyKeymap: false,
        foldKeymap: false,
        completionKeymap: false,
        lintKeymap: false,
        syntaxHighlighting: true,
      }}
      theme={purple}
      extensions={[json()]}
    />

And I'm not seeing any syntax classes added to the DOM:

Screenshot 2024-02-02 at 2 50 59 AM

Any ideas what could be wrong? Just got a basic setup going with Next.js, the color overrides for the background and line highlights is working, just not the syntax highlighting.

@lancejpollard
Copy link
Author

lancejpollard commented Feb 2, 2024

I ran pnpm dedupe and now syntax highlighting is working! Strange .... I just installed everything so I don't see how I would have duplicate libs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant