workspace: use ResourceBrowser widget

This commit is contained in:
p 2026-08-13 12:02:00 +08:00
parent 0805eb219b
commit 751c953ad3
3 changed files with 26 additions and 51 deletions

View File

@ -1,12 +1,9 @@
# workspace_files.dspy - 返回目录下文件列表 # workspace_files.dspy - 返回目录文件列表(无 id 或 id=__root__ 时返回根目录文件)
import os import os
folder_id = (params_kw or {}).get('id', '').strip() folder_id = (params_kw or {}).get('id', '').strip()
if not folder_id:
return {"widgettype": "Text", "options": {"text": "请从左侧选择目录", "cfontsize": 0.9}}
uid = await get_user() uid = await get_user()
if not uid: if not uid:
uid = 'user-01' uid = 'user-01'
@ -30,20 +27,20 @@ async with DBPools().sqlorContext(dbname) as sor:
org_id = getattr(proj[0], 'org_id', '0') or '0' org_id = getattr(proj[0], 'org_id', '0') or '0'
ws_dir = workspace_base + '/' + org_id + '/' + pname ws_dir = workspace_base + '/' + org_id + '/' + pname
# __root__ means workspace root directory # 无 id 或 __root__ 都指向工作空间根目录
if folder_id == '__root__': if not folder_id or folder_id == '__root__':
full_dir = ws_dir full_dir = ws_dir
else: else:
full_dir = ws_dir + '/' + folder_id if ws_dir else folder_id full_dir = ws_dir + '/' + folder_id if ws_dir else folder_id
if not os.path.isdir(full_dir): if not os.path.isdir(full_dir):
return {"widgettype": "Text", "options": {"text": "目录不可用: " + folder_id}} return {"widgettype": "Text", "options": {"text": "目录不可用: " + folder_id, "cfontsize": 0.9}}
files = [] files = []
try: try:
entries = sorted(os.listdir(full_dir)) entries = sorted(os.listdir(full_dir))
except Exception as e: except Exception as e:
return {"widgettype": "Text", "options": {"text": "读取错误: " + str(e)}} return {"widgettype": "Text", "options": {"text": "读取错误: " + str(e), "cfontsize": 0.9}}
for name in entries: for name in entries:
full = os.path.join(full_dir, name) full = os.path.join(full_dir, name)
@ -54,30 +51,19 @@ for name in entries:
ext = os.path.splitext(name)[1].lower() 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'] 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 "📁" icon = "📄" if is_text else "🎬" if ext in ['.png','.jpg','.mp4'] else "📁"
fpath_escaped = full.replace("\\", "\\\\").replace("'", "\\'")
fname_escaped = name.replace("'", "\\'")
row = { row = {
"widgettype": "HBox", "widgettype": "HBox",
"options": {"padding": "6px 10px", "alignItems": "center", "gap": "8px", "borderRadius": "4px"}, "options": {"padding": "6px 10px", "alignItems": "center", "gap": "8px"},
"subwidgets": [ "subwidgets": [
{"widgettype": "Text", "options": {"text": icon, "cfontsize": 1.2}}, {"widgettype": "Text", "options": {"text": icon, "cfontsize": 1.2}},
{"widgettype": "Text", "options": {"text": name, "cfontsize": 0.9, "color": "#1e293b", "css": "filler"}}, {"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"}}, {"widgettype": "Text", "options": {"text": f"{size/1024:.1f}KB", "cfontsize": 0.75, "color": "#94a3b8"}},
], ],
} }
if is_text:
row["options"]["style"] = {"cursor": "pointer"}
row["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)}}}})}});}})"
}]
files.append(row) files.append(row)
if not files: if not files:
return {"widgettype": "Text", "options": {"text": "(空目录)", "cfontsize": 0.85, "color": "#94a3b8"}} return {"widgettype": "Text", "options": {"text": "(空目录)", "cfontsize": 0.85, "color": "#94a3b8", "padding": "8px"}}
return { return {
"widgettype": "VScrollPanel", "widgettype": "VScrollPanel",

View File

@ -1,4 +1,4 @@
# workspace_popup.dspy - 用 VBox 代替 VScrollPanel # workspace_popup.dspy - 返回 PopupWindow,内含 ResourceBrowser 控件
uid = await get_user() uid = await get_user()
if not uid: if not uid:
@ -37,36 +37,25 @@ async with DBPools().sqlorContext(dbname) as sor:
"auto_open": True "auto_open": True
}, },
"subwidgets": [{ "subwidgets": [{
"widgettype": "HBox", "widgettype": "ResourceBrowser",
"options": {"height": "100%"}, "options": {
"subwidgets": [ "tree_options": {
{ "widgettype": "Tree",
"widgettype": "VBox",
"options": {"width": "280px"},
"subwidgets": [{
"widgettype": "Tree",
"id": "ws_tree",
"options": {
"dataurl": entire_url("/pipeline-sdlc/api/workspace_tree.dspy"),
"textField": "label",
"idField": "id",
"cfontsize": 1.0,
"css": "filler",
"padding": "4px",
"cheight": "100%"
}
}]
},
{
"widgettype": "VBox",
"id": "ws_files",
"options": { "options": {
"css": "filler", "dataurl": entire_url("/pipeline-sdlc/api/workspace_tree.dspy"),
"padding": "8px", "textField": "label",
"gap": "4px" "idField": "id",
"cfontsize": 1.0
} }
} },
] "browser_options": {
"widgettype": "urlwidget",
"options": {
"url": entire_url("/pipeline-sdlc/api/workspace_files.dspy")
}
},
"tree_width": "280px"
}
}] }]
} }

File diff suppressed because one or more lines are too long