toolbar-panel.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. border: 'none',
  174. color: 'inherit',
  175. borderRadius: isBootstrap5() ? 'var(--border-radius-base)' : '0',
  176. opacity: 0.8,
  177. width: '120px',
  178. fontSize: '13px',
  179. fontWeight: '700',
  180. display: 'flex',
  181. alignItems: 'center',
  182. justifyContent: 'space-between',
  183. padding: '5px 6px',
  184. '&:hover, &:focus, &.active': {
  185. backgroundColor: 'rgba(125, 125, 125, 0.1)',
  186. opacity: '1',
  187. color: 'inherit',
  188. },
  189. '& .caret': {
  190. marginTop: '0',
  191. },
  192. },
  193. '.ol-cm-toolbar-menu-popover': {
  194. border: 'none',
  195. borderRadius: '0',
  196. borderBottomLeftRadius: '4px',
  197. borderBottomRightRadius: '4px',
  198. boxShadow: '0 2px 5px rgb(0 0 0 / 20%)',
  199. backgroundColor: 'var(--editor-toolbar-bg)',
  200. color: 'var(--toolbar-btn-color)',
  201. padding: '0',
  202. '&.bottom': {
  203. marginTop: '1px',
  204. },
  205. '&.top': {
  206. marginBottom: '1px',
  207. },
  208. '& .arrow, & .popover-arrow': {
  209. display: 'none',
  210. },
  211. '& .popover-content, & > .popover-body': {
  212. padding: '0',
  213. color: 'inherit',
  214. },
  215. '& .ol-cm-toolbar-menu': {
  216. width: '120px',
  217. display: 'flex',
  218. flexDirection: 'column',
  219. boxSizing: 'border-box',
  220. fontSize: '14px',
  221. },
  222. '& .ol-cm-toolbar-menu-item': {
  223. border: 'none',
  224. background: 'none',
  225. padding: '4px 12px',
  226. height: '40px',
  227. display: 'flex',
  228. alignItems: 'center',
  229. fontWeight: 'bold',
  230. color: 'inherit',
  231. '&.ol-cm-toolbar-menu-item-active': {
  232. backgroundColor: 'rgba(125, 125, 125, 0.1)',
  233. },
  234. '&:hover': {
  235. backgroundColor: 'rgba(125, 125, 125, 0.2)',
  236. color: 'inherit',
  237. },
  238. '&.section-level-section': {
  239. fontSize: '1.44em',
  240. },
  241. '&.section-level-subsection': {
  242. fontSize: '1.2em',
  243. },
  244. '&.section-level-body': {
  245. fontWeight: 'normal',
  246. },
  247. },
  248. },
  249. '&.overall-theme-dark .ol-cm-toolbar-table-grid-popover': {
  250. color: '#fff',
  251. },
  252. '&.overall-theme-dark .ol-cm-toolbar-table-grid': {
  253. '& td.active': {
  254. outlineColor: 'white',
  255. background: 'rgb(125, 125, 125)',
  256. },
  257. },
  258. '.ol-cm-toolbar-table-grid': {
  259. borderCollapse: 'separate',
  260. tableLayout: 'fixed',
  261. fontSize: '6px',
  262. cursor: 'pointer',
  263. width: '160px',
  264. '& td': {
  265. outline: '1px solid #E7E9EE',
  266. outlineOffset: '-2px',
  267. width: '16px',
  268. height: '16px',
  269. '&.active': {
  270. outlineColor: '#3265B2',
  271. background: '#F1F4F9',
  272. },
  273. },
  274. },
  275. '.ol-cm-toolbar-table-size-label': {
  276. maxWidth: '160px',
  277. fontFamily: 'Lato, sans-serif',
  278. fontSize: '12px',
  279. },
  280. '.ol-cm-toolbar-table-grid-popover': {
  281. maxWidth: 'unset',
  282. padding: '8px',
  283. boxShadow: '0 5px 10px rgba(0, 0, 0, 0.2)',
  284. borderRadius: '4px',
  285. backgroundColor: 'var(--editor-toolbar-bg)',
  286. pointerEvents: 'all',
  287. },
  288. '.ol-cm-toolbar-button-menu-popover-unstyled': {
  289. maxWidth: 'unset',
  290. background: 'transparent',
  291. border: 0,
  292. padding: '0 8px 8px 160px',
  293. boxShadow: 'none',
  294. pointerEvents: 'none',
  295. },
  296. })
  297. /**
  298. * A panel which contains the editor toolbar, provided by a state field which allows the toolbar to be toggled,
  299. * and styles for the toolbar.
  300. */
  301. export const toolbarPanel = () => [toolbarState, toolbarTheme]