baseTheme.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import {
  2. EditorView,
  3. keymap,
  4. highlightSpecialChars,
  5. highlightActiveLine,
  6. } from "@codemirror/view"
  7. import {
  8. HighlightStyle,
  9. tags as t,
  10. defaultHighlightStyle,
  11. } from "@codemirror/highlight"
  12. import { foldKeymap, foldGutter } from "@codemirror/fold"
  13. import { Extension, EditorState } from "@codemirror/state"
  14. import { history, historyKeymap } from "@codemirror/history"
  15. import { indentOnInput } from "@codemirror/language"
  16. import { lineNumbers, highlightActiveLineGutter } from "@codemirror/gutter"
  17. import { defaultKeymap } from "@codemirror/commands"
  18. import { bracketMatching } from "@codemirror/matchbrackets"
  19. import { closeBrackets, closeBracketsKeymap } from "@codemirror/closebrackets"
  20. import { searchKeymap, highlightSelectionMatches } from "@codemirror/search"
  21. import { autocompletion, completionKeymap } from "@codemirror/autocomplete"
  22. import { commentKeymap } from "@codemirror/comment"
  23. import { rectangularSelection } from "@codemirror/rectangular-selection"
  24. import { lintKeymap } from "@codemirror/lint"
  25. export const baseTheme = EditorView.theme({
  26. "&": {
  27. fontSize: "var(--body-font-size)",
  28. },
  29. ".cm-content": {
  30. caretColor: "var(--secondary-light-color)",
  31. fontFamily: "var(--font-mono)",
  32. backgroundColor: "var(--primary-color)",
  33. },
  34. ".cm-cursor": {
  35. borderColor: "var(--secondary-color)",
  36. },
  37. ".cm-selectionBackground, .cm-content ::selection, .cm-line ::selection": {
  38. backgroundColor: "var(--divider-color)",
  39. },
  40. ".cm-panels": {
  41. backgroundColor: "var(--primary-light-color)",
  42. color: "var(--secondary-light-color)",
  43. },
  44. ".cm-panels.cm-panels-top": {
  45. borderBottom: "1px solid var(--divider-light-color)",
  46. },
  47. ".cm-panels.cm-panels-bottom": {
  48. borderTop: "1px solid var(--divider-light-color)",
  49. },
  50. ".cm-search": {
  51. display: "flex",
  52. alignItems: "center",
  53. flexWrap: "nowrap",
  54. flexShrink: 0,
  55. overflow: "auto",
  56. },
  57. ".cm-search label": {
  58. display: "inline-flex",
  59. alignItems: "center",
  60. },
  61. ".cm-textfield": {
  62. backgroundColor: "var(--primary-dark-color)",
  63. color: "var(--secondary-light-color)",
  64. borderColor: "var(--divider-light-color)",
  65. borderRadius: "3px",
  66. },
  67. ".cm-button": {
  68. backgroundColor: "var(--primary-dark-color)",
  69. color: "var(--secondary-light-color)",
  70. backgroundImage: "none",
  71. border: "none",
  72. },
  73. ".cm-tooltip": {
  74. backgroundColor: "var(--primary-dark-color)",
  75. color: "var(--secondary-light-color)",
  76. border: "none",
  77. borderRadius: "3px",
  78. },
  79. ".cm-completionLabel": {
  80. color: "var(--secondary-color)",
  81. },
  82. ".cm-tooltip.cm-tooltip-autocomplete > ul": {
  83. fontFamily: "var(--font-mono)",
  84. },
  85. ".cm-tooltip-autocomplete ul li[aria-selected]": {
  86. backgroundColor: "var(--accent-dark-color)",
  87. color: "var(--accent-contrast-color)",
  88. },
  89. ".cm-tooltip-autocomplete ul li[aria-selected] .cm-completionLabel": {
  90. color: "var(--accent-contrast-color)",
  91. },
  92. ".cm-activeLine": { backgroundColor: "var(--primary-light-color)" },
  93. ".cm-searchMatch": {
  94. outline: "1px solid var(--accent-dark-color)",
  95. },
  96. ".cm-selectionMatch": {
  97. outline: "1px solid var(--accent-dark-color)",
  98. },
  99. ".cm-matchingBracket, .cm-nonmatchingBracket": {
  100. backgroundColor: "var(--divider-color)",
  101. outline: "1px solid var(--accent-dark-color)",
  102. },
  103. ".cm-gutters": {
  104. fontFamily: "var(--font-mono)",
  105. backgroundColor: "var(--primary-color)",
  106. borderColor: "var(--divider-light-color)",
  107. },
  108. ".cm-lineNumbers": {
  109. minWidth: "3em",
  110. color: "var(--secondary-light-color)",
  111. },
  112. ".cm-foldGutter": {
  113. minWidth: "2em",
  114. color: "var(--secondary-light-color)",
  115. },
  116. ".cm-foldGutter .cm-gutterElement": {
  117. textAlign: "center",
  118. },
  119. ".cm-line": {
  120. paddingLeft: "0.5em",
  121. paddingRight: "0.5em",
  122. color: "var(--secondary-dark-color)",
  123. },
  124. ".cm-activeLineGutter": {
  125. backgroundColor: "var(--primary-dark-color)",
  126. },
  127. ".cm-scroller::-webkit-scrollbar": {
  128. display: "none",
  129. },
  130. })
  131. const editorTypeColor = "var(--editor-type-color)"
  132. const editorNameColor = "var(--editor-name-color)"
  133. const editorOperatorColor = "var(--editor-operator-color)"
  134. const editorInvalidColor = "var(--editor-invalid-color)"
  135. const editorSeparatorColor = "var(--editor-separator-color)"
  136. const editorMetaColor = "var(--editor-meta-color)"
  137. const editorVariableColor = "var(--editor-variable-color)"
  138. const editorLinkColor = "var(--editor-link-color)"
  139. const editorProcessColor = "var(--editor-process-color)"
  140. const editorConstantColor = "var(--editor-constant-color)"
  141. const editorKeywordColor = "var(--editor-keyword-color)"
  142. export const baseHighlightStyle = HighlightStyle.define([
  143. { tag: t.keyword, color: editorKeywordColor },
  144. {
  145. tag: [t.name, t.deleted, t.character, t.propertyName, t.macroName],
  146. color: editorNameColor,
  147. },
  148. {
  149. tag: [t.function(t.variableName), t.labelName],
  150. color: editorVariableColor,
  151. },
  152. {
  153. tag: [t.color, t.constant(t.name), t.standard(t.name)],
  154. color: editorConstantColor,
  155. },
  156. { tag: [t.definition(t.name), t.separator], color: editorSeparatorColor },
  157. {
  158. tag: [
  159. t.typeName,
  160. t.className,
  161. t.number,
  162. t.changed,
  163. t.annotation,
  164. t.modifier,
  165. t.self,
  166. t.namespace,
  167. ],
  168. color: editorTypeColor,
  169. },
  170. {
  171. tag: [
  172. t.operator,
  173. t.operatorKeyword,
  174. t.url,
  175. t.escape,
  176. t.regexp,
  177. t.link,
  178. t.special(t.string),
  179. ],
  180. color: editorOperatorColor,
  181. },
  182. { tag: [t.meta, t.comment], color: editorMetaColor },
  183. { tag: t.strong, fontWeight: "bold" },
  184. { tag: t.emphasis, fontStyle: "italic" },
  185. { tag: t.strikethrough, textDecoration: "line-through" },
  186. { tag: t.link, color: editorLinkColor, textDecoration: "underline" },
  187. { tag: t.heading, fontWeight: "bold", color: editorNameColor },
  188. {
  189. tag: [t.atom, t.bool, t.special(t.variableName)],
  190. color: editorConstantColor,
  191. },
  192. {
  193. tag: [t.processingInstruction, t.string, t.inserted],
  194. color: editorProcessColor,
  195. },
  196. { tag: t.invalid, color: editorInvalidColor },
  197. ])
  198. const baseFoldStyle = foldGutter({
  199. openText: "▾",
  200. closedText: "▸",
  201. })
  202. export const basicSetup: Extension = [
  203. lineNumbers(),
  204. highlightActiveLineGutter(),
  205. highlightSpecialChars(),
  206. history(),
  207. baseFoldStyle,
  208. EditorState.allowMultipleSelections.of(true),
  209. indentOnInput(),
  210. defaultHighlightStyle.fallback,
  211. bracketMatching(),
  212. closeBrackets(),
  213. autocompletion(),
  214. rectangularSelection(),
  215. highlightActiveLine(),
  216. highlightSelectionMatches(),
  217. keymap.of([
  218. ...closeBracketsKeymap,
  219. ...defaultKeymap,
  220. ...searchKeymap,
  221. ...historyKeymap,
  222. ...foldKeymap,
  223. ...commentKeymap,
  224. ...completionKeymap,
  225. ...lintKeymap,
  226. ]),
  227. ]