comparison .cms/lib/codemirror/src/input/movement.js @ 0:78edf6b517a0 draft

24.10
author Coffee CMS <info@coffee-cms.ru>
date Fri, 11 Oct 2024 22:40:23 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:78edf6b517a0
1 import { Pos } from "../line/pos.js"
2 import { prepareMeasureForLine, measureCharPrepared, wrappedLineExtentChar } from "../measurement/position_measurement.js"
3 import { getBidiPartAt, getOrder } from "../util/bidi.js"
4 import { findFirst, lst, skipExtendingChars } from "../util/misc.js"
5
6 function moveCharLogically(line, ch, dir) {
7 let target = skipExtendingChars(line.text, ch + dir, dir)
8 return target < 0 || target > line.text.length ? null : target
9 }
10
11 export function moveLogically(line, start, dir) {
12 let ch = moveCharLogically(line, start.ch, dir)
13 return ch == null ? null : new Pos(start.line, ch, dir < 0 ? "after" : "before")
14 }
15
16 export function endOfLine(visually, cm, lineObj, lineNo, dir) {
17 if (visually) {
18 if (cm.doc.direction == "rtl") dir = -dir
19 let order = getOrder(lineObj, cm.doc.direction)
20 if (order) {
21 let part = dir < 0 ? lst(order) : order[0]
22 let moveInStorageOrder = (dir < 0) == (part.level == 1)
23 let sticky = moveInStorageOrder ? "after" : "before"
24 let ch
25 // With a wrapped rtl chunk (possibly spanning multiple bidi parts),
26 // it could be that the last bidi part is not on the last visual line,
27 // since visual lines contain content order-consecutive chunks.
28 // Thus, in rtl, we are looking for the first (content-order) character
29 // in the rtl chunk that is on the last line (that is, the same line
30 // as the last (content-order) character).
31 if (part.level > 0 || cm.doc.direction == "rtl") {
32 let prep = prepareMeasureForLine(cm, lineObj)
33 ch = dir < 0 ? lineObj.text.length - 1 : 0
34 let targetTop = measureCharPrepared(cm, prep, ch).top
35 ch = findFirst(ch => measureCharPrepared(cm, prep, ch).top == targetTop, (dir < 0) == (part.level == 1) ? part.from : part.to - 1, ch)
36 if (sticky == "before") ch = moveCharLogically(lineObj, ch, 1)
37 } else ch = dir < 0 ? part.to : part.from
38 return new Pos(lineNo, ch, sticky)
39 }
40 }
41 return new Pos(lineNo, dir < 0 ? lineObj.text.length : 0, dir < 0 ? "before" : "after")
42 }
43
44 export function moveVisually(cm, line, start, dir) {
45 let bidi = getOrder(line, cm.doc.direction)
46 if (!bidi) return moveLogically(line, start, dir)
47 if (start.ch >= line.text.length) {
48 start.ch = line.text.length
49 start.sticky = "before"
50 } else if (start.ch <= 0) {
51 start.ch = 0
52 start.sticky = "after"
53 }
54 let partPos = getBidiPartAt(bidi, start.ch, start.sticky), part = bidi[partPos]
55 if (cm.doc.direction == "ltr" && part.level % 2 == 0 && (dir > 0 ? part.to > start.ch : part.from < start.ch)) {
56 // Case 1: We move within an ltr part in an ltr editor. Even with wrapped lines,
57 // nothing interesting happens.
58 return moveLogically(line, start, dir)
59 }
60
61 let mv = (pos, dir) => moveCharLogically(line, pos instanceof Pos ? pos.ch : pos, dir)
62 let prep
63 let getWrappedLineExtent = ch => {
64 if (!cm.options.lineWrapping) return {begin: 0, end: line.text.length}
65 prep = prep || prepareMeasureForLine(cm, line)
66 return wrappedLineExtentChar(cm, line, prep, ch)
67 }
68 let wrappedLineExtent = getWrappedLineExtent(start.sticky == "before" ? mv(start, -1) : start.ch)
69
70 if (cm.doc.direction == "rtl" || part.level == 1) {
71 let moveInStorageOrder = (part.level == 1) == (dir < 0)
72 let ch = mv(start, moveInStorageOrder ? 1 : -1)
73 if (ch != null && (!moveInStorageOrder ? ch >= part.from && ch >= wrappedLineExtent.begin : ch <= part.to && ch <= wrappedLineExtent.end)) {
74 // Case 2: We move within an rtl part or in an rtl editor on the same visual line
75 let sticky = moveInStorageOrder ? "before" : "after"
76 return new Pos(start.line, ch, sticky)
77 }
78 }
79
80 // Case 3: Could not move within this bidi part in this visual line, so leave
81 // the current bidi part
82
83 let searchInVisualLine = (partPos, dir, wrappedLineExtent) => {
84 let getRes = (ch, moveInStorageOrder) => moveInStorageOrder
85 ? new Pos(start.line, mv(ch, 1), "before")
86 : new Pos(start.line, ch, "after")
87
88 for (; partPos >= 0 && partPos < bidi.length; partPos += dir) {
89 let part = bidi[partPos]
90 let moveInStorageOrder = (dir > 0) == (part.level != 1)
91 let ch = moveInStorageOrder ? wrappedLineExtent.begin : mv(wrappedLineExtent.end, -1)
92 if (part.from <= ch && ch < part.to) return getRes(ch, moveInStorageOrder)
93 ch = moveInStorageOrder ? part.from : mv(part.to, -1)
94 if (wrappedLineExtent.begin <= ch && ch < wrappedLineExtent.end) return getRes(ch, moveInStorageOrder)
95 }
96 }
97
98 // Case 3a: Look for other bidi parts on the same visual line
99 let res = searchInVisualLine(partPos + dir, dir, wrappedLineExtent)
100 if (res) return res
101
102 // Case 3b: Look for other bidi parts on the next visual line
103 let nextCh = dir > 0 ? wrappedLineExtent.end : mv(wrappedLineExtent.begin, -1)
104 if (nextCh != null && !(dir > 0 && nextCh == line.text.length)) {
105 res = searchInVisualLine(dir > 0 ? 0 : bidi.length - 1, dir, getWrappedLineExtent(nextCh))
106 if (res) return res
107 }
108
109 // Case 4: Nowhere to move
110 return null
111 }