visual-theme.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. import { EditorView } from '@codemirror/view'
  2. import { HighlightStyle, syntaxHighlighting } from '@codemirror/language'
  3. import { tags } from '@lezer/highlight'
  4. /**
  5. * A syntax highlighter for content types that are only styled in the visual editor.
  6. */
  7. export const visualHighlightStyle = syntaxHighlighting(
  8. HighlightStyle.define([
  9. { tag: tags.link, class: 'ol-cm-link-text' },
  10. { tag: tags.url, class: 'ol-cm-url' },
  11. { tag: tags.typeName, class: 'ol-cm-monospace' },
  12. { tag: tags.attributeValue, class: 'ol-cm-monospace' },
  13. { tag: tags.keyword, class: 'ol-cm-monospace' },
  14. { tag: tags.string, class: 'ol-cm-monospace' },
  15. { tag: tags.punctuation, class: 'ol-cm-punctuation' },
  16. { tag: tags.literal, class: 'ol-cm-monospace' },
  17. {
  18. tag: tags.monospace,
  19. fontFamily: 'var(--source-font-family)',
  20. lineHeight: 1,
  21. overflowWrap: 'break-word',
  22. },
  23. ])
  24. )
  25. export const visualTheme = EditorView.theme({
  26. '&.cm-editor': {
  27. '--visual-font-family':
  28. "'Noto Serif', 'Palatino Linotype', 'Book Antiqua', Palatino, serif !important",
  29. '--visual-font-size': 'calc(var(--font-size) * 1.15)',
  30. '& .cm-content': {
  31. opacity: 0,
  32. },
  33. '&.ol-cm-parsed .cm-content': {
  34. opacity: 1,
  35. transition: 'opacity 0.1s ease-out',
  36. },
  37. },
  38. '.cm-content.cm-content': {
  39. overflowX: 'hidden', // needed so the callout elements don't overflow (requires line wrapping to be on)
  40. padding: '0 max(calc((100% - 100ch) / 2), 8%)', // max 100 characters per line
  41. fontFamily: 'var(--visual-font-family)',
  42. fontSize: 'var(--visual-font-size)',
  43. },
  44. '.cm-cursor-primary.cm-cursor-primary': {
  45. fontFamily: 'var(--visual-font-family)',
  46. fontSize: 'var(--visual-font-size)',
  47. },
  48. '.cm-line': {
  49. overflowX: 'visible', // needed so the callout elements can overflow when the content has padding
  50. },
  51. '.cm-gutter': {
  52. opacity: '0.5',
  53. },
  54. '.cm-tooltip': {
  55. fontSize: 'calc(var(--font-size) * 1.15) !important',
  56. },
  57. '.ol-cm-link-text': {
  58. textDecoration: 'underline',
  59. fontFamily: 'inherit',
  60. },
  61. '.ol-cm-monospace': {
  62. fontFamily: 'var(--source-font-family)',
  63. lineHeight: 1,
  64. fontWeight: 'normal',
  65. fontStyle: 'normal',
  66. fontVariant: 'normal',
  67. textDecoration: 'none',
  68. },
  69. '.ol-cm-punctuation': {
  70. fontFamily: 'var(--source-font-family)',
  71. lineHeight: 1,
  72. },
  73. '.ol-cm-brace': {
  74. opacity: '0.5',
  75. },
  76. '.ol-cm-math': {
  77. overflow: 'hidden', // stop the margin from the inner math element affecting the block height
  78. },
  79. '.ol-cm-maketitle': {
  80. textAlign: 'center',
  81. paddingBottom: '2em',
  82. },
  83. '.ol-cm-title': {
  84. fontSize: '1.7em',
  85. cursor: 'pointer',
  86. padding: '0.5em',
  87. lineHeight: 'calc(var(--line-height) * 5/6)',
  88. },
  89. '.ol-cm-authors': {
  90. display: 'flex',
  91. justifyContent: 'space-evenly',
  92. gap: '0.5em',
  93. flexWrap: 'wrap',
  94. },
  95. '.ol-cm-author': {
  96. cursor: 'pointer',
  97. display: 'inline-block',
  98. minWidth: '150px',
  99. },
  100. '.ol-cm-icon-brace': {
  101. filter: 'grayscale(1)',
  102. marginRight: '2px',
  103. },
  104. '.ol-cm-begin': {
  105. fontFamily: 'var(--source-font-family)',
  106. minHeight: '1em',
  107. textAlign: 'center',
  108. display: 'flex',
  109. alignItems: 'center',
  110. justifyContent: 'center',
  111. },
  112. '.ol-cm-end': {
  113. fontFamily: 'var(--source-font-family)',
  114. paddingBottom: '1.5em',
  115. minHeight: '1em',
  116. textAlign: 'center',
  117. justifyContent: 'center',
  118. background: `linear-gradient(180deg, rgba(0,0,0,0) calc(50% - 1px), rgba(192,192,192,1) calc(50%), rgba(0,0,0,0) calc(50% + 1px))`,
  119. },
  120. '.ol-cm-environment-top': {
  121. paddingTop: '1em',
  122. },
  123. '.ol-cm-environment-bottom': {
  124. paddingBottom: '1em',
  125. },
  126. '.ol-cm-environment-first-line': {
  127. paddingTop: '0.5em !important',
  128. borderTopLeftRadius: '8px',
  129. borderTopRightRadius: '8px',
  130. },
  131. '.ol-cm-environment-last-line': {
  132. paddingBottom: '1em !important',
  133. borderBottomLeftRadius: '8px',
  134. borderBottomRightRadius: '8px',
  135. },
  136. '.ol-cm-environment-figure.ol-cm-environment-line, .ol-cm-environment-table.ol-cm-environment-line':
  137. {
  138. backgroundColor: 'rgba(125, 125, 125, 0.05)',
  139. padding: '0 12px',
  140. },
  141. '.ol-cm-environment-figure.ol-cm-environment-last-line, .ol-cm-environment-table.ol-cm-environment-last-line, .ol-cm-preamble-line.ol-cm-environment-last-line':
  142. {
  143. boxShadow: '0 2px 5px -3px rgb(125, 125, 125, 0.5)',
  144. },
  145. '.ol-cm-environment-theorem-plain': {
  146. fontStyle: 'italic',
  147. },
  148. '.ol-cm-begin-proof > .ol-cm-environment-name': {
  149. fontStyle: 'italic',
  150. },
  151. '.ol-cm-environment-padding': {
  152. flex: 1,
  153. height: '1px',
  154. background: `linear-gradient(180deg, rgba(0,0,0,0) calc(50% - 1px), rgba(192,192,192,1) calc(50%), rgba(0,0,0,0) calc(50% + 1px))`,
  155. },
  156. '.ol-cm-environment-name': {
  157. padding: '0 1em',
  158. },
  159. '.ol-cm-begin-abstract > .ol-cm-environment-name': {
  160. fontFamily: 'var(--visual-font-family)',
  161. fontSize: '1.2em',
  162. fontWeight: 550,
  163. textTransform: 'capitalize',
  164. },
  165. '.ol-cm-begin-theorem > .ol-cm-environment-name': {
  166. fontFamily: 'var(--visual-font-family)',
  167. fontWeight: 550,
  168. padding: '0 6px',
  169. textTransform: 'capitalize',
  170. },
  171. '.ol-cm-begin-theorem > .ol-cm-environment-padding:first-of-type': {
  172. flex: 0,
  173. },
  174. '.ol-cm-item': {
  175. paddingInlineStart: 'calc(var(--list-depth) * 2ch)',
  176. },
  177. '.ol-cm-item::before': {
  178. counterReset: 'list-item var(--list-ordinal)',
  179. content: 'counter(list-item, var(--list-type)) var(--list-suffix)',
  180. },
  181. '.ol-cm-heading': {
  182. fontWeight: 550,
  183. lineHeight: '1.35',
  184. color: 'inherit !important',
  185. background: 'inherit !important',
  186. },
  187. '.ol-cm-command-part': {
  188. fontSize: '2em',
  189. },
  190. '.ol-cm-command-chapter': {
  191. fontSize: '1.6em',
  192. },
  193. '.ol-cm-command-section': {
  194. fontSize: '1.44em',
  195. },
  196. '.ol-cm-command-subsection': {
  197. fontSize: '1.2em',
  198. },
  199. '.ol-cm-command-subsubsection': {
  200. fontSize: '1em',
  201. },
  202. '.ol-cm-command-paragraph': {
  203. fontSize: '1em',
  204. },
  205. '.ol-cm-command-subparagraph': {
  206. fontSize: '1em',
  207. },
  208. '.ol-cm-frame-title': {
  209. fontSize: '1.44em',
  210. },
  211. '.ol-cm-frame-subtitle': {
  212. fontSize: '1em',
  213. },
  214. '.ol-cm-divider': {
  215. borderBottom: '1px solid rgba(125, 125, 125, 0.1)',
  216. padding: '0.5em 6px',
  217. '&.ol-cm-frame-widget': {
  218. borderBottom: 'none',
  219. borderTop: '1px solid rgba(125, 125, 125, 0.1)',
  220. },
  221. },
  222. '.ol-cm-command-textbf': {
  223. fontWeight: 700,
  224. },
  225. '.ol-cm-command-textit': {
  226. fontStyle: 'italic',
  227. },
  228. '.ol-cm-command-textsc': {
  229. fontVariant: 'small-caps',
  230. },
  231. '.ol-cm-command-texttt': {
  232. fontFamily: 'monospace',
  233. },
  234. '.ol-cm-command-textmd, .ol-cm-command-textmd > .ol-cm-command-textbf': {
  235. fontWeight: 'normal',
  236. },
  237. '.ol-cm-command-textsf': {
  238. fontFamily: 'var(--source-font-family)',
  239. },
  240. '.ol-cm-command-underline': {
  241. textDecoration: 'underline',
  242. },
  243. '.ol-cm-command-sout': {
  244. textDecoration: 'line-through',
  245. },
  246. '.ol-cm-command-emph': {
  247. fontStyle: 'italic',
  248. '& .ol-cm-command-textit': {
  249. fontStyle: 'normal',
  250. },
  251. '.ol-cm-command-textit &': {
  252. fontStyle: 'normal',
  253. },
  254. },
  255. '.ol-cm-command-url': {
  256. textDecoration: 'underline',
  257. // copied from tags.monospace
  258. fontFamily: 'var(--source-font-family)',
  259. lineHeight: 1,
  260. overflowWrap: 'break-word',
  261. hyphens: 'auto',
  262. },
  263. '.ol-cm-environment-centered': {
  264. '&.ol-cm-label-line, &.ol-cm-caption-line': {
  265. textAlign: 'center',
  266. },
  267. '&.ol-cm-caption-line': {
  268. padding: '0 10%',
  269. },
  270. },
  271. '.ol-cm-caption-line .ol-cm-label': {
  272. marginRight: '1ch',
  273. },
  274. '.ol-cm-environment-verbatim': {
  275. fontFamily: 'var(--source-font-family)',
  276. },
  277. '.ol-cm-environment-lstlisting': {
  278. fontFamily: 'var(--source-font-family)',
  279. },
  280. '.ol-cm-tex': {
  281. textTransform: 'uppercase',
  282. '& sup': {
  283. position: 'inherit',
  284. fontSize: '0.85em',
  285. verticalAlign: '0.15em',
  286. marginLeft: '-0.36em',
  287. marginRight: '-0.15em',
  288. },
  289. '& sub': {
  290. position: 'inherit',
  291. fontSize: '1em',
  292. verticalAlign: '-0.5ex',
  293. marginLeft: '-0.1667em',
  294. marginRight: '-0.125em',
  295. },
  296. },
  297. '.ol-cm-graphics': {
  298. display: 'block',
  299. maxWidth: 'min(300px, 100%)',
  300. paddingTop: '1em',
  301. paddingBottom: '1em',
  302. cursor: 'pointer',
  303. '.ol-cm-graphics-inline &': {
  304. display: 'inline',
  305. },
  306. },
  307. '.ol-cm-graphics-inline-edit-wrapper': {
  308. display: 'inline-block',
  309. position: 'relative',
  310. verticalAlign: 'middle',
  311. '& .ol-cm-graphics': {
  312. paddingTop: 0,
  313. paddingBottom: 0,
  314. },
  315. },
  316. '.ol-cm-graphics-loading': {
  317. height: '300px', // guess that the height is the same as the max width
  318. },
  319. '.ol-cm-graphics-error': {
  320. border: '1px solid red',
  321. padding: '8px',
  322. },
  323. '.ol-cm-environment-centered .ol-cm-graphics': {
  324. margin: '0 auto',
  325. },
  326. '.ol-cm-command-verb .ol-cm-monospace': {
  327. color: 'inherit', // remove syntax highlighting colour from verbatim content
  328. },
  329. '.ol-cm-preamble-wrapper': {
  330. padding: '0.5em 0',
  331. '&.ol-cm-preamble-expanded': {
  332. paddingBottom: '0',
  333. },
  334. },
  335. '.ol-cm-preamble-widget, .ol-cm-end-document-widget': {
  336. padding: '0.25em 1em',
  337. borderRadius: '8px',
  338. fontFamily: '"Lato", sans-serif',
  339. fontSize: '14px',
  340. '.ol-cm-preamble-expanded &': {
  341. borderBottomLeftRadius: '0',
  342. borderBottomRightRadius: '0',
  343. borderBottom: '1px solid rgba(125, 125, 125, 0.2)',
  344. },
  345. },
  346. '.ol-cm-preamble-widget': {
  347. cursor: 'pointer',
  348. display: 'flex',
  349. justifyContent: 'space-between',
  350. alignItems: 'center',
  351. },
  352. '.ol-cm-preamble-expand-icon': {
  353. width: '32px',
  354. lineHeight: '32px',
  355. textAlign: 'center',
  356. transition: '0.2s ease-out',
  357. opacity: '0.5',
  358. '.ol-cm-preamble-widget:hover &': {
  359. opacity: '1',
  360. },
  361. '.ol-cm-preamble-expanded &': {
  362. transform: 'rotate(180deg)',
  363. },
  364. },
  365. '.ol-cm-preamble-line, .ol-cm-end-document-widget, .ol-cm-preamble-widget': {
  366. backgroundColor: 'rgba(125, 125, 125, 0.05)',
  367. },
  368. '.ol-cm-preamble-line': {
  369. padding: '0 12px',
  370. '&.ol-cm-environment-first-line': {
  371. borderRadius: '0',
  372. },
  373. },
  374. '.ol-cm-end-document-widget': {
  375. textAlign: 'center',
  376. },
  377. '.ol-cm-environment-figure': {
  378. position: 'relative',
  379. },
  380. '.ol-cm-graphics-edit-button': {
  381. position: 'absolute',
  382. top: '18px',
  383. right: '18px',
  384. },
  385. '.ol-cm-footnote': {
  386. display: 'inline-flex',
  387. padding: '0 0.1em',
  388. background: 'rgba(125, 125, 125, 0.25)',
  389. borderRadius: '2px',
  390. height: '1em',
  391. cursor: 'pointer',
  392. verticalAlign: 'text-top',
  393. '&:not(.ol-cm-footnote-view):hover': {
  394. background: 'rgba(125, 125, 125, 0.5)',
  395. },
  396. '&.ol-cm-footnote-view': {
  397. height: 'auto',
  398. verticalAlign: 'unset',
  399. display: 'inline',
  400. padding: '0 0.5em',
  401. },
  402. },
  403. })