visual-theme.ts 10.0 KB

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