       atom.notifications.addInfo('You need to start markdown-preview-enhanced preview first')
 
   runAllCodeChunks: ()->
-    editor = atom.workspace.getActiveTextEditor()
+    editor = atom.workspace.getActivePaneItem()
     preview = @getPreviewForEditor(editor)
     if preview?.isOnDom()
       preview.runAllCodeChunks()

    # console.log 'deactivate markdown-preview-enhanced'

  toggle: ->
    editor = atom.workspace.getActiveTextEditor()
    preview = @getPreviewForEditor(editor)
    if preview?.isOnDom()
      pane = atom.workspace.paneForItem(preview)
      pane.destroyItem(preview) # this will trigger preview.destroy()
      delete @previewsMap[editor.getPath()]
    else
      ## check if it is valid markdown file
      @startMDPreview(editor)

  getPreviewForEditor: (editor)->
    if @singlePreview
      return @getSinglePreview()
    else if editor.getURI?().startsWith('markdown-preview-enhanced://')
      return editor
    else
      return @previewsMap[editor?.getPath?()]

