Skip to content

Commit

Permalink
Merge pull request #119 from arduino/bugfix/open-file
Browse files Browse the repository at this point in the history
Switch to tab if file is already open
  • Loading branch information
ubidefeo authored Apr 23, 2024
2 parents 7a880a3 + 36b169a commit d16a173
Showing 1 changed file with 67 additions and 72 deletions.
139 changes: 67 additions & 72 deletions ui/arduino/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -1021,17 +1021,19 @@ async function store(state, emitter) {

emitter.on('toggle-file-selection', (file, source, event) => {
log('toggle-file-selection', file, source, event)
let parentFolder = source == 'board' ? state.boardNavigationPath : state.diskNavigationPath
// Single file selection unless holding keyboard key
if (event && !event.ctrlKey && !event.metaKey) {
state.selectedFiles = [{
fileName: file.fileName,
type: file.type,
source: source,
parentFolder: file.parentFolder
parentFolder: parentFolder
}]
emitter.emit('render')
return
}

const isSelected = state.selectedFiles.find((f) => {
return f.fileName === file.fileName && f.source === source
})
Expand All @@ -1044,80 +1046,90 @@ async function store(state, emitter) {
fileName: file.fileName,
type: file.type,
source: source,
parentFolder: file.parentFolder
parentFolder: parentFolder
})
}
emitter.emit('render')
})
emitter.on('open-selected-files', async () => {
log('open-selected-files')
let files = []
let filesToOpen = []
let filesAlreadyOpen = []
for (let i in state.selectedFiles) {
let selectedFile = state.selectedFiles[i]
let openFile = null
if (selectedFile.type == 'folder') {
// Don't open folders
continue
}
if (selectedFile.source == 'board') {
const fileContent = await serial.loadFile(
serial.getFullPath(
'/',
state.boardNavigationPath,
selectedFile.fileName
// ALl good until here

const alreadyOpen = state.openFiles.find((f) => {
return f.fileName == selectedFile.fileName
&& f.source == selectedFile.source
&& f.parentFolder == selectedFile.parentFolder
})
console.log('already open', alreadyOpen)

if (!alreadyOpen) {
// This file is not open yet,
// load content and append it to the list of files to open
let file = null
if (selectedFile.source == 'board') {
const fileContent = await serial.loadFile(
serial.getFullPath(
state.boardNavigationRoot,
state.boardNavigationPath,
selectedFile.fileName
)
)
)
openFile = createFile({
parentFolder: state.boardNavigationPath,
fileName: selectedFile.fileName,
source: selectedFile.source,
content: fileContent
})
openFile.editor.onChange = function() {
openFile.hasChanges = true
emitter.emit('render')
}
} else if (selectedFile.source == 'disk') {
const fileContent = await disk.loadFile(
disk.getFullPath(
state.diskNavigationRoot,
state.diskNavigationPath,
selectedFile.fileName
file = createFile({
parentFolder: state.boardNavigationPath,
fileName: selectedFile.fileName,
source: selectedFile.source,
content: fileContent
})
file.editor.onChange = function() {
file.hasChanges = true
emitter.emit('render')
}
} else if (selectedFile.source == 'disk') {
const fileContent = await disk.loadFile(
disk.getFullPath(
state.diskNavigationRoot,
state.diskNavigationPath,
selectedFile.fileName
)
)
)
openFile = createFile({
parentFolder: state.diskNavigationPath,
fileName: selectedFile.fileName,
source: selectedFile.source,
content: fileContent
})
openFile.editor.onChange = function() {
openFile.hasChanges = true
emitter.emit('render')
file = createFile({
parentFolder: state.diskNavigationPath,
fileName: selectedFile.fileName,
source: selectedFile.source,
content: fileContent
})
file.editor.onChange = function() {
file.hasChanges = true
emitter.emit('render')
}
}
filesToOpen.push(file)
} else {
// This file is already open,
// append it to the list of files that are already open
filesAlreadyOpen.push(alreadyOpen)
}
files.push(openFile)
}

files = files.filter((f) => { // find files to open
let isAlready = false
state.openFiles.forEach((g) => { // check if file is already open
if (
g.fileName == f.fileName
&& g.source == f.source
&& g.parentFolder == f.parentFolder
) {
isAlready = true
}
})
return !isAlready
})

if (files.length > 0) {
state.openFiles = state.openFiles.concat(files)
state.editingFile = files[0].id
// If opening an already open file, switch to its tab
if (filesAlreadyOpen.length > 0) {
state.editingFile = filesAlreadyOpen[0].id
}
// If there are new files to open, they take priority
if (filesToOpen.length > 0) {
state.editingFile = filesToOpen[0].id
}

state.openFiles = state.openFiles.concat(filesToOpen)

state.view = 'editor'
emitter.emit('render')
})
Expand Down Expand Up @@ -1476,23 +1488,6 @@ function canEdit({ selectedFiles }) {
return files.length != 0
}

function toggleFileSelection({ fileName, source, selectedFiles }) {
let result = []
let file = selectedFiles.find((f) => {
return f.fileName === fileName && f.source === source
})
if (file) {
// filter file out
result = selectedFiles.filter((f) => {
return f.fileName !== fileName && f.source !== source
})
} else {
// push file
selectedFiles.push({ fileName, source })
}
return result
}

async function removeBoardFolder(fullPath) {
// TODO: Replace with getting the file tree from the board and deleting one by one
let output = await serial.execFile('./ui/arduino/helpers.py')
Expand Down

0 comments on commit d16a173

Please sign in to comment.