Skip to content

Commit

Permalink
Can now see files in the location
Browse files Browse the repository at this point in the history
  • Loading branch information
james-strauss-uwa committed Oct 31, 2024
1 parent 1098360 commit f089f80
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 16 deletions.
5 changes: 0 additions & 5 deletions src/Repositories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ export class Repositories {
// use a custom modal to ask user for repository service and url at the same time
addCustomRepository = () : void => {
Utils.requestUserAddCustomRepository((completed : boolean, repositoryService : Repository.Service, repositoryName : string, repositoryBranch : string) : void => {
console.log("requestUserAddCustomRepository callback", completed, repositoryService, repositoryName);

if (!completed){
console.log("No repo entered");
return;
Expand Down Expand Up @@ -146,7 +144,6 @@ export class Repositories {
};

addLocalDirectory = async () => {
console.log("addLocalDirectory()");
let dirHandle: FileSystemDirectoryHandle;

try {
Expand All @@ -159,8 +156,6 @@ export class Repositories {
return;
}

console.log("dirHandle", dirHandle);

$('#gitCustomRepositoryModal').data('dirHandle', dirHandle);
$('#gitCustomRepositoryModalDirectoryNameInput').val(dirHandle.name).trigger('change');
};
Expand Down
39 changes: 29 additions & 10 deletions src/Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export class Repository {
expanded : ko.Observable<boolean>
files : ko.ObservableArray<RepositoryFile>
folders : ko.ObservableArray<RepositoryFolder>

dirHandle : FileSystemDirectoryHandle

// NOTE: I think we should be able to use the Repository.Service.Unknown enum here, but it causes a javascript error. Not sure why.
Expand All @@ -36,6 +35,7 @@ export class Repository {
this.expanded = ko.observable(false);
this.files = ko.observableArray();
this.folders = ko.observableArray();
this.dirHandle = null;
}

htmlId : ko.PureComputed<string> = ko.pureComputed(()=>{
Expand Down Expand Up @@ -183,21 +183,23 @@ export class Repository {
// flag the repository as being fetched
repository.isFetching(true);

const dirHandle = repository.dirHandle;
const dirHandle: FileSystemDirectoryHandle = repository.dirHandle;

for await (const [key, value] of (<any>dirHandle).entries()) {
console.log({ key, value });
for await (const [key, value] of dirHandle.entries()) {
console.log(key, value);

// add files to repo
if (value.kind === 'file'){
// if file is not a .graph, .palette, or .json, just ignore it!
if (Utils.verifyFileExtension(key)){
repository.files.push(new RepositoryFile(repository, "", key));
const newFile = new RepositoryFile(repository, "", key);
newFile.fileHandle = value as FileSystemFileHandle;
repository.files.push(newFile);
}
}

// add folders to repo
if (value.find === 'directory'){
if (value.kind === 'directory'){
repository.folders.push(new RepositoryFolder(key));
}
}
Expand All @@ -208,19 +210,36 @@ export class Repository {
repository.expanded(true);
}

static openLocalFile(repositoryService : Repository.Service, repositoryName : string, repositoryBranch : string, filePath : string, fileName : string, callback: (error : string, data : string) => void ) : void {
static async openLocalFile(repositoryService : Repository.Service, repositoryName : string, repositoryBranch : string, filePath : string, fileName : string, callback: (error : string, data : string) => void ): Promise<void> {
// find the repository
const localDirectory = Repositories.get(repositoryService, repositoryName, repositoryBranch);

// check we found it
if (localDirectory === null){
console.error("Repository.openLocalFile(): can't find Repository. Service:", repositoryService, "Name:", repositoryName, "Branch:", repositoryBranch);
callback("Repository.openLocalFile(): can't find Repository. Service: " + repositoryService + " Name: " + repositoryName + " Branch: " + repositoryBranch, null);
return;
}

let fileHandle: FileSystemFileHandle = null;

// find the file in the repository
for (const file of localDirectory.files()){
if (file.path === filePath && file.name === fileName){
fileHandle = file.fileHandle;
}
}

// abort if file not found
if (fileHandle === null){
callback("openLocalFile(): can't find file in directory: " + filePath + " " + fileName, null);
return;
}

const dirHandle: FileSystemDirectoryHandle = localDirectory.dirHandle;
// open file
const file: File = await fileHandle.getFile();
const fileData = await file.text();

// TODO: open file
callback(null, fileData);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/RepositoryFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export class RepositoryFile {
path : string
type : Eagle.FileType;
isFetching: ko.Observable<boolean>;
fileHandle: FileSystemFileHandle;

constructor(repository : Repository, path : string, name : string){
this._id = Math.floor(Math.random() * 1000000000000);
Expand All @@ -19,6 +20,7 @@ export class RepositoryFile {
this.path = path;
this.isFetching = ko.observable(false);
this.type = Utils.getFileTypeFromFileName(this.name);
this.fileHandle = null;
}

getIconUrl : ko.PureComputed<string> = ko.pureComputed(() : string => {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"module": "umd",
"moduleResolution": "node",
"target": "es2015",
"lib": ["es2021", "dom"],
"lib": ["es2021", "DOM", "DOM.Iterable", "DOM.AsyncIterable"],
"allowJs": false,
"types": ["jquery", "bootstrap", "bootstrap-notify"]
},
Expand Down

0 comments on commit f089f80

Please sign in to comment.