pasted-content.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. fontFamily: '"Lato", sans-serif',
  106. },
  107. '.ol-cm-pasted-content-menu-item': {
  108. border: 'none',
  109. background: 'none',
  110. padding: '8px 16px',
  111. width: '100%',
  112. display: 'flex',
  113. justifyContent: 'space-between',
  114. alignItems: 'center',
  115. whiteSpace: 'nowrap',
  116. gap: '12px',
  117. '&[aria-disabled="true"]': {
  118. color: 'rgba(125, 125, 125, 0.5)',
  119. },
  120. '&:hover': {
  121. backgroundColor: 'rgba(125, 125, 125, 0.2)',
  122. },
  123. },
  124. '.ol-cm-pasted-content-menu-item-label': {
  125. flex: 1,
  126. textAlign: 'left',
  127. },
  128. '.ol-cm-pasted-content-menu-item-shortcut': {
  129. textAlign: 'right',
  130. },
  131. }),
  132. ]
  133. },
  134. })
  135. class PastedContentMenuWidget extends WidgetType {
  136. constructor(
  137. private pastedContent: PastedContent,
  138. private formatted: boolean
  139. ) {
  140. super()
  141. }
  142. toDOM(view: EditorView) {
  143. const element = document.createElement('span')
  144. ReactDOM.render(
  145. <SplitTestProvider>
  146. <PastedContentMenu
  147. insertPastedContent={this.insertPastedContent}
  148. view={view}
  149. formatted={this.formatted}
  150. pastedContent={this.pastedContent}
  151. />
  152. </SplitTestProvider>,
  153. element
  154. )
  155. return element
  156. }
  157. insertPastedContent(
  158. view: EditorView,
  159. pastedContent: PastedContent,
  160. formatted: boolean
  161. ) {
  162. undo(view)
  163. view.dispatch(
  164. insertPastedContent(view, {
  165. latex: formatted ? pastedContent.latex : pastedContent.text,
  166. text: pastedContent.text,
  167. })
  168. )
  169. view.dispatch(storePastedContent(pastedContent, formatted))
  170. view.focus()
  171. }
  172. eq(widget: PastedContentMenuWidget) {
  173. return (
  174. widget.pastedContent === this.pastedContent &&
  175. widget.formatted === this.formatted
  176. )
  177. }
  178. }