4d91f0c9af1754f90a8763e510d2d0ffa5959360d00ccdb0f31871d1eaece363697ef7472d253048477b501965f5422b37a1d9f5e7c5024a696825e0145f6e 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { runInOp } from "../display/operations.js"
  2. import { ensureCursorVisible } from "../display/scrolling.js"
  3. import { Pos } from "../line/pos.js"
  4. import { getLine } from "../line/utils_line.js"
  5. import { makeChange } from "../model/changes.js"
  6. import { ios, webkit } from "../util/browser.js"
  7. import { elt } from "../util/dom.js"
  8. import { lst, map } from "../util/misc.js"
  9. import { signalLater } from "../util/operation_group.js"
  10. import { splitLinesAuto } from "../util/feature_detection.js"
  11. import { indentLine } from "./indent.js"
  12. // This will be set to a {lineWise: bool, text: [string]} object, so
  13. // that, when pasting, we know what kind of selections the copied
  14. // text was made out of.
  15. export let lastCopied = null
  16. export function setLastCopied(newLastCopied) {
  17. lastCopied = newLastCopied
  18. }
  19. export function applyTextInput(cm, inserted, deleted, sel, origin) {
  20. let doc = cm.doc
  21. cm.display.shift = false
  22. if (!sel) sel = doc.sel
  23. let recent = +new Date - 200
  24. let paste = origin == "paste" || cm.state.pasteIncoming > recent
  25. let textLines = splitLinesAuto(inserted), multiPaste = null
  26. // When pasting N lines into N selections, insert one line per selection
  27. if (paste && sel.ranges.length > 1) {
  28. if (lastCopied && lastCopied.text.join("\n") == inserted) {
  29. if (sel.ranges.length % lastCopied.text.length == 0) {
  30. multiPaste = []
  31. for (let i = 0; i < lastCopied.text.length; i++)
  32. multiPaste.push(doc.splitLines(lastCopied.text[i]))
  33. }
  34. } else if (textLines.length == sel.ranges.length && cm.options.pasteLinesPerSelection) {
  35. multiPaste = map(textLines, l => [l])
  36. }
  37. }
  38. let updateInput = cm.curOp.updateInput
  39. // Normal behavior is to insert the new text into every selection
  40. for (let i = sel.ranges.length - 1; i >= 0; i--) {
  41. let range = sel.ranges[i]
  42. let from = range.from(), to = range.to()
  43. if (range.empty()) {
  44. if (deleted && deleted > 0) // Handle deletion
  45. from = Pos(from.line, from.ch - deleted)
  46. else if (cm.state.overwrite && !paste) // Handle overwrite
  47. to = Pos(to.line, Math.min(getLine(doc, to.line).text.length, to.ch + lst(textLines).length))
  48. else if (paste && lastCopied && lastCopied.lineWise && lastCopied.text.join("\n") == textLines.join("\n"))
  49. from = to = Pos(from.line, 0)
  50. }
  51. let changeEvent = {from: from, to: to, text: multiPaste ? multiPaste[i % multiPaste.length] : textLines,
  52. origin: origin || (paste ? "paste" : cm.state.cutIncoming > recent ? "cut" : "+input")}
  53. makeChange(cm.doc, changeEvent)
  54. signalLater(cm, "inputRead", cm, changeEvent)
  55. }
  56. if (inserted && !paste)
  57. triggerElectric(cm, inserted)
  58. ensureCursorVisible(cm)
  59. if (cm.curOp.updateInput < 2) cm.curOp.updateInput = updateInput
  60. cm.curOp.typing = true
  61. cm.state.pasteIncoming = cm.state.cutIncoming = -1
  62. }
  63. export function handlePaste(e, cm) {
  64. let pasted = e.clipboardData && e.clipboardData.getData("Text")
  65. if (pasted) {
  66. e.preventDefault()
  67. if (!cm.isReadOnly() && !cm.options.disableInput && cm.hasFocus())
  68. runInOp(cm, () => applyTextInput(cm, pasted, 0, null, "paste"))
  69. return true
  70. }
  71. }
  72. export function triggerElectric(cm, inserted) {
  73. // When an 'electric' character is inserted, immediately trigger a reindent
  74. if (!cm.options.electricChars || !cm.options.smartIndent) return
  75. let sel = cm.doc.sel
  76. for (let i = sel.ranges.length - 1; i >= 0; i--) {
  77. let range = sel.ranges[i]
  78. if (range.head.ch > 100 || (i && sel.ranges[i - 1].head.line == range.head.line)) continue
  79. let mode = cm.getModeAt(range.head)
  80. let indented = false
  81. if (mode.electricChars) {
  82. for (let j = 0; j < mode.electricChars.length; j++)
  83. if (inserted.indexOf(mode.electricChars.charAt(j)) > -1) {
  84. indented = indentLine(cm, range.head.line, "smart")
  85. break
  86. }
  87. } else if (mode.electricInput) {
  88. if (mode.electricInput.test(getLine(cm.doc, range.head.line).text.slice(0, range.head.ch)))
  89. indented = indentLine(cm, range.head.line, "smart")
  90. }
  91. if (indented) signalLater(cm, "electricInput", cm, range.head.line)
  92. }
  93. }
  94. export function copyableRanges(cm) {
  95. let text = [], ranges = []
  96. for (let i = 0; i < cm.doc.sel.ranges.length; i++) {
  97. let line = cm.doc.sel.ranges[i].head.line
  98. let lineRange = {anchor: Pos(line, 0), head: Pos(line + 1, 0)}
  99. ranges.push(lineRange)
  100. text.push(cm.getRange(lineRange.anchor, lineRange.head))
  101. }
  102. return {text: text, ranges: ranges}
  103. }
  104. export function disableBrowserMagic(field, spellcheck, autocorrect, autocapitalize) {
  105. field.setAttribute("autocorrect", autocorrect ? "on" : "off")
  106. field.setAttribute("autocapitalize", autocapitalize ? "on" : "off")
  107. field.setAttribute("spellcheck", !!spellcheck)
  108. }
  109. export function hiddenTextarea() {
  110. let te = elt("textarea", null, null, "position: absolute; bottom: -1em; padding: 0; width: 1px; height: 1em; min-height: 1em; outline: none")
  111. let div = elt("div", [te], null, "overflow: hidden; position: relative; width: 3px; height: 0px;")
  112. // The textarea is kept positioned near the cursor to prevent the
  113. // fact that it'll be scrolled into view on input from scrolling
  114. // our fake cursor out of view. On webkit, when wrap=off, paste is
  115. // very slow. So make the area wide instead.
  116. if (webkit) te.style.width = "1000px"
  117. else te.setAttribute("wrap", "off")
  118. // If border: 0; -- iOS fails to open keyboard (issue #1287)
  119. if (ios) te.style.border = "1px solid black"
  120. return div
  121. }