toolbar-panel.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. export const toolbarPanel = () => [
  25. toolbarState,
  26. EditorView.theme({
  27. '.ol-cm-toolbar': {
  28. backgroundColor: 'var(--editor-toolbar-bg)',
  29. color: 'var(--toolbar-btn-color)',
  30. flex: 1,
  31. display: 'flex',
  32. overflowX: 'hidden',
  33. },
  34. '&.overall-theme-dark .ol-cm-toolbar': {
  35. '& img': {
  36. filter: 'invert(1)',
  37. },
  38. },
  39. '.ol-cm-toolbar-overflow': {
  40. display: 'flex',
  41. flexWrap: 'wrap',
  42. },
  43. '#popover-toolbar-overflow': {
  44. padding: 0,
  45. borderColor: 'rgba(125, 125, 125, 0.2)',
  46. backgroundColor: 'var(--editor-toolbar-bg)',
  47. color: 'var(--toolbar-btn-color)',
  48. '& .popover-content': {
  49. padding: 0,
  50. },
  51. '& .arrow': {
  52. borderBottomColor: 'rgba(125, 125, 125, 0.2)',
  53. '&:after': {
  54. borderBottomColor: 'var(--editor-toolbar-bg)',
  55. },
  56. },
  57. },
  58. '.ol-cm-toolbar-button-menu-popover': {
  59. '& > .popover-content': {
  60. padding: 0,
  61. },
  62. '& .arrow': {
  63. display: 'none',
  64. },
  65. '& .list-group': {
  66. marginBottom: 0,
  67. },
  68. '& .list-group-item': {
  69. width: '100%',
  70. textAlign: 'start',
  71. },
  72. },
  73. '.ol-cm-toolbar-button-group': {
  74. display: 'flex',
  75. alignItems: 'center',
  76. whiteSpace: 'nowrap',
  77. flexWrap: 'nowrap',
  78. padding: '0 4px',
  79. margin: '4px 0',
  80. lineHeight: '1',
  81. '&:not(:first-of-type)': {
  82. borderLeft: '1px solid rgba(125, 125, 125, 0.3)',
  83. '&.ol-cm-toolbar-end': {
  84. borderLeft: 'none',
  85. },
  86. '&.overflow-hidden': {
  87. borderLeft: 'none',
  88. },
  89. },
  90. '&.overflow-hidden': {
  91. width: 0,
  92. padding: 0,
  93. },
  94. },
  95. '.ol-cm-toolbar-button': {
  96. display: 'inline-flex',
  97. alignItems: 'center',
  98. justifyContent: 'center',
  99. padding: '0',
  100. margin: '0 1px',
  101. backgroundColor: 'transparent',
  102. border: 'none',
  103. borderRadius: '1px',
  104. lineHeight: '1',
  105. width: '24px',
  106. height: '24px',
  107. overflow: 'hidden',
  108. '&:hover, &:focus, &:active, &.active': {
  109. backgroundColor: 'rgba(125, 125, 125, 0.1)',
  110. color: 'inherit',
  111. boxShadow: 'none',
  112. '&[disabled]': {
  113. opacity: '0.2',
  114. },
  115. },
  116. '&.active, &:active': {
  117. backgroundColor: 'rgba(125, 125, 125, 0.2)',
  118. },
  119. '&[disabled]': {
  120. opacity: '0.2',
  121. },
  122. '.overflow-hidden &': {
  123. display: 'none',
  124. },
  125. '&.ol-cm-toolbar-button-math': {
  126. fontFamily: '"Noto Serif", serif',
  127. fontSize: '16px',
  128. fontWeight: 700,
  129. },
  130. },
  131. '&.overall-theme-dark .ol-cm-toolbar-button': {
  132. opacity: 0.8,
  133. '&:hover, &:focus, &:active, &.active': {
  134. backgroundColor: 'rgba(125, 125, 125, 0.2)',
  135. },
  136. '&.active, &:active': {
  137. backgroundColor: 'rgba(125, 125, 125, 0.4)',
  138. },
  139. '&[disabled]': {
  140. opacity: 0.2,
  141. },
  142. },
  143. '.ol-cm-toolbar-end': {
  144. flex: 1,
  145. justifyContent: 'flex-end',
  146. },
  147. '.ol-cm-toolbar-overflow-toggle': {
  148. display: 'none',
  149. '&.ol-cm-toolbar-overflow-toggle-visible': {
  150. display: 'flex',
  151. },
  152. },
  153. '.ol-cm-toolbar-menu-toggle': {
  154. background: 'transparent',
  155. boxShadow: 'none !important',
  156. border: 'none',
  157. whiteSpace: 'nowrap',
  158. color: 'inherit',
  159. borderRadius: '0',
  160. opacity: 0.8,
  161. width: '120px',
  162. fontSize: '13px',
  163. fontFamily: 'Lato',
  164. fontWeight: '700',
  165. display: 'flex',
  166. alignItems: 'center',
  167. justifyContent: 'space-between',
  168. padding: '5px 6px',
  169. '&:hover, &:focus, &.active': {
  170. backgroundColor: 'rgba(125, 125, 125, 0.1)',
  171. opacity: '1',
  172. color: 'inherit',
  173. },
  174. '& .caret': {
  175. marginTop: '0',
  176. },
  177. },
  178. '.ol-cm-toolbar-menu-popover': {
  179. border: 'none',
  180. borderRadius: '0',
  181. borderBottomLeftRadius: '4px',
  182. borderBottomRightRadius: '4px',
  183. boxShadow: '0 2px 5px rgb(0 0 0 / 20%)',
  184. backgroundColor: 'var(--editor-toolbar-bg)',
  185. color: 'var(--toolbar-btn-color)',
  186. padding: '0',
  187. '&.bottom': {
  188. marginTop: '1px',
  189. },
  190. '&.top': {
  191. marginBottom: '1px',
  192. },
  193. '& .arrow': {
  194. display: 'none',
  195. },
  196. '& .popover-content': {
  197. padding: '0',
  198. },
  199. '& .ol-cm-toolbar-menu': {
  200. width: '120px',
  201. display: 'flex',
  202. flexDirection: 'column',
  203. boxSizing: 'border-box',
  204. fontSize: '14px',
  205. },
  206. '& .ol-cm-toolbar-menu-item': {
  207. border: 'none',
  208. background: 'none',
  209. padding: '4px 12px',
  210. height: '40px',
  211. display: 'flex',
  212. alignItems: 'center',
  213. fontWeight: 'bold',
  214. '&.ol-cm-toolbar-menu-item-active': {
  215. backgroundColor: 'rgba(125, 125, 125, 0.1)',
  216. },
  217. '&:hover': {
  218. backgroundColor: 'rgba(125, 125, 125, 0.2)',
  219. color: 'inherit',
  220. },
  221. '&.section-level-section': {
  222. fontSize: '1.44em',
  223. },
  224. '&.section-level-subsection': {
  225. fontSize: '1.2em',
  226. },
  227. '&.section-level-body': {
  228. fontWeight: 'normal',
  229. },
  230. },
  231. },
  232. }),
  233. ]