toolbar-panel.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. '&:not(:first-of-type)': {
  88. borderLeft: '1px solid rgba(125, 125, 125, 0.3)',
  89. '&.ol-cm-toolbar-end': {
  90. borderLeft: 'none',
  91. },
  92. '&.ol-cm-toolbar-stretch': {
  93. flex: 1,
  94. },
  95. '&.overflow-hidden': {
  96. borderLeft: 'none',
  97. },
  98. },
  99. '&.overflow-hidden': {
  100. width: 0,
  101. padding: 0,
  102. },
  103. },
  104. '.formatting-buttons-wrapper': {
  105. flex: 1,
  106. },
  107. '.ol-cm-toolbar-button': {
  108. display: 'inline-flex',
  109. alignItems: 'center',
  110. justifyContent: 'center',
  111. padding: '0',
  112. margin: '0 1px',
  113. backgroundColor: 'transparent',
  114. border: 'none',
  115. borderRadius: '1px',
  116. lineHeight: '1',
  117. width: '24px',
  118. height: '24px',
  119. overflow: 'hidden',
  120. '&:hover, &:focus, &:active, &.active': {
  121. backgroundColor: 'rgba(125, 125, 125, 0.1)',
  122. color: 'inherit',
  123. boxShadow: 'none',
  124. '&[aria-disabled="true"]': {
  125. opacity: '0.2',
  126. },
  127. },
  128. '&.active, &:active': {
  129. backgroundColor: 'rgba(125, 125, 125, 0.2)',
  130. },
  131. '&[aria-disabled="true"]': {
  132. opacity: '0.2',
  133. cursor: 'not-allowed',
  134. },
  135. '.overflow-hidden &': {
  136. display: 'none',
  137. },
  138. '&.ol-cm-toolbar-button-math': {
  139. fontFamily: '"Noto Serif", serif',
  140. fontSize: '16px',
  141. fontWeight: 700,
  142. },
  143. },
  144. '&.overall-theme-dark .ol-cm-toolbar-button': {
  145. opacity: 0.8,
  146. '&:hover, &:focus, &:active, &.active': {
  147. backgroundColor: 'rgba(125, 125, 125, 0.2)',
  148. },
  149. '&.active, &:active': {
  150. backgroundColor: 'rgba(125, 125, 125, 0.4)',
  151. },
  152. '&[aria-disabled="true"]': {
  153. opacity: 0.2,
  154. },
  155. },
  156. '.ol-cm-toolbar-end': {
  157. justifyContent: 'flex-end',
  158. '& .badge': {
  159. marginRight: '5px',
  160. },
  161. },
  162. '.ol-cm-toolbar-overflow-toggle': {
  163. display: 'none',
  164. '&.ol-cm-toolbar-overflow-toggle-visible': {
  165. display: 'flex',
  166. },
  167. },
  168. '.ol-cm-toolbar-menu-toggle': {
  169. background: 'transparent',
  170. boxShadow: 'none !important',
  171. border: 'none',
  172. whiteSpace: 'nowrap',
  173. color: 'inherit',
  174. borderRadius: '0',
  175. opacity: 0.8,
  176. width: '120px',
  177. fontSize: '13px',
  178. fontFamily: 'Lato',
  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': {
  209. display: 'none',
  210. },
  211. '& .popover-content': {
  212. padding: '0',
  213. },
  214. '& .ol-cm-toolbar-menu': {
  215. width: '120px',
  216. display: 'flex',
  217. flexDirection: 'column',
  218. boxSizing: 'border-box',
  219. fontSize: '14px',
  220. },
  221. '& .ol-cm-toolbar-menu-item': {
  222. border: 'none',
  223. background: 'none',
  224. padding: '4px 12px',
  225. height: '40px',
  226. display: 'flex',
  227. alignItems: 'center',
  228. fontWeight: 'bold',
  229. '&.ol-cm-toolbar-menu-item-active': {
  230. backgroundColor: 'rgba(125, 125, 125, 0.1)',
  231. },
  232. '&:hover': {
  233. backgroundColor: 'rgba(125, 125, 125, 0.2)',
  234. color: 'inherit',
  235. },
  236. '&.section-level-section': {
  237. fontSize: '1.44em',
  238. },
  239. '&.section-level-subsection': {
  240. fontSize: '1.2em',
  241. },
  242. '&.section-level-body': {
  243. fontWeight: 'normal',
  244. },
  245. },
  246. },
  247. '&.overall-theme-dark .ol-cm-toolbar-table-grid-popover': {
  248. color: '#fff',
  249. },
  250. '&.overall-theme-dark .ol-cm-toolbar-table-grid': {
  251. '& td.active': {
  252. outlineColor: 'white',
  253. background: 'rgb(125, 125, 125)',
  254. },
  255. },
  256. '.ol-cm-toolbar-table-grid': {
  257. borderCollapse: 'separate',
  258. tableLayout: 'fixed',
  259. fontSize: '6px',
  260. cursor: 'pointer',
  261. width: '160px',
  262. '& td': {
  263. outline: '1px solid #E7E9EE',
  264. outlineOffset: '-2px',
  265. width: '16px',
  266. height: '16px',
  267. '&.active': {
  268. outlineColor: '#3265B2',
  269. background: '#F1F4F9',
  270. },
  271. },
  272. },
  273. '.ol-cm-toolbar-table-size-label': {
  274. maxWidth: '160px',
  275. fontFamily: 'Lato, sans-serif',
  276. fontSize: '12px',
  277. },
  278. '.ol-cm-toolbar-table-grid-popover': {
  279. maxWidth: 'unset',
  280. padding: '8px',
  281. boxShadow: '0 5px 10px rgba(0, 0, 0, 0.2)',
  282. borderRadius: '4px',
  283. backgroundColor: 'var(--editor-toolbar-bg)',
  284. pointerEvents: 'all',
  285. },
  286. '.ol-cm-toolbar-button-menu-popover-unstyled': {
  287. maxWidth: 'unset',
  288. background: 'transparent',
  289. border: 0,
  290. padding: '0 8px 8px 160px',
  291. boxShadow: 'none',
  292. pointerEvents: 'none',
  293. },
  294. })
  295. /**
  296. * A panel which contains the editor toolbar, provided by a state field which allows the toolbar to be toggled,
  297. * and styles for the toolbar.
  298. */
  299. export const toolbarPanel = () => [toolbarState, toolbarTheme]