toolbar-panel.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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-group': {
  108. display: 'flex',
  109. alignItems: 'center',
  110. whiteSpace: 'nowrap',
  111. flexWrap: 'nowrap',
  112. padding: '0 4px',
  113. margin: '4px 0',
  114. lineHeight: '1',
  115. borderLeft: '1px solid rgba(125, 125, 125, 0.3)',
  116. '&.ol-cm-toolbar-end': {
  117. borderLeft: 'none',
  118. },
  119. '&.ol-cm-toolbar-stretch': {
  120. flex: 1,
  121. '.editor-toggle-switch + &': {
  122. borderLeft: 'none', // avoid a left border when no toolbar buttons are shown
  123. },
  124. },
  125. '&.overflow-hidden': {
  126. borderLeft: 'none',
  127. width: 0,
  128. padding: 0,
  129. },
  130. },
  131. '.ol-cm-toolbar-button': {
  132. display: 'inline-flex',
  133. alignItems: 'center',
  134. justifyContent: 'center',
  135. padding: '0',
  136. margin: '0 1px',
  137. backgroundColor: 'transparent',
  138. border: 'none',
  139. borderRadius: 'var(--border-radius-base)',
  140. lineHeight: '1',
  141. width: '24px',
  142. height: '24px',
  143. overflow: 'hidden',
  144. color: 'inherit',
  145. '&:hover, &:focus, &:active, &.active': {
  146. backgroundColor: 'rgba(125, 125, 125, 0.1)',
  147. color: 'inherit',
  148. boxShadow: 'none',
  149. '&[aria-disabled="true"]': {
  150. opacity: '0.2',
  151. },
  152. },
  153. '&.active, &:active': {
  154. backgroundColor: 'rgba(125, 125, 125, 0.2)',
  155. },
  156. '&[aria-disabled="true"]': {
  157. opacity: '0.2',
  158. cursor: 'not-allowed',
  159. },
  160. '.overflow-hidden &': {
  161. display: 'none',
  162. },
  163. '&.ol-cm-toolbar-button-math': {
  164. fontFamily: '"Noto Serif", serif',
  165. fontSize: '16px',
  166. fontWeight: 700,
  167. },
  168. },
  169. '&.overall-theme-dark .ol-cm-toolbar-button': {
  170. opacity: 0.8,
  171. '&:hover, &:focus, &:active, &.active': {
  172. backgroundColor: 'rgba(125, 125, 125, 0.2)',
  173. },
  174. '&.active, &:active': {
  175. backgroundColor: 'rgba(125, 125, 125, 0.4)',
  176. },
  177. '&[aria-disabled="true"]': {
  178. opacity: 0.2,
  179. },
  180. },
  181. '.ol-cm-toolbar-end': {
  182. justifyContent: 'flex-end',
  183. '& .badge': {
  184. marginRight: '5px',
  185. },
  186. },
  187. '.ol-cm-toolbar-overflow-toggle': {
  188. display: 'none',
  189. '&.ol-cm-toolbar-overflow-toggle-visible': {
  190. display: 'flex',
  191. },
  192. },
  193. '.ol-cm-toolbar-menu-toggle': {
  194. background: 'transparent',
  195. border: 'none',
  196. color: 'inherit',
  197. borderRadius: 'var(--border-radius-base)',
  198. opacity: 0.8,
  199. width: '120px',
  200. fontSize: '13px',
  201. fontWeight: '700',
  202. display: 'flex',
  203. alignItems: 'center',
  204. justifyContent: 'space-between',
  205. padding: '5px 6px',
  206. '&:hover, &:focus, &.active': {
  207. backgroundColor: 'rgba(125, 125, 125, 0.1)',
  208. opacity: '1',
  209. color: 'inherit',
  210. },
  211. '& .caret': {
  212. marginTop: '0',
  213. },
  214. },
  215. '.ol-cm-toolbar-menu-popover': {
  216. border: 'none',
  217. borderRadius: '0',
  218. borderBottomLeftRadius: '4px',
  219. borderBottomRightRadius: '4px',
  220. boxShadow: '0 2px 5px rgb(0 0 0 / 20%)',
  221. backgroundColor: 'var(--editor-toolbar-bg)',
  222. color: 'var(--toolbar-btn-color)',
  223. padding: '0',
  224. '&.bottom': {
  225. marginTop: '1px',
  226. },
  227. '&.top': {
  228. marginBottom: '1px',
  229. },
  230. '& .arrow, & .popover-arrow': {
  231. display: 'none',
  232. },
  233. '& .popover-content, & > .popover-body': {
  234. padding: '0',
  235. color: 'inherit',
  236. },
  237. '& .ol-cm-toolbar-menu': {
  238. minWidth: '120px',
  239. display: 'flex',
  240. flexDirection: 'column',
  241. boxSizing: 'border-box',
  242. fontSize: '14px',
  243. },
  244. '& .ol-cm-toolbar-menu-item': {
  245. border: 'none',
  246. background: 'none',
  247. padding: '4px 12px',
  248. height: '40px',
  249. display: 'flex',
  250. alignItems: 'center',
  251. fontWeight: 'bold',
  252. color: 'inherit',
  253. '&.ol-cm-toolbar-menu-item-active': {
  254. backgroundColor: 'rgba(125, 125, 125, 0.1)',
  255. },
  256. '&:hover': {
  257. backgroundColor: 'rgba(125, 125, 125, 0.2)',
  258. color: 'inherit',
  259. },
  260. '&.section-level-section': {
  261. fontSize: '1.44em',
  262. },
  263. '&.section-level-subsection': {
  264. fontSize: '1.2em',
  265. },
  266. '&.section-level-body': {
  267. fontWeight: 'normal',
  268. },
  269. },
  270. },
  271. '&.overall-theme-dark .ol-cm-toolbar-table-grid': {
  272. '& td.active': {
  273. outlineColor: 'white',
  274. background: 'rgb(125, 125, 125)',
  275. },
  276. },
  277. '.ol-cm-toolbar-table-grid': {
  278. borderCollapse: 'separate',
  279. tableLayout: 'fixed',
  280. fontSize: '6px',
  281. cursor: 'pointer',
  282. width: '160px',
  283. '& td': {
  284. outline: '1px solid #E7E9EE',
  285. outlineOffset: '-2px',
  286. width: '16px',
  287. height: '16px',
  288. '&.active': {
  289. outlineColor: '#3265B2',
  290. background: '#F1F4F9',
  291. },
  292. },
  293. },
  294. '.ol-cm-toolbar-table-size-label': {
  295. maxWidth: '160px',
  296. fontFamily: 'Lato, sans-serif',
  297. fontSize: '12px',
  298. },
  299. '.ol-cm-toolbar-table-grid-popover': {
  300. maxWidth: 'unset',
  301. padding: '8px',
  302. boxShadow: '0 5px 10px rgba(0, 0, 0, 0.2)',
  303. borderRadius: '4px',
  304. backgroundColor: 'var(--editor-toolbar-bg)',
  305. pointerEvents: 'all',
  306. color: 'var(--toolbar-btn-color)',
  307. },
  308. '.ol-cm-toolbar-button-menu-popover-unstyled': {
  309. maxWidth: 'unset',
  310. background: 'transparent',
  311. border: 0,
  312. padding: '0 8px 8px 160px',
  313. boxShadow: 'none',
  314. pointerEvents: 'none',
  315. },
  316. })
  317. const toolbarBorderTheme = EditorView.baseTheme({
  318. '&light.overall-theme-dark .cm-panels-top': {
  319. borderBottom: '1px solid var(--border-divider-dark)',
  320. },
  321. })
  322. /**
  323. * A panel which contains the editor toolbar, provided by a state field which allows the toolbar to be toggled,
  324. * and styles for the toolbar.
  325. */
  326. export const toolbarPanel = () => [
  327. toolbarState,
  328. toolbarTheme,
  329. toolbarBorderTheme,
  330. ]