visual-theme.ts 10 KB

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