pasted-content.tsx 4.8 KB

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