@@ -5,7 +5,7 @@ defmodule ComponentsGuideWeb.ColorLive do
5
5
defmodule State do
6
6
defstruct color: { :lab , 50 , 100 , - 128 }
7
7
8
- def from_input (
8
+ def decode (
9
9
"#" <>
10
10
<< r1 :: utf8 >> <>
11
11
<< r2 :: utf8 >> <> << g1 :: utf8 >> <> << g2 :: utf8 >> <> << b1 :: utf8 >> <> << b2 :: utf8 >>
@@ -25,6 +25,19 @@ defmodule ComponentsGuideWeb.ColorLive do
25
25
% State { color: color }
26
26
end
27
27
28
+ def decode ( :lab , input ) when is_binary ( input ) do
29
+ { l , << "," :: utf8 >> <> rest } = Integer . parse ( input )
30
+ { a , << "," :: utf8 >> <> rest } = Integer . parse ( rest )
31
+ { b , "" } = Integer . parse ( rest )
32
+
33
+ % __MODULE__ { color: { :lab , l , a , b } }
34
+ end
35
+
36
+ def encode ( % __MODULE__ { color: color } ) do
37
+ { :lab , l , a , b } = color
38
+ "#{ l } ,#{ a } ,#{ b } "
39
+ end
40
+
28
41
def set_color ( state = % __MODULE__ { } , color ) , do: % { state | color: color }
29
42
30
43
def l ( % __MODULE__ { color: { :lab , l , _ , _ } } ) , do: l
@@ -103,7 +116,7 @@ defmodule ComponentsGuideWeb.ColorLive do
103
116
end
104
117
105
118
def handle_params ( % { "definition" => definition } , _path , socket ) do
106
- state = State . from_input ( definition )
119
+ state = State . decode ( :lab , definition )
107
120
{ :noreply , socket |> assign ( :state , state ) }
108
121
end
109
122
@@ -118,11 +131,12 @@ defmodule ComponentsGuideWeb.ColorLive do
118
131
b = b |> String . to_integer ( )
119
132
120
133
state = socket . assigns . state |> State . set_color ( { :lab , l , a , b } )
121
- hex = state |> State . hex ( )
134
+ encoded = State . encode ( state )
122
135
136
+ # TODO: throttle for Safari’s
137
+ # SecurityError: Attempt to use history.replaceState() more than 100 times per 30 seconds
123
138
{ :noreply ,
124
139
socket
125
- |> assign ( :state , state )
126
- |> push_patch ( to: Routes . color_path ( socket , :show , hex ) , replace: true ) }
140
+ |> push_patch ( to: Routes . color_path ( socket , :lab , encoded ) , replace: true ) }
127
141
end
128
142
end
0 commit comments