toolbar-panel.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. border: '1px solid var(--editor-toolbar-popover-border-color)',
  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. display: 'none',
  54. },
  55. },
  56. '.ol-cm-toolbar-header': {
  57. color: 'var(--toolbar-btn-color)',
  58. },
  59. '.ol-cm-toolbar-dropdown-divider': {
  60. borderBottom: '1px solid',
  61. borderColor: 'var(--toolbar-dropdown-divider-color)',
  62. },
  63. // here render both the icons, and hide one depending on if its dark or light mode with &.overall-theme-dark
  64. '.ol-cm-toolbar-ai-sparkle-gradient': {
  65. display: 'block',
  66. },
  67. '.ol-cm-toolbar-ai-sparkle-white': {
  68. display: 'none',
  69. },
  70. '&.overall-theme-dark .ol-cm-toolbar-ai-sparkle-gradient': {
  71. display: 'none',
  72. },
  73. '&.overall-theme-dark .ol-cm-toolbar-ai-sparkle-white': {
  74. display: 'block',
  75. },
  76. '.ol-cm-toolbar-button-menu-popover': {
  77. backgroundColor: 'initial',
  78. border: '1px solid var(--editor-toolbar-popover-border-color)',
  79. '& > .popover-content, & > .popover-body': {
  80. padding: 0,
  81. color: 'initial',
  82. },
  83. '& .arrow, & .popover-arrow': {
  84. display: 'none',
  85. },
  86. '& .list-group': {
  87. marginBottom: 0,
  88. backgroundColor: 'var(--editor-toolbar-bg)',
  89. borderRadius: '4px',
  90. },
  91. '& .list-group-item': {
  92. width: '100%',
  93. textAlign: 'start',
  94. display: 'flex',
  95. alignItems: 'center',
  96. gap: '5px',
  97. color: 'var(--toolbar-btn-color)',
  98. borderColor: 'var(--editor-toolbar-bg)',
  99. background: 'none',
  100. '&.active': {
  101. backgroundColor: 'rgba(125, 125, 125, 0.1)',
  102. },
  103. '&:hover, &:focus': {
  104. backgroundColor: 'rgba(125, 125, 125, 0.2)',
  105. },
  106. '&.ol-cm-section-heading-menu-item': {
  107. border: 'none',
  108. padding: '4px 12px',
  109. height: '40px',
  110. fontSize: '14px',
  111. fontWeight: 'bold',
  112. },
  113. '&.section-level-section': {
  114. fontSize: '1.44em',
  115. },
  116. '&.section-level-subsection': {
  117. fontSize: '1.2em',
  118. },
  119. '&.section-level-text': {
  120. fontWeight: 'normal',
  121. },
  122. },
  123. },
  124. '.ol-cm-toolbar-button-menu-popover-horizontal': {
  125. '& .list-group': {
  126. flexDirection: 'row',
  127. },
  128. },
  129. '.ol-cm-toolbar-button-group': {
  130. display: 'flex',
  131. alignItems: 'center',
  132. whiteSpace: 'nowrap',
  133. flexWrap: 'nowrap',
  134. padding: '0 4px',
  135. margin: '4px 0',
  136. lineHeight: '1',
  137. '&:not(:first-child)': {
  138. borderLeft: '1px solid rgba(125, 125, 125, 0.3)',
  139. },
  140. '&:has(> [data-overflow]):not(:has(> [data-overflow]:not(.overflow-hidden)))':
  141. {
  142. borderLeft: 'none',
  143. padding: 0,
  144. },
  145. '&:empty': {
  146. display: 'none',
  147. },
  148. '&.ol-cm-toolbar-end': {
  149. borderLeft: 'none',
  150. },
  151. '&.ol-cm-toolbar-stretch': {
  152. flex: 1,
  153. },
  154. '&.overflow-hidden': {
  155. borderLeft: 'none',
  156. width: 0,
  157. padding: 0,
  158. },
  159. '& > [data-overflow].overflow-hidden': {
  160. width: 0,
  161. overflow: 'hidden',
  162. },
  163. },
  164. '.ol-cm-toolbar-wrapper-needs-border .ol-cm-toolbar-button-group:first-child':
  165. {
  166. borderLeft: '1px solid rgba(125, 125, 125, 0.3)',
  167. },
  168. '.ol-cm-toolbar-button': {
  169. display: 'inline-flex',
  170. alignItems: 'center',
  171. justifyContent: 'center',
  172. padding: '0',
  173. margin: '0 1px',
  174. backgroundColor: 'transparent',
  175. border: 'none',
  176. borderRadius: 'var(--border-radius-base)',
  177. lineHeight: '1',
  178. width: '24px',
  179. height: '24px',
  180. overflow: 'hidden',
  181. color: 'inherit',
  182. '&:hover, &:focus, &:active, &.active': {
  183. backgroundColor: 'rgba(125, 125, 125, 0.1)',
  184. color: 'inherit',
  185. boxShadow: 'none',
  186. '&[aria-disabled="true"]': {
  187. opacity: '0.2',
  188. },
  189. },
  190. '&.active, &:active': {
  191. backgroundColor: 'rgba(125, 125, 125, 0.2)',
  192. },
  193. '&[aria-disabled="true"]': {
  194. opacity: '0.2',
  195. cursor: 'not-allowed',
  196. },
  197. '.overflow-hidden &': {
  198. display: 'none',
  199. },
  200. '&.ol-cm-toolbar-button-math': {
  201. fontFamily: '"Noto Serif", serif',
  202. fontSize: '16px',
  203. fontWeight: 700,
  204. },
  205. },
  206. '.ol-cm-toolbar-button.ol-cm-toolbar-button-wide': {
  207. width: 'auto',
  208. },
  209. '&.overall-theme-dark .ol-cm-toolbar-button': {
  210. opacity: 0.8,
  211. '&:hover, &:focus, &:active, &.active': {
  212. backgroundColor: 'rgba(125, 125, 125, 0.2)',
  213. },
  214. '&.active, &:active': {
  215. backgroundColor: 'rgba(125, 125, 125, 0.4)',
  216. },
  217. '&[aria-disabled="true"]': {
  218. opacity: 0.2,
  219. },
  220. },
  221. '.ol-cm-toolbar-end': {
  222. justifyContent: 'flex-end',
  223. '& .badge': {
  224. marginRight: '5px',
  225. },
  226. },
  227. '.ol-cm-toolbar-overflow-toggle': {
  228. display: 'none',
  229. '&.ol-cm-toolbar-overflow-toggle-visible': {
  230. display: 'flex',
  231. },
  232. },
  233. '&.overall-theme-dark .ol-cm-toolbar-table-grid': {
  234. '& td.active': {
  235. outlineColor: 'white',
  236. background: 'rgb(125, 125, 125)',
  237. },
  238. },
  239. '.ol-cm-toolbar-table-grid': {
  240. borderCollapse: 'separate',
  241. tableLayout: 'fixed',
  242. fontSize: '6px',
  243. cursor: 'pointer',
  244. width: '160px',
  245. '& td': {
  246. outline: '1px solid #E7E9EE',
  247. outlineOffset: '-2px',
  248. width: '16px',
  249. height: '16px',
  250. '&.active': {
  251. outlineColor: '#3265B2',
  252. background: '#F1F4F9',
  253. },
  254. },
  255. },
  256. '.ol-cm-toolbar-table-size-label': {
  257. maxWidth: '160px',
  258. fontFamily: 'Lato, sans-serif',
  259. fontSize: '12px',
  260. },
  261. '.ol-cm-toolbar-table-grid-popover': {
  262. maxWidth: 'unset',
  263. padding: '8px',
  264. boxShadow: '0 5px 10px rgba(0, 0, 0, 0.2)',
  265. borderRadius: '4px',
  266. backgroundColor: 'var(--editor-toolbar-bg)',
  267. pointerEvents: 'all',
  268. color: 'var(--toolbar-btn-color)',
  269. },
  270. '.ol-cm-toolbar-button-menu-popover-unstyled': {
  271. maxWidth: 'unset',
  272. background: 'transparent',
  273. border: 0,
  274. padding: '0 8px 8px 160px',
  275. boxShadow: 'none',
  276. pointerEvents: 'none',
  277. },
  278. })
  279. const toolbarBorderTheme = EditorView.baseTheme({
  280. '&.overall-theme-dark .cm-panels-top': {
  281. borderBottom: '1px solid var(--border-divider-dark)',
  282. },
  283. })
  284. /**
  285. * A panel which contains the editor toolbar, provided by a state field which allows the toolbar to be toggled,
  286. * and styles for the toolbar.
  287. */
  288. export const toolbarPanel = () => [
  289. toolbarState,
  290. toolbarTheme,
  291. toolbarBorderTheme,
  292. ]