JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
partly implemented: click to place cursor
authorJason Woofenden <jason@jasonwoof.com>
Wed, 24 Feb 2016 04:50:01 +0000 (23:50 -0500)
committerJason Woofenden <jason@jasonwoof.com>
Wed, 24 Feb 2016 04:50:01 +0000 (23:50 -0500)
editor.coffee

index 863d1ff..81ee87e 100644 (file)
@@ -19,8 +19,20 @@ TYPE_TEXT = peach_parser.TYPE_TEXT
 TYPE_COMMENT = peach_parser.TYPE_COMMENT
 TYPE_DOCTYPE = peach_parser.TYPE_DOCTYPE
 
+# text nodes don't have getBoundingClientRect(), so wrap it in a span, measure
+# and then put it back
+get_text_bounding_rect = (el) ->
+       span = el.ownerDocument.createElement 'span'
+       el.parentNode.replaceChild span, el
+       span.appendChild el
+       ret = span.getBoundingClientRect()
+       span.parentNode.replaceChild el, span
+       return ret
 get_el_bounds = (el) ->
-       rect = el.getBoundingClientRect()
+       if el.getBoundingClientRect
+               rect = el.getBoundingClientRect()
+       else
+               rect = get_text_bounding_rect el
        doc = el.ownerDocument.documentElement
        win = el.ownerDocument.defaultView
        y_fix = win.pageYOffset - doc.clientTop
@@ -254,7 +266,6 @@ find_loc_cursor_position = (tree, loc) ->
        console.log tree, loc
        for c in tree
                if c.type is TYPE_TAG or c.type is TYPE_TEXT
-                       # FIXME get_el_bounds doesn't work on TYPE_TEXT
                        bounds = get_el_bounds c.el
                        console.log bounds
                        continue if loc.left < bounds.x
@@ -262,8 +273,21 @@ find_loc_cursor_position = (tree, loc) ->
                        continue if loc.top < bounds.y
                        continue if loc.top > bounds.y + bounds.h
                        if c.type is TYPE_TEXT
-                               # FIXME find position, don't just pass 0
-                               return [c, 0]
+                               # click is within bounding box that contains all text.
+                               return [c, 0] if c.text.length is 0
+                               #if c.text.length is 1
+                                       # definitely no linebreak in it
+                                       #if loc
+                               #
+                               # for inline elements, this can include text in other tags on
+                               # the first or last lines.
+                               #
+                               # they clicked on text. now determine where in the text
+                               tries = {}
+                               cur = Math.floor(c.text.length / 2)
+                               #loop
+                               #       xy = tries[cur] = cursor_to_loc c, cur
+                               return [c, cur]
                        if c.children.length
                                console.log "in"
                                ret = find_loc_cursor_position c.children, loc