visual-theme.ts 13 KB

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