5c25cdd05521c2986e30e2a1e107e37696b7361947578c7c42f0d7c3aa1c6962ef0be968844a1f8173763d2363c688c16ed6fe374af61c31c59702c6130346 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. import { deleteNearSelection } from "./deleteNearSelection.js"
  2. import { commands } from "./commands.js"
  3. import { attachDoc } from "../model/document_data.js"
  4. import { activeElt, addClass, rmClass, root, win } from "../util/dom.js"
  5. import { eventMixin, signal } from "../util/event.js"
  6. import { getLineStyles, getContextBefore, takeToken } from "../line/highlight.js"
  7. import { indentLine } from "../input/indent.js"
  8. import { triggerElectric } from "../input/input.js"
  9. import { onKeyDown, onKeyPress, onKeyUp } from "./key_events.js"
  10. import { onMouseDown } from "./mouse_events.js"
  11. import { getKeyMap } from "../input/keymap.js"
  12. import { endOfLine, moveLogically, moveVisually } from "../input/movement.js"
  13. import { endOperation, methodOp, operation, runInOp, startOperation } from "../display/operations.js"
  14. import { clipLine, clipPos, equalCursorPos, Pos } from "../line/pos.js"
  15. import { charCoords, charWidth, clearCaches, clearLineMeasurementCache, coordsChar, cursorCoords, displayHeight, displayWidth, estimateLineHeights, fromCoordSystem, intoCoordSystem, scrollGap, textHeight } from "../measurement/position_measurement.js"
  16. import { Range } from "../model/selection.js"
  17. import { replaceOneSelection, skipAtomic } from "../model/selection_updates.js"
  18. import { addToScrollTop, ensureCursorVisible, scrollIntoView, scrollToCoords, scrollToCoordsRange, scrollToRange } from "../display/scrolling.js"
  19. import { heightAtLine } from "../line/spans.js"
  20. import { updateGutterSpace } from "../display/update_display.js"
  21. import { indexOf, insertSorted, isWordChar, sel_dontScroll, sel_move } from "../util/misc.js"
  22. import { signalLater } from "../util/operation_group.js"
  23. import { getLine, isLine, lineAtHeight } from "../line/utils_line.js"
  24. import { regChange, regLineChange } from "../display/view_tracking.js"
  25. // The publicly visible API. Note that methodOp(f) means
  26. // 'wrap f in an operation, performed on its `this` parameter'.
  27. // This is not the complete set of editor methods. Most of the
  28. // methods defined on the Doc type are also injected into
  29. // CodeMirror.prototype, for backwards compatibility and
  30. // convenience.
  31. export default function(CodeMirror) {
  32. let optionHandlers = CodeMirror.optionHandlers
  33. let helpers = CodeMirror.helpers = {}
  34. CodeMirror.prototype = {
  35. constructor: CodeMirror,
  36. focus: function(){win(this).focus(); this.display.input.focus()},
  37. setOption: function(option, value) {
  38. let options = this.options, old = options[option]
  39. if (options[option] == value && option != "mode") return
  40. options[option] = value
  41. if (optionHandlers.hasOwnProperty(option))
  42. operation(this, optionHandlers[option])(this, value, old)
  43. signal(this, "optionChange", this, option)
  44. },
  45. getOption: function(option) {return this.options[option]},
  46. getDoc: function() {return this.doc},
  47. addKeyMap: function(map, bottom) {
  48. this.state.keyMaps[bottom ? "push" : "unshift"](getKeyMap(map))
  49. },
  50. removeKeyMap: function(map) {
  51. let maps = this.state.keyMaps
  52. for (let i = 0; i < maps.length; ++i)
  53. if (maps[i] == map || maps[i].name == map) {
  54. maps.splice(i, 1)
  55. return true
  56. }
  57. },
  58. addOverlay: methodOp(function(spec, options) {
  59. let mode = spec.token ? spec : CodeMirror.getMode(this.options, spec)
  60. if (mode.startState) throw new Error("Overlays may not be stateful.")
  61. insertSorted(this.state.overlays,
  62. {mode: mode, modeSpec: spec, opaque: options && options.opaque,
  63. priority: (options && options.priority) || 0},
  64. overlay => overlay.priority)
  65. this.state.modeGen++
  66. regChange(this)
  67. }),
  68. removeOverlay: methodOp(function(spec) {
  69. let overlays = this.state.overlays
  70. for (let i = 0; i < overlays.length; ++i) {
  71. let cur = overlays[i].modeSpec
  72. if (cur == spec || typeof spec == "string" && cur.name == spec) {
  73. overlays.splice(i, 1)
  74. this.state.modeGen++
  75. regChange(this)
  76. return
  77. }
  78. }
  79. }),
  80. indentLine: methodOp(function(n, dir, aggressive) {
  81. if (typeof dir != "string" && typeof dir != "number") {
  82. if (dir == null) dir = this.options.smartIndent ? "smart" : "prev"
  83. else dir = dir ? "add" : "subtract"
  84. }
  85. if (isLine(this.doc, n)) indentLine(this, n, dir, aggressive)
  86. }),
  87. indentSelection: methodOp(function(how) {
  88. let ranges = this.doc.sel.ranges, end = -1
  89. for (let i = 0; i < ranges.length; i++) {
  90. let range = ranges[i]
  91. if (!range.empty()) {
  92. let from = range.from(), to = range.to()
  93. let start = Math.max(end, from.line)
  94. end = Math.min(this.lastLine(), to.line - (to.ch ? 0 : 1)) + 1
  95. for (let j = start; j < end; ++j)
  96. indentLine(this, j, how)
  97. let newRanges = this.doc.sel.ranges
  98. if (from.ch == 0 && ranges.length == newRanges.length && newRanges[i].from().ch > 0)
  99. replaceOneSelection(this.doc, i, new Range(from, newRanges[i].to()), sel_dontScroll)
  100. } else if (range.head.line > end) {
  101. indentLine(this, range.head.line, how, true)
  102. end = range.head.line
  103. if (i == this.doc.sel.primIndex) ensureCursorVisible(this)
  104. }
  105. }
  106. }),
  107. // Fetch the parser token for a given character. Useful for hacks
  108. // that want to inspect the mode state (say, for completion).
  109. getTokenAt: function(pos, precise) {
  110. return takeToken(this, pos, precise)
  111. },
  112. getLineTokens: function(line, precise) {
  113. return takeToken(this, Pos(line), precise, true)
  114. },
  115. getTokenTypeAt: function(pos) {
  116. pos = clipPos(this.doc, pos)
  117. let styles = getLineStyles(this, getLine(this.doc, pos.line))
  118. let before = 0, after = (styles.length - 1) / 2, ch = pos.ch
  119. let type
  120. if (ch == 0) type = styles[2]
  121. else for (;;) {
  122. let mid = (before + after) >> 1
  123. if ((mid ? styles[mid * 2 - 1] : 0) >= ch) after = mid
  124. else if (styles[mid * 2 + 1] < ch) before = mid + 1
  125. else { type = styles[mid * 2 + 2]; break }
  126. }
  127. let cut = type ? type.indexOf("overlay ") : -1
  128. return cut < 0 ? type : cut == 0 ? null : type.slice(0, cut - 1)
  129. },
  130. getModeAt: function(pos) {
  131. let mode = this.doc.mode
  132. if (!mode.innerMode) return mode
  133. return CodeMirror.innerMode(mode, this.getTokenAt(pos).state).mode
  134. },
  135. getHelper: function(pos, type) {
  136. return this.getHelpers(pos, type)[0]
  137. },
  138. getHelpers: function(pos, type) {
  139. let found = []
  140. if (!helpers.hasOwnProperty(type)) return found
  141. let help = helpers[type], mode = this.getModeAt(pos)
  142. if (typeof mode[type] == "string") {
  143. if (help[mode[type]]) found.push(help[mode[type]])
  144. } else if (mode[type]) {
  145. for (let i = 0; i < mode[type].length; i++) {
  146. let val = help[mode[type][i]]
  147. if (val) found.push(val)
  148. }
  149. } else if (mode.helperType && help[mode.helperType]) {
  150. found.push(help[mode.helperType])
  151. } else if (help[mode.name]) {
  152. found.push(help[mode.name])
  153. }
  154. for (let i = 0; i < help._global.length; i++) {
  155. let cur = help._global[i]
  156. if (cur.pred(mode, this) && indexOf(found, cur.val) == -1)
  157. found.push(cur.val)
  158. }
  159. return found
  160. },
  161. getStateAfter: function(line, precise) {
  162. let doc = this.doc
  163. line = clipLine(doc, line == null ? doc.first + doc.size - 1: line)
  164. return getContextBefore(this, line + 1, precise).state
  165. },
  166. cursorCoords: function(start, mode) {
  167. let pos, range = this.doc.sel.primary()
  168. if (start == null) pos = range.head
  169. else if (typeof start == "object") pos = clipPos(this.doc, start)
  170. else pos = start ? range.from() : range.to()
  171. return cursorCoords(this, pos, mode || "page")
  172. },
  173. charCoords: function(pos, mode) {
  174. return charCoords(this, clipPos(this.doc, pos), mode || "page")
  175. },
  176. coordsChar: function(coords, mode) {
  177. coords = fromCoordSystem(this, coords, mode || "page")
  178. return coordsChar(this, coords.left, coords.top)
  179. },
  180. lineAtHeight: function(height, mode) {
  181. height = fromCoordSystem(this, {top: height, left: 0}, mode || "page").top
  182. return lineAtHeight(this.doc, height + this.display.viewOffset)
  183. },
  184. heightAtLine: function(line, mode, includeWidgets) {
  185. let end = false, lineObj
  186. if (typeof line == "number") {
  187. let last = this.doc.first + this.doc.size - 1
  188. if (line < this.doc.first) line = this.doc.first
  189. else if (line > last) { line = last; end = true }
  190. lineObj = getLine(this.doc, line)
  191. } else {
  192. lineObj = line
  193. }
  194. return intoCoordSystem(this, lineObj, {top: 0, left: 0}, mode || "page", includeWidgets || end).top +
  195. (end ? this.doc.height - heightAtLine(lineObj) : 0)
  196. },
  197. defaultTextHeight: function() { return textHeight(this.display) },
  198. defaultCharWidth: function() { return charWidth(this.display) },
  199. getViewport: function() { return {from: this.display.viewFrom, to: this.display.viewTo}},
  200. addWidget: function(pos, node, scroll, vert, horiz) {
  201. let display = this.display
  202. pos = cursorCoords(this, clipPos(this.doc, pos))
  203. let top = pos.bottom, left = pos.left
  204. node.style.position = "absolute"
  205. node.setAttribute("cm-ignore-events", "true")
  206. this.display.input.setUneditable(node)
  207. display.sizer.appendChild(node)
  208. if (vert == "over") {
  209. top = pos.top
  210. } else if (vert == "above" || vert == "near") {
  211. let vspace = Math.max(display.wrapper.clientHeight, this.doc.height),
  212. hspace = Math.max(display.sizer.clientWidth, display.lineSpace.clientWidth)
  213. // Default to positioning above (if specified and possible); otherwise default to positioning below
  214. if ((vert == 'above' || pos.bottom + node.offsetHeight > vspace) && pos.top > node.offsetHeight)
  215. top = pos.top - node.offsetHeight
  216. else if (pos.bottom + node.offsetHeight <= vspace)
  217. top = pos.bottom
  218. if (left + node.offsetWidth > hspace)
  219. left = hspace - node.offsetWidth
  220. }
  221. node.style.top = top + "px"
  222. node.style.left = node.style.right = ""
  223. if (horiz == "right") {
  224. left = display.sizer.clientWidth - node.offsetWidth
  225. node.style.right = "0px"
  226. } else {
  227. if (horiz == "left") left = 0
  228. else if (horiz == "middle") left = (display.sizer.clientWidth - node.offsetWidth) / 2
  229. node.style.left = left + "px"
  230. }
  231. if (scroll)
  232. scrollIntoView(this, {left, top, right: left + node.offsetWidth, bottom: top + node.offsetHeight})
  233. },
  234. triggerOnKeyDown: methodOp(onKeyDown),
  235. triggerOnKeyPress: methodOp(onKeyPress),
  236. triggerOnKeyUp: onKeyUp,
  237. triggerOnMouseDown: methodOp(onMouseDown),
  238. execCommand: function(cmd) {
  239. if (commands.hasOwnProperty(cmd))
  240. return commands[cmd].call(null, this)
  241. },
  242. triggerElectric: methodOp(function(text) { triggerElectric(this, text) }),
  243. findPosH: function(from, amount, unit, visually) {
  244. let dir = 1
  245. if (amount < 0) { dir = -1; amount = -amount }
  246. let cur = clipPos(this.doc, from)
  247. for (let i = 0; i < amount; ++i) {
  248. cur = findPosH(this.doc, cur, dir, unit, visually)
  249. if (cur.hitSide) break
  250. }
  251. return cur
  252. },
  253. moveH: methodOp(function(dir, unit) {
  254. this.extendSelectionsBy(range => {
  255. if (this.display.shift || this.doc.extend || range.empty())
  256. return findPosH(this.doc, range.head, dir, unit, this.options.rtlMoveVisually)
  257. else
  258. return dir < 0 ? range.from() : range.to()
  259. }, sel_move)
  260. }),
  261. deleteH: methodOp(function(dir, unit) {
  262. let sel = this.doc.sel, doc = this.doc
  263. if (sel.somethingSelected())
  264. doc.replaceSelection("", null, "+delete")
  265. else
  266. deleteNearSelection(this, range => {
  267. let other = findPosH(doc, range.head, dir, unit, false)
  268. return dir < 0 ? {from: other, to: range.head} : {from: range.head, to: other}
  269. })
  270. }),
  271. findPosV: function(from, amount, unit, goalColumn) {
  272. let dir = 1, x = goalColumn
  273. if (amount < 0) { dir = -1; amount = -amount }
  274. let cur = clipPos(this.doc, from)
  275. for (let i = 0; i < amount; ++i) {
  276. let coords = cursorCoords(this, cur, "div")
  277. if (x == null) x = coords.left
  278. else coords.left = x
  279. cur = findPosV(this, coords, dir, unit)
  280. if (cur.hitSide) break
  281. }
  282. return cur
  283. },
  284. moveV: methodOp(function(dir, unit) {
  285. let doc = this.doc, goals = []
  286. let collapse = !this.display.shift && !doc.extend && doc.sel.somethingSelected()
  287. doc.extendSelectionsBy(range => {
  288. if (collapse)
  289. return dir < 0 ? range.from() : range.to()
  290. let headPos = cursorCoords(this, range.head, "div")
  291. if (range.goalColumn != null) headPos.left = range.goalColumn
  292. goals.push(headPos.left)
  293. let pos = findPosV(this, headPos, dir, unit)
  294. if (unit == "page" && range == doc.sel.primary())
  295. addToScrollTop(this, charCoords(this, pos, "div").top - headPos.top)
  296. return pos
  297. }, sel_move)
  298. if (goals.length) for (let i = 0; i < doc.sel.ranges.length; i++)
  299. doc.sel.ranges[i].goalColumn = goals[i]
  300. }),
  301. // Find the word at the given position (as returned by coordsChar).
  302. findWordAt: function(pos) {
  303. let doc = this.doc, line = getLine(doc, pos.line).text
  304. let start = pos.ch, end = pos.ch
  305. if (line) {
  306. let helper = this.getHelper(pos, "wordChars")
  307. if ((pos.sticky == "before" || end == line.length) && start) --start; else ++end
  308. let startChar = line.charAt(start)
  309. let check = isWordChar(startChar, helper)
  310. ? ch => isWordChar(ch, helper)
  311. : /\s/.test(startChar) ? ch => /\s/.test(ch)
  312. : ch => (!/\s/.test(ch) && !isWordChar(ch))
  313. while (start > 0 && check(line.charAt(start - 1))) --start
  314. while (end < line.length && check(line.charAt(end))) ++end
  315. }
  316. return new Range(Pos(pos.line, start), Pos(pos.line, end))
  317. },
  318. toggleOverwrite: function(value) {
  319. if (value != null && value == this.state.overwrite) return
  320. if (this.state.overwrite = !this.state.overwrite)
  321. addClass(this.display.cursorDiv, "CodeMirror-overwrite")
  322. else
  323. rmClass(this.display.cursorDiv, "CodeMirror-overwrite")
  324. signal(this, "overwriteToggle", this, this.state.overwrite)
  325. },
  326. hasFocus: function() { return this.display.input.getField() == activeElt(root(this)) },
  327. isReadOnly: function() { return !!(this.options.readOnly || this.doc.cantEdit) },
  328. scrollTo: methodOp(function (x, y) { scrollToCoords(this, x, y) }),
  329. getScrollInfo: function() {
  330. let scroller = this.display.scroller
  331. return {left: scroller.scrollLeft, top: scroller.scrollTop,
  332. height: scroller.scrollHeight - scrollGap(this) - this.display.barHeight,
  333. width: scroller.scrollWidth - scrollGap(this) - this.display.barWidth,
  334. clientHeight: displayHeight(this), clientWidth: displayWidth(this)}
  335. },
  336. scrollIntoView: methodOp(function(range, margin) {
  337. if (range == null) {
  338. range = {from: this.doc.sel.primary().head, to: null}
  339. if (margin == null) margin = this.options.cursorScrollMargin
  340. } else if (typeof range == "number") {
  341. range = {from: Pos(range, 0), to: null}
  342. } else if (range.from == null) {
  343. range = {from: range, to: null}
  344. }
  345. if (!range.to) range.to = range.from
  346. range.margin = margin || 0
  347. if (range.from.line != null) {
  348. scrollToRange(this, range)
  349. } else {
  350. scrollToCoordsRange(this, range.from, range.to, range.margin)
  351. }
  352. }),
  353. setSize: methodOp(function(width, height) {
  354. let interpret = val => typeof val == "number" || /^\d+$/.test(String(val)) ? val + "px" : val
  355. if (width != null) this.display.wrapper.style.width = interpret(width)
  356. if (height != null) this.display.wrapper.style.height = interpret(height)
  357. if (this.options.lineWrapping) clearLineMeasurementCache(this)
  358. let lineNo = this.display.viewFrom
  359. this.doc.iter(lineNo, this.display.viewTo, line => {
  360. if (line.widgets) for (let i = 0; i < line.widgets.length; i++)
  361. if (line.widgets[i].noHScroll) { regLineChange(this, lineNo, "widget"); break }
  362. ++lineNo
  363. })
  364. this.curOp.forceUpdate = true
  365. signal(this, "refresh", this)
  366. }),
  367. operation: function(f){return runInOp(this, f)},
  368. startOperation: function(){return startOperation(this)},
  369. endOperation: function(){return endOperation(this)},
  370. refresh: methodOp(function() {
  371. let oldHeight = this.display.cachedTextHeight
  372. regChange(this)
  373. this.curOp.forceUpdate = true
  374. clearCaches(this)
  375. scrollToCoords(this, this.doc.scrollLeft, this.doc.scrollTop)
  376. updateGutterSpace(this.display)
  377. if (oldHeight == null || Math.abs(oldHeight - textHeight(this.display)) > .5 || this.options.lineWrapping)
  378. estimateLineHeights(this)
  379. signal(this, "refresh", this)
  380. }),
  381. swapDoc: methodOp(function(doc) {
  382. let old = this.doc
  383. old.cm = null
  384. // Cancel the current text selection if any (#5821)
  385. if (this.state.selectingText) this.state.selectingText()
  386. attachDoc(this, doc)
  387. clearCaches(this)
  388. this.display.input.reset()
  389. scrollToCoords(this, doc.scrollLeft, doc.scrollTop)
  390. this.curOp.forceScroll = true
  391. signalLater(this, "swapDoc", this, old)
  392. return old
  393. }),
  394. phrase: function(phraseText) {
  395. let phrases = this.options.phrases
  396. return phrases && Object.prototype.hasOwnProperty.call(phrases, phraseText) ? phrases[phraseText] : phraseText
  397. },
  398. getInputField: function(){return this.display.input.getField()},
  399. getWrapperElement: function(){return this.display.wrapper},
  400. getScrollerElement: function(){return this.display.scroller},
  401. getGutterElement: function(){return this.display.gutters}
  402. }
  403. eventMixin(CodeMirror)
  404. CodeMirror.registerHelper = function(type, name, value) {
  405. if (!helpers.hasOwnProperty(type)) helpers[type] = CodeMirror[type] = {_global: []}
  406. helpers[type][name] = value
  407. }
  408. CodeMirror.registerGlobalHelper = function(type, name, predicate, value) {
  409. CodeMirror.registerHelper(type, name, value)
  410. helpers[type]._global.push({pred: predicate, val: value})
  411. }
  412. }
  413. // Used for horizontal relative motion. Dir is -1 or 1 (left or
  414. // right), unit can be "codepoint", "char", "column" (like char, but
  415. // doesn't cross line boundaries), "word" (across next word), or
  416. // "group" (to the start of next group of word or
  417. // non-word-non-whitespace chars). The visually param controls
  418. // whether, in right-to-left text, direction 1 means to move towards
  419. // the next index in the string, or towards the character to the right
  420. // of the current position. The resulting position will have a
  421. // hitSide=true property if it reached the end of the document.
  422. function findPosH(doc, pos, dir, unit, visually) {
  423. let oldPos = pos
  424. let origDir = dir
  425. let lineObj = getLine(doc, pos.line)
  426. let lineDir = visually && doc.direction == "rtl" ? -dir : dir
  427. function findNextLine() {
  428. let l = pos.line + lineDir
  429. if (l < doc.first || l >= doc.first + doc.size) return false
  430. pos = new Pos(l, pos.ch, pos.sticky)
  431. return lineObj = getLine(doc, l)
  432. }
  433. function moveOnce(boundToLine) {
  434. let next
  435. if (unit == "codepoint") {
  436. let ch = lineObj.text.charCodeAt(pos.ch + (dir > 0 ? 0 : -1))
  437. if (isNaN(ch)) {
  438. next = null
  439. } else {
  440. let astral = dir > 0 ? ch >= 0xD800 && ch < 0xDC00 : ch >= 0xDC00 && ch < 0xDFFF
  441. next = new Pos(pos.line, Math.max(0, Math.min(lineObj.text.length, pos.ch + dir * (astral ? 2 : 1))), -dir)
  442. }
  443. } else if (visually) {
  444. next = moveVisually(doc.cm, lineObj, pos, dir)
  445. } else {
  446. next = moveLogically(lineObj, pos, dir)
  447. }
  448. if (next == null) {
  449. if (!boundToLine && findNextLine())
  450. pos = endOfLine(visually, doc.cm, lineObj, pos.line, lineDir)
  451. else
  452. return false
  453. } else {
  454. pos = next
  455. }
  456. return true
  457. }
  458. if (unit == "char" || unit == "codepoint") {
  459. moveOnce()
  460. } else if (unit == "column") {
  461. moveOnce(true)
  462. } else if (unit == "word" || unit == "group") {
  463. let sawType = null, group = unit == "group"
  464. let helper = doc.cm && doc.cm.getHelper(pos, "wordChars")
  465. for (let first = true;; first = false) {
  466. if (dir < 0 && !moveOnce(!first)) break
  467. let cur = lineObj.text.charAt(pos.ch) || "\n"
  468. let type = isWordChar(cur, helper) ? "w"
  469. : group && cur == "\n" ? "n"
  470. : !group || /\s/.test(cur) ? null
  471. : "p"
  472. if (group && !first && !type) type = "s"
  473. if (sawType && sawType != type) {
  474. if (dir < 0) {dir = 1; moveOnce(); pos.sticky = "after"}
  475. break
  476. }
  477. if (type) sawType = type
  478. if (dir > 0 && !moveOnce(!first)) break
  479. }
  480. }
  481. let result = skipAtomic(doc, pos, oldPos, origDir, true)
  482. if (equalCursorPos(oldPos, result)) result.hitSide = true
  483. return result
  484. }
  485. // For relative vertical movement. Dir may be -1 or 1. Unit can be
  486. // "page" or "line". The resulting position will have a hitSide=true
  487. // property if it reached the end of the document.
  488. function findPosV(cm, pos, dir, unit) {
  489. let doc = cm.doc, x = pos.left, y
  490. if (unit == "page") {
  491. let pageSize = Math.min(cm.display.wrapper.clientHeight, win(cm).innerHeight || doc(cm).documentElement.clientHeight)
  492. let moveAmount = Math.max(pageSize - .5 * textHeight(cm.display), 3)
  493. y = (dir > 0 ? pos.bottom : pos.top) + dir * moveAmount
  494. } else if (unit == "line") {
  495. y = dir > 0 ? pos.bottom + 3 : pos.top - 3
  496. }
  497. let target
  498. for (;;) {
  499. target = coordsChar(cm, x, y)
  500. if (!target.outside) break
  501. if (dir < 0 ? y <= 0 : y >= doc.height) { target.hitSide = true; break }
  502. y += dir * 5
  503. }
  504. return target
  505. }