Skip to content

Commit 71c7c41

Browse files
committed
Prompt on code change
1 parent f3e89db commit 71c7c41

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

ui/arduino/components/editor.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
function Editor(state, emit) {
2+
const el = state.cache(AceEditor, 'editor')
3+
el.codeChange = () => emit('code-change')
24
return html`
3-
${state.cache(AceEditor, 'editor').render()}
5+
${el.render()}
46
`
57
}
68

@@ -23,6 +25,7 @@ for i in range(0, 10):
2325
sleep(0.1)
2426
2527
`)
28+
this.editor.session.on('change', () => this.codeChange())
2629
}
2730

2831
createElement(content) {
@@ -31,9 +34,12 @@ for i in range(0, 10):
3134

3235
update(newContent) {
3336
if (newContent) {
37+
console.log('newContent', newContent)
3438
this.editor.setValue(newContent)
3539
}
3640
setTimeout(() => this.editor.resize(), 10)
3741
return false
3842
}
43+
44+
codeChange() { return }
3945
}

ui/arduino/store.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ function store(state, emitter) {
3535
state.panelHeight = null
3636

3737
state.blocking = false
38+
state.unsavedChanges = false
3839

3940
// SERIAL CONNECTION
4041
emitter.on('load-ports', async () => {
@@ -145,6 +146,7 @@ function store(state, emitter) {
145146
state.selectedDevice = dev
146147
let editor = state.cache(AceEditor, 'editor').editor
147148
state.selectedFile = null
149+
state.unsavedChanges = false
148150
editor.setValue('')
149151
emitter.emit('close-new-file-dialog')
150152
emitter.emit('render')
@@ -157,7 +159,6 @@ function store(state, emitter) {
157159
let filename = state.selectedFile || 'undefined'
158160
let deviceName = state.selectedDevice === 'serial' ? 'board' : 'disk'
159161

160-
161162
state.blocking = true
162163
emitter.emit('message', `Saving ${filename} on ${deviceName}`)
163164

@@ -167,6 +168,7 @@ function store(state, emitter) {
167168
contents
168169
)
169170
setTimeout(() => emitter.emit('update-files'), 1000)
171+
state.unsavedChanges = false
170172
emitter.emit('message', `Saved`, 1000)
171173
}
172174

@@ -177,6 +179,7 @@ function store(state, emitter) {
177179
contents
178180
)
179181
setTimeout(() => emitter.emit('update-files'), 100)
182+
state.unsavedChanges = false
180183
emitter.emit('message', `Saved`, 500)
181184
}
182185

@@ -210,7 +213,10 @@ function store(state, emitter) {
210213
})
211214
emitter.on('select-file', async (device, filename) => {
212215
log('select-file')
213-
216+
if (state.unsavedChanges) {
217+
let response = confirm(`Loading a new file will discard any unsaved changes.\nPress OK to accept or Cancel to abort.`)
218+
if (!response) return
219+
}
214220
state.selectedDevice = device
215221

216222
/*
@@ -237,10 +243,11 @@ function store(state, emitter) {
237243
)
238244
}
239245

240-
state.selectedFile = filename
241246
let editor = state.cache(AceEditor, 'editor').editor
242247
editor.setValue(cleanCharacters(content))
243248

249+
state.selectedFile = filename
250+
state.unsavedChanges = false
244251
state.blocking = false
245252
emitter.emit('render')
246253
})
@@ -381,6 +388,11 @@ function store(state, emitter) {
381388
}
382389
})
383390

391+
emitter.on('code-change', () => {
392+
log('code-changed')
393+
state.unsavedChanges = true
394+
})
395+
384396
// PANEL MANAGEMENT
385397
emitter.on('show-terminal', () => {
386398
log('show-terminal')
@@ -481,6 +493,7 @@ function store(state, emitter) {
481493
state.selectedFile = oldFilename
482494
state.isEditingFilename = false
483495
state.blocking = false
496+
state.unsavedChanges = false
484497
emitter.emit('render')
485498
}
486499
}
@@ -530,6 +543,7 @@ function store(state, emitter) {
530543
}
531544
if (state.selectedDevice === device) {
532545
state.selectedFile = null
546+
state.unsavedChanges = false
533547
}
534548
emitter.emit('update-files')
535549
})
@@ -553,6 +567,7 @@ function store(state, emitter) {
553567
}
554568
if (state.selectedDevice === device) {
555569
state.selectedFile = null
570+
state.unsavedChanges = false
556571
}
557572
emitter.emit('update-files')
558573
})

0 commit comments

Comments
 (0)