toolbar-panel.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. },
  70. '& .list-group-item': {
  71. width: '100%',
  72. textAlign: 'start',
  73. display: 'flex',
  74. alignItems: 'center',
  75. gap: '5px',
  76. color: 'var(--toolbar-btn-color)',
  77. borderColor: 'var(--editor-toolbar-bg)',
  78. background: 'none',
  79. '&:hover, &:focus': {
  80. backgroundColor: 'rgba(125, 125, 125, 0.2)',
  81. },
  82. },
  83. },
  84. '.ol-cm-toolbar-button-group': {
  85. display: 'flex',
  86. alignItems: 'center',
  87. whiteSpace: 'nowrap',
  88. flexWrap: 'nowrap',
  89. padding: '0 4px',
  90. margin: '4px 0',
  91. lineHeight: '1',
  92. '&:not(:first-of-type)': {
  93. borderLeft: '1px solid rgba(125, 125, 125, 0.3)',
  94. '&.ol-cm-toolbar-end': {
  95. borderLeft: 'none',
  96. },
  97. '&.ol-cm-toolbar-stretch': {
  98. flex: 1,
  99. },
  100. '&.overflow-hidden': {
  101. borderLeft: 'none',
  102. },
  103. },
  104. '&.overflow-hidden': {
  105. width: 0,
  106. padding: 0,
  107. },
  108. },
  109. '.formatting-buttons-wrapper': {
  110. flex: 1,
  111. },
  112. '.ol-cm-toolbar-button': {
  113. display: 'inline-flex',
  114. alignItems: 'center',
  115. justifyContent: 'center',
  116. padding: '0',
  117. margin: '0 1px',
  118. backgroundColor: 'transparent',
  119. border: 'none',
  120. borderRadius: '1px',
  121. lineHeight: '1',
  122. width: '24px',
  123. height: '24px',
  124. overflow: 'hidden',
  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. boxShadow: 'none !important',
  176. border: 'none',
  177. whiteSpace: 'nowrap',
  178. color: 'inherit',
  179. borderRadius: '0',
  180. opacity: 0.8,
  181. width: '120px',
  182. fontSize: '13px',
  183. fontFamily: 'Lato',
  184. fontWeight: '700',
  185. display: 'flex',
  186. alignItems: 'center',
  187. justifyContent: 'space-between',
  188. padding: '5px 6px',
  189. '&:hover, &:focus, &.active': {
  190. backgroundColor: 'rgba(125, 125, 125, 0.1)',
  191. opacity: '1',
  192. color: 'inherit',
  193. },
  194. '& .caret': {
  195. marginTop: '0',
  196. },
  197. },
  198. '.ol-cm-toolbar-menu-popover': {
  199. border: 'none',
  200. borderRadius: '0',
  201. borderBottomLeftRadius: '4px',
  202. borderBottomRightRadius: '4px',
  203. boxShadow: '0 2px 5px rgb(0 0 0 / 20%)',
  204. backgroundColor: 'var(--editor-toolbar-bg)',
  205. color: 'var(--toolbar-btn-color)',
  206. padding: '0',
  207. '&.bottom': {
  208. marginTop: '1px',
  209. },
  210. '&.top': {
  211. marginBottom: '1px',
  212. },
  213. '& .arrow': {
  214. display: 'none',
  215. },
  216. '& .popover-content': {
  217. padding: '0',
  218. },
  219. '& .ol-cm-toolbar-menu': {
  220. width: '120px',
  221. display: 'flex',
  222. flexDirection: 'column',
  223. boxSizing: 'border-box',
  224. fontSize: '14px',
  225. },
  226. '& .ol-cm-toolbar-menu-item': {
  227. border: 'none',
  228. background: 'none',
  229. padding: '4px 12px',
  230. height: '40px',
  231. display: 'flex',
  232. alignItems: 'center',
  233. fontWeight: 'bold',
  234. '&.ol-cm-toolbar-menu-item-active': {
  235. backgroundColor: 'rgba(125, 125, 125, 0.1)',
  236. },
  237. '&:hover': {
  238. backgroundColor: 'rgba(125, 125, 125, 0.2)',
  239. color: 'inherit',
  240. },
  241. '&.section-level-section': {
  242. fontSize: '1.44em',
  243. },
  244. '&.section-level-subsection': {
  245. fontSize: '1.2em',
  246. },
  247. '&.section-level-body': {
  248. fontWeight: 'normal',
  249. },
  250. },
  251. },
  252. '&.overall-theme-dark .ol-cm-toolbar-table-grid-popover': {
  253. color: '#fff',
  254. },
  255. '&.overall-theme-dark .ol-cm-toolbar-table-grid': {
  256. '& td.active': {
  257. outlineColor: 'white',
  258. background: 'rgb(125, 125, 125)',
  259. },
  260. },
  261. '.ol-cm-toolbar-table-grid': {
  262. borderCollapse: 'separate',
  263. tableLayout: 'fixed',
  264. fontSize: '6px',
  265. cursor: 'pointer',
  266. width: '160px',
  267. '& td': {
  268. outline: '1px solid #E7E9EE',
  269. outlineOffset: '-2px',
  270. width: '16px',
  271. height: '16px',
  272. '&.active': {
  273. outlineColor: '#3265B2',
  274. background: '#F1F4F9',
  275. },
  276. },
  277. },
  278. '.ol-cm-toolbar-table-size-label': {
  279. maxWidth: '160px',
  280. fontFamily: 'Lato, sans-serif',
  281. fontSize: '12px',
  282. },
  283. '.ol-cm-toolbar-table-grid-popover': {
  284. padding: '8px',
  285. marginLeft: '80px',
  286. backgroundColor: 'var(--editor-toolbar-bg)',
  287. },
  288. }),
  289. ]