toolbar-panel.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. import { StateEffect, StateField } from '@codemirror/state'
  2. import { EditorView, showPanel } from '@codemirror/view'
  3. const toggleToolbarEffect = StateEffect.define<boolean>()
  4. const toolbarState = StateField.define<boolean>({
  5. create: () => true,
  6. update: (value, tr) => {
  7. for (const effect of tr.effects) {
  8. if (effect.is(toggleToolbarEffect)) {
  9. value = effect.value
  10. }
  11. }
  12. return value
  13. },
  14. provide: f => showPanel.from(f, on => (on ? createToolbarPanel : null)),
  15. })
  16. export function createToolbarPanel() {
  17. const dom = document.createElement('div')
  18. dom.classList.add('ol-cm-toolbar-portal')
  19. dom.id = 'ol-cm-toolbar-portal'
  20. return { dom, top: true }
  21. }
  22. const toolbarTheme = EditorView.theme({
  23. '.ol-cm-toolbar-wrapper': {
  24. backgroundColor: 'var(--editor-toolbar-bg)',
  25. color: 'var(--toolbar-btn-color)',
  26. },
  27. '.ol-cm-toolbar': {
  28. flex: 1,
  29. display: 'flex',
  30. overflowX: 'hidden',
  31. },
  32. '&.overall-theme-dark .ol-cm-toolbar': {
  33. '& img': {
  34. filter: 'invert(1)',
  35. },
  36. },
  37. '.ol-cm-toolbar-overflow': {
  38. display: 'flex',
  39. flexWrap: 'wrap',
  40. },
  41. '#popover-toolbar-overflow': {
  42. padding: 0,
  43. borderColor: 'rgba(125, 125, 125, 0.2)',
  44. backgroundColor: 'var(--editor-toolbar-bg)',
  45. color: 'var(--toolbar-btn-color)',
  46. '& .popover-content, & .popover-body': {
  47. padding: 0,
  48. },
  49. '& .popover-body': {
  50. color: 'inherit',
  51. },
  52. '& .arrow, & .popover-arrow': {
  53. borderBottomColor: 'rgba(125, 125, 125, 0.2)',
  54. '&:after': {
  55. borderBottomColor: 'var(--editor-toolbar-bg)',
  56. },
  57. },
  58. },
  59. '.ol-cm-toolbar-header': {
  60. color: 'var(--toolbar-btn-color)',
  61. },
  62. '.ol-cm-toolbar-dropdown-divider': {
  63. borderBottom: '1px solid',
  64. borderColor: 'var(--toolbar-dropdown-divider-color)',
  65. },
  66. // here render both the icons, and hide one depending on if its dark or light mode with &.overall-theme-dark
  67. '.ol-cm-toolbar-ai-sparkle-gradient': {
  68. display: 'block',
  69. },
  70. '.ol-cm-toolbar-ai-sparkle-white': {
  71. display: 'none',
  72. },
  73. '&.overall-theme-dark .ol-cm-toolbar-ai-sparkle-gradient': {
  74. display: 'none',
  75. },
  76. '&.overall-theme-dark .ol-cm-toolbar-ai-sparkle-white': {
  77. display: 'block',
  78. },
  79. '.ol-cm-toolbar-button-menu-popover': {
  80. backgroundColor: 'initial',
  81. '& > .popover-content, & > .popover-body': {
  82. padding: 0,
  83. color: 'initial',
  84. },
  85. '& .arrow, & .popover-arrow': {
  86. display: 'none',
  87. },
  88. '& .list-group': {
  89. marginBottom: 0,
  90. backgroundColor: 'var(--editor-toolbar-bg)',
  91. borderRadius: '4px',
  92. },
  93. '& .list-group-item': {
  94. width: '100%',
  95. textAlign: 'start',
  96. display: 'flex',
  97. alignItems: 'center',
  98. gap: '5px',
  99. color: 'var(--toolbar-btn-color)',
  100. borderColor: 'var(--editor-toolbar-bg)',
  101. background: 'none',
  102. '&:hover, &:focus': {
  103. backgroundColor: 'rgba(125, 125, 125, 0.2)',
  104. },
  105. },
  106. },
  107. '.ol-cm-toolbar-button-menu-popover-horizontal': {
  108. '& .list-group': {
  109. flexDirection: 'row',
  110. },
  111. },
  112. '.ol-cm-toolbar-button-group': {
  113. display: 'flex',
  114. alignItems: 'center',
  115. whiteSpace: 'nowrap',
  116. flexWrap: 'nowrap',
  117. padding: '0 4px',
  118. margin: '4px 0',
  119. lineHeight: '1',
  120. borderLeft: '1px solid rgba(125, 125, 125, 0.3)',
  121. '&.ol-cm-toolbar-end': {
  122. borderLeft: 'none',
  123. },
  124. '&.ol-cm-toolbar-stretch': {
  125. flex: 1,
  126. '.editor-toggle-switch + &': {
  127. borderLeft: 'none', // avoid a left border when no toolbar buttons are shown
  128. },
  129. },
  130. '&.overflow-hidden': {
  131. borderLeft: 'none',
  132. width: 0,
  133. padding: 0,
  134. },
  135. },
  136. '.ol-cm-toolbar-button': {
  137. display: 'inline-flex',
  138. alignItems: 'center',
  139. justifyContent: 'center',
  140. padding: '0',
  141. margin: '0 1px',
  142. backgroundColor: 'transparent',
  143. border: 'none',
  144. borderRadius: 'var(--border-radius-base)',
  145. lineHeight: '1',
  146. width: '24px',
  147. height: '24px',
  148. overflow: 'hidden',
  149. color: 'inherit',
  150. '&:hover, &:focus, &:active, &.active': {
  151. backgroundColor: 'rgba(125, 125, 125, 0.1)',
  152. color: 'inherit',
  153. boxShadow: 'none',
  154. '&[aria-disabled="true"]': {
  155. opacity: '0.2',
  156. },
  157. },
  158. '&.active, &:active': {
  159. backgroundColor: 'rgba(125, 125, 125, 0.2)',
  160. },
  161. '&[aria-disabled="true"]': {
  162. opacity: '0.2',
  163. cursor: 'not-allowed',
  164. },
  165. '.overflow-hidden &': {
  166. display: 'none',
  167. },
  168. '&.ol-cm-toolbar-button-math': {
  169. fontFamily: '"Noto Serif", serif',
  170. fontSize: '16px',
  171. fontWeight: 700,
  172. },
  173. },
  174. '&.overall-theme-dark .ol-cm-toolbar-button': {
  175. opacity: 0.8,
  176. '&:hover, &:focus, &:active, &.active': {
  177. backgroundColor: 'rgba(125, 125, 125, 0.2)',
  178. },
  179. '&.active, &:active': {
  180. backgroundColor: 'rgba(125, 125, 125, 0.4)',
  181. },
  182. '&[aria-disabled="true"]': {
  183. opacity: 0.2,
  184. },
  185. },
  186. '.ol-cm-toolbar-end': {
  187. justifyContent: 'flex-end',
  188. '& .badge': {
  189. marginRight: '5px',
  190. },
  191. },
  192. '.ol-cm-toolbar-overflow-toggle': {
  193. display: 'none',
  194. '&.ol-cm-toolbar-overflow-toggle-visible': {
  195. display: 'flex',
  196. },
  197. },
  198. '.ol-cm-toolbar-menu-toggle': {
  199. background: 'transparent',
  200. border: 'none',
  201. color: 'inherit',
  202. borderRadius: 'var(--border-radius-base)',
  203. opacity: 0.8,
  204. width: '120px',
  205. fontSize: '13px',
  206. fontWeight: '700',
  207. display: 'flex',
  208. alignItems: 'center',
  209. justifyContent: 'space-between',
  210. padding: '5px 6px',
  211. '&:hover, &:focus, &.active': {
  212. backgroundColor: 'rgba(125, 125, 125, 0.1)',
  213. opacity: '1',
  214. color: 'inherit',
  215. },
  216. '& .caret': {
  217. marginTop: '0',
  218. },
  219. },
  220. '.ol-cm-toolbar-menu-popover': {
  221. border: 'none',
  222. borderRadius: '0',
  223. borderBottomLeftRadius: '4px',
  224. borderBottomRightRadius: '4px',
  225. boxShadow: '0 2px 5px rgb(0 0 0 / 20%)',
  226. backgroundColor: 'var(--editor-toolbar-bg)',
  227. color: 'var(--toolbar-btn-color)',
  228. padding: '0',
  229. '&.bottom': {
  230. marginTop: '1px',
  231. },
  232. '&.top': {
  233. marginBottom: '1px',
  234. },
  235. '& .arrow, & .popover-arrow': {
  236. display: 'none',
  237. },
  238. '& .popover-content, & > .popover-body': {
  239. padding: '0',
  240. color: 'inherit',
  241. },
  242. '& .ol-cm-toolbar-menu': {
  243. minWidth: '120px',
  244. display: 'flex',
  245. flexDirection: 'column',
  246. boxSizing: 'border-box',
  247. fontSize: '14px',
  248. },
  249. '& .ol-cm-toolbar-menu-item': {
  250. border: 'none',
  251. background: 'none',
  252. padding: '4px 12px',
  253. height: '40px',
  254. display: 'flex',
  255. alignItems: 'center',
  256. fontWeight: 'bold',
  257. color: 'inherit',
  258. '&.ol-cm-toolbar-menu-item-active': {
  259. backgroundColor: 'rgba(125, 125, 125, 0.1)',
  260. },
  261. '&:hover': {
  262. backgroundColor: 'rgba(125, 125, 125, 0.2)',
  263. color: 'inherit',
  264. },
  265. '&.section-level-section': {
  266. fontSize: '1.44em',
  267. },
  268. '&.section-level-subsection': {
  269. fontSize: '1.2em',
  270. },
  271. '&.section-level-body': {
  272. fontWeight: 'normal',
  273. },
  274. },
  275. },
  276. '&.overall-theme-dark .ol-cm-toolbar-table-grid': {
  277. '& td.active': {
  278. outlineColor: 'white',
  279. background: 'rgb(125, 125, 125)',
  280. },
  281. },
  282. '.ol-cm-toolbar-table-grid': {
  283. borderCollapse: 'separate',
  284. tableLayout: 'fixed',
  285. fontSize: '6px',
  286. cursor: 'pointer',
  287. width: '160px',
  288. '& td': {
  289. outline: '1px solid #E7E9EE',
  290. outlineOffset: '-2px',
  291. width: '16px',
  292. height: '16px',
  293. '&.active': {
  294. outlineColor: '#3265B2',
  295. background: '#F1F4F9',
  296. },
  297. },
  298. },
  299. '.ol-cm-toolbar-table-size-label': {
  300. maxWidth: '160px',
  301. fontFamily: 'Lato, sans-serif',
  302. fontSize: '12px',
  303. },
  304. '.ol-cm-toolbar-table-grid-popover': {
  305. maxWidth: 'unset',
  306. padding: '8px',
  307. boxShadow: '0 5px 10px rgba(0, 0, 0, 0.2)',
  308. borderRadius: '4px',
  309. backgroundColor: 'var(--editor-toolbar-bg)',
  310. pointerEvents: 'all',
  311. color: 'var(--toolbar-btn-color)',
  312. },
  313. '.ol-cm-toolbar-button-menu-popover-unstyled': {
  314. maxWidth: 'unset',
  315. background: 'transparent',
  316. border: 0,
  317. padding: '0 8px 8px 160px',
  318. boxShadow: 'none',
  319. pointerEvents: 'none',
  320. },
  321. })
  322. const toolbarBorderTheme = EditorView.baseTheme({
  323. '&.overall-theme-dark .cm-panels-top': {
  324. borderBottom: '1px solid var(--border-divider-dark)',
  325. },
  326. })
  327. /**
  328. * A panel which contains the editor toolbar, provided by a state field which allows the toolbar to be toggled,
  329. * and styles for the toolbar.
  330. */
  331. export const toolbarPanel = () => [
  332. toolbarState,
  333. toolbarTheme,
  334. toolbarBorderTheme,
  335. ]