-
-
Notifications
You must be signed in to change notification settings - Fork 217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: Add webview output experience #2481
Draft
bpringe
wants to merge
39
commits into
dev
Choose a base branch
from
add_new_output_experience
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+361
−11
Draft
Changes from 27 commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
17ad745
Add repl output ns
bpringe c9b5334
Merge branch 'dev' into node-library_target_inject_vscode
bpringe 514f9df
Add dev code
bpringe 5871dfa
Add cljs code to get project root uri
bpringe 6ec215d
Add function for creating repl output file (markdown)
bpringe 3af8dfd
Add more markdown experimentation code
bpringe 81303d5
Merge remote-tracking branch 'origin/dev' into add_new_output_experience
bpringe 84f45e5
Merge remote-tracking branch 'origin/dev' into add_new_output_experience
bpringe 26c6dad
Create webview-output ns - WIP
bpringe a193bcc
Use html file for webview html
bpringe 756d366
Add highlight.js
bpringe 56087e9
Begin setup for using reagent in output webview - WIP
bpringe 94d73dc
Add reagent UI for repl output - WIP
bpringe f97c478
Make reagent UI work
bpringe 0ad4652
Make posting results as messages work
bpringe 06953ce
Merge remote-tracking branch 'origin/dev' into add_new_output_experience
bpringe 45a629a
Add build to watch-cljs script
bpringe 695afde
Open webview output on activation and print 10 messages
bpringe 65f4229
Fix comment typo
bpringe 2f85dff
Merge remote-tracking branch 'origin/dev' into add_new_output_experience
bpringe e005870
Update package-lock.json
bpringe d50b123
Remove comment form
bpringe 5386de6
Merge remote-tracking branch 'origin/dev' into add_new_output_experience
bpringe 11dfb42
Remove markdown output file
bpringe 202329f
Fix project-root-uri-key
bpringe abe1a17
Get rid of react warning about every element needing a key
bpringe 53a3718
Use create-root so that react 18 is used
bpringe 18803b4
Merge remote-tracking branch 'origin/dev' into add_new_output_experience
bpringe f71343f
WIP: Update webview CSP
bpringe 3d00c9a
Move hightlightAll call to cljs
bpringe 4eb3975
Add comment
bpringe 23022f6
Merge remote-tracking branch 'origin/dev' into add_new_output_experience
bpringe 2c60af5
Add replicant
bpringe 5be9a7d
Merge remote-tracking branch 'origin/dev' into add_new_output_experience
bpringe 8f9eca9
Add replicant and set up a simple multimethod command system
bpringe 77e87f8
Fix typo
bpringe af177c1
Update deps-clj-version and deps.clj.jar
bpringe 45ea2d3
Remove react and reagent deps, move snitch dep to dev alias, refactor…
bpringe 32c27e7
Add multimethod for getting hiccup for output element
bpringe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ | |
/.clj-kondo | ||
/site | ||
/test-data | ||
/repl-output-ui |
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 |
---|---|---|
|
@@ -50,6 +50,7 @@ jspm_packages | |
lib/ | ||
cljs-out/ | ||
test-out/ | ||
repl-output-ui/ | ||
|
||
# This and that | ||
.nrepl-port | ||
|
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 |
---|---|---|
|
@@ -9,3 +9,4 @@ | |
**/.shadow-cljs/ | ||
**/out/ | ||
clojure.tmLanguage.json | ||
/repl-output-ui |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,97 @@ | ||
(ns calva.repl.webview.core | ||
(:require | ||
[calva.util :as util])) | ||
|
||
(defonce repl-output-webview-panel (atom nil)) | ||
|
||
(defn dispose-repl-output-webview-panel [] | ||
(println "Disposing repl-output-webview-panel") | ||
(reset! repl-output-webview-panel nil)) | ||
|
||
;; TODO: See if there's a way to not have to use ^js in so many places without shadow-cljs warnings | ||
(defn create-or-get-repl-output-webview-panel [] | ||
(or @repl-output-webview-panel | ||
(let [webview-panel (.. ^js @util/vscode -window | ||
(createWebviewPanel "calva:repl-output" | ||
"REPL Output" | ||
(.. ^js @util/vscode -ViewColumn -Two) | ||
#js {:enableScripts true}))] | ||
(.. ^js webview-panel (onDidDispose dispose-repl-output-webview-panel)) | ||
(reset! repl-output-webview-panel webview-panel)))) | ||
|
||
(defn get-webview-html | ||
[js-src] | ||
(str " | ||
<!DOCTYPE html> | ||
<html lang=\"en\"> | ||
<head> | ||
<meta charset=\"UTF-8\" /> | ||
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" /> | ||
<title>REPL Output</title> | ||
<link | ||
rel=\"stylesheet\" | ||
href=\"https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/atom-one-dark.min.css\" | ||
/> | ||
<script src=\"https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js\"></script> | ||
<script src=\"https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/clojure.min.js\"></script> | ||
<script> | ||
hljs.highlightAll(); | ||
</script> | ||
</head> | ||
<body> | ||
<!-- TODO: Disable inline scripts - see security section in webview docs --> | ||
<div id=\"output\"></div> | ||
|
||
<script src=\"" js-src "\"></script> | ||
</body> | ||
</html>")) | ||
|
||
(defn post-message-to-webview [message] | ||
(.. ^js @repl-output-webview-panel | ||
-webview | ||
(postMessage (clj->js (merge | ||
{:id (str (random-uuid))} ;; Provide an id if one wasn't provided by the caller | ||
message))))) | ||
|
||
(defn show-repl-output-webview-panel [] | ||
(let [^js repl-output-webview-panel (create-or-get-repl-output-webview-panel) | ||
js-path (.. ^js @util/vscode | ||
-Uri | ||
(joinPath (.. ^js @util/context -extensionUri) "repl-output-ui" "js" "main.js")) | ||
js-src (.. repl-output-webview-panel -webview (asWebviewUri js-path)) | ||
webview-html (get-webview-html js-src)] | ||
(set! (.. ^js repl-output-webview-panel -webview -html) webview-html) | ||
(let [interval-id (js/setInterval post-message-to-webview | ||
1000 | ||
{:command-name "show-result" | ||
:result "Hello world!!!"})] | ||
(js/setTimeout #(js/clearInterval interval-id) | ||
11000)))) | ||
|
||
;; TODO: See if can send repl output to webview when it's hidden and see it once unhidden | ||
;; "You cannot send messages to a hidden webview, even when retainContextWhenHidden is enabled." | ||
;; https://code.visualstudio.com/api/extension-guides/webview#theming-webview-content | ||
|
||
(comment | ||
(show-repl-output-webview-panel) | ||
|
||
;; TODO: Implement this interface for communicating with the webview | ||
;; Message | ||
{;; This message contains a command | ||
:command {;; Command name | ||
:name "show-result" | ||
;; Command args | ||
:args {:result "Hello world"}} | ||
;; Message id | ||
:id "1234"} | ||
|
||
(post-message-to-webview {:command-name "show-result" | ||
:result "send while hidden"}) | ||
|
||
(post-message-to-webview {:command "clear-output"}) | ||
|
||
@repl-output-webview-panel | ||
|
||
;; TODO: Don't worry about scrolling yet. We know we can do that. Explore other important unknowns first. | ||
|
||
:rcf) |
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
(ns calva.repl.webview.ui | ||
(:require | ||
[reagent.dom.client :as rdom.client] | ||
[reagent.core :as r])) | ||
|
||
(defn output-element | ||
[element-data] | ||
(merge element-data | ||
{:output-element/id (random-uuid)})) | ||
|
||
(defonce output-elements | ||
(r/atom [(output-element {:output-element/type :output-element.type/eval-result | ||
:output-element/content "{:a 1}"}) | ||
(output-element {:output-element/type :output-element.type/stdout | ||
:output-element/content "Hello world"})])) | ||
|
||
(defn add-output-element | ||
[element] | ||
(swap! output-elements conj element)) | ||
|
||
(defn add-eval-result | ||
[content] | ||
(add-output-element (output-element {:output-element/type :output-element.type/eval-result | ||
:output-element/content content}))) | ||
|
||
(defn add-stdout | ||
[content] | ||
(add-output-element (output-element {:output-element/type :output-element.type/stdout | ||
:output-element/content content}))) | ||
|
||
(defn repl-output [] | ||
(let [elements @output-elements] | ||
[:div | ||
(for [{:output-element/keys [content id]} elements] | ||
[:p {:key id} content])])) | ||
|
||
(defn main [] | ||
(.. js/window | ||
(addEventListener "message" | ||
(fn [^js message] | ||
(js/console.log "message" message) | ||
;; TODO: Convert message data to CLJ before accessing its properties | ||
(let [id (.. message -data -id) | ||
command (aget message "data" "command-name") | ||
data (.. message -data -result)] | ||
(js/console.log "id" id) | ||
(js/console.log "command" command) | ||
(case command | ||
"show-result" (add-eval-result data) | ||
"show-stdout" (add-stdout data)))))) | ||
(rdom.client/render (rdom.client/create-root (js/document.getElementById "output")) [repl-output])) | ||
|
||
(comment | ||
(set! *print-namespace-maps* false) | ||
|
||
(binding [*print-namespace-maps* false] | ||
@output-elements) | ||
|
||
(println {:user/a 1, :user/b 2}) | ||
|
||
(reset! output-elements []) | ||
|
||
(print) | ||
|
||
@output-elements | ||
|
||
;; For some reason this is not causing the UI to update... | ||
;; Reason: It was because I was connected to the calva-lib (node) build and not the repl-output-ui (browser) build | ||
(swap! output-elements conj {:output-element/type :output-element.type/eval-result | ||
:output-element/content "{:a 1}"}) | ||
|
||
:rcf) |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if we need React for something in particular. If we do, then of course we should use it. Otherwise, it may be better to use something with less bloat, like Dumdom, or maybe Replicant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's for reagent, which I consider very lightweight and should be used lightly for this. If you think there's any performance issue or other issues with it that would be significant for our use-case, we should discuss that, but switching it out later should be pretty simple.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm open to alternatives if there's a good reason to use something else.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dumdom looks interesting. Using a library that supports react gives us reach into the react ecosystem though, which could be nice in the future for more rich features and interactivity (making it less likely we'll need to recreate the wheel).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My experience with React is that it changes all the time in breaking ways, making it high maintenance. With Dumdom we would avoid this, as the vdom library it uses is super tiny and stable. With Replicant we would get a pure ClojureScript vdom as well, and have even more of the familiar Clojure stability (though I am not sure if the Replicant API is marked as stable yet).
However, I am not opposed to React if we have a good reason to use it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I say we move forward with it for a first-release, and maybe be conscious of our usage of it (keeping it light), and if we come across issues we can switch it out later.
I'll continue to keep other options in mind and think about the React usage for now...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I may reconsider this. I need to look at Dumdom more closely.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having had a closer look at Replicant, In think it's better to use that instead of Dumdom. They are quite similar, but Replicant drops the dependency on Snabbdom, as well as some legacy API requirements that Dumdom carries.