-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also export eval-region extension as module available at ``` import * as evalRegion from '@nextjournal/clojure-mode/extensions/eval-region' ```
- Loading branch information
Showing
11 changed files
with
187 additions
and
264 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,14 @@ | |
} | ||
} | ||
</style> | ||
<script type="importmap"> | ||
{ | ||
"imports": { | ||
"squint-cljs/core.js": "https://unpkg.com/[email protected]/core.js" | ||
} | ||
} | ||
</script> | ||
|
||
</head> | ||
<body> | ||
<div class="landing-page pt-10"> | ||
|
@@ -88,12 +96,12 @@ <h2 id="try-it" class="mt-0 mb-12 text-center text-3xl font-bold"> | |
</h2> | ||
<div class="flex flex-col-reverse md:flex-row"> | ||
<div class="md:w-1/2 flex-shrink-0 md:px-6 mt-12 md:mt-0"> | ||
<h3 class="text-center sans-serif font-bold text-lg mt-0 mb-1">Try evaluating any of these forms with <span class="kbd alt font-normal">Alt</span> <span class="font-normal">+</span> <span class="kbd font-normal">⏎</span> !</h3> | ||
<h3 class="text-center sans-serif font-bold text-lg mt-0 mb-1">Try evaluating any of these forms with <span class="kbd mod font-normal">Mod</span> <span class="font-normal">+</span> <span class="kbd font-normal">⏎</span> !</h3> | ||
<p class="sans-serif text-sm text-center mb-6 mt-0"> | ||
In-browser eval is powered by <a href="https://github.com/borkdude/sci">Sci</a>. | ||
In-browser eval is powered by <a href="https://github.com/squint-cljs/squint">Squint</a>. | ||
</p> | ||
<div id="editor" class="rounded-md mb-0 text-sm monospace overflow-auto relative border shadow-lg bg-white"> | ||
</div> | ||
<div id="editor" class="rounded-md mb-0 text-sm monospace overflow-auto relative border shadow-lg bg-white"></div> | ||
<div id="result" class="mt-3.mv-4.pl-6" style="white-space: pre-wrap; font-family: var(--code-font)"></div> | ||
</div> | ||
<div class="md:w-1/2 flex-shrink-0 md:px-6 sans-serif"> | ||
<ul class="text-lg"> | ||
|
@@ -162,23 +170,23 @@ <h3 class="text-center sans-serif font-bold text-lg mt-0 mb-1">Try evaluating an | |
At Cursor | ||
</td> | ||
<td class="py-1 text-right"> | ||
<span class="kbd alt">Alt</span> + <span class="kbd">⏎</span> | ||
<span class="kbd mod">Mod</span> + <span class="kbd">⏎</span> | ||
</td> | ||
</tr> | ||
<tr class="border-t"> | ||
<td class="py-1 pr-12"> | ||
Top-level form | ||
</td> | ||
<td class="py-1 text-right"> | ||
<span class="kbd alt">Alt</span> + <span class="kbd">⇧</span> + <span class="kbd">⏎</span> | ||
<span class="kbd alt">Mod</span> + <span class="kbd">⇧</span> + <span class="kbd">⏎</span> | ||
</td> | ||
</tr> | ||
<tr class="border-t"> | ||
<td class="py-1 pr-12"> | ||
Cell | ||
</td> | ||
<td class="py-1 text-right"> | ||
<span class="kbd mod">Mod</span> + <span class="kbd">⏎</span> | ||
<span class="kbd alt">Alt</span> + <span class="kbd">⏎</span> | ||
</td> | ||
</tr> | ||
</tbody> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import { default_extensions, complete_keymap } from '@nextjournal/clojure-mode'; | ||
import { extension as eval_ext, cursor_node_string, top_level_string } from '@nextjournal/clojure-mode/extensions/eval-region'; | ||
import { EditorView, drawSelection, keymap } from '@codemirror/view'; | ||
import { EditorState } from '@codemirror/state'; | ||
import { syntaxHighlighting, defaultHighlightStyle, foldGutter } from '@codemirror/language'; | ||
import { compileString } from 'squint-cljs'; | ||
|
||
let theme = EditorView.theme({ | ||
".cm-content": {whitespace: "pre-wrap", | ||
|
@@ -23,14 +25,79 @@ let theme = EditorView.theme({ | |
"&.cm-focused .cm-cursor": {visibility: "visible"} | ||
}); | ||
|
||
let evalCode = async function (code) { | ||
let js = compileString(`(do ${code})`, {repl: true, | ||
context: 'return', | ||
"elide-exports": true}) | ||
let result; | ||
try { | ||
result = {value: await eval(`(async function() { ${js} })()`)}; | ||
} | ||
catch (e) { | ||
result = {error: true, ex: e}; | ||
} | ||
if (result.error) { | ||
document.getElementById("result").innerText = result.ex; | ||
} else { | ||
document.getElementById("result").innerText = '' + JSONstringify(result.value); | ||
} | ||
} | ||
|
||
let evalCell = (opts) => { | ||
let code = opts.state.doc.toString(); | ||
evalCode(code); | ||
return true; | ||
} | ||
|
||
let evalToplevel = function (opts) { | ||
let state = opts.state; | ||
let code = top_level_string(state); | ||
evalCode(code); | ||
return true; | ||
} | ||
|
||
function JSONstringify(json) { | ||
json = JSON.stringify(json, function(key, value) { | ||
if (!value) return value; | ||
if (typeof value === 'string') return value; | ||
if (Array.isArray(value) || value.constructor === Object) return value; | ||
if (value[Symbol.iterator]) { | ||
return [...value]; | ||
} | ||
if (typeof value === 'object') { | ||
return `#object[${value.constructor.name}]`; | ||
} else { | ||
return value; | ||
} | ||
}); | ||
return json; | ||
} | ||
|
||
let evalAtCursor = function (opts) { | ||
let state = opts.state; | ||
let code = cursor_node_string(state); | ||
evalCode(code); | ||
return true; | ||
} | ||
|
||
let squintExtension = ( opts ) => { | ||
return keymap.of([{key: "Alt-Enter", run: evalCell}, | ||
{key: opts.modifier + "-Enter", | ||
run: evalAtCursor, | ||
shift: evalToplevel | ||
}])} | ||
|
||
|
||
let extensions = [ theme, foldGutter(), | ||
syntaxHighlighting(defaultHighlightStyle), | ||
drawSelection(), | ||
keymap.of(complete_keymap), | ||
...default_extensions | ||
...default_extensions, | ||
eval_ext({modifier: "Meta"}), | ||
squintExtension({modifier: "Meta"}) | ||
]; | ||
|
||
let state = EditorState.create( {doc: `(comment | ||
let doc = `(comment | ||
(fizz-buzz 1) | ||
(fizz-buzz 3) | ||
(fizz-buzz 5) | ||
|
@@ -43,9 +110,48 @@ let state = EditorState.create( {doc: `(comment | |
15 "fizzbuzz" | ||
3 "fizz" | ||
5 "buzz" | ||
n))`, | ||
n)) | ||
(require '["https://esm.sh/[email protected]$default" :as confetti]) | ||
(do | ||
(js-await (confetti)) | ||
(+ 1 2 3)) | ||
` ; | ||
|
||
evalCode(doc); | ||
|
||
let state = EditorState.create( {doc: doc, | ||
extensions: extensions }); | ||
|
||
let editorElt = document.querySelector('#editor'); | ||
let editor = new EditorView({state: state, | ||
parent: editorElt, | ||
extensions: extensions }); | ||
extensions: extensions }) | ||
|
||
let keys = {"ArrowUp": "↑", | ||
"ArrowDown": "↓", | ||
"ArrowRight": "→", | ||
"ArrowLeft": "←", | ||
"Mod": "Ctrl"} | ||
|
||
let macKeys = {"Alt": "⌥", | ||
"Shift": "⇧", | ||
"Enter": "⏎", | ||
"Ctrl": "⌃", | ||
"Mod": "⌘"} | ||
|
||
let mac; | ||
|
||
if (/^(Mac)|(iPhone)|(iPad)|(iPod)$/.test(window.navigator.platform.substring(0,3))) { | ||
mac = true; | ||
Object.assign(keys, macKeys); | ||
} | ||
|
||
document.querySelectorAll(".mod,.alt,.ctrl").forEach(node => { | ||
let k = node.innerHTML; | ||
let symbol = keys[k]; | ||
if (symbol) { | ||
node.innerHTML = symbol; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
{:paths ["src-shared" "src-squint" "test" "node_modules/squint-macros/src"] | ||
{:paths ["src-shared" "src-squint" "test" | ||
"node_modules/@squint-cljs/macros/src"] | ||
:output-dir "dist"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.