about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2021-12-20 18:33:32 +0100
committerbptato <nincsnevem662@gmail.com>2021-12-20 18:33:32 +0100
commitcbcdafaf3fab6129c8c387bc352bd1f17f663684 (patch)
tree3cc26d9d296d83708d9a5bd99670058310ccadee /src
parent8f4b4977eed45bbe961a9fe3f7d75d93f53015c4 (diff)
downloadchawan-cbcdafaf3fab6129c8c387bc352bd1f17f663684.tar.gz
Implement gotoAnchor
Diffstat (limited to 'src')
-rw-r--r--src/html/dom.nim5
-rw-r--r--src/io/buffer.nim24
2 files changed, 21 insertions, 8 deletions
diff --git a/src/html/dom.nim b/src/html/dom.nim
index 85a48174..faa3c846 100644
--- a/src/html/dom.nim
+++ b/src/html/dom.nim
@@ -306,3 +306,8 @@ func newAttr*(parent: Element, key, value: string): Attr =
   result.ownerElement = parent
   result.name = key
   result.value = value
+
+func getElementById*(document: Document, id: string): Element =
+  if id.len == 0 or id notin document.id_elements:
+    return nil
+  return document.id_elements[id][0]
diff --git a/src/io/buffer.nim b/src/io/buffer.nim
index b4d19e36..922d45ac 100644
--- a/src/io/buffer.nim
+++ b/src/io/buffer.nim
@@ -677,13 +677,20 @@ proc scrollLeft*(buffer: Buffer) =
       buffer.cursorLeft()
     buffer.redraw = true
 
-proc gotoAnchor*(buffer: Buffer): bool =
-  discard
-  #TODO
-  #if buffer.location.anchor != "":
-  #  let node =  buffer.getElementById(buffer.location.anchor)
-  #  if node != nil:
-  #    buffer.scrollTo(max(node.y - buffer.height div 2, 0))
+proc gotoAnchor*(buffer: Buffer, id: string) =
+  let anchor = buffer.document.getElementById(id)
+  if anchor == nil: return
+  for y in 0..(buffer.numLines - 1):
+    let line = buffer.lines[y]
+    var i = 0
+    while i < line.formats.len:
+      let format = line.formats[i]
+      if anchor in format.nodes:
+        buffer.setCursorY(y)
+        buffer.centerLine()
+        buffer.setCursorXB(format.pos)
+        return
+      inc i
 
 proc setLocation*(buffer: Buffer, uri: Uri) =
   buffer.location = uri
@@ -1018,7 +1025,8 @@ proc inputLoop(attrs: TermAttributes, buffer: Buffer): bool =
       nostatus = false
 
 proc displayPage*(attrs: TermAttributes, buffer: Buffer): bool =
-  discard buffer.gotoAnchor()
+  eprint buffer.location.anchor
+  buffer.gotoAnchor(buffer.location.anchor)
   buffer.refreshDisplay()
   buffer.displayBuffer()
   buffer.updateHover()