toolbar-panel.ts 7.8 KB

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