Get path of directory from 'right-click' event


#1

Hello,

I am developing my first package for Atom and I am looking for guidance. I have been following the sample package but it only got me so far. So I am looking for help from the community.

For my first step, I want to get the path of the directory when the user right-clicks anywhere on any directory from the tree-view. Here is what I have written so far.

./package.json

{
  "name": "atom-gi",
  "main": "./lib/atom-gi",
  "version": "0.0.0",
  "description": "A short description of your package",
  "keywords": [
  ],
  "activationCommands": {
    "atom-workspace": "atom-gi:create"
  },
  "repository": "https://github.com/atom/atom-gi",
  "license": "MIT",
  "engines": {
    "atom": ">=1.0.0 <2.0.0"
  },
  "dependencies": {
  }
}

./lib/atom-gi.coffee

AtomGiView = require './atom-gi-view'
{CompositeDisposable} = require 'atom'

module.exports = AtomGi =
  atomGiView: null
  modalPanel: null
  subscriptions: null

  activate: (state) ->
    @atomGiView = new AtomGiView(state.atomGiViewState)
    @modalPanel = atom.workspace.addModalPanel(item: @atomGiView.getElement(), visible: false)

    # Events subscribed to in atom's system can be easily cleaned up with a CompositeDisposable
    @subscriptions = new CompositeDisposable

    # Register commands that create .gitignore file
    @subscriptions.add atom.commands.add 'atom-workspace .tree-view .directory .header .name', 'atom-gi:create', @create
    
  deactivate: ->
    @modalPanel.destroy()
    @subscriptions.dispose()
    @atomGiView.destroy()

  serialize: ->
    atomGiViewState: @atomGiView.serialize()

  create: (event) =>
    console.log 'atom-gi called from Tree View'
    console.log(event.target)

./menu/atom-gi.cson

'context-menu':
  '.tree-view .directory .header .name': [
    {
      'label': 'Create .gitignore file'
      'command': 'atom-gi:create'
    }
  ]
  1. This only allows me to get the ‘Create .gitignore file’ right-click menu option on folders. How can I show the option when the user clicks anywhere in the directory.
  2. I need to fetch the path of directory that was right-clicked in the previous step. With what I have, I get this output
atom-gi called from Tree View

<span class="name icon icon-file-directory" data-name="atom-gi" title="atom-gi" data-path="/Users/hasitmistry/github/atom-gi">atom-gi</span>

Help will be greatly appreciated.


#2

Your selector only targets .name elements that are children of .header elements. .tree-view .name would cover files and folders.