toolbar-panel.ts 7.0 KB

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