Skip to content
This repository has been archived by the owner on Jun 13, 2022. It is now read-only.

Commit

Permalink
Merge pull request #85 from UziTech/function-type
Browse files Browse the repository at this point in the history
Allow custom function as callback
  • Loading branch information
cakecatz authored Nov 3, 2016
2 parents 1a3db5a + d76486e commit f293927
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 7 deletions.
73 changes: 69 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
This is a plugin for
the [Atom Tool Bar](https://atom.io/packages/tool-bar) package.

You can configure your toolbar buttons with a `CSON`, `JSON`, `JSON5` file
You can configure your toolbar buttons with a `CSON`, `JSON`, `JSON5`, `js`, `coffee` file
to perform specific actions in Atom
or to open web sites in your default browser.

Expand All @@ -18,8 +18,8 @@ type `Flex Tool Bar: Edit Config File` in the Atom command palette.

## Configuration

**Flex Tool Bar** has three `type`s you can configure:
`button`, `url` and `spacer`.
**Flex Tool Bar** has four `type`s you can configure:
`button`, `url`, `function` and `spacer`.

- `button` creates default buttons for your toolbar.

Expand All @@ -37,11 +37,16 @@ type `Flex Tool Bar: Edit Config File` in the Atom command palette.
Also Atom URI are allowed. For example
`atom://config/packages/flex-tool-bar` will open Flex Tool Bar's settings.

- `function` creates buttons that can call a function with the previous target as a parameter

This requires the config file to be a `.js` or `.coffee` file that exports the array of buttons

- `spacer` adds separators between toolbar buttons.

### Features

- multiple callback
- function callback
- button style
- hide/disable a button in certain cases

Expand All @@ -63,6 +68,13 @@ style: {
callback: ["callback1", "callback2"]
```

### Function callback

```coffeescript
callback: target ->
console.log target
```

### Hide(Show), Disable(Enable) button

You can hide or disable buttons when a certain grammar is
Expand Down Expand Up @@ -118,7 +130,7 @@ show: "Markdown"

This is same above.

### Example
### .cson Example

```coffeescript
[
Expand Down Expand Up @@ -161,6 +173,59 @@ This is same above.
]
```

### .coffee Example

```coffeescript
module.exports = [
{
type: "function"
icon: "bug"
callback: (target) ->
console.dir target
tooltip: "Debug Target"
}
{
type: "spacer"
}
{
type: "url"
icon: "octoface"
url: "https://github.com/"
tooltip: "Github Page"
}
{
type: "spacer"
}
{
type: "button"
icon: "document"
callback: "application:new-file"
tooltip: "New File"
iconset: "ion"
mode: "dev"
}
{
type: "button"
icon: "columns"
iconset: "fa"
callback: ["pane:split-right", "pane:split-right"]
}
{
type: "button"
icon: "circuit-board"
callback: "git-diff:toggle-diff-list"
style:
color: "#FA4F28"
}
{
type: "button"
icon: "markdown"
callback: "markdown-preview:toggle"
disable: "!markdown"
}
]
```

### Per Project Configuration

If you want buttons that are only for a specific project. Create a toolbar configuration file at the root of your project directory that is listed in the Atom Tree View. All buttons added to the project toolbar will append to the global toolbar buttons.
Expand Down
14 changes: 11 additions & 3 deletions lib/flex-tool-bar.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ module.exports =
# Default directory
@configFilePath = process.env.ATOM_HOME unless @configFilePath

# If configFilePath is a folder, check for `toolbar.(json|cson|json5)` file
# If configFilePath is a folder, check for `toolbar.(json|cson|json5|js|coffee)` file
unless fs.isFileSync(@configFilePath)
@configFilePath = fs.resolve @configFilePath, 'toolbar', ['cson', 'json5', 'json']
@configFilePath = fs.resolve @configFilePath, 'toolbar', ['cson', 'json5', 'json', 'js', 'coffee']

return true if @configFilePath

Expand Down Expand Up @@ -94,7 +94,7 @@ module.exports =
while count < projectCount
pathToCheck = atom.project.getPaths()[count]
if editor.buffer.file.getParent().path.includes(pathToCheck)
@projectToolbarConfigPath = fs.resolve pathToCheck, 'toolbar', ['cson', 'json5', 'json']
@projectToolbarConfigPath = fs.resolve pathToCheck, 'toolbar', ['cson', 'json5', 'json', 'js', 'coffee']
count++

if @projectToolbarConfigPath is @configFilePath
Expand Down Expand Up @@ -195,6 +195,10 @@ module.exports =
ext = path.extname @configFilePath

switch ext
when '.js', '.coffee'
config = require(@configFilePath)
delete require.cache[@configFilePath]

when '.json'
config = require @configFilePath
delete require.cache[@configFilePath]
Expand All @@ -212,6 +216,10 @@ module.exports =
ext = path.extname @projectToolbarConfigPath

switch ext
when '.js', '.coffee'
projConfig = require(@projectToolbarConfigPath)
delete require.cache[@projectToolbarConfigPath]

when '.json'
projConfig = require @projectToolbarConfigPath
delete require.cache[@projectToolbarConfigPath]
Expand Down
11 changes: 11 additions & 0 deletions types/function.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = (toolBar, button) ->
options =
icon: button.icon
tooltip: button.tooltip
iconset: button.iconset
priority: button.priority or 45
data: button.callback
callback: (data, target) ->
data(target)

return toolBar.addButton options

0 comments on commit f293927

Please sign in to comment.