Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Link to directory #72

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions frontend/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions frontend/app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ oort_simulation_worker = { path = "../simulation_worker" }
oort_simulator = { path = "../../shared/simulator", features = ["js"], default-features = false }
oort_proto = { path = "../../shared/proto" }
oort_envelope = { path = "../../shared/envelope" }
oort_multifile = { path = "../../shared/multifile" }
oort_version = { path = "../../shared/version" }
oort_version_control = { path = "../version_control" }
bincode = "1.3.3"
Expand Down Expand Up @@ -73,6 +74,7 @@ features = [
'DomRect',
'Navigator',
'FileSystemFileEntry',
'FileSystemDirectoryEntry',
'DataTransfer',
'DataTransferItem',
'DataTransferItemList',
Expand Down
129 changes: 129 additions & 0 deletions frontend/app/js/filesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,132 @@ export async function open() {
}
return new FileHandle(handle);
}

export class DirectoryHandle {
/**
* @param {FileSystemDirectoryEntry} handle
*/
constructor(handle) {
this._handle = handle;
}

/** @type {FileSystemDirectoryEntry} */
_handle

/** @type {Map<string, { lastModifiedDate: Date, contents: string }>} */
fileCache = new Map();

async getFiles() {
let files = [];
for await (let [_name, handle] of this._handle.entries()) {
if (handle.kind == "file" && handle.name.endsWith(".rs")) {
files.push(handle);
}
}
return files;
}

async load_files() {
// Load all rs files in our directory
let fileHandles = await this.getFiles();

// Sanity check that there are rs files here
if (!fileHandles.length) {
alert('No Rust source files found in this directory')
throw "No .rs files found";
}

let files = await Promise.all(fileHandles.map(f => f.getFile()));

return await Promise.all(files.map(async (file) => {
let contents

let cached = this.fileCache.get(file.name)

if (!cached || cached.lastModifiedDate != file.lastModifiedDate) {
// Reload from disk if we don't have it or it's changed
contents = new TextDecoder().decode(await file.arrayBuffer())
this.fileCache.set(file.name, { lastModifiedDate: file.lastModifiedDate, contents })
} else {
contents = cached.contents
}

return {
name: file.name,
lastModified: file.lastModifiedDate.getTime(),
contents
}
}))
}
}

export async function open_directory() {
let entry = await showDirectoryPicker({
id: 'oort-code-directory',
mode: 'read'
});

return new DirectoryHandle(entry);
}

/**
* Displays an editor overlay with a list of items to pick from
* @param {IStandaloneCodeEditor} editor
* @param {string} prompt
* @param {Record<string, string>} items
* @returns
*/
export async function pick(editor, prompt, items) {
return await new Promise((resolve, reject) => {
// TODO Monaco has an API for this but it is not exposed in rust-monaco
let widget = {
domNode: null,
getId: function () {
return 'oort.picker'
},
getDomNode: function () {
if (!this.domNode) {
this.domNode = document.createElement('div')
this.domNode.classList.add('oort-picker')

let title = document.createElement('h2')
title.innerText = prompt
this.domNode.appendChild(title)

let list = document.createElement('ul')
this.domNode.appendChild(list)

for (let { value, display } of items) {
let itemEl = document.createElement('li')
itemEl.innerText = display
itemEl.addEventListener('click', () => {
resolve(value)
setTimeout(() => editor.removeOverlayWidget(widget), 0)
})
itemEl.tabIndex = 0;
list.appendChild(itemEl)
}

let removalListener = (evt) => {
if (!this.domNode.contains(evt.target)) {
reject('No selection')
editor.removeOverlayWidget(widget)
document.removeEventListener('click', removalListener)
}
}

document.addEventListener('click', removalListener)
}
return this.domNode
},
getPosition: function () {
return {
preference: [2] // TOP_CENTER
}
}
}

editor.removeOverlayWidget(widget)
editor.addOverlayWidget(widget)
})
}
Loading