Skip to content

Commit 8a959cd

Browse files
committed
fixup: bug: cursor not to end on restoring
1 parent e38f9da commit 8a959cd

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

Python/karaxpython.nim

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ var historyNode: Node
6161
type HistoryTrackPos = object
6262
offset: Natural ## neg order
6363

64-
proc reset(self: var HistoryTrackPos) = self.offset = 0
64+
proc reset*(self: var HistoryTrackPos) = self.offset = 0
6565

6666
proc stepToPastImpl(self: var HistoryTrackPos) =
6767
let hi = stream.high
@@ -76,8 +76,37 @@ proc stepToNowImpl(self: var HistoryTrackPos) =
7676
template getHistoryRecord(self: HistoryTrackPos): untyped =
7777
stream[stream.high - self.offset]
7878

79+
{.push noconv.}
80+
proc createRange(doc: Document): Range{.importcpp.}
81+
proc setStart(rng: Range, node: Node, pos: int) {.importcpp.}
82+
proc collapse(rng: Range, b: bool) {.importcpp.}
83+
proc addRange(s: Selection, rng: Range){.importcpp.}
84+
{.pop.}
85+
86+
proc setCursorPos(element: Node, position: int) =
87+
## .. note:: position is starting from 1, not 0
88+
# from JS code:
89+
# Create a new range
90+
let range = document.createRange()
91+
92+
# Get the text node
93+
let textNode = element.firstChild
94+
95+
# Set the position
96+
range.setStart(textNode, position)
97+
range.collapse(true)
98+
99+
# Apply the selection
100+
let selection = document.getSelection()
101+
selection.removeAllRanges()
102+
selection.addRange(range)
103+
104+
# Focus the element
105+
element.focus();
106+
107+
79108
template genStep(pastOrNext){.dirty.} =
80-
proc `stepTo pastOrNext`*(self: var HistoryTrackPos, n: var Node) =
109+
proc `stepTo pastOrNext`*(self: var HistoryTrackPos, input: var Node) =
81110
self.`stepTo pastOrNext Impl`
82111
var tup: tuple[prompt, info: kstring]
83112

@@ -87,14 +116,18 @@ template genStep(pastOrNext){.dirty.} =
87116
self.`stepTo pastOrNext Impl`
88117
tup = self.getHistoryRecord
89118

90-
n.innerHtml = tup.info
119+
let hisInp = tup.info
120+
input.innerHTML = hisInp
121+
# set cursor to end (otherwise it'll just be at the begining)
122+
let le = hisInp.len # XXX: suitable for Unicode?
123+
input.setCursorPos(le)
91124

92125
genStep Past
93126
genStep Now
94127

95128
var historyInputPos: HistoryTrackPos
96129

97-
# TODO: arrow-up / arrow-down for history
130+
98131
proc pushHistory(prompt: kstring, exp: string) =
99132
stream.add (prompt, kstring exp)
100133

0 commit comments

Comments
 (0)