# implementation: insert a span tag where we want the cursor, and ask the
# browser where it put that span
cursor_to_loc = (n, i) ->
- span = domify span: style: "height: 1em"
+ span = domify span: style: "height: 1em; border-left: 1px solid black; margin-left: -1px"
parent = n.el.parentNode
if i is 0
# cursor at start of text
if c.children.length
instantiate_tree c.children, c.el
-find_next_cursor_position = (n, i) ->
- if n.type is TYPE_TEXT
- if n.text.length > i
- return [n, i + 1]
- return null
- if n.type is TYPE_TAG
- if n.children.length
- for c in n.children
- ret = find_next_cursor_position c, -1
- return ret if ret?
- return null
- else
- # FIXME create an empty text node if needed depending on tag name
+traverse_tree = (tree, state, cb) ->
+ for c in tree
+ cb c, state
+ break if state.done?
+ if c.children.length
+ traverse_tree c.children, state, cb
+ break if state.done?
+ return state
+# find the next element in top (and decendants) that is after n and can contain text
+# TODO make it so cursor can go places that don't have text but could
+find_next_cursor_position = (top, n, i) ->
+ if n? and n.type is TYPE_TEXT and n.text.length > i
+ return [n, i + 1]
+ found = traverse_tree top, before: n?, (node, state) ->
+ if node.type is TYPE_TEXT and state.before is false
+ state.node = node
+ state.done = true
+ if node is n
+ state.before = false
+ if found.node?
+ return [found.node, 0]
+ return null
+
+# TODO make it so cursor can go places that don't have text but could
+find_prev_cursor_position = (top, n, i) ->
+ if n? and n.type is TYPE_TEXT and i > 0
+ return [n, i - 1]
+ found = traverse_tree top, before: n?, (node, state) ->
+ if node.type is TYPE_TEXT
+ unless n?
+ state.node = node
+ state.done = true
+ if node is n
+ if state.prev?
+ state.node = state.prev
+ state.done = true
+ if node
+ state.prev = node
+ if found.node?
+ return [found.node, found.node.text.length]
return null
class PeachHTML5Editor
constructor: (in_el, options = {}) ->
@in_el = in_el
@tree = []
- @iframe = document.createElement('iframe')
+ @iframe = domify iframe: class: 'peach_html5_editor'
@cursor = null
@cursor_el = null
@cursor_visible = false
@parser_opts = {}
if opt_fragment
@parser_opts.fragment = 'body'
- @in_el.parentNode.appendChild @iframe
- @idoc = @iframe.contentDocument
- ignore_key_codes =
- '18': true # alt
- '20': true # capslock
- '17': true # ctrl
- '144': true # numlock
- '16': true # shift
- '91': true # windows "start" key
- control_key_codes = # we react to these, but they aren't typing
- '37': KEY_LEFT
- '38': KEY_UP
- '39': KEY_RIGHT
- '40': KEY_DOWN
- '35': KEY_END
- '8': KEY_BACKSPACE
- '46': KEY_DELETE
- '13': KEY_ENTER
- '27': KEY_ESCAPE
- '36': KEY_HOME
- '45': KEY_INSERT
- '33': KEY_PAGE_UP
- '34': KEY_PAGE_DOWN
- '9': KEY_TAB
+ @iframe.onload = =>
+ @idoc = @iframe.contentDocument
- @idoc.body.onkeyup = (e) =>
- return if e.ctrlKey
- return false if ignore_key_codes[e.keyCode]?
- #return false if control_key_codes[e.keyCode]?
- @idoc.body.onkeydown = (e) =>
- return if e.ctrlKey
- return false if ignore_key_codes[e.keyCode]?
- #return false if control_key_codes[e.keyCode]?
- switch e.keyCode
- when KEY_LEFT
- return false
- when KEY_UP
- return false
- when KEY_RIGHT
- window.ce = @cursor_el
- if @cursor?
- new_cursor = find_next_cursor_position @cursor...
- if new_cursor?
- @move_cursor new_cursor
- else
- after = false
- for c in @cursor[0].parent.children
- if after
- new_cursor = find_next_cursor_position c, -1
- if new_cursor?
- @move_cursor new_cursor
- break
- if c is @cursor[0]
- after = true
- else
- for c in @tree
- new_cursor = find_next_cursor_position c, -1
+ ignore_key_codes =
+ '18': true # alt
+ '20': true # capslock
+ '17': true # ctrl
+ '144': true # numlock
+ '16': true # shift
+ '91': true # windows "start" key
+ control_key_codes = # we react to these, but they aren't typing
+ '37': KEY_LEFT
+ '38': KEY_UP
+ '39': KEY_RIGHT
+ '40': KEY_DOWN
+ '35': KEY_END
+ '8': KEY_BACKSPACE
+ '46': KEY_DELETE
+ '13': KEY_ENTER
+ '27': KEY_ESCAPE
+ '36': KEY_HOME
+ '45': KEY_INSERT
+ '33': KEY_PAGE_UP
+ '34': KEY_PAGE_DOWN
+ '9': KEY_TAB
+
+ @idoc.body.onkeyup = (e) =>
+ return if e.ctrlKey
+ return false if ignore_key_codes[e.keyCode]?
+ #return false if control_key_codes[e.keyCode]?
+ @idoc.body.onkeydown = (e) =>
+ return if e.ctrlKey
+ return false if ignore_key_codes[e.keyCode]?
+ #return false if control_key_codes[e.keyCode]?
+ switch e.keyCode
+ when KEY_LEFT
+ if @cursor?
+ new_cursor = find_prev_cursor_position @tree, @cursor...
if new_cursor?
@move_cursor new_cursor
- break
- return false
- when KEY_DOWN
- return false
- when KEY_END
- return false
- when KEY_BACKSPACE
- return false
- when KEY_DELETE
- return false
- when KEY_ENTER
- return false
- when KEY_ESCAPE
- return false
- when KEY_HOME
- return false
- when KEY_INSERT
- return false
- when KEY_PAGE_UP
- return false
- when KEY_PAGE_DOWN
- return false
- when KEY_TAB
- return false
- @idoc.body.onkeypress = (e) =>
- return if e.ctrlKey
- return false if ignore_key_codes[e.keyCode]?
- # in firefox, keyCode is only set for non-typing keys
- switch e.keyCode
- when KEY_LEFT
- return false
- when KEY_UP
- return false
- when KEY_RIGHT
- console.log "hi"
- console.log @cursor
- if @cursor?
- new_cursor = find_next_cursor_position @cursor...
- if new_cursor?
- @move_cursor new_cursor
else
- after = false
- for c in @cursor[0].parent.children
- if after
- new_cursor = find_next_cursor_position c, -1
- if new_cursor?
- @move_cursor new_cursor
- break
- if c is @cursor[0]
- after = true
- else
- for c in @tree
- new_cursor = find_next_cursor_position c, -1
+ for c in @tree
+ new_cursor = find_next_cursor_position @tree, c, -1
+ if new_cursor?
+ @move_cursor new_cursor
+ break
+ return false
+ when KEY_UP
+ return false
+ when KEY_RIGHT
+ if @cursor?
+ new_cursor = find_next_cursor_position @tree, @cursor...
if new_cursor?
@move_cursor new_cursor
- break
- return false
- when KEY_DOWN
- return false
- when KEY_END
- return false
- when KEY_BACKSPACE
- return false
- when KEY_DELETE
- return false
- when KEY_ENTER
- return false
- when KEY_ESCAPE
- return false
- when KEY_HOME
- return false
- when KEY_INSERT
- return false
- when KEY_PAGE_UP
- return false
- when KEY_PAGE_DOWN
- return false
- when KEY_TAB
- return false
- char = e.charCode ? e.keyCode
- @in_el.value += String.fromCharCode char
+ else
+ for c in @tree
+ new_cursor = find_prev_cursor_position @tree, c, -1
+ if new_cursor?
+ @move_cursor new_cursor
+ break
+ return false
+ when KEY_DOWN
+ return false
+ when KEY_END
+ return false
+ when KEY_BACKSPACE
+ return false
+ when KEY_DELETE
+ return false
+ when KEY_ENTER
+ return false
+ when KEY_ESCAPE
+ return false
+ when KEY_HOME
+ return false
+ when KEY_INSERT
+ return false
+ when KEY_PAGE_UP
+ return false
+ when KEY_PAGE_DOWN
+ return false
+ when KEY_TAB
+ return false
+ @idoc.body.onkeypress = (e) =>
+ return if e.ctrlKey
+ return false if ignore_key_codes[e.keyCode]?
+ return false if control_key_codes[e.keyCode]? # handled in keydown
+ char = e.charCode ? e.keyCode
+ if char and @cursor?
+ char = String.fromCharCode char
+ if @cursor[1] is 0
+ @cursor[0].text = char + @cursor[0].text
+ else if @cursor[1] is @cursor[0].text.length - 1
+ @cursor[0].text += char
+ else
+ @cursor[0].text =
+ @cursor[0].text.substr(0, @cursor[1]) +
+ char +
+ @cursor[0].text.substr(@cursor[1])
+ @cursor[0].el.nodeValue = @cursor[0].text
+ @move_cursor [@cursor[0], @cursor[1] + 1]
+ return false
+ if options.stylesheet # TODO test this
+ istyle = @idoc.createElement 'style'
+ istyle.setAttribute 'src', options.stylesheet
+ @idoc.head.appendChild istyle
+ icss = @idoc.createElement 'style'
+ icss.appendChild @idoc.createTextNode css
+ @idoc.head.appendChild icss
@load_html @in_el.value
- return false
- if options.stylesheet # TODO test this
- istyle = @idoc.createElement 'style'
- istyle.setAttribute 'src', options.stylesheet
- @idoc.head.appendChild istyle
- icss = @idoc.createElement 'style'
- icss.appendChild @idoc.createTextNode css
- @idoc.head.appendChild icss
- @load_html @in_el.value
+
+ @in_el.parentNode.appendChild @iframe
clear_dom: ->
# FIXME add parent node, so we don't empty body and delete cursor_el
while @idoc.body.childNodes.length
@clear_dom()
instantiate_tree @tree, @idoc.body
move_cursor: (cursor) ->
+ return if @cursor? and cursor? and @cursor[0] is cursor[0] and @cursor[1] is cursor[1]
@cursor = cursor
- unless @cursor_el?
- @cursor_el = domify div: id: 'peach_html5_editor_cursor'
- unless @cursor_visible
- @idoc.body.appendChild @cursor_el
- @cursor_visible = true
+ # replace cursor, to reset blink animation
+ if @cursor_visible
+ @cursor_el.parentNode.removeChild @cursor_el
+ @cursor_el = domify div: id: 'peach_html5_editor_cursor'
+ @idoc.body.appendChild @cursor_el
+ @cursor_visible = true
# TODO figure out top/left coords for cursor
loc = cursor_to_loc cursor[0], cursor[1]
@cursor_el.style.top = "#{loc.top}px"