# workspace_files.dspy - 返回目录下文件列表(Bricks widget 格式) import os folder_id = (params_kw or {}).get('id', '').strip() if not folder_id or folder_id == '__root__': return {"widgettype": "Text", "options": {"text": "请从左侧选择目录", "cfontsize": 0.9}} if not os.path.isdir(folder_id): return {"widgettype": "Text", "options": {"text": "目录不可用"}} # 文件列表 files = [] try: entries = sorted(os.listdir(folder_id)) except Exception as e: return {"widgettype": "Text", "options": {"text": "读取错误: " + str(e)}} for name in entries: full = os.path.join(folder_id, name) if name.startswith('.'): continue if os.path.isfile(full): size = os.path.getsize(full) ext = os.path.splitext(name)[1].lower() is_text = ext in ['.md','.py','.js','.html','.css','.json','.txt','.xml','.yaml','.yml','.toml','.cfg','.sh','.sql','.dspy','.ui'] icon = "📄" if is_text else "🎬" if ext in ['.png','.jpg','.mp4'] else "📁" fpath_escaped = full.replace("\\", "\\\\").replace("'", "\\'") fname_escaped = name.replace("'", "\\'") row = { "widgettype": "HBox", "options": { "padding": "6px 10px", "alignItems": "center", "gap": "8px", "borderRadius": "4px", "style": {"cursor": "pointer"}, }, "subwidgets": [ {"widgettype": "Text", "options": {"text": icon, "cfontsize": 1.2}}, {"widgettype": "Text", "options": {"text": name, "cfontsize": 0.9, "color": "#1e293b", "css": "filler"}}, {"widgettype": "Text", "options": {"text": f"{size/1024:.1f}KB", "cfontsize": 0.75, "color": "#94a3b8"}}, ], "binds": [{ "wid": "self", "event": "click", "actiontype": "script", "target": "self", "script": f"var pw=new bricks.PopupWindow({{title:'{fname_escaped}',cwidth:70,cheight:32,auto_open:true}});var wt=new bricks.Wterm({{css:'filler',padding:'0'}});pw.content_w.add_widget(wt);var sb=new bricks.Button({{label:'保存'}});pw.content_w.add_widget(sb);fetch('/pipeline-sdlc/api/workspace_file.dspy?action=read&path='+encodeURIComponent('{fpath_escaped}')).then(function(r){{return r.json()}}).then(function(d){{if(d.error){{wt.write('Error: '+d.error);return}};wt.write(d.content||'');sb.bind('click',function(){{var c=wt.get_value();fetch('/pipeline-sdlc/api/workspace_file.dspy',{{method:'POST',headers:{{'Content-Type':'application/x-www-form-urlencoded'}},body:'action=save&path='+encodeURIComponent('{fpath_escaped}')+'&content='+encodeURIComponent(c)}}).then(function(r){{return r.json()}}).then(function(d){{if(d.success){{sb.set_text('已保存');setTimeout(function(){{sb.set_text('保存')}},2000)}}}})}});}})" }] if is_text else [] } if is_text else { "widgettype": "HBox", "options": { "padding": "6px 10px", "alignItems": "center", "gap": "8px", "borderRadius": "4px", }, "subwidgets": [ {"widgettype": "Text", "options": {"text": icon, "cfontsize": 1.2}}, {"widgettype": "Text", "options": {"text": name, "cfontsize": 0.9, "color": "#94a3b8", "css": "filler"}}, {"widgettype": "Text", "options": {"text": f"{size/1024:.1f}KB", "cfontsize": 0.75, "color": "#94a3b8"}}, ], } files.append(row) if not files: return {"widgettype": "Text", "options": {"text": "(空目录)", "cfontsize": 0.85, "color": "#94a3b8"}} return { "widgettype": "VScrollPanel", "options": {"css": "filler", "padding": "8px", "gap": "4px"}, "subwidgets": files }