toolbar-panel.ts 7.6 KB

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