Skip to content
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
wants to merge 39 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
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 Aug 20, 2023
c9b5334
Merge branch 'dev' into node-library_target_inject_vscode
bpringe Sep 3, 2023
514f9df
Add dev code
bpringe Sep 3, 2023
5871dfa
Add cljs code to get project root uri
bpringe Sep 3, 2023
6ec215d
Add function for creating repl output file (markdown)
bpringe Sep 3, 2023
3af8dfd
Add more markdown experimentation code
bpringe Nov 19, 2023
81303d5
Merge remote-tracking branch 'origin/dev' into add_new_output_experience
bpringe Nov 19, 2023
84f45e5
Merge remote-tracking branch 'origin/dev' into add_new_output_experience
bpringe Nov 20, 2023
26c6dad
Create webview-output ns - WIP
bpringe Nov 21, 2023
a193bcc
Use html file for webview html
bpringe Nov 21, 2023
756d366
Add highlight.js
bpringe Nov 21, 2023
56087e9
Begin setup for using reagent in output webview - WIP
bpringe Nov 22, 2023
94d73dc
Add reagent UI for repl output - WIP
bpringe Nov 24, 2023
f97c478
Make reagent UI work
bpringe Nov 25, 2023
0ad4652
Make posting results as messages work
bpringe Nov 25, 2023
06953ce
Merge remote-tracking branch 'origin/dev' into add_new_output_experience
bpringe Dec 4, 2023
45a629a
Add build to watch-cljs script
bpringe Dec 4, 2023
695afde
Open webview output on activation and print 10 messages
bpringe Dec 4, 2023
65f4229
Fix comment typo
bpringe Feb 26, 2024
2f85dff
Merge remote-tracking branch 'origin/dev' into add_new_output_experience
bpringe Feb 26, 2024
e005870
Update package-lock.json
bpringe Feb 26, 2024
d50b123
Remove comment form
bpringe Feb 26, 2024
5386de6
Merge remote-tracking branch 'origin/dev' into add_new_output_experience
bpringe Mar 31, 2024
11dfb42
Remove markdown output file
bpringe Mar 31, 2024
202329f
Fix project-root-uri-key
bpringe Mar 31, 2024
abe1a17
Get rid of react warning about every element needing a key
bpringe Mar 31, 2024
53a3718
Use create-root so that react 18 is used
bpringe Mar 31, 2024
18803b4
Merge remote-tracking branch 'origin/dev' into add_new_output_experience
bpringe Apr 6, 2024
f71343f
WIP: Update webview CSP
bpringe Apr 6, 2024
3d00c9a
Move hightlightAll call to cljs
bpringe Apr 6, 2024
4eb3975
Add comment
bpringe Apr 6, 2024
23022f6
Merge remote-tracking branch 'origin/dev' into add_new_output_experience
bpringe Apr 20, 2024
2c60af5
Add replicant
bpringe Apr 20, 2024
5be9a7d
Merge remote-tracking branch 'origin/dev' into add_new_output_experience
bpringe May 5, 2024
8f9eca9
Add replicant and set up a simple multimethod command system
bpringe May 5, 2024
77e87f8
Fix typo
bpringe May 5, 2024
af177c1
Update deps-clj-version and deps.clj.jar
bpringe May 19, 2024
45ea2d3
Remove react and reagent deps, move snitch dep to dev alias, refactor…
bpringe May 19, 2024
32c27e7
Add multimethod for getting hiccup for output element
bpringe May 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
/.clj-kondo
/site
/test-data
/repl-output-ui
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ jspm_packages
lib/
cljs-out/
test-out/
repl-output-ui/

# This and that
.nrepl-port
Expand Down
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
**/.shadow-cljs/
**/out/
clojure.tmLanguage.json
/repl-output-ui
3 changes: 2 additions & 1 deletion deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
org.clojars.liverm0r/dartclojure {:mvn/version "0.2.22-SNAPSHOT"}
vvvvalvalval/supdate {:mvn/version "0.2.3"}
camel-snake-kebab/camel-snake-kebab {:mvn/version "0.4.3"}
reagent/reagent {:mvn/version "1.2.0"}
#_#_org.clojars.liverm0r/dartclojure {:local/root "../DartClojure"}}
:paths ["src/cljs-lib/src"
"src/cljs-lib/test"]}
"src/cljs-lib/test"]}
84 changes: 80 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3212,7 +3212,7 @@
"compile-ts": "npx tsc --project ./tsconfig.json",
"compile": "npm run compile-cljs && npm run compile-ts",
"watch-ts": "npx tsc --watch --project ./tsconfig.json",
"watch-cljs": "npx shadow-cljs -d cider/cider-nrepl:0.28.5 watch :calva-lib :test",
"watch-cljs": "npx shadow-cljs -d cider/cider-nrepl:0.28.5 watch :calva-lib :test :repl-output-ui",
"watch-ts-with-strict-nulls": "npx tsc --watch --project ./tsconfig.json --strictNullChecks",
"release-cljs": "npx shadow-cljs release :calva-lib :test",
"release": "webpack --mode production",
Expand Down Expand Up @@ -3254,6 +3254,7 @@
"open": "^6.3.0",
"parinfer": "^3.12.0",
"posthtml-parser": "^0.11.0",
"react-dom": "^18.2.0",
Copy link
Collaborator

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.

Copy link
Member Author

@bpringe bpringe Apr 5, 2024

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.

Copy link
Member Author

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.

Copy link
Member Author

@bpringe bpringe Apr 5, 2024

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).

Copy link
Collaborator

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.

Copy link
Member Author

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...

Copy link
Member Author

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

I may reconsider this. I need to look at Dumdom more closely.

Copy link
Collaborator

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.

"semver": "^7.3.7",
"tree-kill": "^1.2.2",
"uuidv4": "6.2.12",
Expand Down
14 changes: 12 additions & 2 deletions shadow-cljs.edn
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,20 @@
:js2cljs calva.js2cljs.converter/convert-bridge
:dart2clj calva.dartclojure/convert-bridge
:readConfigEdn calva.read-config/config-edn->js-bridge
:html2hiccup calva.html2hiccup/html->hiccup-convert-bridge}
:html2hiccup calva.html2hiccup/html->hiccup-convert-bridge
:initializeCljs calva.util/initialize-cljs
:showReplOutputWebviewPanel calva.repl.webview.core/show-repl-output-webview-panel}
:output-to "out/cljs-lib/cljs-lib.js"}
:test
{:target :node-test
:output-to "out/cljs-lib/test/cljs-lib-tests.js"
:ns-regexp "-test$"
:autorun true}}}
:autorun true}
:repl-output-ui
{:target :browser
;; TODO: Do the asset-path and output-dir values make sense?
:asset-path "js"
:output-dir "repl-output-ui/js"
:modules {:main {:init-fn calva.repl.webview.ui/main}}
:devtools {:loader-mode :eval
:devtools-url "http://localhost:9630"}}}}
97 changes: 97 additions & 0 deletions src/cljs-lib/src/calva/repl/webview/core.cljs
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)
72 changes: 72 additions & 0 deletions src/cljs-lib/src/calva/repl/webview/ui.cljs
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)
2 changes: 1 addition & 1 deletion src/cljs-lib/src/calva/state.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
(comment
(set-state-value! "hello" "world")
(get-state)
(remove-state-value! "hello"))
(remove-state-value! "hello"))
Loading