visual-theme.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. textWrap: 'balance',
  89. },
  90. '.ol-cm-authors': {
  91. display: 'flex',
  92. justifyContent: 'space-evenly',
  93. gap: '0.5em',
  94. flexWrap: 'wrap',
  95. },
  96. '.ol-cm-author': {
  97. cursor: 'pointer',
  98. display: 'inline-block',
  99. minWidth: '150px',
  100. },
  101. '.ol-cm-icon-brace': {
  102. filter: 'grayscale(1)',
  103. marginRight: '2px',
  104. },
  105. '.ol-cm-begin': {
  106. fontFamily: 'var(--source-font-family)',
  107. minHeight: '1em',
  108. textAlign: 'center',
  109. display: 'flex',
  110. alignItems: 'center',
  111. justifyContent: 'center',
  112. },
  113. '.ol-cm-end': {
  114. fontFamily: 'var(--source-font-family)',
  115. paddingBottom: '1.5em',
  116. minHeight: '1em',
  117. textAlign: 'center',
  118. justifyContent: 'center',
  119. 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))`,
  120. },
  121. '.ol-cm-environment-top': {
  122. paddingTop: '1em',
  123. },
  124. '.ol-cm-environment-bottom': {
  125. paddingBottom: '1em',
  126. },
  127. '.ol-cm-environment-first-line': {
  128. paddingTop: '0.5em !important',
  129. borderTopLeftRadius: '8px',
  130. borderTopRightRadius: '8px',
  131. },
  132. '.ol-cm-environment-last-line': {
  133. paddingBottom: '1em !important',
  134. borderBottomLeftRadius: '8px',
  135. borderBottomRightRadius: '8px',
  136. },
  137. '.ol-cm-environment-figure.ol-cm-environment-line, .ol-cm-environment-table.ol-cm-environment-line':
  138. {
  139. backgroundColor: 'rgba(125, 125, 125, 0.05)',
  140. padding: '0 12px',
  141. },
  142. '.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':
  143. {
  144. boxShadow: '0 2px 5px -3px rgb(125, 125, 125, 0.5)',
  145. },
  146. '.ol-cm-environment-theorem-plain': {
  147. fontStyle: 'italic',
  148. },
  149. '.ol-cm-begin-proof > .ol-cm-environment-name': {
  150. fontStyle: 'italic',
  151. },
  152. '.ol-cm-environment-quote-block.ol-cm-environment-line': {
  153. borderLeft: '4px solid rgba(125, 125, 125, 0.25)',
  154. paddingLeft: '1em',
  155. borderRadius: '0',
  156. },
  157. '.ol-cm-environment-padding': {
  158. flex: 1,
  159. height: '1px',
  160. 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))`,
  161. },
  162. '.ol-cm-environment-name': {
  163. padding: '0 1em',
  164. },
  165. '.ol-cm-begin-abstract > .ol-cm-environment-name': {
  166. fontFamily: 'var(--visual-font-family)',
  167. fontSize: '1.2em',
  168. fontWeight: 550,
  169. textTransform: 'capitalize',
  170. },
  171. '.ol-cm-begin-theorem > .ol-cm-environment-name': {
  172. fontFamily: 'var(--visual-font-family)',
  173. fontWeight: 550,
  174. padding: '0 6px',
  175. textTransform: 'capitalize',
  176. },
  177. '.ol-cm-begin-theorem > .ol-cm-environment-padding:first-of-type': {
  178. flex: 0,
  179. },
  180. '.ol-cm-item': {
  181. paddingInlineStart: 'calc(var(--list-depth) * 2ch)',
  182. },
  183. '.ol-cm-item::before': {
  184. counterReset: 'list-item var(--list-ordinal)',
  185. content: 'counter(list-item, var(--list-type)) var(--list-suffix)',
  186. },
  187. '.ol-cm-heading': {
  188. fontWeight: 550,
  189. lineHeight: '1.35',
  190. color: 'inherit !important',
  191. background: 'inherit !important',
  192. },
  193. '.ol-cm-command-part': {
  194. fontSize: '2em',
  195. },
  196. '.ol-cm-command-chapter': {
  197. fontSize: '1.6em',
  198. },
  199. '.ol-cm-command-section': {
  200. fontSize: '1.44em',
  201. },
  202. '.ol-cm-command-subsection': {
  203. fontSize: '1.2em',
  204. },
  205. '.ol-cm-command-subsubsection': {
  206. fontSize: '1em',
  207. },
  208. '.ol-cm-command-paragraph': {
  209. fontSize: '1em',
  210. },
  211. '.ol-cm-command-subparagraph': {
  212. fontSize: '1em',
  213. },
  214. '.ol-cm-frame-title': {
  215. fontSize: '1.44em',
  216. },
  217. '.ol-cm-frame-subtitle': {
  218. fontSize: '1em',
  219. },
  220. '.ol-cm-divider': {
  221. borderBottom: '1px solid rgba(125, 125, 125, 0.1)',
  222. padding: '0.5em 6px',
  223. '&.ol-cm-frame-widget': {
  224. borderBottom: 'none',
  225. borderTop: '1px solid rgba(125, 125, 125, 0.1)',
  226. },
  227. },
  228. '.ol-cm-command-textbf': {
  229. fontWeight: 700,
  230. },
  231. '.ol-cm-command-textit': {
  232. fontStyle: 'italic',
  233. },
  234. '.ol-cm-command-textsc': {
  235. fontVariant: 'small-caps',
  236. },
  237. '.ol-cm-command-texttt': {
  238. fontFamily: 'monospace',
  239. },
  240. '.ol-cm-command-textmd, .ol-cm-command-textmd > .ol-cm-command-textbf': {
  241. fontWeight: 'normal',
  242. },
  243. '.ol-cm-command-textsf': {
  244. fontFamily: 'var(--source-font-family)',
  245. },
  246. '.ol-cm-command-underline': {
  247. textDecoration: 'underline',
  248. },
  249. '.ol-cm-command-sout': {
  250. textDecoration: 'line-through',
  251. },
  252. '.ol-cm-command-emph': {
  253. fontStyle: 'italic',
  254. '& .ol-cm-command-textit': {
  255. fontStyle: 'normal',
  256. },
  257. '.ol-cm-command-textit &': {
  258. fontStyle: 'normal',
  259. },
  260. },
  261. '.ol-cm-command-url': {
  262. textDecoration: 'underline',
  263. // copied from tags.monospace
  264. fontFamily: 'var(--source-font-family)',
  265. lineHeight: 1,
  266. overflowWrap: 'break-word',
  267. hyphens: 'auto',
  268. },
  269. '.ol-cm-environment-centered': {
  270. '&.ol-cm-label-line, &.ol-cm-caption-line': {
  271. textAlign: 'center',
  272. },
  273. '&.ol-cm-caption-line': {
  274. padding: '0 10%',
  275. },
  276. },
  277. '.ol-cm-caption-line .ol-cm-label': {
  278. marginRight: '1ch',
  279. },
  280. '.ol-cm-environment-verbatim': {
  281. fontFamily: 'var(--source-font-family)',
  282. },
  283. '.ol-cm-environment-lstlisting': {
  284. fontFamily: 'var(--source-font-family)',
  285. },
  286. '.ol-cm-tex': {
  287. textTransform: 'uppercase',
  288. '& sup': {
  289. position: 'inherit',
  290. fontSize: '0.85em',
  291. verticalAlign: '0.15em',
  292. marginLeft: '-0.36em',
  293. marginRight: '-0.15em',
  294. },
  295. '& sub': {
  296. position: 'inherit',
  297. fontSize: '1em',
  298. verticalAlign: '-0.5ex',
  299. marginLeft: '-0.1667em',
  300. marginRight: '-0.125em',
  301. },
  302. },
  303. '.ol-cm-graphics': {
  304. display: 'block',
  305. maxWidth: 'min(300px, 100%)',
  306. paddingTop: '1em',
  307. paddingBottom: '1em',
  308. cursor: 'pointer',
  309. '.ol-cm-graphics-inline &': {
  310. display: 'inline',
  311. },
  312. },
  313. '.ol-cm-graphics-inline-edit-wrapper': {
  314. display: 'inline-block',
  315. position: 'relative',
  316. verticalAlign: 'middle',
  317. '& .ol-cm-graphics': {
  318. paddingTop: 0,
  319. paddingBottom: 0,
  320. },
  321. },
  322. '.ol-cm-graphics-loading': {
  323. height: '300px', // guess that the height is the same as the max width
  324. },
  325. '.ol-cm-graphics-error': {
  326. border: '1px solid red',
  327. padding: '8px',
  328. },
  329. '.ol-cm-environment-centered .ol-cm-graphics': {
  330. margin: '0 auto',
  331. },
  332. '.ol-cm-command-verb .ol-cm-monospace': {
  333. color: 'inherit', // remove syntax highlighting colour from verbatim content
  334. },
  335. '.ol-cm-preamble-wrapper': {
  336. padding: '0.5em 0',
  337. '&.ol-cm-preamble-expanded': {
  338. paddingBottom: '0',
  339. },
  340. },
  341. '.ol-cm-preamble-widget, .ol-cm-end-document-widget': {
  342. padding: '0.25em 1em',
  343. borderRadius: '8px',
  344. fontFamily: '"Lato", sans-serif',
  345. fontSize: '14px',
  346. '.ol-cm-preamble-expanded &': {
  347. borderBottomLeftRadius: '0',
  348. borderBottomRightRadius: '0',
  349. borderBottom: '1px solid rgba(125, 125, 125, 0.2)',
  350. },
  351. },
  352. '.ol-cm-preamble-widget': {
  353. cursor: 'pointer',
  354. display: 'flex',
  355. justifyContent: 'space-between',
  356. alignItems: 'center',
  357. },
  358. '.ol-cm-preamble-expand-icon': {
  359. width: '32px',
  360. lineHeight: '32px',
  361. textAlign: 'center',
  362. transition: '0.2s ease-out',
  363. opacity: '0.5',
  364. '.ol-cm-preamble-widget:hover &': {
  365. opacity: '1',
  366. },
  367. '.ol-cm-preamble-expanded &': {
  368. transform: 'rotate(180deg)',
  369. },
  370. },
  371. '.ol-cm-preamble-line, .ol-cm-end-document-widget, .ol-cm-preamble-widget': {
  372. backgroundColor: 'rgba(125, 125, 125, 0.05)',
  373. },
  374. '.ol-cm-preamble-line': {
  375. padding: '0 12px',
  376. '&.ol-cm-environment-first-line': {
  377. borderRadius: '0',
  378. },
  379. },
  380. '.ol-cm-end-document-widget': {
  381. textAlign: 'center',
  382. },
  383. '.ol-cm-environment-figure': {
  384. position: 'relative',
  385. },
  386. '.ol-cm-graphics-edit-button': {
  387. position: 'absolute',
  388. top: '18px',
  389. right: '18px',
  390. },
  391. '.ol-cm-footnote': {
  392. display: 'inline-flex',
  393. padding: '0 0.1em',
  394. background: 'rgba(125, 125, 125, 0.25)',
  395. borderRadius: '2px',
  396. height: '1em',
  397. cursor: 'pointer',
  398. verticalAlign: 'text-top',
  399. '&:not(.ol-cm-footnote-view):hover': {
  400. background: 'rgba(125, 125, 125, 0.5)',
  401. },
  402. '&.ol-cm-footnote-view': {
  403. height: 'auto',
  404. verticalAlign: 'unset',
  405. display: 'inline',
  406. padding: '0 0.5em',
  407. },
  408. },
  409. })