From 63e400d96ef75b0d00ce62b2e00c797ff0ecdd07 Mon Sep 17 00:00:00 2001 From: Fons van der Plas Date: Sun, 13 Sep 2020 03:10:42 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=A6=A1=20Fix=20#239?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/common/PlutoConnection.js | 5 +- frontend/components/CellInput.js | 26 ++++++-- frontend/components/DropRuler.js | 22 +++---- frontend/components/Editor.js | 97 +++++++++++------------------- 4 files changed, 68 insertions(+), 82 deletions(-) diff --git a/frontend/common/PlutoConnection.js b/frontend/common/PlutoConnection.js index 891bae69d2..29b4cf2115 100644 --- a/frontend/common/PlutoConnection.js +++ b/frontend/common/PlutoConnection.js @@ -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 ( @@ -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?") @@ -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 } diff --git a/frontend/components/CellInput.js b/frontend/components/CellInput.js index 356dcdd54c..77bfe23c7b 100644 --- a/frontend/components/CellInput.js +++ b/frontend/components/CellInput.js @@ -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)) } } }) @@ -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 + } +} diff --git a/frontend/components/DropRuler.js b/frontend/components/DropRuler.js index 28f6f96774..d611e42a4f 100644 --- a/frontend/components/DropRuler.js +++ b/frontend/components/DropRuler.js @@ -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() + } } }) } diff --git a/frontend/components/Editor.js b/frontend/components/Editor.js index 11b55a1fb4..5f0e24eecd 100644 --- a/frontend/components/Editor.js +++ b/frontend/components/Editor.js @@ -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() } })