Skip to content

Instantly share code, notes, and snippets.

@lallmon
Last active October 20, 2015 18:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lallmon/c87b8494ea9eb9aef7e7 to your computer and use it in GitHub Desktop.
Save lallmon/c87b8494ea9eb9aef7e7 to your computer and use it in GitHub Desktop.
Code examples from a couple projects.
/** This is an extension for Adobe's open source editor Brackets.io that creates Gists easily
I think its pretty clever code on a couple levels, but mainly because operating systems prevent
browser technologies from accessing the clipboard directly. So instead, I exposed the URL as a string within
a text field, and select it, so the user only needs to hit their Copy shortcut.
I think its cool.
*/
define(function (require, exports, module) {
'use strict';
var CommandManager = brackets.getModule("command/CommandManager"),
Dialogs = brackets.getModule("widgets/Dialogs"),
EditorManager = brackets.getModule("editor/EditorManager"),
Menus = brackets.getModule("command/Menus"),
Strings = brackets.getModule("strings"),
GistrDialogTemplate = require("text!gistr-dialog.html");
var emptyMessage = "You'll need to select some text before you can create a Gist.",
errorMessage = "Unable to create Gist. :( ",
errorTitle = "Error!",
gistDescription = "Created with Gistr for Brackets.io",
gistrLink = "https://github.com/LucasKA/Gistr",
githubApiUrl = "https://api.github.com/gists",
menuName = "Create Gist",
myCommandId = "lka.gistr",
successTitle = "Gist Successfully Created!";
function handleAction() {
// Retrieve selection
var selectedText = EditorManager.getCurrentFullEditor().getSelectedText();
//Throw an error in a modal if the selection is an empty string.
if (selectedText === "") {
Dialogs.showModalDialog("error-dialog", errorTitle, emptyMessage);
return;
}
// Gist description to be sent to github
var postdata = {
"description": gistDescription + gistrLink,
"public": true,
"files": {
}
};
//You can't use a variable as the key of JSON unless you do this.
postdata.files[EditorManager.getActiveEditor().document.file.name] = {
"content": selectedText
};
var postdataString = JSON.stringify(postdata);
// Send to github
$.ajax({
url: githubApiUrl,
type: "POST",
dataType: "json",
data: postdataString,
error: function (data) {
Dialogs.showModalDialog("error-dialog", errorTitle, errorMessage);
} ,
success: function (data) {
var templateVars = {
title: successTitle,
data: data.html_url,
buttons: [{ className: "primary", id: "ok", text: Strings.OK }, { htmlId: "goToGist", className: "left", id: "ok", text: "Go To Gist" }]
};
Dialogs.showModalDialogUsingTemplate(Mustache.render(GistrDialogTemplate, templateVars));
var $dlg = $('.gistr-dialog.instance');
//Select the text in the input, so the user can copy to clipboard, which is probably the easiest way, as chrome locks system level clipboard out as a security issue.
$dlg.find('#gistr-data').select();
$dlg.find('#goToGist').on('click', function(){
brackets.app.openURLInDefaultBrowser(data.html_url);
});
}
});
}
// Register the command and insert in the Edit menu
CommandManager.register(menuName, myCommandId, handleAction);
var menu = Menus.getMenu(Menus.AppMenuBar.EDIT_MENU);
menu.addMenuDivider();
menu.addMenuItem(myCommandId);
var contextMenu = Menus.getContextMenu(Menus.ContextMenuIds.EDITOR_MENU);
contextMenu.addMenuItem(myCommandId);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment