Skip to content

Commit

Permalink
Merge branch 'main' into fix-running-forever-maybe
Browse files Browse the repository at this point in the history
  • Loading branch information
fonsp committed Aug 12, 2024
2 parents c836a0c + 01135f7 commit 1c40f9c
Show file tree
Hide file tree
Showing 18 changed files with 428 additions and 412 deletions.
292 changes: 82 additions & 210 deletions frontend/components/CellInput.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions frontend/components/CellOutput.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ import register from "../imports/PreactCustomElement.js"

import { EditorState, EditorView, defaultHighlightStyle, syntaxHighlighting } from "../imports/CodemirrorPlutoSetup.js"

import { pluto_syntax_colors, ENABLE_CM_MIXED_PARSER } from "./CellInput.js"
import { pluto_syntax_colors_julia, ENABLE_CM_MIXED_PARSER } from "./CellInput.js"

import hljs from "../imports/highlightjs.js"
import { julia_mixed } from "./CellInput/mixedParsers.js"
import { julia_andrey } from "../imports/CodemirrorPlutoSetup.js"
import { SafePreviewSanitizeMessage } from "./SafePreviewUI.js"

const prettyAssignee = (assignee) =>
assignee && assignee.startsWith("const ") ? html`<span style="color: var(--cm-keyword-color)">const</span> ${assignee.slice(6)}` : assignee
assignee && assignee.startsWith("const ") ? html`<span style="color: var(--cm-color-keyword)">const</span> ${assignee.slice(6)}` : assignee

export class CellOutput extends Component {
constructor() {
Expand Down Expand Up @@ -676,7 +676,7 @@ export let highlight = (code_element, language) => {
.replace(/Main.workspace#(\d+)/, 'Main.var"workspace#$1"'),

extensions: [
syntaxHighlighting(pluto_syntax_colors),
syntaxHighlighting(pluto_syntax_colors_julia),
syntaxHighlighting(defaultHighlightStyle, { fallback: true }),
EditorState.tabSize.of(4),
// TODO Other languages possibly?
Expand Down
28 changes: 15 additions & 13 deletions frontend/components/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -812,14 +812,16 @@ all patches: ${JSON.stringify(patches, null, 1)}
const set_waiting = () => {
let from_update = message?.response?.update_went_well != null
let is_just_acknowledgement = from_update && message.patches.length === 0
// console.log("Received patches!", message.patches, message.response, is_just_acknowledgement)
let is_relevant_for_bonds = message.patches.some(({ path }) => path.length === 0 || path[0] !== "status_tree")

// console.debug("Received patches!", is_just_acknowledgement, is_relevant_for_bonds, message.patches, message.response)

if (!is_just_acknowledgement) {
this.waiting_for_bond_to_trigger_execution = false
}
}
apply_promise.finally(set_waiting).then(() => {
this.send_queued_bond_changes()
this.maybe_send_queued_bond_changes()
})

break
Expand Down Expand Up @@ -978,18 +980,18 @@ all patches: ${JSON.stringify(patches, null, 1)}

// Not completely happy with this yet, but it will do for now - DRAL
/** Patches that are being delayed until all cells have finished running. */
this.bonds_changes_to_apply_when_done = []
this.send_queued_bond_changes = () => {
if (this.notebook_is_idle() && this.bonds_changes_to_apply_when_done.length !== 0) {
// console.log("Applying queued bond changes!", this.bonds_changes_to_apply_when_done)
let bonds_patches = this.bonds_changes_to_apply_when_done
this.bonds_changes_to_apply_when_done = []
this.bond_changes_to_apply_when_done = []
this.maybe_send_queued_bond_changes = () => {
if (this.notebook_is_idle() && this.bond_changes_to_apply_when_done.length !== 0) {
// console.log("Applying queued bond changes!", this.bond_changes_to_apply_when_done)
let bonds_patches = this.bond_changes_to_apply_when_done
this.bond_changes_to_apply_when_done = []
this.update_notebook((notebook) => {
applyPatches(notebook, bonds_patches)
})
}
}
/** Whether we just set a bond value which will trigger a cell to run, but we are still waiting for the server to process the bond value (and run the cell). During this time, we won't send new bond values. See https://github.com/fonsp/Pluto.jl/issues/1891 for more info. */
/** This tracks whether we just set a bond value which will trigger a cell to run, but we are still waiting for the server to process the bond value (and run the cell). During this time, we won't send new bond values. See https://github.com/fonsp/Pluto.jl/issues/1891 for more info. */
this.waiting_for_bond_to_trigger_execution = false
/** Number of local updates that have not yet been applied to the server's state. */
this.pending_local_updates = 0
Expand All @@ -999,7 +1001,7 @@ all patches: ${JSON.stringify(patches, null, 1)}
*/
this.js_init_set = new SetWithEmptyCallback(() => {
// console.info("All scripts finished!")
this.send_queued_bond_changes()
this.maybe_send_queued_bond_changes()
})

// @ts-ignore This is for tests
Expand Down Expand Up @@ -1067,7 +1069,7 @@ all patches: ${JSON.stringify(patches, null, 1)}
let is_idle = this.notebook_is_idle()
let changes_involving_bonds = changes.filter((x) => x.path[0] === "bonds")
if (!is_idle) {
this.bonds_changes_to_apply_when_done = [...this.bonds_changes_to_apply_when_done, ...changes_involving_bonds]
this.bond_changes_to_apply_when_done = [...this.bond_changes_to_apply_when_done, ...changes_involving_bonds]
changes = changes.filter((x) => x.path[0] !== "bonds")
}

Expand Down Expand Up @@ -1437,7 +1439,7 @@ The notebook file saves every time you run a cell.`
document.title = "🎈 " + new_state.notebook.shortpath + " — Pluto.jl"
}

this.send_queued_bond_changes()
this.maybe_send_queued_bond_changes()

if (old_state.backend_launch_phase !== this.state.backend_launch_phase && this.state.backend_launch_phase != null) {
const phase = Object.entries(BackendLaunchPhase).find(([k, v]) => v == this.state.backend_launch_phase)?.[0]
Expand Down Expand Up @@ -1588,9 +1590,9 @@ The notebook file saves every time you run a cell.`
value=${notebook.in_temp_dir ? "" : notebook.path}
on_submit=${this.submit_file_change}
on_desktop_submit=${this.desktop_submit_file_change}
clear_on_blur=${true}
suggest_new_file=${{
base: this.client.session_options?.server?.notebook_path_suggestion ?? "",
name: notebook.shortpath,
}}
placeholder="Save notebook..."
button_label=${notebook.in_temp_dir ? "Choose" : "Move"}
Expand Down
4 changes: 1 addition & 3 deletions frontend/components/ErrorMessage.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { cl } from "../common/ClassTable.js"
import { PlutoActionsContext } from "../common/PlutoContext.js"
import { EditorState, EditorView, julia_andrey, lineNumbers, syntaxHighlighting } from "../imports/CodemirrorPlutoSetup.js"
import { html, useContext, useEffect, useLayoutEffect, useRef, useState } from "../imports/Preact.js"
import { pluto_syntax_colors } from "./CellInput.js"
import { highlight } from "./CellOutput.js"
import { Editor } from "./Editor.js"
import { PkgTerminalView } from "./PkgTerminalView.js"
import _ from "../imports/lodash.js"

const extract_cell_id = (/** @type {string} */ file) => {
const sep_index = file.indexOf("#==#")
Expand Down
57 changes: 22 additions & 35 deletions frontend/components/FilePicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,12 @@ const assert_not_null = (x) => {
}

const set_cm_value = (/** @type{EditorView} */ cm, /** @type {string} */ value, scroll = true) => {
let had_focus_before = cm.hasFocus

cm.dispatch({
changes: { from: 0, to: cm.state.doc.length, insert: value },
selection: EditorSelection.cursor(value.length),
// a long path like /Users/fons/Documents/article-test-1/asdfasdfasdfsadf.jl does not fit in the little box, so we scroll it to the left so that you can see the filename easily.
scrollIntoView: scroll,
})

if (!had_focus_before) {
// and blur the DOM again (because the previous transaction might have re-focused it)
cm.contentDOM.blur()
}
}

const is_desktop = !!window.plutoDesktop
Expand All @@ -63,9 +56,10 @@ if (is_desktop) {
* on_submit: (new_path: String) => Promise<void>,
* on_desktop_submit?: (loc?: string) => Promise<void>,
* client: import("../common/PlutoConnection.js").PlutoConnection,
* clear_on_blur: Boolean,
* }} props
*/
export const FilePicker = ({ value, suggest_new_file, button_label, placeholder, on_submit, on_desktop_submit, client }) => {
export const FilePicker = ({ value, suggest_new_file, button_label, placeholder, on_submit, on_desktop_submit, client, clear_on_blur }) => {
const [is_button_disabled, set_is_button_disabled] = useState(true)
const [url_value, set_url_value] = useState("")
const forced_value = useRef("")
Expand Down Expand Up @@ -100,7 +94,9 @@ export const FilePicker = ({ value, suggest_new_file, button_label, placeholder,
try {
if (is_desktop && on_desktop_submit) {
await on_desktop_submit((await guess_notebook_location(url_value)).path_or_url)
} else await on_submit(current_cm.state.doc.toString())
} else {
await on_submit(current_cm.state.doc.toString())
}
current_cm.dom.blur()
} catch (error) {
set_cm_value(current_cm, forced_value.current, true)
Expand All @@ -110,6 +106,19 @@ export const FilePicker = ({ value, suggest_new_file, button_label, placeholder,
return true
}

const onBlur = (e) => {
const still_in_focus = base.current?.matches(":focus-within") || base.current?.contains(e.relatedTarget)
if (still_in_focus) return
const current_cm = cm.current
if (current_cm == null) return
if (clear_on_blur)
requestAnimationFrame(() => {
if (!current_cm.hasFocus) {
set_cm_value(current_cm, forced_value.current, true)
}
})
}

const request_path_completions = () => {
const current_cm = cm.current
if (current_cm == null) return
Expand All @@ -135,19 +144,12 @@ export const FilePicker = ({ value, suggest_new_file, button_label, placeholder,
setTimeout(() => {
if (suggest_new_file) {
suggest_not_tmp()
} else if (cm.state.doc.length === 0) {
} else {
request_path_completions()
}
}, 0)
return true
},
blur: (event, cm) => {
setTimeout(() => {
if (!cm.hasFocus) {
set_cm_value(cm, forced_value.current, true)
}
}, 200)
},
}),
EditorView.updateListener.of((update) => {
if (update.docChanged) {
Expand Down Expand Up @@ -217,21 +219,6 @@ export const FilePicker = ({ value, suggest_new_file, button_label, placeholder,
mac: "Cmd-Shift-Enter",
run: keyMapSubmit,
},
{
key: "Escape",
run: (cm) => {
assert_not_null(close_autocomplete_command).run(cm)
cm.dispatch({
changes: { from: 0, to: cm.state.doc.length, insert: forced_value.current },
selection: EditorSelection.cursor(value.length),
effects: EditorView.scrollIntoView(forced_value.current.length),
})
// @ts-ignore
document.activeElement.blur()
return true
},
preventDefault: true,
},
{
key: "Tab",
run: (cm) => {
Expand Down Expand Up @@ -285,15 +272,15 @@ export const FilePicker = ({ value, suggest_new_file, button_label, placeholder,
</div>
</div>`
: html`
<pluto-filepicker ref=${base}>
<pluto-filepicker ref=${base} onfocusout=${onBlur}>
<button onClick=${onSubmit} disabled=${is_button_disabled}>${button_label}</button>
</pluto-filepicker>
`
}

const pathhints =
({ client, suggest_new_file }) =>
(ctx) => {
(/** @type {autocomplete.CompletionContext} */ ctx) => {
const cursor = ctx.state.selection.main.to
const oldLine = ctx.state.doc.toString()

Expand All @@ -302,7 +289,7 @@ const pathhints =
query: oldLine,
})
.then((update) => {
const queryFileName = oldLine.split("/").pop().split("\\").pop()
const queryFileName = (oldLine.split("/").pop() ?? "").split("\\").pop() ?? ""

const results = update.message.results
const from = utf8index_to_ut16index(oldLine, update.message.start)
Expand Down
20 changes: 11 additions & 9 deletions frontend/components/welcome/Open.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,20 @@ export const Open = ({ client, connected, CustomPicker, show_samples, on_start_n
value=""
on_submit=${on_open_path}
on_desktop_submit=${desktop_on_open_path}
clear_on_blur=${false}
button_label=${window.plutoDesktop ? "Open File" : "Open"}
placeholder=${picker.placeholder}
/>
${window.plutoDesktop &&
html`<${FilePicker}
key=${picker.placeholder}
client=${client}
value=""
on_desktop_submit=${desktop_on_open_url}
button_label="Open from URL"
placeholder=${picker.placeholder}
/>`}
${window.plutoDesktop != null
? html`<${FilePicker}
key=${picker.placeholder}
client=${client}
value=""
on_desktop_submit=${desktop_on_open_url}
button_label="Open from URL"
placeholder=${picker.placeholder}
/>`
: null}
</div>`
}

Expand Down
Loading

0 comments on commit 1c40f9c

Please sign in to comment.