Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
blurymind committed Apr 23, 2024
1 parent 1e15866 commit aacec27
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 46 deletions.
30 changes: 15 additions & 15 deletions src/js/classes/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const path = require('path');
const inkjs = require('inkjs');
import { Node } from './node';
import { Utils } from './utils';
import {getFileType, FILETYPE} from './storage'
import { getFileType, FILETYPE } from './storage';

export const data = {
appInstanceStates: ko.observable([]),
Expand Down Expand Up @@ -330,7 +330,7 @@ export const data = {
reader.readAsText(file);
});
},

openFolder: function(e, foldername) {
editingFolder = foldername;
Swal.fire({
Expand Down Expand Up @@ -951,14 +951,14 @@ export const data = {

tryOpenFile: function() /// Refactor to send signal to the main process
{
app.storage.openLocalFile().then(yarnData=>{
data.addDocumentState({
editingName: app.storage.fileName,
editingType: app.storage.fileType,
yarnData,
app.storage.openLocalFile().then(yarnData => {
data.addDocumentState({
editingName: app.storage.fileName,
editingType: app.storage.fileType,
yarnData,
});
data.loadData(yarnData, app.storage.fileType, true);
});
data.loadData(yarnData, app.storage.fileType, true)
})
},

promptFileNameAndFormat: function(
Expand Down Expand Up @@ -1097,7 +1097,7 @@ export const data = {
if (gists.hasGistSettings()) {
const previouslyOpenedGist =
data.lastStorageHost() === 'GIST' ? data.editingName() : '';
gists.getGistFile().then(({inputOptions, filesInGist}) => {
gists.getGistFile().then(({ inputOptions, filesInGist }) => {
Swal.fire({
title: '🐙 Open file from a gist',
input: 'select',
Expand Down Expand Up @@ -1129,15 +1129,15 @@ export const data = {
},

tryAppend: function() {
app.storage.openLocalFile().then(yarnData=>{
app.storage.openLocalFile().then(yarnData => {
data.loadData(yarnData, app.storage.fileType, false);
})
});
},

trySave: function(type) {
const fileName =
(data.editingName() || '').replace(/\.[^/.]+$/, '') + '.' + type;
app.storage.saveAsFile(fileName, data.getSaveData).then(result=>{
(data.editingName() || '').replace(/\.[^/.]+$/, '') + '.' + type;
app.storage.saveAsFile(fileName, data.getSaveData).then(result => {
data.setNewFileStats(result.chosenFileName, '', 'LOCAL');
data.editingType(result.type);
});
Expand Down Expand Up @@ -1168,7 +1168,7 @@ export const data = {
});
} else if (data.editingPath().length > 0 && data.editingType().length > 0) {
data.getSaveData(data.editingType()).then(saveData => {
// this only works with electron. We need to use file access api instead
// this only works with electron. We need to use file access api instead
});
}
},
Expand Down
56 changes: 27 additions & 29 deletions src/js/classes/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const FILETYPE = {
RENPY: 'rpy',
};
export const getFileType = filename => {
if(!filename) return FILETYPE.UNKNOWN;
if (!filename) return FILETYPE.UNKNOWN;

const lowerFileName = filename.toLowerCase();
if (lowerFileName.endsWith('.json')) return FILETYPE.JSON;
Expand All @@ -25,26 +25,24 @@ export const getFileType = filename => {
return FILETYPE.UNKNOWN;
};


export const StorageJs = (type = 'gist', credentials) => {

if (type === 'gist') {
return {
getFileType,
FILETYPE,
lastStorageHost: 'GIST', // or LOCAL
fileType: FILETYPE.UNKNOWN,// same as editingFormat?
fileType: FILETYPE.UNKNOWN, // same as editingFormat?
fileHandle: undefined,
writable: undefined,
saveAsFile: async function(suggestedName, getContent){
return new Promise(async (resolve, reject) =>{
saveAsFile: async function(suggestedName, getContent) {
return new Promise(async (resolve, reject) => {
const fileHandle = await window.showSaveFilePicker({
suggestedName,
types: [
{
description: 'Yarn editor files',
accept: {
'text/plain': Object.values(FILETYPE).map(item=>`.${item}`),
'text/plain': Object.values(FILETYPE).map(item => `.${item}`),
},
},
],
Expand All @@ -58,34 +56,34 @@ export const StorageJs = (type = 'gist', credentials) => {
this.writable = writable;
await writable.write(content);
await writable.close();
console.log({fileHandle,writable})
resolve({type, content, chosenFileName, fileHandle, writable})
})

console.log({ fileHandle, writable });
resolve({ type, content, chosenFileName, fileHandle, writable });
});
},
downloadContent: function(content, fileName){
downloadContent: function(content, fileName) {
const file = new File([content], fileName, {
type: 'text/plain',
})
const link = document.createElement('a')
const url = URL.createObjectURL(file)
link.href = url
link.download = file.name
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
window.URL.revokeObjectURL(url)
});
const link = document.createElement('a');
const url = URL.createObjectURL(file);

link.href = url;
link.download = file.name;
document.body.appendChild(link);
link.click();

document.body.removeChild(link);
window.URL.revokeObjectURL(url);
},
saveToCurrentFile: async function(content, fileName){
console.log({writable: this.writable, handle: this.fileHandle})
if(this.fileHandle) {
saveToCurrentFile: async function(content, fileName) {
console.log({ writable: this.writable, handle: this.fileHandle });
if (this.fileHandle) {
const writable = await this.fileHandle.createWritable();
await writable.write(content);
await writable.close();
} else {
this.downloadContent(content, fileName)
this.downloadContent(content, fileName);
}
},
openFileOrFiles: async function(multiple = false) {
Expand All @@ -111,7 +109,7 @@ export const StorageJs = (type = 'gist', credentials) => {
if (!multiple) {
// Only one file is requested.
this.fileHandle = files[0];
fileOrFiles = files[0]
fileOrFiles = files[0];
console.log({ fileOrFiles });
}
} catch (err) {
Expand Down Expand Up @@ -217,7 +215,7 @@ export const StorageJs = (type = 'gist', credentials) => {
this.fileName = file.name;
this.fileHandle = file;
this.lastStorageHost = 'GIST';
return file.text()
return file.text();
})
.then(rawContent => {
resolve(rawContent);
Expand Down
2 changes: 0 additions & 2 deletions src/js/classes/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import {FILETYPE} from './storage'

export var Utils = {
createAutocompleter: function(
allowedTokens,
Expand Down

0 comments on commit aacec27

Please sign in to comment.