toolbar-panel.ts 7.6 KB

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