11import _ from "lodash" ;
22import { type ReactNode , useEffect , useState } from "react" ;
3- import { Button , Typography } from "antd" ;
3+ import { Button , Flex , Form , Typography } from "antd" ;
44import { variablesUsedByElement } from "../../utils/variable-parsing/findVariablesUsedByElement" ;
55import { transformVariableToReact } from "../../utils/variable-parsing/transformVariableToReact" ;
66import "./TokenDetails.scss" ;
7+ import { changeVariableValue } from "../../utils/variable-updates/changeVariableValue" ;
8+ import type { Color } from "antd/es/color-picker" ;
79
810const { Text } = Typography ;
911
1012export const TokenDetails = ( ) => {
1113 const [ target , setTarget ] = useState < HTMLElement | null > ( null ) ;
1214 const [ frozen , setFrozen ] = useState ( false ) ; // State to manage if the target is frozen
1315 const [ editable , setEditable ] = useState ( false ) ; // State to manage if the variable values should be editable
16+ const [ tokenEditingForm ] = Form . useForm ( ) ;
1417
15- const saveVariableValues = ( ) => {
18+ const onValuesChange = ( values : Record < string , string | Color > ) => {
19+ for ( const [ key , value ] of Object . entries ( values ) ) {
20+ if ( typeof value === "object" ) {
21+ if ( "toCssString" in value ) {
22+ // If the value is a ColorPicker, convert it to a CSS string
23+ changeVariableValue ( key , value . toCssString ( ) ) ;
24+ continue ;
25+ }
26+ }
27+ changeVariableValue ( key , value ) ;
28+ }
29+ } ;
30+
31+ const onFinish = ( ) => {
1632 setEditable ( false ) ;
33+ setFrozen ( false ) ;
1734 } ;
1835
1936 const targetTokenDetails = ( target : HTMLElement | null ) : ReactNode => {
20- if ( target ) {
21- try {
22- const elementName = target . tagName ;
23- const elementVariables = variablesUsedByElement ( target ) ;
24- return (
25- < >
26- < Text strong > { _ . lowerCase ( elementName ) } </ Text >
27- < br />
28- < br />
37+ if ( ! target ) {
38+ return < h4 > No target selected </ h4 > ;
39+ }
40+
41+ try {
42+ const elementName = target . tagName ;
43+ const elementVariables = variablesUsedByElement ( target ) ;
44+ return (
45+ < >
46+ < Text strong > { _ . lowerCase ( elementName ) } </ Text >
47+ < br />
48+ < br />
49+ < Form
50+ form = { tokenEditingForm }
51+ name = "variableValues"
52+ onValuesChange = { onValuesChange }
53+ onFinish = { onFinish }
54+ >
2955 { elementVariables . map ( ( variable ) =>
30- transformVariableToReact ( variable , editable )
56+ transformVariableToReact (
57+ variable ,
58+ editable ,
59+ ( name : string , value : string ) => {
60+ changeVariableValue ( name , value ) ;
61+ }
62+ )
3163 ) }
3264 < br />
33- < Button
34- onClick = { ( ) =>
35- editable ? saveVariableValues ( ) : setEditable ( true )
36- }
37- type = "primary"
38- size = "small"
39- >
40- { editable ? "Save" : "Edit" }
41- </ Button >
42- < br />
43- < br />
44- < Button
45- onClick = { ( ) => setFrozen ( false ) }
46- color = "cyan"
47- variant = "solid"
48- size = "small"
49- >
50- Unfreeze
51- </ Button >
52- </ >
53- ) ;
54- } catch ( e ) {
55- console . log ( e ) ;
56- }
57- } else {
58- return < h4 > No target selected </ h4 > ;
65+ < Flex gap = { 8 } >
66+ { editable ? (
67+ < Form . Item noStyle >
68+ < Button type = "primary" size = "small" htmlType = "submit" >
69+ Save
70+ </ Button >
71+ </ Form . Item >
72+ ) : null }
73+ { editable ? null : (
74+ < Button
75+ onClick = { ( ) => setEditable ( true ) }
76+ type = "primary"
77+ size = "small"
78+ >
79+ Edit
80+ </ Button >
81+ ) }
82+ { frozen ? (
83+ < >
84+ < br />
85+ < Button
86+ id = "unfreeze-button"
87+ onClick = { ( ) => {
88+ setFrozen ( false ) ;
89+ } }
90+ color = "cyan"
91+ variant = "solid"
92+ size = "small"
93+ >
94+ Unfreeze
95+ </ Button >
96+ </ >
97+ ) : null }
98+ </ Flex >
99+ </ Form >
100+ </ >
101+ ) ;
102+ } catch ( e ) {
103+ console . log ( e ) ;
59104 }
60105 } ;
61106
107+ useEffect ( ( ) => {
108+ if ( target ) {
109+ if ( frozen ) {
110+ target . classList . add ( "token-details__selected-target--frozen" ) ;
111+ } else {
112+ target . classList . remove ( "token-details__selected-target--frozen" ) ;
113+ }
114+ }
115+ } , [ target , frozen ] ) ;
116+
62117 useEffect ( ( ) => {
63118 const interceptClick = ( event : MouseEvent ) => {
64119 if ( ! ( event . target instanceof HTMLElement ) ) {
65120 return ;
66121 }
67122
68- if ( event . target . matches ( ".tokenInspector *" ) ) {
69- console . log ( "Click on token inspector, NOT freezing target" ) ;
123+ if (
124+ event . target . matches ( ".tokenInspector *" ) ||
125+ event . target . matches ( "#unfreeze-button *" )
126+ ) {
70127 // If the click is not on the token inspector, do not freeze
71128 return ;
72129 }
73130
74131 event . preventDefault ( ) ;
75132 event . stopPropagation ( ) ;
76133
134+ if ( frozen ) {
135+ if ( event . target . matches ( ".token-details__selected-target" ) ) {
136+ // If the click is on the selected target, unfreeze it
137+ setFrozen ( false ) ;
138+ return ;
139+ }
140+ // If the target is currently frozen, do not change anything
141+ return ;
142+ }
143+
77144 setFrozen ( true ) ;
78145 } ;
79146
@@ -99,7 +166,6 @@ export const TokenDetails = () => {
99166 el . classList . remove ( "token-details__selected-target" ) ;
100167 }
101168 } ) ;
102- console . log ( "Changing target!" ) ;
103169 setTarget ( newTarget ) ;
104170 }
105171 } ;
0 commit comments