toolbar-panel.ts 5.6 KB

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