mirror of
https://github.com/atom/atom.git
synced 2026-02-13 08:04:56 -05:00
Add package to create a Gist from the editor
This commit is contained in:
committed by
Kevin Sawicki
parent
0f314f573b
commit
d1c6caabd6
1
src/packages/gists/index.coffee
Normal file
1
src/packages/gists/index.coffee
Normal file
@@ -0,0 +1 @@
|
||||
module.exports = require './lib/gists'
|
||||
2
src/packages/gists/keymaps/gists.cson
Normal file
2
src/packages/gists/keymaps/gists.cson
Normal file
@@ -0,0 +1,2 @@
|
||||
'body':
|
||||
'alt-meta-g': 'gist:create'
|
||||
19
src/packages/gists/lib/gists.coffee
Normal file
19
src/packages/gists/lib/gists.coffee
Normal file
@@ -0,0 +1,19 @@
|
||||
$ = require 'jquery'
|
||||
|
||||
module.exports =
|
||||
activate: (rootView) ->
|
||||
rootView.command 'gist:create', '.editor', (e) =>
|
||||
@createGist(e.currentTargetView())
|
||||
|
||||
createGist: (editor) ->
|
||||
gist = { public: false, files: {} }
|
||||
gist.files[editor.getBuffer().getBaseName()] =
|
||||
content: editor.getSelectedText() or editor.getText()
|
||||
|
||||
$.ajax
|
||||
url: 'https://api.github.com/gists'
|
||||
type: 'POST'
|
||||
dataType: 'json'
|
||||
contentType: 'application/json; charset=UTF-8'
|
||||
data: JSON.stringify(gist)
|
||||
success: (response) -> pasteboard.write(response.html_url)
|
||||
45
src/packages/gists/spec/gists-spec.coffee
Normal file
45
src/packages/gists/spec/gists-spec.coffee
Normal file
@@ -0,0 +1,45 @@
|
||||
RootView = require 'root-view'
|
||||
$ = require 'jquery'
|
||||
|
||||
describe "Gists package", ->
|
||||
|
||||
[rootView, editor] = []
|
||||
|
||||
beforeEach ->
|
||||
rootView = new RootView(fixturesProject.resolve('sample.js'))
|
||||
atom.loadPackage('gists')
|
||||
editor = rootView.getActiveEditor()
|
||||
spyOn($, 'ajax')
|
||||
|
||||
afterEach ->
|
||||
rootView.deactivate()
|
||||
|
||||
describe "when gist:create is triggered on an editor", ->
|
||||
|
||||
describe "when the editor has no selection", ->
|
||||
it "creates an Ajax request to api.github.com with the entire buffer contents as the Gist's content", ->
|
||||
editor.trigger 'gist:create'
|
||||
expect($.ajax).toHaveBeenCalled()
|
||||
request = $.ajax.argsForCall[0][0]
|
||||
expect(request.url).toBe 'https://api.github.com/gists'
|
||||
expect(request.type).toBe 'POST'
|
||||
requestData = JSON.parse(request.data)
|
||||
expect(requestData.public).toBeFalsy()
|
||||
expect(requestData.files).toEqual 'sample.js': content: editor.getText()
|
||||
|
||||
it "places the created Gist's URL on the clipboard", ->
|
||||
editor.trigger 'gist:create'
|
||||
request = $.ajax.argsForCall[0][0]
|
||||
request.success(html_url: 'https://gist.github.com/1')
|
||||
expect(pasteboard.read()[0]).toBe 'https://gist.github.com/1'
|
||||
|
||||
describe "when the editor has a selection", ->
|
||||
beforeEach ->
|
||||
editor.setSelectedBufferRange [[4, 0], [8, 0]]
|
||||
|
||||
it "creates an Ajax with the selected text as the Gist's content", ->
|
||||
editor.trigger 'gist:create'
|
||||
expect($.ajax).toHaveBeenCalled()
|
||||
request = $.ajax.argsForCall[0][0]
|
||||
requestData = JSON.parse(request.data)
|
||||
expect(requestData.files).toEqual 'sample.js': content: editor.getSelectedText()
|
||||
Reference in New Issue
Block a user