Skip to content
This repository has been archived by the owner on May 23, 2020. It is now read-only.

Updated to newest Atom API #10

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
npm-debug.log
node_modules
9 changes: 5 additions & 4 deletions lib/npm-docs-view.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ScrollView} = require 'atom'
{ScrollView} = require 'atom-space-pen-views'

module.exports =
class NpmDocsView extends ScrollView
Expand All @@ -22,12 +22,13 @@ class NpmDocsView extends ScrollView
path: @path

handleEvents: ->
@subscribe this, 'core:move-up', => @scrollUp()
@subscribe this, 'core:move-down', => @scrollDown()
@on 'core:move-up', this, => @scrollUp()
@on 'core:move-down',this, => @scrollDown()

# Tear down any state and detach
destroy: ->
@unsubscribe()
@off('core:move-up',this)
@off('core:move-down',this)

getTitle: ->
"npm-docs: #{@path}"
45 changes: 24 additions & 21 deletions lib/npm-docs.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,42 @@ module.exports =
npmDocsView: null

activate: (state) ->
atom.workspace.registerOpener (uriToOpen) ->
atom.workspace.addOpener (uriToOpen) ->
{protocol, host} = url.parse(uriToOpen)
return unless protocol is 'npm-docs:'
new NpmDocsView(host)

atom.workspaceView.command "npm-docs:open", =>
open("https://npmjs.org/package/#{@getSelection()}")
atom.commands.add 'atom-workspace',
"npm-docs:open": =>
selection = @getSelection()
if(selection.trim() == '') then return
open("https://npmjs.org/package/#{selection}")

atom.workspaceView.command "npm-docs:homepage", =>
@search (err, json, selection) ->
if (err) then throw err
open(json.homepage)
"npm-docs:homepage": =>
@search (err, json, selection) ->
if (err) then throw err
open(json.homepage)

atom.workspaceView.command "npm-docs:readme", =>
@search (err, json, selection) ->
if (err) then throw err
markdown = json.readme
if !markdown then return
roaster markdown, {}, (err, contents) ->
"npm-docs:readme": =>
@search (err, json, selection) ->
if (err) then throw err
uri = "npm-docs://#{selection}"
previousActivePane = atom.workspace.getActivePane()
atom.workspace.open(uri, split: 'right', searchAllPanes: true).done (npmDocsView) ->
npmDocsView.renderContents(contents)
previousActivePane.activate()
markdown = json.readme
if !markdown then return
roaster markdown, {}, (err, contents) ->
if (err) then throw err
uri = "npm-docs://#{selection}"
previousActivePane = atom.workspace.getActivePane()
atom.workspace.open(uri, split: 'right', searchAllPanes: true).done (npmDocsView) ->
npmDocsView.renderContents(contents)
previousActivePane.activate()

getSelection: ->
editor = atom.workspace.getActiveEditor()
editor.getSelection().getText() || editor.getWordUnderCursor()
editor = atom.workspace.getActiveTextEditor()
editor.getSelectedText() || editor.getWordUnderCursor()

search: (cb) ->
selection = @getSelection()

if(selection.trim() == '') then return
request.get "https://registry.npmjs.org/#{selection}", (err, res) ->
if (err) then throw err

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "npm-docs",
"main": "./lib/npm-docs",
"version": "0.2.0",
"version": "0.3.1",
"private": true,
"description": "Helpful information for node modules for the Atom text editor",
"activationEvents": [
Expand All @@ -16,6 +16,7 @@
"atom": ">0.50.0"
},
"dependencies": {
"atom-space-pen-views": "^2.2.0",
"request": "~2.34.0",
"roaster": "~1.0.1",
"open": "0.0.4"
Expand Down
25 changes: 14 additions & 11 deletions spec/npm-docs-spec.coffee
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
NpmDocs = require '../lib/npm-docs'
{$} = require 'atom-space-pen-views'

# Use the command `window:run-package-specs` (cmd-alt-ctrl-p) to run specs.
#
# To run a specific `it` or `describe` block add an `f` to the front (e.g. `fit`
# or `fdescribe`). Remove the `f` to unfocus the block.

describe "NpmDocs", ->
activationPromise = null
loadpackage = null

beforeEach ->
atom.workspaceView = new WorkspaceView
activationPromise = atom.packages.activatePackage('npmDocs')

atom.workspaceView = atom.views.getView(atom.workspace)
loadpackage = atom.packages.enablePackage("npm-docs")
console.log loadpackage
describe "when the npm-docs:search event is triggered", ->
it "attaches and then detaches the view", ->
expect(atom.workspaceView.find('.npm-docs')).not.toExist()
expect($(atom.workspaceView).find('.npm-docs')).not.toExist()

# This is an activation event, triggering it will cause the package to be
# activated.
atom.workspaceView.trigger 'npm-docs:toggle'
atom.commands.dispatch atom.workspaceView, 'npm-docs:toggle'

waitsForPromise ->
activationPromise
#waitsForPromise ->
#loadpackage.activationPromise

runs ->
expect(atom.workspaceView.find('.npm-docs')).toExist()
atom.workspaceView.trigger 'npm-docs:search'
expect(atom.workspaceView.find('.npm-docs')).not.toExist()
console.log loadpackage
#console.log $(atom.workspaceView).find('.npm-docs')
expect($(atom.workspaceView).find('.npm-docs')).toExist()
atom.commands.dispatch atom.workspaceView, 'npm-docs:search'
expect($(atom.workspaceView).find('.npm-docs')).not.toExist()