Skip to content

Alert user before closing tab with unsaved changes #108

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

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
68 changes: 48 additions & 20 deletions ui/arduino/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ async function store(state, emitter) {
parentFolder: null, // Null parent folder means not saved?
source: 'disk'
})
newFile.editor.onChange = function() {
newFile.hasChanges = true
emitter.emit('render')
}
state.openFiles.push(newFile)
state.editingFile = newFile.id

Expand Down Expand Up @@ -351,6 +355,7 @@ async function store(state, emitter) {
log('error', e)
}

openFile.hasChanges = false
state.isSaving = false
state.savingProgress = 0
emitter.emit('refresh-files')
Expand All @@ -365,6 +370,11 @@ async function store(state, emitter) {
})
emitter.on('close-tab', (id) => {
log('close-tab', id)
const currentTab = state.openFiles.find(f => f.id === id)
if (currentTab.hasChanges && currentTab.parentFolder !== null) {
let response = confirm("Your file has unsaved changes. Are you sure you want to proceed?")
if (!response) return false
}
state.openFiles = state.openFiles.filter(f => f.id !== id)
// state.editingFile = null

Expand All @@ -375,6 +385,10 @@ async function store(state, emitter) {
source: 'disk',
parentFolder: null
})
newFile.editor.onChange = function() {
newFile.hasChanges = true
emitter.emit('render')
}
state.openFiles.push(newFile)
state.editingFile = newFile.id
}
Expand Down Expand Up @@ -995,6 +1009,7 @@ async function store(state, emitter) {
}
}

openFile.hasChanges = false
state.renamingTab = null
state.isSaving = false
state.savingProgress = 0
Expand Down Expand Up @@ -1037,43 +1052,49 @@ async function store(state, emitter) {
let files = []
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') {
if (selectedFile.source == 'board') {
const fileContent = await serial.loadFile(
serial.getFullPath(
'/',
state.boardNavigationPath,
selectedFile.fileName
)
)
files.push(
createFile({
parentFolder: state.boardNavigationPath,
fileName: selectedFile.fileName,
source: selectedFile.source,
content: fileContent
})
)
} else {
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
)
)
files.push(
createFile({
parentFolder: state.diskNavigationPath,
fileName: selectedFile.fileName,
source: selectedFile.source,
content: fileContent
})
)
openFile = createFile({
parentFolder: state.diskNavigationPath,
fileName: selectedFile.fileName,
source: selectedFile.source,
content: fileContent
})
openFile.editor.onChange = function() {
openFile.hasChanges = true
emitter.emit('render')
}
}
files.push(openFile)
}

files = files.filter((f) => { // find files to open
Expand Down Expand Up @@ -1280,11 +1301,17 @@ async function store(state, emitter) {
emitter.emit('render')
})

function createFile({ source, parentFolder, fileName, content = newFileContent }) {
function createFile(args) {
const {
source,
parentFolder,
fileName,
content = newFileContent,
hasChanges = false
} = args
const id = generateHash()
const editor = state.cache(CodeMirrorEditor, `editor_${id}`)
editor.content = content
const hasChanges = false
return {
id,
source,
Expand All @@ -1300,6 +1327,7 @@ async function store(state, emitter) {
fileName: generateFileName(),
parentFolder,
source,
hasChanges: true
})
}
}
Expand Down
8 changes: 6 additions & 2 deletions ui/arduino/views/components/elements/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ class CodeMirrorEditor extends Component {

load(el) {
const onCodeChange = (update) => {
this.content = update.state.doc.toString()
// console.log('code change', this.content)
this.content = update.state.doc.toString()
this.onChange()
}
this.editor = createEditor(this.content, el, onCodeChange)

}

createElement(content) {
Expand All @@ -22,4 +22,8 @@ class CodeMirrorEditor extends Component {
update() {
return false
}

onChange() {
return false
}
}
11 changes: 8 additions & 3 deletions ui/arduino/views/components/elements/tab.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ function Tab(args) {
onFinishRenaming = () => false,
disabled = false,
active = false,
renaming = false
renaming = false,
hasChanges = false
} = args

if (active) {
Expand Down Expand Up @@ -41,7 +42,9 @@ function Tab(args) {
return html`
<div class="tab active" tabindex="0">
<img class="icon" src="media/${icon}" />
<div class="text" ondblclick=${onStartRenaming}>${text}</div>
<div class="text" ondblclick=${onStartRenaming}>
${hasChanges ? ' *' : ''} ${text}
</div>
<div class="options" >
<button onclick=${onCloseTab}>
<img class="icon" src="media/close.svg" />
Expand All @@ -65,7 +68,9 @@ function Tab(args) {
onclick=${selectTab}
>
<img class="icon" src="media/${icon}" />
<div class="text">${text}</div>
<div class="text">
${hasChanges ? '*' : ''} ${text}
</div>
<div class="options">
<button onclick=${onCloseTab}>
<img class="icon" src="media/close.svg" />
Expand Down
1 change: 1 addition & 0 deletions ui/arduino/views/components/tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ function Tabs(state, emit) {
icon: file.source === 'board'? 'connect.svg': 'computer.svg',
active: file.id === state.editingFile,
renaming: file.id === state.renamingTab,
hasChanges: file.hasChanges,
onSelectTab: () => emit('select-tab', file.id),
onCloseTab: () => emit('close-tab', file.id),
onStartRenaming: () => emit('rename-tab', file.id),
Expand Down