baseTheme.ts 6.8 KB

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