pasted-content.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import {
  2. EditorSelection,
  3. Range,
  4. StateEffect,
  5. StateField,
  6. } from '@codemirror/state'
  7. import { Decoration, EditorView, WidgetType } from '@codemirror/view'
  8. import { undo } from '@codemirror/commands'
  9. import { ancestorNodeOfType } from '../../utils/tree-operations/ancestors'
  10. import ReactDOM from 'react-dom'
  11. import { PastedContentMenu } from '../../components/paste-html/pasted-content-menu'
  12. import { SplitTestProvider } from '../../../../shared/context/split-test-context'
  13. export type PastedContent = { latex: string; text: string }
  14. const pastedContentEffect = StateEffect.define<{
  15. content: PastedContent
  16. formatted: boolean
  17. }>()
  18. export const insertPastedContent = (
  19. view: EditorView,
  20. { latex, text }: PastedContent
  21. ) =>
  22. view.state.changeByRange(range => {
  23. // avoid pasting formatted content into a math container
  24. if (ancestorNodeOfType(view.state, range.anchor, '$MathContainer')) {
  25. return {
  26. range: EditorSelection.cursor(range.from + text.length),
  27. changes: { from: range.from, to: range.to, insert: text },
  28. }
  29. }
  30. return {
  31. range: EditorSelection.cursor(range.from + latex.length),
  32. changes: { from: range.from, to: range.to, insert: latex },
  33. }
  34. })
  35. export const storePastedContent = (
  36. content: PastedContent,
  37. formatted: boolean
  38. ) => ({
  39. effects: pastedContentEffect.of({ content, formatted }),
  40. })
  41. const pastedContentTheme = EditorView.baseTheme({
  42. '.ol-cm-pasted-content-menu-toggle': {
  43. background: 'none',
  44. borderRadius: '8px',
  45. border: '1px solid rgb(125, 125, 125)',
  46. color: 'inherit',
  47. margin: '0 4px',
  48. opacity: '0.7',
  49. '&:hover': {
  50. opacity: '1',
  51. },
  52. '& .material-symbols': {
  53. verticalAlign: 'text-bottom',
  54. },
  55. },
  56. '.ol-cm-pasted-content-menu-popover': {
  57. backgroundColor: '#fff',
  58. maxWidth: 'unset',
  59. '& .popover-content': {
  60. padding: 0,
  61. },
  62. '& .popover-body': {
  63. color: 'inherit',
  64. padding: 0,
  65. },
  66. '& .popover-arrow::after': {
  67. borderBottomColor: '#fff',
  68. },
  69. },
  70. '&dark .ol-cm-pasted-content-menu-popover': {
  71. background: 'rgba(0, 0, 0)',
  72. },
  73. '.ol-cm-pasted-content-menu': {
  74. display: 'flex',
  75. flexDirection: 'column',
  76. boxSizing: 'border-box',
  77. fontSize: '14px',
  78. fontFamily: 'var(--font-sans)',
  79. },
  80. '.ol-cm-pasted-content-menu-item': {
  81. color: 'inherit',
  82. border: 'none',
  83. background: 'none',
  84. padding: '8px 16px',
  85. width: '100%',
  86. display: 'flex',
  87. justifyContent: 'space-between',
  88. alignItems: 'center',
  89. whiteSpace: 'nowrap',
  90. gap: '12px',
  91. '&[aria-disabled="true"]': {
  92. color: 'rgba(125, 125, 125, 0.5)',
  93. },
  94. '&:hover': {
  95. backgroundColor: 'rgba(125, 125, 125, 0.2)',
  96. },
  97. },
  98. '.ol-cm-pasted-content-menu-item-label': {
  99. flex: 1,
  100. textAlign: 'left',
  101. },
  102. '.ol-cm-pasted-content-menu-item-shortcut': {
  103. textAlign: 'right',
  104. },
  105. })
  106. export const pastedContent = StateField.define<{
  107. content: PastedContent
  108. formatted: boolean
  109. selection: EditorSelection
  110. } | null>({
  111. create() {
  112. return null
  113. },
  114. update(value, tr) {
  115. if (tr.docChanged) {
  116. // TODO: exclude remote changes (if they don't intersect with changed ranges)?
  117. value = null
  118. } else {
  119. for (const effect of tr.effects) {
  120. if (effect.is(pastedContentEffect)) {
  121. value = {
  122. ...effect.value,
  123. selection: tr.state.selection,
  124. }
  125. }
  126. }
  127. }
  128. return value
  129. },
  130. provide(field) {
  131. return [
  132. EditorView.decorations.compute([field], state => {
  133. const value = state.field(field)
  134. if (!value) {
  135. return Decoration.none
  136. }
  137. const decorations: Range<Decoration>[] = []
  138. const { content, selection, formatted } = value
  139. decorations.push(
  140. Decoration.widget({
  141. widget: new PastedContentMenuWidget(content, formatted),
  142. side: 1,
  143. }).range(selection.main.to)
  144. )
  145. return Decoration.set(decorations, true)
  146. }),
  147. pastedContentTheme,
  148. ]
  149. },
  150. })
  151. class PastedContentMenuWidget extends WidgetType {
  152. constructor(
  153. private pastedContent: PastedContent,
  154. private formatted: boolean
  155. ) {
  156. super()
  157. }
  158. toDOM(view: EditorView) {
  159. const element = document.createElement('span')
  160. ReactDOM.render(
  161. <SplitTestProvider>
  162. <PastedContentMenu
  163. insertPastedContent={this.insertPastedContent}
  164. view={view}
  165. formatted={this.formatted}
  166. pastedContent={this.pastedContent}
  167. />
  168. </SplitTestProvider>,
  169. element
  170. )
  171. return element
  172. }
  173. insertPastedContent(
  174. view: EditorView,
  175. pastedContent: PastedContent,
  176. formatted: boolean
  177. ) {
  178. undo(view)
  179. view.dispatch(
  180. insertPastedContent(view, {
  181. latex: formatted ? pastedContent.latex : pastedContent.text,
  182. text: pastedContent.text,
  183. })
  184. )
  185. view.dispatch(storePastedContent(pastedContent, formatted))
  186. view.focus()
  187. }
  188. eq(widget: PastedContentMenuWidget) {
  189. return (
  190. widget.pastedContent === this.pastedContent &&
  191. widget.formatted === this.formatted
  192. )
  193. }
  194. }