toolbar-panel.ts 7.8 KB

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