-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathdev.exs
183 lines (156 loc) · 4.82 KB
/
dev.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
Mix.install([{:phoenix_playground, "~> 0.1"}, {:live_monaco_editor, path: "."}])
defmodule DemoLive do
use Phoenix.LiveView
def mount(_params, _session, socket) do
{:ok,
assign(socket, :value, ~S"""
# My Code Editor
From LiveMonacoEditor.code_editor component
""")}
end
def render(assigns) do
~H"""
<h1>Default options</h1>
<LiveMonacoEditor.code_editor value={@value} />
<h1>Change language and value</h1>
<button phx-click="html">HTML</button>
<button phx-click="markdown">Markdown</button>
<LiveMonacoEditor.code_editor id="lang" path="file_b" value="# file_b" />
<h1>Inside form</h1>
<form>
<LiveMonacoEditor.code_editor id="form" path="file_c" value="# file_c" change="set_editor_value" />
</form>
<h1>Elixir</h1>
<LiveMonacoEditor.code_editor
id="elixir"
path="elixir"
style="min-height: 250px; width: 100%;"
value={~S{
defmodule Math do
def sum_list([head | tail], accumulator) do
sum_list(tail, head + accumulator)
end
def sum_list([], accumulator) do
accumulator
end
end
IO.puts Math.sum_list([1, 2, 3], 0)
}}
opts={
Map.merge(
LiveMonacoEditor.default_opts(),
%{"language" => "elixir"}
)
}
/>
<h1>HTML</h1>
<LiveMonacoEditor.code_editor
id="html"
path="html"
style="min-height: 250px; width: 100%;"
value={~S|
<div class="space-y-5">
<div class="p-3 bg-white shadow rounded-lg">
<h3 class="text-xs border-b">font-sans</h3>
<p class="font-sans">
The quick brown fox jumps over the lazy dog.
</p>
</div>
</div>
|}
opts={
Map.merge(
LiveMonacoEditor.default_opts(),
%{"language" => "html"}
)
}
/>
"""
end
def handle_event("markdown", _params, socket) do
{:noreply,
socket
|> LiveMonacoEditor.change_language("markdown", to: "file_b")
|> LiveMonacoEditor.set_value(
~S"""
# Title
new content
""",
to: "file_b"
)}
end
def handle_event("html", _params, socket) do
{:noreply,
socket
|> LiveMonacoEditor.change_language("html", to: "file_b")
|> LiveMonacoEditor.set_value("<h1>new value</h1>", to: "file_b")}
end
def handle_event("set_editor_value", %{"value" => value}, socket) do
IO.puts(value)
{:noreply, socket}
end
end
defmodule DemoLayout do
use Phoenix.Component
@doc false
def render(template, assigns)
def render("root.html", assigns) do
~H"""
<!DOCTYPE html>
<html lang="en" class="h-full">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="csrf-token" content={Plug.CSRFProtection.get_csrf_token()} />
</head>
<body>
<script src="/assets/phoenix/phoenix.js">
</script>
<script src="/assets/phoenix_live_view/phoenix_live_view.js">
</script>
<link rel="stylesheet" href="/assets/live_monaco_editor/live_monaco_editor.css" />
<script src="/assets/live_monaco_editor/live_monaco_editor.js">
</script>
<script>
let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content")
let liveSocket =
new window.LiveView.LiveSocket(
"/live",
window.Phoenix.Socket,
{ hooks: { CodeEditorHook: window.LiveMonacoEditor.CodeEditorHook }, params: {_csrf_token: csrfToken} })
liveSocket.connect()
window.addEventListener("phx:live_reload:attached", ({detail: reloader}) => {
reloader.enableServerLogs()
window.liveReloader = reloader
})
</script>
{@inner_content}
</body>
</html>
"""
end
end
defmodule Demo.Router do
use Phoenix.Router
import Phoenix.LiveView.Router
pipeline :browser do
plug :put_root_layout, html: {DemoLayout, :root}
end
scope "/" do
pipe_through :browser
live "/", DemoLive
end
end
defmodule Demo.Endpoint do
use Phoenix.Endpoint, otp_app: :phoenix_playground
plug Plug.Logger
socket "/live", Phoenix.LiveView.Socket
plug Plug.Static, from: {:phoenix, "priv/static"}, at: "/assets/phoenix"
plug Plug.Static, from: {:phoenix_live_view, "priv/static"}, at: "/assets/phoenix_live_view"
plug Plug.Static, from: {:live_monaco_editor, "priv/static"}, at: "/assets/live_monaco_editor"
socket "/phoenix/live_reload/socket", Phoenix.LiveReloader.Socket
plug Phoenix.LiveReloader
plug Phoenix.CodeReloader, reloader: &PhoenixPlayground.CodeReloader.reload/2
plug Demo.Router
end
PhoenixPlayground.start(endpoint: Demo.Endpoint, live: DemoLive, open_browser: true)