toolbar-panel.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. },
  72. '& .list-group-item': {
  73. width: '100%',
  74. textAlign: 'start',
  75. display: 'flex',
  76. alignItems: 'center',
  77. gap: '5px',
  78. },
  79. },
  80. '.ol-cm-toolbar-button-group': {
  81. display: 'flex',
  82. alignItems: 'center',
  83. whiteSpace: 'nowrap',
  84. flexWrap: 'nowrap',
  85. padding: '0 4px',
  86. margin: '4px 0',
  87. lineHeight: '1',
  88. '&:not(:first-of-type)': {
  89. borderLeft: '1px solid rgba(125, 125, 125, 0.3)',
  90. '&.ol-cm-toolbar-end': {
  91. borderLeft: 'none',
  92. },
  93. '&.ol-cm-toolbar-stretch': {
  94. flex: 1,
  95. },
  96. '&.overflow-hidden': {
  97. borderLeft: 'none',
  98. },
  99. },
  100. '&.overflow-hidden': {
  101. width: 0,
  102. padding: 0,
  103. },
  104. },
  105. '.formatting-buttons-wrapper': {
  106. flex: 1,
  107. },
  108. '.ol-cm-toolbar-button': {
  109. display: 'inline-flex',
  110. alignItems: 'center',
  111. justifyContent: 'center',
  112. padding: '0',
  113. margin: '0 1px',
  114. backgroundColor: 'transparent',
  115. border: 'none',
  116. borderRadius: '1px',
  117. lineHeight: '1',
  118. width: '24px',
  119. height: '24px',
  120. overflow: 'hidden',
  121. '&:hover, &:focus, &:active, &.active': {
  122. backgroundColor: 'rgba(125, 125, 125, 0.1)',
  123. color: 'inherit',
  124. boxShadow: 'none',
  125. '&[disabled]': {
  126. opacity: '0.2',
  127. },
  128. },
  129. '&.active, &:active': {
  130. backgroundColor: 'rgba(125, 125, 125, 0.2)',
  131. },
  132. '&[disabled]': {
  133. opacity: '0.2',
  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. '&[disabled]': {
  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. }),
  248. ]