toolbar-panel.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. dom.id = 'ol-cm-toolbar-portal'
  20. return { dom, top: true }
  21. }
  22. const toolbarTheme = EditorView.theme({
  23. '.ol-cm-toolbar-wrapper': {
  24. backgroundColor: 'var(--editor-toolbar-bg)',
  25. color: 'var(--toolbar-btn-color)',
  26. },
  27. '.ol-cm-toolbar': {
  28. flex: 1,
  29. display: 'flex',
  30. overflowX: 'hidden',
  31. },
  32. '&.overall-theme-dark .ol-cm-toolbar': {
  33. '& img': {
  34. filter: 'invert(1)',
  35. },
  36. },
  37. '.ol-cm-toolbar-overflow': {
  38. display: 'flex',
  39. flexWrap: 'wrap',
  40. },
  41. '#popover-toolbar-overflow': {
  42. padding: 0,
  43. borderColor: 'rgba(125, 125, 125, 0.2)',
  44. backgroundColor: 'var(--editor-toolbar-bg)',
  45. color: 'var(--toolbar-btn-color)',
  46. '& .popover-content, & .popover-body': {
  47. padding: 0,
  48. },
  49. '& .popover-body': {
  50. color: 'inherit',
  51. },
  52. '& .arrow, & .popover-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. backgroundColor: 'initial',
  61. '& > .popover-content, & > .popover-body': {
  62. padding: 0,
  63. color: 'initial',
  64. },
  65. '& .arrow, & .popover-arrow': {
  66. display: 'none',
  67. },
  68. '& .list-group': {
  69. marginBottom: 0,
  70. backgroundColor: 'var(--editor-toolbar-bg)',
  71. borderRadius: '4px',
  72. },
  73. '& .list-group-item': {
  74. width: '100%',
  75. textAlign: 'start',
  76. display: 'flex',
  77. alignItems: 'center',
  78. gap: '5px',
  79. color: 'var(--toolbar-btn-color)',
  80. borderColor: 'var(--editor-toolbar-bg)',
  81. background: 'none',
  82. '&:hover, &:focus': {
  83. backgroundColor: 'rgba(125, 125, 125, 0.2)',
  84. },
  85. },
  86. },
  87. '.ol-cm-toolbar-button-group': {
  88. display: 'flex',
  89. alignItems: 'center',
  90. whiteSpace: 'nowrap',
  91. flexWrap: 'nowrap',
  92. padding: '0 4px',
  93. margin: '4px 0',
  94. lineHeight: '1',
  95. borderLeft: '1px solid rgba(125, 125, 125, 0.3)',
  96. '&.ol-cm-toolbar-end': {
  97. borderLeft: 'none',
  98. },
  99. '&.ol-cm-toolbar-stretch': {
  100. flex: 1,
  101. '.editor-toggle-switch + &': {
  102. borderLeft: 'none', // avoid a left border when no toolbar buttons are shown
  103. },
  104. },
  105. '&.overflow-hidden': {
  106. borderLeft: 'none',
  107. width: 0,
  108. padding: 0,
  109. },
  110. },
  111. '.ol-cm-toolbar-button': {
  112. display: 'inline-flex',
  113. alignItems: 'center',
  114. justifyContent: 'center',
  115. padding: '0',
  116. margin: '0 1px',
  117. backgroundColor: 'transparent',
  118. border: 'none',
  119. borderRadius: 'var(--border-radius-base)',
  120. lineHeight: '1',
  121. width: '24px',
  122. height: '24px',
  123. overflow: 'hidden',
  124. color: 'inherit',
  125. '&:hover, &:focus, &:active, &.active': {
  126. backgroundColor: 'rgba(125, 125, 125, 0.1)',
  127. color: 'inherit',
  128. boxShadow: 'none',
  129. '&[aria-disabled="true"]': {
  130. opacity: '0.2',
  131. },
  132. },
  133. '&.active, &:active': {
  134. backgroundColor: 'rgba(125, 125, 125, 0.2)',
  135. },
  136. '&[aria-disabled="true"]': {
  137. opacity: '0.2',
  138. cursor: 'not-allowed',
  139. },
  140. '.overflow-hidden &': {
  141. display: 'none',
  142. },
  143. '&.ol-cm-toolbar-button-math': {
  144. fontFamily: '"Noto Serif", serif',
  145. fontSize: '16px',
  146. fontWeight: 700,
  147. },
  148. },
  149. '&.overall-theme-dark .ol-cm-toolbar-button': {
  150. opacity: 0.8,
  151. '&:hover, &:focus, &:active, &.active': {
  152. backgroundColor: 'rgba(125, 125, 125, 0.2)',
  153. },
  154. '&.active, &:active': {
  155. backgroundColor: 'rgba(125, 125, 125, 0.4)',
  156. },
  157. '&[aria-disabled="true"]': {
  158. opacity: 0.2,
  159. },
  160. },
  161. '.ol-cm-toolbar-end': {
  162. justifyContent: 'flex-end',
  163. '& .badge': {
  164. marginRight: '5px',
  165. },
  166. },
  167. '.ol-cm-toolbar-overflow-toggle': {
  168. display: 'none',
  169. '&.ol-cm-toolbar-overflow-toggle-visible': {
  170. display: 'flex',
  171. },
  172. },
  173. '.ol-cm-toolbar-menu-toggle': {
  174. background: 'transparent',
  175. border: 'none',
  176. color: 'inherit',
  177. borderRadius: 'var(--border-radius-base)',
  178. opacity: 0.8,
  179. width: '120px',
  180. fontSize: '13px',
  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': {
  252. '& td.active': {
  253. outlineColor: 'white',
  254. background: 'rgb(125, 125, 125)',
  255. },
  256. },
  257. '.ol-cm-toolbar-table-grid': {
  258. borderCollapse: 'separate',
  259. tableLayout: 'fixed',
  260. fontSize: '6px',
  261. cursor: 'pointer',
  262. width: '160px',
  263. '& td': {
  264. outline: '1px solid #E7E9EE',
  265. outlineOffset: '-2px',
  266. width: '16px',
  267. height: '16px',
  268. '&.active': {
  269. outlineColor: '#3265B2',
  270. background: '#F1F4F9',
  271. },
  272. },
  273. },
  274. '.ol-cm-toolbar-table-size-label': {
  275. maxWidth: '160px',
  276. fontFamily: 'Lato, sans-serif',
  277. fontSize: '12px',
  278. },
  279. '.ol-cm-toolbar-table-grid-popover': {
  280. maxWidth: 'unset',
  281. padding: '8px',
  282. boxShadow: '0 5px 10px rgba(0, 0, 0, 0.2)',
  283. borderRadius: '4px',
  284. backgroundColor: 'var(--editor-toolbar-bg)',
  285. pointerEvents: 'all',
  286. color: 'var(--toolbar-btn-color)',
  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]