toolbar-panel.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. },
  76. },
  77. '.ol-cm-toolbar-button-group': {
  78. display: 'flex',
  79. alignItems: 'center',
  80. whiteSpace: 'nowrap',
  81. flexWrap: 'nowrap',
  82. padding: '0 4px',
  83. margin: '4px 0',
  84. lineHeight: '1',
  85. '&:not(:first-of-type)': {
  86. borderLeft: '1px solid rgba(125, 125, 125, 0.3)',
  87. '&.ol-cm-toolbar-end': {
  88. borderLeft: 'none',
  89. },
  90. '&.overflow-hidden': {
  91. borderLeft: 'none',
  92. },
  93. },
  94. '&.overflow-hidden': {
  95. width: 0,
  96. padding: 0,
  97. },
  98. },
  99. '.ol-cm-toolbar-button': {
  100. display: 'inline-flex',
  101. alignItems: 'center',
  102. justifyContent: 'center',
  103. padding: '0',
  104. margin: '0 1px',
  105. backgroundColor: 'transparent',
  106. border: 'none',
  107. borderRadius: '1px',
  108. lineHeight: '1',
  109. width: '24px',
  110. height: '24px',
  111. overflow: 'hidden',
  112. '&:hover, &:focus, &:active, &.active': {
  113. backgroundColor: 'rgba(125, 125, 125, 0.1)',
  114. color: 'inherit',
  115. boxShadow: 'none',
  116. '&[disabled]': {
  117. opacity: '0.2',
  118. },
  119. },
  120. '&.active, &:active': {
  121. backgroundColor: 'rgba(125, 125, 125, 0.2)',
  122. },
  123. '&[disabled]': {
  124. opacity: '0.2',
  125. },
  126. '.overflow-hidden &': {
  127. display: 'none',
  128. },
  129. '&.ol-cm-toolbar-button-math': {
  130. fontFamily: '"Noto Serif", serif',
  131. fontSize: '16px',
  132. fontWeight: 700,
  133. },
  134. },
  135. '&.overall-theme-dark .ol-cm-toolbar-button': {
  136. opacity: 0.8,
  137. '&:hover, &:focus, &:active, &.active': {
  138. backgroundColor: 'rgba(125, 125, 125, 0.2)',
  139. },
  140. '&.active, &:active': {
  141. backgroundColor: 'rgba(125, 125, 125, 0.4)',
  142. },
  143. '&[disabled]': {
  144. opacity: 0.2,
  145. },
  146. },
  147. '.ol-cm-toolbar-end': {
  148. flex: 1,
  149. justifyContent: 'flex-end',
  150. },
  151. '.ol-cm-toolbar-overflow-toggle': {
  152. display: 'none',
  153. '&.ol-cm-toolbar-overflow-toggle-visible': {
  154. display: 'flex',
  155. },
  156. },
  157. '.ol-cm-toolbar-menu-toggle': {
  158. background: 'transparent',
  159. boxShadow: 'none !important',
  160. border: 'none',
  161. whiteSpace: 'nowrap',
  162. color: 'inherit',
  163. borderRadius: '0',
  164. opacity: 0.8,
  165. width: '120px',
  166. fontSize: '13px',
  167. fontFamily: 'Lato',
  168. fontWeight: '700',
  169. display: 'flex',
  170. alignItems: 'center',
  171. justifyContent: 'space-between',
  172. padding: '5px 6px',
  173. '&:hover, &:focus, &.active': {
  174. backgroundColor: 'rgba(125, 125, 125, 0.1)',
  175. opacity: '1',
  176. color: 'inherit',
  177. },
  178. '& .caret': {
  179. marginTop: '0',
  180. },
  181. },
  182. '.ol-cm-toolbar-menu-popover': {
  183. border: 'none',
  184. borderRadius: '0',
  185. borderBottomLeftRadius: '4px',
  186. borderBottomRightRadius: '4px',
  187. boxShadow: '0 2px 5px rgb(0 0 0 / 20%)',
  188. backgroundColor: 'var(--editor-toolbar-bg)',
  189. color: 'var(--toolbar-btn-color)',
  190. padding: '0',
  191. '&.bottom': {
  192. marginTop: '1px',
  193. },
  194. '&.top': {
  195. marginBottom: '1px',
  196. },
  197. '& .arrow': {
  198. display: 'none',
  199. },
  200. '& .popover-content': {
  201. padding: '0',
  202. },
  203. '& .ol-cm-toolbar-menu': {
  204. width: '120px',
  205. display: 'flex',
  206. flexDirection: 'column',
  207. boxSizing: 'border-box',
  208. fontSize: '14px',
  209. },
  210. '& .ol-cm-toolbar-menu-item': {
  211. border: 'none',
  212. background: 'none',
  213. padding: '4px 12px',
  214. height: '40px',
  215. display: 'flex',
  216. alignItems: 'center',
  217. fontWeight: 'bold',
  218. '&.ol-cm-toolbar-menu-item-active': {
  219. backgroundColor: 'rgba(125, 125, 125, 0.1)',
  220. },
  221. '&:hover': {
  222. backgroundColor: 'rgba(125, 125, 125, 0.2)',
  223. color: 'inherit',
  224. },
  225. '&.section-level-section': {
  226. fontSize: '1.44em',
  227. },
  228. '&.section-level-subsection': {
  229. fontSize: '1.2em',
  230. },
  231. '&.section-level-body': {
  232. fontWeight: 'normal',
  233. },
  234. },
  235. },
  236. }),
  237. ]