Skip to content

Commit

Permalink
finalize
Browse files Browse the repository at this point in the history
  • Loading branch information
hrgdavor committed Jan 27, 2024
1 parent c42a448 commit 721c0ed
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
2 changes: 2 additions & 0 deletions apps/jscad-web/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ async function initFs() {
sw.defProjectName = 'jscad'
sw.onfileschange = files => {
sendNotify('clearFileCache', { files })
editor.filesChanged(files)
if (sw.fileToRun) runScript({ url: sw.fileToRun, base: sw.base })
}
sw.getFile = path => getFile(path, sw)
Expand Down Expand Up @@ -282,6 +283,7 @@ editor.init(
await writable.close()
}
},
path=>sw?.getFile(path)
)
menu.init(loadExample)
welcome.init()
Expand Down
33 changes: 24 additions & 9 deletions apps/jscad-web/src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ let view

let compileFn
let saveFn
let getFileFn

// file selector
let currentFile = '/index.js'
Expand All @@ -28,7 +29,7 @@ const save = (code, path) => {
saveFn(code, path)
}

export const init = (defaultCode, fn, _saveFn) => {
export const init = (defaultCode, fn, _saveFn, _getFileFn) => {
// by calling document.getElementById here instead outside of init we allow the flow
// where javascript is included in the page before the tempalte is loaded into the DOM
// it was causing issue to users trying to replicate the app in Vue, and would likely some others too
Expand All @@ -37,6 +38,7 @@ export const init = (defaultCode, fn, _saveFn) => {

compileFn = fn
saveFn = _saveFn
getFileFn = _getFileFn
// Initialize codemirror
const editorDiv = document.getElementById('editor-container')
view = new EditorView({
Expand Down Expand Up @@ -86,6 +88,26 @@ export const setSource = (source, path = '/index.js') => {
currentFile = path
}

export function filesChanged(files){
files.forEach(async path=>{
if(path == currentFile){
let file = await getFileFn(path)
readSource(file, path)
}
})
}

async function readSource(file, currentFile){
// Read FileEntry
file.file((file) => {
const reader = new FileReader()
reader.onloadend = () => {
setSource(reader.result, currentFile)
}
reader.readAsText(file)
})
}

export const setFiles = (files) => {
const editorFiles = document.getElementById('editor-files')
if (files.length < 2) {
Expand All @@ -101,14 +123,7 @@ export const setFiles = (files) => {
button.addEventListener('click', () => {
currentFile = file.fsPath
editorFile.innerHTML = currentFile
// Read FileEntry
file.file((file) => {
const reader = new FileReader()
reader.onloadend = () => {
setSource(reader.result, currentFile)
}
reader.readAsText(file)
})
readSource(file, currentFile)
})
item.appendChild(button)
editorFiles.appendChild(item)
Expand Down

0 comments on commit 721c0ed

Please sign in to comment.