Skip to content

Commit

Permalink
Fix bug where files within directories could not be loaded. Better ty…
Browse files Browse the repository at this point in the history
…ping for FileSystemHandle variables
  • Loading branch information
james-strauss-uwa committed Oct 31, 2024
1 parent f089f80 commit c484a57
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/Repositories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export class Repositories {

// create a new Repository and add the dirHandle
const newRepo = new Repository(Repository.Service.LocalDirectory, dirHandle.name, "", false);
newRepo.dirHandle = dirHandle;
newRepo.handle = dirHandle;

// add new Repository to the repositories list
Repositories.repositories.push(newRepo);
Expand Down
84 changes: 58 additions & 26 deletions src/Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class Repository {
expanded : ko.Observable<boolean>
files : ko.ObservableArray<RepositoryFile>
folders : ko.ObservableArray<RepositoryFolder>
dirHandle : FileSystemDirectoryHandle
handle : 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.
static readonly DUMMY = new Repository(<Repository.Service>"Unknown", "", "", false);
Expand All @@ -35,7 +35,7 @@ export class Repository {
this.expanded = ko.observable(false);
this.files = ko.observableArray();
this.folders = ko.observableArray();
this.dirHandle = null;
this.handle = null;
}

htmlId : ko.PureComputed<string> = ko.pureComputed(()=>{
Expand Down Expand Up @@ -97,6 +97,30 @@ export class Repository {
}
}

// find a single file within the repo, based on the path and filename
findFile = (path: string, filename: string): RepositoryFile => {
const pathParts: string[] = path.split('/');
let pointer: Repository | RepositoryFolder = this;

// traverse down the folder structure
for (const pathPart of pathParts){
for (const folder of pointer.folders()){
if (folder.name === pathPart){
pointer = folder;
}
}
}

// find the file here
for (const file of pointer.files()){
if (file.name === filename){
return file;
}
}

return null;
}

deleteFile = (file: RepositoryFile) : void => {
let pointer: Repository | RepositoryFolder = this;
let lastPointer: Repository | RepositoryFolder = null;
Expand Down Expand Up @@ -183,57 +207,65 @@ export class Repository {
// flag the repository as being fetched
repository.isFetching(true);

const dirHandle: FileSystemDirectoryHandle = repository.dirHandle;
// parse the folder
Repository._parseFolder(repository, repository, [], repository.handle);

// flag the repository as fetched and expand by default
repository.isFetching(false);
repository.fetched(true);
repository.expanded(true);
}

for await (const [key, value] of dirHandle.entries()) {
console.log(key, value);
static async _parseFolder(repository: Repository, parent: Repository | RepositoryFolder, pathParts: string[], dirHandle: FileSystemDirectoryHandle){

for await (const [name, handle] of dirHandle.entries()) {
// add files to repo
if (value.kind === 'file'){
if (handle.kind === 'file'){
// if file is not a .graph, .palette, or .json, just ignore it!
if (Utils.verifyFileExtension(key)){
const newFile = new RepositoryFile(repository, "", key);
newFile.fileHandle = value as FileSystemFileHandle;
repository.files.push(newFile);
if (Utils.verifyFileExtension(name)){
const newFile = new RepositoryFile(repository, pathParts.join('/'), name);
newFile.handle = handle as FileSystemFileHandle;
parent.files.push(newFile);
}
}

// add folders to repo
if (value.kind === 'directory'){
repository.folders.push(new RepositoryFolder(key));
if (handle.kind === 'directory'){
const newFolder: RepositoryFolder = new RepositoryFolder(name);
newFolder.handle = handle as FileSystemDirectoryHandle;
await Repository._parseFolder(repository, newFolder, pathParts.concat([name]), handle as FileSystemDirectoryHandle);
parent.folders.push(newFolder);
}
}

// flag the repository as fetched and expand by default
repository.isFetching(false);
repository.fetched(true);
repository.expanded(true);
}

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);
const localDirectory: Repository = Repositories.get(repositoryService, repositoryName, repositoryBranch);

// check we found it
if (localDirectory === null){
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;
}
}
const repositoryFile: RepositoryFile = localDirectory.findFile(filePath, fileName)

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

// get the fileHandle from the file
const fileHandle: FileSystemFileHandle = repositoryFile.handle;

// abort if file doesn't have a fileHandle
if (fileHandle === null){
callback("openLocalFile(): no handle attached to RepositoryFile: " + filePath + " " + fileName, null);
return;
}

// open file
const file: File = await fileHandle.getFile();
Expand Down
4 changes: 2 additions & 2 deletions src/RepositoryFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class RepositoryFile {
path : string
type : Eagle.FileType;
isFetching: ko.Observable<boolean>;
fileHandle: FileSystemFileHandle;
handle: FileSystemFileHandle;

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

getIconUrl : ko.PureComputed<string> = ko.pureComputed(() : string => {
Expand Down
2 changes: 2 additions & 0 deletions src/RepositoryFolder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ export class RepositoryFolder {
expanded : ko.Observable<boolean>
folders : ko.ObservableArray<RepositoryFolder>
files : ko.ObservableArray<RepositoryFile>
handle: FileSystemDirectoryHandle

constructor(name : string){
this.name = name;
this.expanded = ko.observable(false);
this.folders = ko.observableArray([]);
this.files = ko.observableArray([]);
this.handle = null;
}

htmlId : ko.PureComputed<string> = ko.pureComputed(() => {
Expand Down

0 comments on commit c484a57

Please sign in to comment.