Skip to content

Commit

Permalink
wip make the api a bit nicer
Browse files Browse the repository at this point in the history
  • Loading branch information
blurymind committed Apr 15, 2024
1 parent 5e54611 commit 2d08afe
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
23 changes: 8 additions & 15 deletions src/js/classes/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -1166,8 +1166,8 @@ export const data = {
},

trySaveGist: function(gists) {
if (gists && gists.file && gists.file.length > 0) {
gists.get(gists.file).then(gist => {
if (gists.hasGistSettings()) {
gists.getGistFile().then(gist => {
const gistFiles = Object.keys(gist.body.files);
console.log(gistFiles);
data.promptFileNameAndFormat(({ editingName, yarnData }) => {
Expand Down Expand Up @@ -1204,17 +1204,10 @@ export const data = {
},

tryOpenGist: function(gists) {
if (gists && gists.file && gists.file.length > 0) {
if (gists.hasGistSettings()) {
const previouslyOpenedGist =
data.lastStorageHost() === 'GIST' ? data.editingName() : '';
gists.get(gists.file).then(gist => {
console.log('GOT', gist);
const gistFiles = gist.body.files;
console.log({ gistFiles });
const inputOptions = {};
Object.keys(gistFiles).forEach(key => {
inputOptions[key] = key;
});
gists.getGistFile().then(({inputOptions, filesInGist}) => {
Swal.fire({
title: '🐙 Open file from a gist',
input: 'select',
Expand All @@ -1227,8 +1220,8 @@ export const data = {
showCancelButton: true,
}).then(({ value }) => {
if (value) {
const content = gistFiles[value].content;
const rawUrl = gistFiles[value].raw_url;
const content = filesInGist[value].content;
const rawUrl = filesInGist[value].raw_url;
gists.getContentOrRaw(content, rawUrl).then(content => {
data.openGist(content, value);
});
Expand Down Expand Up @@ -1275,10 +1268,10 @@ export const data = {

if (data.lastStorageHost() === 'GIST') {
const gists = app.gists;
gists.get(gists.file).then(gist => {
gists.getGistFile().then(gist => {
data.getSaveData(data.editingType()).then(yarnData => {
data.getSaveData(data.editingType());
gists.edit(gists.file, data.editingName(), yarnData);
gists.editGistFile(data.editingName(), yarnData);
data.lastStorageHost('GIST');
data.isDocumentDirty(false);
app.refreshWindowTitle();
Expand Down
22 changes: 21 additions & 1 deletion src/js/classes/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ export const StorageJs = (type= 'gist', credentials) => {
},
token: credentials.token,
file: credentials.file,
filesInGist: {},
getFilesInGist: function(fileKey){
if(!fileKey) return this.filesInGist;
if (fileKey in this.filesInGist) return this.filesInGist[fileKey]
console.error(`${fileKey} not found in gist`, this.filesInGist)
},
test: function(){ console.log("CRED",this.file); return this.testing},
get: function(gistId){
return fetch('https://api.github.com/gists/' + gistId, {
Expand All @@ -23,9 +29,20 @@ export const StorageJs = (type= 'gist', credentials) => {
})
.then(content => {
console.log('NEW from get::', { content });
return { body: content };
this.filesInGist = content.files;
const inputOptions = {};
Object.keys(this.filesInGist).forEach(key => {
inputOptions[key] = key;
});
return { body: content, filesInGist: this.filesInGist, inputOptions, fileList: Object.keys(this.filesInGist) };
});
},
hasGistSettings: function(){
return this.file && this.file.length > 0
},
getGistFile: function(){
return this.get(this.file);
},
getContentOrRaw: function(content, rawUrl) {
// sometimes github comes back empty handed for content, but has raw_url
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -56,6 +73,9 @@ export const StorageJs = (type= 'gist', credentials) => {
}),
}).then(res => res.json());
},
editGistFile: function(fileName, content){
return this.edit(this.file, fileName, content)
}
};
} else if (type === 'github') {
// todo implement
Expand Down
7 changes: 3 additions & 4 deletions src/public/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,9 @@ export var Plugins = function(app) {

// register plugins stored on a gist - todo cache all this
if (app.settings.gistPluginsFile() !== null) {
app.gists.get(app.settings.gistPluginsFile()).then(gist => {
const gistFiles = gist.body.files;
console.log({ gistFiles });
Object.values(gistFiles).forEach(gistFile => {
app.gists.get(app.settings.gistPluginsFile()).then(({fileList}) => {
console.log({ fileList });
fileList.forEach(gistFile => {
if (gistFile.language === 'JavaScript') {
console.log({ gistFile });
try {
Expand Down

0 comments on commit 2d08afe

Please sign in to comment.