Skip to content

Commit 0718853

Browse files
committed
Improve URL
1 parent d454310 commit 0718853

File tree

4 files changed

+169
-11
lines changed

4 files changed

+169
-11
lines changed

apps/components_guide_web/assets/js/app.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ let csrfToken = document
2020
let liveSocket = new LiveSocket("/live", Socket, {
2121
params: { _csrf_token: csrfToken },
2222
hooks: {
23+
PreCode: {
24+
mounted() {
25+
this._highlight(); //
26+
},
27+
updated() {
28+
this._highlight();
29+
},
30+
_highlight() {
31+
window.Prism.highlightElement(this.el);
32+
},
33+
},
2334
SwatchInput: {
2435
mounted() {
2536
const mouseEventHandler = (e) => {

apps/components_guide_web/lib/components_guide_web/template_engines/markdown_engine.ex

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@ defmodule ComponentsGuideWeb.TemplateEngines.MarkdownEngine do
55

66
require Earmark
77

8-
def compile(path, _name) do
9-
IO.puts("compile #{path}")
8+
def compile(path, name) do
9+
IO.puts("compile #{path} #{name}")
1010
html = path
1111
|> File.read!()
1212
|> Earmark.as_html!(%Earmark.Options{code_class_prefix: "language-", smartypants: false})
1313

1414
# regex = ~r{<live-([\w-]+)>(.+)</live-([\w-]+)>}
1515
regex = ~r{<live-([\w-]+)>([^<]+)</live-([^>]+)>}
1616
# regex = ~r{<live-([\w-]+)>}
17-
17+
18+
# live? = Regex.match(regex, html)
19+
1820
html = Regex.replace(regex, html, fn whole, name, content ->
1921
case name do
2022
"render" ->
@@ -24,12 +26,20 @@ defmodule ComponentsGuideWeb.TemplateEngines.MarkdownEngine do
2426
"!" <> name <> "!" <> content
2527
end
2628
end)
27-
29+
2830
# html = Regex.replace(regex, html, fn whole, name, content -> "!" <> name <> "!" <> content end)
29-
31+
3032
# html = Regex.replace(regex, html, fn whole, name, content -> "<div><%= live_render(@conn, ComponentsGuideWeb.FakeSearchLive, session: %{}) %></div>" end)
3133

3234
html |> EEx.compile_string(engine: Phoenix.HTML.Engine, file: path, line: 1)
35+
36+
# case live? do
37+
# true ->
38+
# html |> EEx.compile_string(engine: Phoenix.HTML.Engine, file: path, line: 1)
39+
40+
# false ->
41+
42+
# end
3343
# html |> EEx.compile_string(engine: Phoenix.LiveView.Engine, file: path, line: 1)
3444
end
3545
end

apps/components_guide_web/lib/components_guide_web/templates/web_standards/url.html.md

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,39 @@
22

33
## Anatomy of a URL
44

5+
<live-render>
6+
ComponentsGuideWeb.WebStandards.Live.URL
7+
</live-render>
8+
9+
## Relative URLs
10+
11+
## JavaScript’s `URL`
12+
13+
```js
14+
const url = new URL('https://example.org/songs?first=20&sortBy=releaseDate');
15+
url.protocol; // 'https:'
16+
url.hostname; // 'example.org'
17+
url.origin; // 'https://example.org'
18+
url.pathname; // '/songs'
19+
```
20+
21+
```js
22+
const root = new URL('/');
523
```
6-
https://example.org/songs?first=20&sortBy=releaseDate
24+
25+
```js
26+
// In a browser
27+
const current = new URL(window.location.href);
728
```
829

9-
### Scheme
10-
### Host
11-
### Path
12-
### Query
30+
```js
31+
// With a fetch Request
32+
const current = new URL(request.url);
33+
```
1334

14-
## Relative URLs
35+
## Swift’s `URL`
36+
37+
```swift
38+
let root = URL(string: "/")
39+
40+
```
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
defmodule ComponentsGuideWeb.WebStandards.Live.URL do
2+
use ComponentsGuideWeb, :live_view
3+
alias ComponentsGuideWeb.StylingHelpers
4+
5+
defmodule State do
6+
defstruct raw_url: "https://example.org/songs?first=20&sortBy=releaseDate"
7+
8+
def to_url(%State{raw_url: raw_url}) do
9+
URI.parse(raw_url)
10+
end
11+
12+
def get_field(%State{} = state, field) when field in [:host] do
13+
url = state |> to_url()
14+
url[field]
15+
end
16+
17+
def change(%State{} = state, :scheme, new_scheme) when is_binary(new_scheme) do
18+
url = to_url(state)
19+
url = put_in(url.scheme, new_scheme)
20+
put_in(state.raw_url, url |> URI.to_string())
21+
end
22+
23+
def change(%State{} = state, :host, new_host) when is_binary(new_host) do
24+
url = to_url(state)
25+
url = put_in(url.host, new_host)
26+
IO.inspect(url)
27+
put_in(state.raw_url, url |> URI.to_string())
28+
end
29+
30+
def change(%State{} = state, :path, new_path) when is_binary(new_path) do
31+
url = to_url(state)
32+
url = put_in(url.path, new_path)
33+
IO.inspect(url)
34+
put_in(state.raw_url, url |> URI.to_string())
35+
end
36+
end
37+
38+
def mount(_params, _session, socket) do
39+
{:ok, assign(socket, state: %State{})}
40+
end
41+
42+
def render(assigns) do
43+
~L"""
44+
<form phx-change=change>
45+
46+
<pre class="p-4 my-2" style="color: #d6deeb;">
47+
<code><%= @state |> State.to_url() |> URI.to_string() %></code>
48+
</pre>
49+
50+
<pre class="p-4 my-2" style="color: #d6deeb;">
51+
<code><span class="text-green-400"><%= State.to_url(@state).scheme %></span>://<span class="text-yellow-400"><%= State.to_url(@state).host %></span><span class="text-orange-400"><%= State.to_url(@state).path %></span><span class="text-indigo-400"/songs</span>?first=20&sortBy=releaseDate</code>
52+
</pre>
53+
54+
<div class="flex flex-col space-y-4">
55+
56+
<label>
57+
<span class="font-bold text-green-400 mr-2">Scheme</span>
58+
<input name=scheme type=text value="https" class="text-black text-green-900 bg-green-100 px-2">
59+
</label>
60+
61+
<label>
62+
<span class="font-bold text-yellow-400 mr-2">Host</span>
63+
<input name=host type=text value="example.org" class="text-black text-yellow-900 bg-yellow-100 px-2">
64+
</label>
65+
66+
<label>
67+
<span class="font-bold text-orange-400 mr-2">Path</span>
68+
<input name=path type=text value="/songs" class="text-black text-orange-900 bg-orange-100 px-2">
69+
</label>
70+
71+
<label>
72+
<span class="font-bold text-orange-400 mr-2">Query</span>
73+
<button type=button class="px-2 bg-white text-black rounded">Add query</button>
74+
<div>
75+
<input name=queryKey type=text value="" class="text-black text-orange-900 bg-orange-100 px-2">
76+
<input name=queryValue type=text value="/songs" class="text-black text-orange-900 bg-orange-100 px-2">
77+
</div>
78+
</label>
79+
80+
</div>
81+
82+
</form>
83+
84+
<pre class="language-js" phx-hook=PreCode><code>const url = new URL(
85+
'<%= @state |> State.to_url() |> URI.to_string() %>'
86+
);
87+
url.protocol; // <%= State.to_url(@state).scheme %>:
88+
url.host; // <%= State.to_url(@state).host %>:
89+
url.path; // <%= State.to_url(@state).path %>:
90+
url.search; // <%= State.to_url(@state).query %>:
91+
</code></pre>
92+
"""
93+
end
94+
95+
def handle_event(
96+
"change",
97+
%{"scheme" => new_scheme, "host" => new_host, "path" => new_path},
98+
socket
99+
) do
100+
state =
101+
socket.assigns.state
102+
|> State.change(:scheme, new_scheme)
103+
|> State.change(:host, new_host)
104+
|> State.change(:path, new_path)
105+
106+
{
107+
:noreply,
108+
socket |> assign(:state, state)
109+
}
110+
end
111+
end

0 commit comments

Comments
 (0)