toolbar-panel.ts 7.3 KB

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