+ on_key_enter: (e) -> # enter key pressed
+ return unless @cursor_visible
+ cur_block = @cursor.n
+ loop
+ if cur_block.type is 'tag'
+ if is_display_block cur_block.el
+ break
+ return unless cur_block.parent?
+ cur_block = cur_block.parent
+ # find array to insert new element into
+ if cur_block.parent is @tree_parent # top-level
+ parent_el = @idoc.body
+ pc = @tree
+ else
+ parent_el = cur_block.parent.el
+ pc = cur_block.parent.children
+ # find index of current block in its parent
+ for n, i in pc
+ break if n is cur_block
+ i += 1 # we want to be after it
+ if i < pc.length
+ before = pc[i].el
+ else
+ before = null
+ # TODO if content after cursor
+ # TODO new block is empty
+ new_text = new peach_parser.Node 'text', text: ' '
+ new_node = new peach_parser.Node 'tag', name: 'p', parent: cur_block.parent, attrs: {style: 'white-space: pre-wrap'}, children: [new_text]
+ new_text.parent = new_node
+ new_text.el = domify @idoc, text: ' '
+ new_node.el = domify @idoc, p: style: 'white-space: pre-wrap', children: [new_text.el]
+ pc.splice i, 0, new_node
+ parent_el.insertBefore new_node.el, before
+ @changed()
+ new_cursor = new_cursor_position n: new_text, i: 0
+ throw 'bork bork' unless new_cursor?
+ @move_cursor new_cursor
+ # TODO move content past cursor into this new block
+ # unlike the global function, this takes a Node, not an element
+ is_display_block: (n) ->
+ # TODO stop calling global function, merge it into here, use iframe's window object
+ return false unless n.type is 'tag'
+ return is_display_block n.el
+ find_block_parent: (n) ->
+ loop
+ n = n.parent
+ return null unless n?
+ return n if @is_display_block n
+ return n if n is @tree_parent
+ return null
+ # return a flat array of nodes (text, <br>, and later also inline-block)
+ # that are flowing/wrapping together. n can be the containing block, or any
+ # element inside it.
+ get_text_run: (n) ->
+ ret = []
+ if @is_display_block n
+ block = n
+ else
+ block = @find_block_parent n
+ return ret unless block?
+ traverse_tree block.children, (n) =>
+ if n.type is 'text'
+ ret.push n
+ else if n.type is 'tag'
+ if n.name is 'br'
+ ret.push n
+ else
+ disp = @computed_style n
+ if disp is 'inline-block'
+ ret.push n
+ return false # not done traversing
+ return ret
+ _merge_left: (state) -> # helper for on_key_backspace
+ # the node prev to n was not prev to it a moment ago, merge with it if reasonable
+ pi = state.n.parent.children.indexOf(state.n)
+ if pi > 0
+ prev = state.n.parent.children[pi - 1]
+ if prev.type is 'text'
+ state.i = prev.text.length
+ prev.text = prev.el.textContent = prev.text + state.n.text
+ @remove_node state.n
+ state.n = prev
+ state.changed = true
+ # else # TODO merge possible consecutive matching inline tags at @cursor
+ return state
+ _remove_node_and_inline_parents: (n) -> # helper for on_key_backspace
+ block = @find_block_parent n
+ # delete text node
+ @remove_node n
+ # delete any inline parents
+ n = n.parent
+ while n? and n isnt block
+ while n.children.length > 0
+ @move_node n.children[0], n.parent, n
+ @remove_node n
+ n = n.parent
+ return
+ on_key_backspace: (e) ->
+ return unless @cursor?
+ new_cursor = null
+ run = null
+ changed = true
+ if @cursor.i is 0 # cursor is at start of text node
+ run ?= @get_text_run @cursor.n
+ run_i = run.indexOf(@cursor.n)
+ if run_i is 0 # if at start of text run
+ block = @find_block_parent @cursor.n
+ prev_cursor = find_prev_cursor_position @tree, n: @cursor.n, i: 0
+ if prev_cursor is null # if in first text run of document
+ # do nothing (there's nothing text-like to the left of the cursor)
+ return
+ # else merge with prev/outer text run
+ pcb = @find_block_parent prev_cursor.n
+ while block.children.length > 0
+ @move_node block.children[0], pcb
+ @remove_node block
+ # merge possible consecutive text nodes at @cursor
+ merge_state = n: @cursor.n
+ @_merge_left merge_state
+ @text_cleanup merge_state.n
+ new_cursor = new_cursor_position n: merge_state.n, i: merge_state.i
+ else # at start of text node, but not start of text run
+ prev = run[run_i - 1]
+ if prev.type is 'text' # if previous in text run is text
+ if prev.text.length is 1 # if emptying prev (in text run)
+ @_remove_node_and_inline_parents prev
+ merge_state = n: @cursor.n, i: @cursor.i
+ @_merge_left merge_state
+ @text_cleanup merge_state.n
+ new_cursor = new_cursor_position n: merge_state.n, i: merge_state.i
+ else # prev in run is text with muliple chars
+ # delete last character in prev
+ prev.text = prev.text.substr(0, prev.text.length - 1)
+ prev.el.textContent = prev.text
+ @text_cleanup @cursor.n
+ new_cursor = new_cursor_position n: @cursor.n, i: @cursor.i
+ else if prev.name is 'br' or prev.name is 'hr'
+ @_remove_node_and_inline_parents prev
+ merge_state = n: @cursor.n, i: @cursor.i
+ @_merge_left merge_state
+ @text_cleanup merge_state.n
+ new_cursor = new_cursor_position n: merge_state.n, i: merge_state.i
+ # FIXME CONTINUE
+ # else # if prev (in run) is inline-block
+ # if that inline-block has text in it
+ # delete last char in prev inlineblock
+ # if that empties it
+ # delete it
+ # merge left
+ # else
+ # move cursor inside
+ # else
+ # delete prev (inline) block
+ # merge left
+ # auto-delete this @cursor.parent(s) if this empties them
+ else # cursor is not at start of text node
+ run ?= @get_text_run @cursor.n
+ if @cursor.n.text.length is 1 # if emptying text node
+ if run.length is 1 # if emptying text run (of text/br/hr/inline-block)
+ # remove inline-parents of @cursor.n
+ block = @find_block_parent @cursor.n
+ changed = false
+ n = @cursor.n.parent
+ while n and n isnt block
+ changed = true
+ while n.children.length > 0
+ @move_node n.children[0], n.parent, n
+ @remove_node n
+ n = n.parent
+ # replace @cursor.n with a single (preserved) space
+ if @cursor.n.text != ' '
+ changed = true
+ @cursor.n.text = @cursor.n.el.textContent = ' '
+ if changed
+ @text_cleanup @cursor.n
+ # place the cursor to the left of that space
+ new_cursor = new_cursor_position n: @cursor.n, i: 0
+ else # emptying a text node (but not a whole text run)
+ # figure out where cursor should land
+ block = @find_block_parent @cursor.n
+ new_cursor = find_prev_cursor_position @tree, n: @cursor.n, i: 0
+ ncb = @find_block_parent new_cursor
+ if ncb isnt block
+ new_cursor = find_next_cursor_position @tree, n: @cursor.n, i: 1
+ # delete text node
+ @remove_node @cursor.n
+ # delete any inline parents
+ n = @cursor.n.parent
+ while n and n isnt block
+ while n.children.length > 0
+ @move_node n.children[0], n.parent, n
+ @remove_node n
+ n = n.parent
+ # update cursor dest in case things moved around
+ if new_cursor?
+ new_cursor = new_cursor_position n: new_cursor.n, i: new_cursor.i
+ else # there's a char left of cursor that we can delete without emptying anything
+ # delete character
+ need_text_cleanup = true
+ if @cursor.i > 1 and @cursor.i < @cursor.n.text.length
+ pre = @cursor.n.text.substr(@cursor.i - 2, 3)
+ post = pre.charAt(0) + pre.charAt(2)
+ if str_has_ws_run(pre) is str_has_ws_run(post)
+ need_text_cleanup = false
+ @remove_character(@cursor.n, @cursor.i - 1)
+ # call text_cleanup if whe created/removed a whitespace run
+ if need_text_cleanup
+ @text_cleanup @cursor.n
+ new_cursor = new_cursor_position n: @cursor.n, i: @cursor.i - 1
+ # mark document changed and move the cursor
+ if changed?
+ @changed()
+ if new_cursor?
+ @move_cursor new_cursor
+ else
+ @kill_cursor()
+ return
+ on_page_up_key: (e) ->
+ if @wrap2.scrollTop is 0
+ return unless @cursor?
+ new_cursor = first_cursor_position @tree
+ if new_cursor?
+ if new_cursor.n isnt @cursor.n or new_cursor.i isnt @cursor.i
+ @move_cursor new_cursor
+ return
+ if @cursor?
+ screen_y = @cursor.y - @wrap2.scrollTop
+ scroll_amount = @wrap2_height - breathing_room
+ @wrap2.scrollTop = Math.max 0, @wrap2.scrollTop - scroll_amount
+ if @cursor?
+ @move_cursor_into_view screen_y + @wrap2.scrollTop
+ on_page_down_key: (e) ->
+ lowest_scrollpos = @wrap2.scrollHeight - @wrap2_height
+ if @wrap2.scrollTop is lowest_scrollpos
+ return unless @cursor?
+ new_cursor = last_cursor_position @tree
+ if new_cursor?
+ if new_cursor.n isnt @cursor.n or new_cursor.i isnt @cursor.i
+ @move_cursor new_cursor
+ return
+ if @cursor?
+ screen_y = @cursor.y - @wrap2.scrollTop
+ scroll_amount = @wrap2_height - breathing_room
+ @wrap2.scrollTop = Math.min lowest_scrollpos, @wrap2.scrollTop + scroll_amount
+ if @cursor?
+ @move_cursor_into_view screen_y + @wrap2.scrollTop
+ return
+ move_cursor_into_view: (y_target) ->
+ return if y_target is @cursor.y
+ was = @cursor
+ y_min = @wrap2.scrollTop
+ unless @wrap2.scrollTop is 0
+ y_min += breathing_room
+ y_max = @wrap2.scrollTop + @wrap2_height
+ unless @wrap2.scrollTop is @wrap2.scrollHeight - @wrap2_height # downmost
+ y_max -= breathing_room
+ y_target = Math.min y_target, y_max
+ y_target = Math.max y_target, y_min
+ if y_target < @cursor.y
+ finder = find_up_cursor_position
+ far_enough = (cur, target_y) ->
+ return cur.y + cur.h <= target_y
+ else
+ finder = find_down_cursor_position
+ far_enough = (cur, y_target) ->
+ return cur.y >= y_target
+ loop
+ cur = finder @tree, was, @cursor_ideal_x
+ break unless cur?
+ break if far_enough cur, y_target
+ was = cur
+ if was is @cursor
+ was = null
+ if was?
+ if was.y + was.h > y_max
+ was = null
+ else if was.y < y_min
+ was = null
+ if cur?
+ if cur.y + cur.h > y_max
+ cur = null
+ else if cur.y < y_min
+ cur = null
+ if cur? and was?
+ # both valid, pick best
+ if cur.y < y_min
+ new_cursor = was
+ else if was.y + was.h > y_max
+ new_cursor = cur
+ else if cur.y - y_target < y_target - was.y
+ new_cursor = cur
+ else
+ new_cursor = was
+ else
+ new_cursor = was ? cur
+ if new_cursor?
+ saved_ideal_x = @cursor_ideal_x
+ @move_cursor new_cursor
+ @cursor_ideal_x = saved_ideal_x
+ return