-
Notifications
You must be signed in to change notification settings - Fork 16
/
dev.exs
237 lines (207 loc) · 6.25 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# Development Server for LiveMonacoEditor
#
# Usage:
#
# $ iex -S mix dev
#
# Refs:
#
# https://github.com/phoenixframework/phoenix_live_dashboard/blob/e87bbe03203f67947643f0574bb272b681951fa8/dev.exs
# https://github.com/mcrumm/phoenix_profiler/blob/b882314add2d8783aac76b87c8ded3c123fc71a4/dev.exs
# https://github.com/chrismccord/single_file_phoenix_fly/blob/bd3b372a5ca94cdd77d22b4fa1818cc4b612bcf5/run.exs
# https://github.com/wojtekmach/mix_install_examples/blob/2c30c129f36206d3dfa234421ec5869e5e2e82be/phoenix_live_view.exs
# https://github.com/wojtekmach/mix_install_examples/blob/2c30c129f36206d3dfa234421ec5869e5e2e82be/ecto_sql.exs
require Logger
Logger.configure(level: :debug)
Application.put_env(:phoenix, :json_library, Jason)
Application.put_env(:sample, Sample.Endpoint,
http: [ip: {127, 0, 0, 1}, port: 4002],
server: true,
live_view: [signing_salt: "aaaaaaaa"],
secret_key_base: String.duplicate("a", 64),
debug_errors: true,
check_origin: false,
pubsub_server: Sample.PubSub,
live_reload: [
url: "ws://localhost:4002",
patterns: [
~r"priv/static/.*(js|css|png|jpeg|jpg|gif|svg)$",
~r"lib/.*(ex)$"
]
],
watchers: [
esbuild: {Esbuild, :install_and_run, [:module, ~w(--watch)]}
]
)
defmodule Sample.EditorLive do
use Phoenix.LiveView, layout: {__MODULE__, :live}
def render("live.html", assigns) do
~H"""
<!DOCTYPE html>
<html>
<head>
<meta name="csrf-token" content={Plug.CSRFProtection.get_csrf_token()} />
<link rel="stylesheet" href="/live_monaco_editor/live_monaco_editor.css" />
<script src="https://cdn.jsdelivr.net/npm/[email protected]/priv/static/phoenix.min.js">
</script>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/priv/static/phoenix_live_view.min.js"
>
</script>
<script src="/live_monaco_editor/live_monaco_editor.js">
</script>
<script>
window.addEventListener("lme:editor_mounted", (ev) => {
const hook = ev.detail.hook
const editor = ev.detail.editor.standalone_code_editor
editor.onDidBlurEditorWidget(() => {
console.log(editor.getValue())
})
})
let Hooks = {CodeEditorHook: window.LiveMonacoEditor.CodeEditorHook}
let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content");
let liveSocket = new window.LiveView.LiveSocket("/live", window.Phoenix.Socket, { hooks: Hooks, params: {_csrf_token: csrfToken} })
liveSocket.connect()
liveSocket.enableDebug()
window.liveSocket = liveSocket
</script>
</head>
<body>
<%= @inner_content %>
</body>
</html>
"""
end
def render(assigns) do
assigns =
assign(assigns, :value, ~S"""
# My Code Editor
From LiveMonacoEditor.code_editor component
""")
~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 Sample.Router do
use Phoenix.Router
import Phoenix.LiveView.Router
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_live_flash
plug :protect_from_forgery
plug :put_secure_browser_headers
end
scope "/", Sample do
pipe_through :browser
live "/", EditorLive, :index
end
end
defmodule Sample.Endpoint do
use Phoenix.Endpoint, otp_app: :sample
@session_options [
store: :cookie,
key: "_sample_live_monaco_editor_dev_key",
signing_salt: "pMQYsz0UKEnwxJnQrVwovkBAKvU3MiuL"
]
socket "/live", Phoenix.LiveView.Socket, websocket: [connect_info: [session: @session_options]]
socket "/phoenix/live_reload/socket", Phoenix.LiveReloader.Socket
plug Plug.Static,
at: "/live_monaco_editor",
from: {:live_monaco_editor, "priv/static"}
plug Phoenix.LiveReloader
plug Phoenix.CodeReloader
plug Plug.RequestId
plug Plug.Session, @session_options
plug Sample.Router
end
Task.start(fn ->
children = [
{Phoenix.PubSub, [name: Sample.PubSub]},
Sample.Endpoint
]
{:ok, _} = Supervisor.start_link(children, strategy: :one_for_one)
Process.sleep(:infinity)
end)