commands.ts 4.2 KB

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