commands.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import { EditorSelection, EditorState, SelectionRange } from '@codemirror/state'
  2. import { Command, EditorView } from '@codemirror/view'
  3. import {
  4. closeSearchPanel,
  5. openSearchPanel,
  6. searchPanelOpen,
  7. } from '@codemirror/search'
  8. import { toggleRanges, wrapRanges } from '../../commands/ranges'
  9. import {
  10. ancestorListType,
  11. toggleListForRanges,
  12. unwrapBulletList,
  13. unwrapDescriptionList,
  14. unwrapNumberedList,
  15. wrapInBulletList,
  16. wrapInDescriptionList,
  17. wrapInNumberedList,
  18. } from './lists'
  19. import { snippet } from '@codemirror/autocomplete'
  20. import { snippets } from './snippets'
  21. import { minimumListDepthForSelection } from '../../utils/tree-operations/ancestors'
  22. import { isVisual } from '../visual/visual'
  23. export const toggleBold = toggleRanges('\\textbf')
  24. export const toggleItalic = toggleRanges('\\textit')
  25. // TODO: apply as a snippet?
  26. // TODO: read URL from clipboard?
  27. export const wrapInHref = wrapRanges('\\href{}{', '}', false, (range, view) =>
  28. isVisual(view) ? range : EditorSelection.cursor(range.from - 2)
  29. )
  30. export const toggleBulletList = toggleListForRanges('itemize')
  31. export const toggleNumberedList = toggleListForRanges('enumerate')
  32. export const wrapInInlineMath = wrapRanges('\\(', '\\)')
  33. export const wrapInDisplayMath = wrapRanges('\n\\[', '\\]\n')
  34. export const ensureEmptyLine = (
  35. state: EditorState,
  36. range: SelectionRange,
  37. direction: 'above' | 'below' = 'below'
  38. ) => {
  39. let pos = range.anchor
  40. let suffix = ''
  41. let prefix = ''
  42. const line = state.doc.lineAt(pos)
  43. if (line.text.trim().length) {
  44. if (direction === 'below') {
  45. pos = Math.min(line.to + 1, state.doc.length)
  46. } else {
  47. pos = Math.max(line.from - 1, 0)
  48. }
  49. const neighbouringLine = state.doc.lineAt(pos)
  50. if (neighbouringLine.length && direction === 'below') {
  51. suffix = '\n'
  52. } else if (neighbouringLine.length && direction === 'above') {
  53. prefix = '\n'
  54. }
  55. }
  56. return { pos, suffix, prefix }
  57. }
  58. export const insertFigure: Command = view => {
  59. const { state, dispatch } = view
  60. const { pos, suffix } = ensureEmptyLine(state, state.selection.main)
  61. const template = `\n${snippets.figure}\n${suffix}`
  62. snippet(template)({ state, dispatch }, { label: 'Figure' }, pos, pos)
  63. return true
  64. }
  65. export const insertTable = (view: EditorView, sizeX: number, sizeY: number) => {
  66. const { state, dispatch } = view
  67. const visual = isVisual(view)
  68. const placeholder = visual ? '' : '#{}'
  69. const placeholderAtStart = visual ? '#{}' : ''
  70. const { pos, suffix } = ensureEmptyLine(state, state.selection.main)
  71. const template = `${placeholderAtStart}\n\\begin{table}
  72. \t\\centering
  73. \t\\begin{tabular}{${'c'.repeat(sizeX)}}
  74. ${(
  75. '\t\t' +
  76. `${placeholder} & ${placeholder}`.repeat(sizeX - 1) +
  77. '\\\\\n'
  78. ).repeat(sizeY)}\t\\end{tabular}
  79. \t\\caption{Caption}
  80. \t\\label{tab:my_label}
  81. \\end{table}${suffix}`
  82. snippet(template)({ state, dispatch }, { label: 'Table' }, pos, pos)
  83. return true
  84. }
  85. export const insertCite: Command = view => {
  86. const { state, dispatch } = view
  87. const pos = state.selection.main.anchor
  88. const template = snippets.cite
  89. snippet(template)({ state, dispatch }, { label: 'Cite' }, pos, pos)
  90. return true
  91. }
  92. export const insertRef: Command = view => {
  93. const { state, dispatch } = view
  94. const pos = state.selection.main.anchor
  95. const template = snippets.ref
  96. snippet(template)({ state, dispatch }, { label: 'Ref' }, pos, pos)
  97. return true
  98. }
  99. export const indentDecrease: Command = view => {
  100. if (minimumListDepthForSelection(view.state) < 2) {
  101. return false
  102. }
  103. switch (ancestorListType(view.state)) {
  104. case 'itemize':
  105. return unwrapBulletList(view)
  106. case 'enumerate':
  107. return unwrapNumberedList(view)
  108. case 'description':
  109. return unwrapDescriptionList(view)
  110. default:
  111. return false
  112. }
  113. }
  114. export const cursorIsAtStartOfListItem = (state: EditorState) => {
  115. return state.selection.ranges.every(range => {
  116. const line = state.doc.lineAt(range.from)
  117. const prefix = state.sliceDoc(line.from, range.from)
  118. return /\\item\s*$/.test(prefix)
  119. })
  120. }
  121. export const indentIncrease: Command = view => {
  122. if (minimumListDepthForSelection(view.state) < 1) {
  123. return false
  124. }
  125. switch (ancestorListType(view.state)) {
  126. case 'itemize':
  127. return wrapInBulletList(view)
  128. case 'enumerate':
  129. return wrapInNumberedList(view)
  130. case 'description':
  131. return wrapInDescriptionList(view)
  132. default:
  133. return false
  134. }
  135. }
  136. export const toggleSearch: Command = view => {
  137. if (searchPanelOpen(view.state)) {
  138. closeSearchPanel(view)
  139. } else {
  140. openSearchPanel(view)
  141. }
  142. return true
  143. }