visual-theme.ts 11 KB

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