           sync.saveSnapshot({id: chapter.snapshotId, html: node.innerHTML})
             .then (->) # TODO send a word count update
 
-
 saveHtmlAndLoadDoc = (html) ->
-  sync.saveFreshDoc(DocImport.docFromHtml html)
-    .then app.ports.loadAsCurrentDoc.send
+  sync.saveFreshDoc(DocImport.docFromHtml html).then loadAsCurrentDoc
 
 showFileChooser = ->
   new Promise (resolve, reject) ->

# Looks up the doc and snapshot associated with the given docId,
# writes the snapshot to the editor, and tells Elm about the new currentDocId
loadDocId = (docId) ->
  sync.getDoc(docId).then (doc) ->
    app.ports.loadAsCurrentDoc.send doc

    setUpEditor "edit-title", doc.title, (mutations, node) ->
      sync.getDoc(docId).then (doc) ->
        doc.title = node.textContent
        sync.saveDoc(doc).then -> app.ports.loadAsCurrentDoc.send doc

    setUpEditor "edit-description", doc.description, (mutations, node) ->
      sync.getDoc(docId).then (doc) ->
        doc.description = node.textContent
        sync.saveDoc(doc).then -> app.ports.loadAsCurrentDoc.send doc

    doc.chapters.forEach setUpChapter

setUpChapter = (chapter) ->
  chapterId = chapter.id

  setUpEditor "edit-chapter-heading-#{chapterId}", chapter.heading, (mutations, node) ->
    sync.getCurrentDocId().then (currentDocId) ->
      sync.getDoc(currentDocId).then (doc) ->
        for currentChapter in doc.chapters
          if currentChapter.id == chapterId
            currentChapter.heading = node.textContent

        sync.saveDoc(doc).then -> app.ports.loadAsCurrentDoc.send doc

