visual-keymap.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import { keymap } from '@codemirror/view'
  2. import {
  3. ChangeSpec,
  4. EditorSelection,
  5. Prec,
  6. SelectionRange,
  7. } from '@codemirror/state'
  8. import { ancestorNodeOfType } from '../../utils/tree-query'
  9. import {
  10. cursorIsAtStartOfListItem,
  11. indentDecrease,
  12. indentIncrease,
  13. } from '../toolbar/commands'
  14. import { createListItem } from '@/features/source-editor/extensions/visual/utils/list-item'
  15. import { getListType } from '../../utils/tree-operations/lists'
  16. /**
  17. * A keymap which provides behaviours for the visual editor,
  18. * including lists and text formatting.
  19. */
  20. export const visualKeymap = Prec.highest(
  21. keymap.of([
  22. // create a new list item with the same indentation
  23. {
  24. key: 'Enter',
  25. run: view => {
  26. const { state } = view
  27. let handled = false
  28. const changes = state.changeByRange(range => {
  29. if (range.empty) {
  30. const { from } = range
  31. const listNode = ancestorNodeOfType(state, from, 'ListEnvironment')
  32. if (listNode) {
  33. const line = state.doc.lineAt(range.from)
  34. const endLine = state.doc.lineAt(listNode.to)
  35. if (line.number === endLine.number - 1) {
  36. // last item line
  37. if (/^\\item(\[])?$/.test(line.text.trim())) {
  38. // no content on this line
  39. // outside the end of the current list
  40. const pos = listNode.to + 1
  41. // delete the current line
  42. const deleteCurrentLine = {
  43. from: line.from,
  44. to: line.to + 1,
  45. insert: '',
  46. }
  47. const changes: ChangeSpec[] = [deleteCurrentLine]
  48. // the new cursor position
  49. let range: SelectionRange
  50. // if this is a nested list, insert a new empty list item after this list
  51. if (
  52. listNode.parent?.parent?.parent?.parent?.type.is(
  53. 'ListEnvironment'
  54. )
  55. ) {
  56. const newListItem = createListItem(state, pos)
  57. changes.push({
  58. from: pos,
  59. insert: newListItem + '\n',
  60. })
  61. // place the cursor at the end of the new list item
  62. range = EditorSelection.cursor(pos + newListItem.length)
  63. } else {
  64. // place the cursor outside the end of the current list
  65. range = EditorSelection.cursor(pos)
  66. }
  67. handled = true
  68. return {
  69. changes,
  70. range: range.map(state.changes(deleteCurrentLine)),
  71. }
  72. }
  73. }
  74. // handle a list item that isn't at the end of a list
  75. let insert = '\n' + createListItem(state, from)
  76. const countWhitespaceAfterPosition = (pos: number) => {
  77. const line = state.doc.lineAt(pos)
  78. const followingText = state.sliceDoc(pos, line.to)
  79. const matches = followingText.match(/^(\s+)/)
  80. return matches ? matches[1].length : 0
  81. }
  82. let pos: number
  83. if (getListType(state, listNode) === 'description') {
  84. insert = insert.replace(/\\item $/, '\\item[] ')
  85. // position the cursor inside the square brackets
  86. pos = from + insert.length - 2
  87. } else {
  88. // move the cursor past any whitespace on the new line
  89. pos = from + insert.length + countWhitespaceAfterPosition(from)
  90. }
  91. handled = true
  92. return {
  93. changes: { from, insert },
  94. range: EditorSelection.cursor(pos, -1),
  95. }
  96. }
  97. const sectioningNode = ancestorNodeOfType(
  98. state,
  99. from,
  100. 'SectioningCommand'
  101. )
  102. if (sectioningNode) {
  103. // jump out of a section heading to the start of the next line
  104. const nextLineNumber = state.doc.lineAt(from).number + 1
  105. if (nextLineNumber <= state.doc.lines) {
  106. const line = state.doc.line(nextLineNumber)
  107. handled = true
  108. return {
  109. range: EditorSelection.cursor(line.from),
  110. }
  111. }
  112. }
  113. }
  114. return { range }
  115. })
  116. if (handled) {
  117. view.dispatch(changes, {
  118. scrollIntoView: true,
  119. userEvent: 'input',
  120. })
  121. }
  122. return handled
  123. },
  124. },
  125. // Increase list indent
  126. {
  127. key: 'Mod-]',
  128. preventDefault: true,
  129. run: indentIncrease,
  130. },
  131. // Decrease list indent
  132. {
  133. key: 'Mod-[',
  134. preventDefault: true,
  135. run: indentDecrease,
  136. },
  137. // Increase list indent
  138. {
  139. key: 'Tab',
  140. preventDefault: true,
  141. run: view =>
  142. cursorIsAtStartOfListItem(view.state) && indentIncrease(view),
  143. },
  144. // Decrease list indent
  145. {
  146. key: 'Shift-Tab',
  147. preventDefault: true,
  148. run: indentDecrease,
  149. },
  150. ])
  151. )