X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=editor.coffee;h=9e2154460af8520701db0c3dbc707e688cf7f6c6;hb=3b0ea1cf0431211db79091277e2dec49f2c1bbd3;hp=1b2503e051afcd8484f3aaf14796ef8a29a98d54;hpb=0e48ade448937b84f60cb57d13aa9851e2eb3976;p=peach-html5-editor.git diff --git a/editor.coffee b/editor.coffee index 1b2503e..9e21544 100644 --- a/editor.coffee +++ b/editor.coffee @@ -917,6 +917,9 @@ class PeachHTML5Editor @move_cursor new_cursor return false when KEY_END + new_cursor = last_cursor_position @tree + if new_cursor? + @move_cursor new_cursor return false when KEY_BACKSPACE return false unless @cursor? @@ -943,10 +946,15 @@ class PeachHTML5Editor @kill_cursor() return false when KEY_ENTER + @on_key_enter e return false when KEY_ESCAPE + @kill_cursor() return false when KEY_HOME + new_cursor = first_cursor_position @tree + if new_cursor? + @move_cursor new_cursor return false when KEY_INSERT return false @@ -972,6 +980,43 @@ class PeachHTML5Editor console.log "ERROR: couldn't find cursor position after insert" @kill_cursor() return false + 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?.el? + parent_el = cur_block.parent.el + pc = cur_block.parent.children + else + parent_el = @idoc.body + pc = @tree + # 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 + 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 clear_dom: -> # remove all the editable content (and cursor, overlays, etc) while @idoc.body.childNodes.length @idoc.body.removeChild @idoc.body.childNodes[0] @@ -1066,8 +1111,22 @@ class PeachHTML5Editor return unless parent.el? # insert the character if i is 0 - n.text = char + n.text + # special case: typing before a space that's the only thing in a block/doc + # reason: enter key creates blocks with just a space in them + special_case = false + if n.text is ' ' + if n.parent?.el? + if n.parent.children.length is 1 + if n.parent.children[0] is n + special_case = true + else + special_case = true + if special_case + n.text = char + else + n.text = char + n.text else if i is n.text.length + # replace the space n.text += char else n.text =