Skip to content

Commit

Permalink
🦡 Fix #239
Browse files Browse the repository at this point in the history
  • Loading branch information
fonsp committed Sep 13, 2020
1 parent 59278d5 commit 63e400d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 82 deletions.
5 changes: 2 additions & 3 deletions frontend/common/PlutoConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ export const create_pluto_connection = async ({ on_unrequested_update, on_reconn
ws_connection.send(message)
return p
}
client.send = send

const connect = async () => {
const secret = await (
Expand Down Expand Up @@ -298,6 +299,7 @@ export const create_pluto_connection = async ({ on_unrequested_update, on_reconn
)

console.log(ws_connection)
client.kill = ws_connection.kill

// let's say hello
console.log("Hello?")
Expand All @@ -324,9 +326,6 @@ export const create_pluto_connection = async ({ on_unrequested_update, on_reconn
}
await connect()

client.send = send
client.kill = ws_connection.kill

return client
}

Expand Down
26 changes: 22 additions & 4 deletions frontend/components/CellInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,14 @@ export const CellInput = ({
on_update_doc_query(sel)
}
} else {
const token = cm.getTokenAt(cm.getCursor())
const cursor = cm.getCursor()
const token = cm.getTokenAt(cursor)
if (token.start === 0 && token.type === "operator" && token.string === "?") {
// https://github.com/fonsp/Pluto.jl/issues/321
const second_token = cm.getTokenAt({ ...cm.getCursor(), ch: 2 })
const second_token = cm.getTokenAt({ ...cursor, ch: 2 })
on_update_doc_query(second_token.string)
} else if (token.type != null && token.type !== "string") {
on_update_doc_query(token.string)
on_update_doc_query(module_expanded_selection(cm, token.string, cursor.line, token.start))
}
}
})
Expand Down Expand Up @@ -321,7 +322,24 @@ const juliahints = (cm, options) => {
from: window.CodeMirror.Pos(cursor.line, utf8index_to_ut16index(old_line, update.message.start)),
to: window.CodeMirror.Pos(cursor.line, utf8index_to_ut16index(old_line, update.message.stop)),
}
window.CodeMirror.on(completions, "select", options.on_update_doc_query)
window.CodeMirror.on(completions, "select", (val) => {
options.on_update_doc_query(module_expanded_selection(cm, val, cursor.line, completions.from.ch))
})
return completions
})
}

// https://github.com/fonsp/Pluto.jl/issues/239
const module_expanded_selection = (cm, current, line, ch) => {
const next1 = cm.getTokenAt({ line: line, ch: ch })
if (next1.string === ".") {
const next2 = cm.getTokenAt({ line: line, ch: ch - 1 })
if (next2.type === "variable") {
return module_expanded_selection(cm, next2.string + "." + current, line, next2.start)
} else {
return current
}
} else {
return current
}
}
22 changes: 9 additions & 13 deletions frontend/components/DropRuler.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,19 +158,15 @@ export class DropRuler extends Component {

// Ctrl+A to select all cells
document.addEventListener("keydown", (e) => {
switch (e.keyCode) {
case 65: // a
if (e.ctrlKey) {
// if you are not writing text somewhere else
if (document.activeElement === document.body && window.getSelection().isCollapsed) {
this.props.on_selection({
selection_start_index: 0,
selection_stop_index: Infinity,
})
e.preventDefault()
}
}
break
if (e.key === "a" && e.ctrlKey) {
// if you are not writing text somewhere else
if (document.activeElement === document.body && window.getSelection().isCollapsed) {
this.props.on_selection({
selection_start_index: 0,
selection_stop_index: Infinity,
})
e.preventDefault()
}
}
})
}
Expand Down
97 changes: 35 additions & 62 deletions frontend/components/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -613,69 +613,42 @@ export class Editor extends Component {
}

document.addEventListener("keydown", (e) => {
switch (e.keyCode) {
case 81: // q
if (e.ctrlKey) {
if (this.state.notebook.cells.some((c) => c.running || c.queued)) {
this.requests.interrupt_remote()
}
e.preventDefault()
}
break
case 82: // r
// I commonly have a test notebook that I want to re-run after changing something to the backend
// if I would just reload the page, then the new Pluto session would be asked to open notebook with uuid=b1d2cbdb1c2bb12d, which does not exist in the new session
if (e.ctrlKey) {
if (this.state.notebook.path !== default_path) {
document.location.href = link_open_path(this.state.notebook.path)
}
e.preventDefault()
}
break
case 83: // s
if (e.ctrlKey) {
const some_cells_ran = this.requests.set_and_run_all_changed_remote_cells()
if (!some_cells_ran) {
// all cells were in sync allready
// TODO: let user know that the notebook autosaves
}
e.preventDefault()
}
break
case 8: // backspace
// fall into:
case 46: // delete
const selected = this.state.notebook.cells.filter((c) => c.selected)
if (selected.length > 0) {
this.requests.confirm_delete_multiple(selected)
e.preventDefault()
}
break
case 191: // ? or /
if (!(e.ctrlKey && e.shiftKey)) {
break
}
// fall into:
case 112: // F1
// TODO: show help
alert(
`Shortcuts 🎹
Shift+Enter: run cell
Ctrl+Enter: run cell and add cell below
Delete or Backspace: delete empty cell
PageUp or fn+Up: select cell above
PageDown or fn+Down: select cell below
Ctrl+Q: interrupt notebook
Ctrl+S: submit all changes
The notebook file saves every time you run`
)

console.log(e)
if (e.code === "KeyQ" && e.ctrlKey) {
if (this.state.notebook.cells.some((c) => c.running || c.queued)) {
this.requests.interrupt_remote()
}
e.preventDefault()
} else if (e.code === "KeyS" && e.ctrlKey) {
const some_cells_ran = this.requests.set_and_run_all_changed_remote_cells()
if (!some_cells_ran) {
// all cells were in sync allready
// TODO: let user know that the notebook autosaves
}
e.preventDefault()
} else if (e.code === "Backspace" || e.code === "Delete") {
const selected = this.state.notebook.cells.filter((c) => c.selected)
if (selected.length > 0) {
this.requests.confirm_delete_multiple(selected)
e.preventDefault()
break
}
} else if ((e.key === "?" && e.ctrlKey) || e.key === "F1") {
alert(
`Shortcuts 🎹
Shift+Enter: run cell
Ctrl+Enter: run cell and add cell below
Delete or Backspace: delete empty cell
PageUp or fn+Up: select cell above
PageDown or fn+Down: select cell below
Ctrl+Q: interrupt notebook
Ctrl+S: submit all changes
The notebook file saves every time you run`
)
e.preventDefault()
}
})

Expand Down

0 comments on commit 63e400d

Please sign in to comment.