toolbar-panel.ts 7.2 KB

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