 getChapterBodyElem = (id) ->
   document.getElementById "edit-chapter-body-#{id}"
 
+getNoteTitleElem = ->
+  document.getElementById "current-note-title"
+
+getNoteBodyElem = ->
+  document.getElementById "current-note-body"
+
 deleteChapter = (chapter) ->
   # TODO focus on the END of the previous sibling, once I've figured out
   # how to do that reliably (e.g. when the user presses the up arrow...)


  sync.deleteChapter(chapter).done (newChapters) ->
    app.ports.setChapters.send newChapters

app.ports.setCurrentDocId.subscribe (newDocId) ->
  if newDocId?

  sync.saveNoteWithSnapshot(newNote, html).done (note) ->
    app.ports.setCurrentNote.send note

    setUpEditor getNoteTitleElem, note.title, false, (mutations, node) ->
      if (getDestructiveMutations(mutations).length > 0)
        note = {
          id:               note.id
          title:            node.textContent
          snapshotId:       note.snapshotId
          creationTime:     note.creationTime
          lastModifiedTime: new Date().getTime()
        }

        notes.save(note, getNoteBodyElem().innerHTML).done (savedNote) ->
          app.ports.setCurrentNote.send note

    setUpEditor getNoteBodyElem, html, true, (mutations, node) ->
      if (getDestructiveMutations(mutations).length > 0)
        note = {
          id:               note.id
          title:            note.title
          creationTime:     note.creationTime
          lastModifiedTime: new Date().getTime()
        }

        notes.save(note, node.innerHTML).done (savedNote) ->
          app.ports.setCurrentNote.send note

app.ports.execCommand.subscribe (command) ->
  document.execCommand command, false, null
