1212# 配置参数
1313FIGMA_TOKEN = os .getenv ("FIGMA_TOKEN" ) # 从环境变量读取Token,避免硬编码
1414FILE_ID = "ZuZWPWcPLSqaX8d1p9uTpX" # 替换为你的Figma文件ID
15- NODE_IDS = "3-1377" # 替换为你的按钮组件节点ID(多个用逗号分隔)
15+ NODE_IDS = "58-27" # 替换为你的按钮组件节点ID(多个用逗号分隔)
16+ # https://www.figma.com/design/ZuZWPWcPLSqaX8d1p9uTpX/DesignSystem?node-id=48-12
17+ # https://www.figma.com/design/ZuZWPWcPLSqaX8d1p9uTpX/DesignSystem?node-id=58-27
1618
1719# 自定义属性名映射表(可扩展)
1820PROPERTY_ALIAS_MAP = {
@@ -141,6 +143,23 @@ def match_token(value: Any, token_type: str, tokens: dict) -> str:
141143 # print(f"Token匹配错误: {e}")
142144 # return f"error: {str(value)}" # 保留原始值信息
143145
146+ def extract_component_details (node_data :Dict )-> Dict :
147+ """
148+ 从节点名称提取组件详情(组件名、类型、状态)
149+ """
150+ node_name = node_data .get ("name" , "" )
151+ name_parts = node_name .split ("/" )
152+
153+ component_details = {}
154+ if len (name_parts ) >= 1 :
155+ component_details ["component" ] = name_parts [0 ]
156+ if len (name_parts ) >= 2 :
157+ component_details ["type" ] = name_parts [1 ]
158+ if len (name_parts ) >= 3 :
159+ component_details ["state" ] = name_parts [2 ]
160+
161+ return component_details
162+
144163# ================== 主解析逻辑 ==================
145164def analyze_node (node_id : str , node_data : Dict , tokens : Dict ) -> Tuple [Dict , List ]:
146165 """
@@ -150,14 +169,18 @@ def analyze_node(node_id: str, node_data: Dict, tokens: Dict) -> Tuple[Dict, Lis
150169 mappings = {}
151170 unmapped = []
152171
153- bound_vars = node_data .get ("document" , {}).get ("boundVariables" , {})
172+ # Extract component details
173+ component_details = extract_component_details (node_data )
174+ mappings ["component_details" ] = component_details
175+
176+ bound_vars = node_data .get ("boundVariables" , {})
154177
155178 for raw_prop , var_info in bound_vars .items ():
156179 # 1. 解析属性名
157180 prop = resolve_property_name (raw_prop )
158181
159182 # 2. 提取实际值
160- success , value = extract_actual_value (node_data [ "document" ] , prop )
183+ success , value = extract_actual_value (node_data , prop )
161184 if not success :
162185 unmapped .append ({
163186 "node_id" : node_id ,
@@ -186,31 +209,51 @@ def analyze_node(node_id: str, node_data: Dict, tokens: Dict) -> Tuple[Dict, Lis
186209
187210 return mappings , unmapped
188211
212+ def process_node (nodes :dict , tokens : dict ) -> Tuple [dict , dict ]:
213+ """
214+ 处理节点数据
215+ """
216+ # 初始化报告
217+ all_mappings = {}
218+ all_unmapped = []
219+ for node_id , node_data in nodes .items ():
220+ document = node_data .get ("document" , {})
221+ children = document .get ("children" , [])
222+
223+ for child in children :
224+ if child .get ("type" ) == "COMPONENT" :
225+ node_id = child .get ("id" )
226+ # node_data = nodes.get(node_id, {})
227+ mappings , unmapped = analyze_node (node_id , child , tokens )
228+ all_mappings [node_id ] = mappings
229+ all_unmapped .extend (unmapped )
230+
231+ return all_mappings , all_unmapped
189232# ================== 执行示例 ==================
190233def main ():
191234 # 加载现有tokens
192- with open ("/Users/Pan/Projects/Projects/Demo_DesignSystem/demo/scripts /tokens.json" ) as f :
235+ with open ("/Users/Pan/Projects/Projects/Demo_DesignSystem/demo/design_system /tokens.json" ) as f :
193236 # with open("tokens.json") as f:
194237 tokens = json .load (f )
195238
196239 # raw_data是API返回的原始数据
197- with open ("/Users/Pan/Projects/Projects/Demo_DesignSystem/demo/scripts/raw.json" ) as f :
198- # with open("raw.json") as f:
199- raw_data = json .load (f )
200- # raw_data = fetch_figma_data()
201-
202- # 初始化报告
203- all_mappings = {}
204- all_unmapped = []
240+ # with open("/Users/Pan/Projects/Projects/Demo_DesignSystem/demo/design_system/raw.json") as f:
241+ # raw_data = json.load(f)
242+ raw_data = fetch_figma_data ()
243+ save_to_json (raw_data , "/Users/Pan/Projects/Projects/Demo_DesignSystem/demo/design_system/framed_raw.json" )
205244
206245 # 遍历所有节点
207- for node_id , node_data in raw_data .get ("nodes" , {}).items ():
208- mappings , unmapped = analyze_node (node_id , node_data , tokens )
209- all_mappings [node_id ] = mappings
210- all_unmapped .extend (unmapped )
246+ # 根节点
247+ nodes = raw_data .get ("nodes" , {});
248+ all_mappings , all_unmapped = process_node (nodes , tokens );
249+
250+ # for node_id, node_data in raw_data.get("nodes", {}).items():
251+ # mappings, unmapped = analyze_node(node_id, node_data, tokens)
252+ # all_mappings[node_id] = mappings
253+ # all_unmapped.extend(unmapped)
211254
212255 # 保存结果
213- with open ("variable_mappings .json" , "w" ) as f :
256+ with open ("/Users/Pan/Projects/Projects/Demo_DesignSystem/demo/design_system/mapping/framed_variable_mappings .json" , "w" ) as f :
214257 json .dump (all_mappings , f , indent = 2 )
215258
216259
0 commit comments