Skip to content

Commit

Permalink
🎁 Ask to wrap cell
Browse files Browse the repository at this point in the history
in a `begin ... end` block
  • Loading branch information
fonsp committed Apr 8, 2020
1 parent 720f90f commit 3e197cd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 52 deletions.
60 changes: 15 additions & 45 deletions assets/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -597,55 +597,25 @@ document.addEventListener("DOMContentLoaded", () => {

/* ERROR HINTS */

const hint1 = "Combine all definitions into a single reactive cell using a `begin ... end` block."
const hint2 = "Wrap all code in a `begin ... end` block."

const hint1_interactive = "<a href=\"#\" onclick=\"errorHint1(event)\">" + hint1 + "</a>"
const hint2_interactive = "<a href=\"#\" onclick=\"errorHint2(event)\">" + hint2 + "</a>"

function getOthers(e){
const cellNode = e.target.parentElement.parentElement.parentElement.parentElement
const myError = e.target.parentElement.innerHTML
var others = []
for (var uuid in window.localCells) {
const c = window.localCells[uuid]
if(c.classList.contains("error") && c.querySelector("celloutput>pre>code").innerHTML == myError) {
others.push(uuid)
}
}
return others
}
window.errorHint1 = (e) => {
combineCells(getOthers(e))
}
const errorRewrites = [
{
from: "syntax: extra token after end of expression",
to: "Multiple expressions in one cell.\n<a href=\"#\" onclick=\"errorHint(event)\">Wrap all code in a `begin ... end` block.</a>",
},
]

window.errorHint2 = (e) => {
const cellNode = e.target.parentElement.parentElement.parentElement.parentElement
wrapInBlock(window.codeMirrors[cellNode.id], "begin")
requestChangeRemoteCell(cellNode.id)
}

function rewrittenError(old_raw) {
return old_raw//.replace(hint1, hint1_interactive)
.replace(hint2, hint2_interactive)
var new_raw = old_raw;
errorRewrites.forEach(rw => {
new_raw = new_raw.replace(rw.from, rw.to)
})
return new_raw
}

function combineCells(uuids){
selectedCellNodes = uuids.map(u => localCells[u])
selectedCodeMirrors = uuids.map(u => codeMirrors[u])

client.sendreceive("addcell", {
index: indexOfLocalCell(selectedCellNodes[0]),
}).then(update => {
const combinedCode = selectedCodeMirrors.map(cm => cm.getValue()).join("\n\n")
createLocalCell(update.message.index, update.cellID, combinedCode)
wrapInBlock(codeMirrors[update.cellID], "begin")

requestChangeRemoteCell(update.cellID, true).then( u=> {
uuids.map(requestDeleteRemoteCell)
})

})
window.errorHint = (e) => {
const cellNode = e.target.parentElement.parentElement.parentElement.parentElement
wrapInBlock(window.codeMirrors[cellNode.id], "begin")
requestChangeRemoteCell(cellNode.id)
}

function wrapInBlock(cm, block="begin") {
Expand Down
5 changes: 5 additions & 0 deletions assets/light.css
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ celloutput p:last-child {
margin-block-end: 0px;
}

celloutput>pre>code>a {
color: inherit;
font-weight: 500;
}


/* HEADER */
header {
Expand Down
7 changes: 0 additions & 7 deletions src/react/Errors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ function MultipleDefinitionsError(cell::Cell, all_definers)
MultipleDefinitionsError
end

struct MultipleExpressionsError <: ReactivityError
end

# Also update identically name variables in `editor.js`.
hint1 = "Combine all definitions into a single reactive cell using a `begin ... end` block."
hint2 = "Wrap all code in a `begin ... end` block."
Expand All @@ -44,10 +41,6 @@ function showerror(io::IO, mde::MultipleDefinitionsError)
print(io, "Multiple definitions for $(join(mde.syms, ", ", " and ")).\n$hint1") # TODO: hint about mutable globals
end

function showerror(io::IO, mee::MultipleExpressionsError)
print(io, "Multiple expressions in one cell.\n$hint2")
end

"Send `error` to the frontend without backtrace. Runtime errors are handled by `WorkspaceManager.eval_fetch_in_workspace` - this function is for Reactivity errors."
function relay_reactivity_error!(cell::Cell, error::Exception)
cell.output_repr = nothing
Expand Down

1 comment on commit 3e197cd

@fonsp
Copy link
Owner Author

@fonsp fonsp commented on 3e197cd Apr 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#39

Please sign in to comment.