pasted-content.tsx 5.0 KB

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