feat(workbench): 固定右侧工作台并跨会话保持
This commit is contained in:
parent
57f189c6a8
commit
1ba2f249ac
|
|
@ -2465,10 +2465,26 @@ var native_default = { install };
|
||||||
// src/client/index.js
|
// src/client/index.js
|
||||||
var import_react2 = require("react");
|
var import_react2 = require("react");
|
||||||
var import_react_dom = require("react-dom");
|
var import_react_dom = require("react-dom");
|
||||||
var inject = ["sessions", "uiWorkspace", "uiConversation", "slots", "theme", "layout"];
|
var inject = ["sessions", "uiWorkspace", "uiConversation", "slots", "theme", "layout", "sidebarRightTabs", "sidebarRight"];
|
||||||
var PLUGIN_ID = "dsh-sansheng";
|
var PLUGIN_ID = "dsh-sansheng";
|
||||||
var TAB_KIND = "sansheng-workbench";
|
var TAB_KIND = "sansheng-workbench";
|
||||||
var TAB_TYPE_ID = "dsh-sansheng:workbench";
|
var TAB_TYPE_ID = "dsh-sansheng:workbench";
|
||||||
|
var RIGHT_PIN_KEY = "sansheng.right-workbench.pinned";
|
||||||
|
function readRightWorkbenchPinned() {
|
||||||
|
try {
|
||||||
|
return typeof localStorage !== "undefined" && localStorage.getItem(RIGHT_PIN_KEY) === "1";
|
||||||
|
} catch {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function writeRightWorkbenchPinned(value) {
|
||||||
|
try {
|
||||||
|
if (typeof localStorage === "undefined") return;
|
||||||
|
if (value) localStorage.setItem(RIGHT_PIN_KEY, "1");
|
||||||
|
else localStorage.removeItem(RIGHT_PIN_KEY);
|
||||||
|
} catch {
|
||||||
|
}
|
||||||
|
}
|
||||||
function installStyles(doc, tag, text) {
|
function installStyles(doc, tag, text) {
|
||||||
if (!doc || !doc.head) return null;
|
if (!doc || !doc.head) return null;
|
||||||
const style = doc.createElement("style");
|
const style = doc.createElement("style");
|
||||||
|
|
@ -2691,12 +2707,30 @@ function apply(ctx) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
function rightMountedSessionId() {
|
||||||
|
const right = get("sidebarRight");
|
||||||
|
try {
|
||||||
|
if (right && isFn(right.mounted?.getSnapshot)) return right.mounted.getSnapshot();
|
||||||
|
} catch {
|
||||||
|
}
|
||||||
|
return currentSessionId();
|
||||||
|
}
|
||||||
|
function findRightWorkbenchTab() {
|
||||||
|
const right = get("sidebarRight");
|
||||||
|
if (!right || !isFn(right.openTabs?.getSnapshot)) return null;
|
||||||
|
try {
|
||||||
|
const rows = right.openTabs.getSnapshot();
|
||||||
|
const sessionId = rightMountedSessionId();
|
||||||
|
return (Array.isArray(rows) ? rows : []).find((item) => item && item.kind === TAB_KIND && (!sessionId || item.sessionId === sessionId)) || null;
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
function revealRightTab() {
|
function revealRightTab() {
|
||||||
const right = get("sidebarRight");
|
const right = get("sidebarRight");
|
||||||
if (!right) return;
|
if (!right) return;
|
||||||
try {
|
try {
|
||||||
const rows = isFn(right.openTabs?.getSnapshot) ? right.openTabs.getSnapshot() : [];
|
const row = findRightWorkbenchTab();
|
||||||
const row = Array.isArray(rows) ? rows.find((item) => item && item.kind === TAB_KIND) : null;
|
|
||||||
if (row && isFn(right.focus)) right.focus(row.tabId);
|
if (row && isFn(right.focus)) right.focus(row.tabId);
|
||||||
if (isFn(right.isExpanded) && !right.isExpanded() && isFn(right.toggleExpanded)) right.toggleExpanded();
|
if (isFn(right.isExpanded) && !right.isExpanded() && isFn(right.toggleExpanded)) right.toggleExpanded();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
@ -2720,11 +2754,13 @@ function apply(ctx) {
|
||||||
const right = get("sidebarRight");
|
const right = get("sidebarRight");
|
||||||
if (!right || !isFn(right.openTab)) return false;
|
if (!right || !isFn(right.openTab)) return false;
|
||||||
if (!ensureRightTab()) return false;
|
if (!ensureRightTab()) return false;
|
||||||
try {
|
if (!findRightWorkbenchTab()) {
|
||||||
right.openTab(TAB_KIND);
|
try {
|
||||||
} catch (error) {
|
right.openTab(TAB_KIND);
|
||||||
console.warn("[sansheng] 打开右侧标签失败,改用浮动面板:", error && error.message ? error.message : error);
|
} catch (error) {
|
||||||
return false;
|
console.warn("[sansheng] 打开右侧标签失败,改用浮动面板:", error && error.message ? error.message : error);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
revealRightTab();
|
revealRightTab();
|
||||||
for (const delay of [150, 500, 1200]) setTimeout(revealRightTab, delay);
|
for (const delay of [150, 500, 1200]) setTimeout(revealRightTab, delay);
|
||||||
|
|
@ -2814,6 +2850,7 @@ function apply(ctx) {
|
||||||
}
|
}
|
||||||
const selectedSeatCache = /* @__PURE__ */ new Map();
|
const selectedSeatCache = /* @__PURE__ */ new Map();
|
||||||
let lastSelectedSeatId = SEAT_IDS[0];
|
let lastSelectedSeatId = SEAT_IDS[0];
|
||||||
|
let rightWorkbenchPinned = readRightWorkbenchPinned();
|
||||||
function SanshengWorkbench(props) {
|
function SanshengWorkbench(props) {
|
||||||
return (0, import_react2.createElement)(WorkbenchV2, {
|
return (0, import_react2.createElement)(WorkbenchV2, {
|
||||||
onClose: props && props.onClose,
|
onClose: props && props.onClose,
|
||||||
|
|
@ -2830,9 +2867,25 @@ function apply(ctx) {
|
||||||
const NAV_PANEL_ID = "sansheng-workbench";
|
const NAV_PANEL_ID = "sansheng-workbench";
|
||||||
const NAV_ORDER = 32;
|
const NAV_ORDER = 32;
|
||||||
function openRightWorkbench() {
|
function openRightWorkbench() {
|
||||||
return tryOpenRightTab();
|
const opened = tryOpenRightTab();
|
||||||
|
if (opened) {
|
||||||
|
rightWorkbenchPinned = true;
|
||||||
|
writeRightWorkbenchPinned(true);
|
||||||
|
}
|
||||||
|
return opened;
|
||||||
}
|
}
|
||||||
function closeRightWorkbench() {
|
function closeRightWorkbench() {
|
||||||
|
rightWorkbenchPinned = false;
|
||||||
|
writeRightWorkbenchPinned(false);
|
||||||
|
const right = get("sidebarRight");
|
||||||
|
const row = findRightWorkbenchTab();
|
||||||
|
if (row && right && isFn(right.close)) {
|
||||||
|
try {
|
||||||
|
right.close(row.tabId);
|
||||||
|
return;
|
||||||
|
} catch {
|
||||||
|
}
|
||||||
|
}
|
||||||
const layout = get("layout");
|
const layout = get("layout");
|
||||||
if (layout && isFn(layout.selectPanel)) {
|
if (layout && isFn(layout.selectPanel)) {
|
||||||
try {
|
try {
|
||||||
|
|
@ -2841,7 +2894,6 @@ function apply(ctx) {
|
||||||
} catch {
|
} catch {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const right = get("sidebarRight");
|
|
||||||
if (right && isFn(right.closeTab)) {
|
if (right && isFn(right.closeTab)) {
|
||||||
try {
|
try {
|
||||||
right.closeTab(TAB_KIND);
|
right.closeTab(TAB_KIND);
|
||||||
|
|
@ -2850,6 +2902,24 @@ function apply(ctx) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
function installRightDockPinning() {
|
||||||
|
const right = get("sidebarRight");
|
||||||
|
if (!right || !isFn(right.mounted?.subscribe)) return;
|
||||||
|
const stop = right.mounted.subscribe(() => {
|
||||||
|
if (!rightWorkbenchPinned) return;
|
||||||
|
for (const delay of [80, 240, 700]) {
|
||||||
|
setTimeout(() => {
|
||||||
|
if (!rightWorkbenchPinned) return;
|
||||||
|
try {
|
||||||
|
if (!findRightWorkbenchTab()) openRightWorkbench();
|
||||||
|
else revealRightTab();
|
||||||
|
} catch {
|
||||||
|
}
|
||||||
|
}, delay);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (isFn(ctx.effect)) ctx.effect(() => stop);
|
||||||
|
}
|
||||||
function SanshengNavButton(props) {
|
function SanshengNavButton(props) {
|
||||||
const size = props && typeof props.size === "number" ? props.size : 16;
|
const size = props && typeof props.size === "number" ? props.size : 16;
|
||||||
const [floating, setFloating] = (0, import_react2.useState)(false);
|
const [floating, setFloating] = (0, import_react2.useState)(false);
|
||||||
|
|
@ -2988,6 +3058,12 @@ function apply(ctx) {
|
||||||
}
|
}
|
||||||
registerNavEntry();
|
registerNavEntry();
|
||||||
registerRightTabType();
|
registerRightTabType();
|
||||||
|
installRightDockPinning();
|
||||||
|
if (rightWorkbenchPinned) {
|
||||||
|
for (const delay of [300, 900]) setTimeout(() => {
|
||||||
|
if (rightWorkbenchPinned) openRightWorkbench();
|
||||||
|
}, delay);
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
install(ctx);
|
install(ctx);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
|
||||||
|
|
@ -9,11 +9,24 @@ import { request } from './transport.js'
|
||||||
import { CSS as WORKBENCH_CSS } from './styles.js'
|
import { CSS as WORKBENCH_CSS } from './styles.js'
|
||||||
import { install as installNativePanel } from '../native/index.js'
|
import { install as installNativePanel } from '../native/index.js'
|
||||||
|
|
||||||
export const inject = ['sessions', 'uiWorkspace', 'uiConversation', 'slots', 'theme', 'layout']
|
export const inject = ['sessions', 'uiWorkspace', 'uiConversation', 'slots', 'theme', 'layout', 'sidebarRightTabs', 'sidebarRight']
|
||||||
|
|
||||||
const PLUGIN_ID = 'dsh-sansheng'
|
const PLUGIN_ID = 'dsh-sansheng'
|
||||||
const TAB_KIND = 'sansheng-workbench'
|
const TAB_KIND = 'sansheng-workbench'
|
||||||
const TAB_TYPE_ID = 'dsh-sansheng:workbench'
|
const TAB_TYPE_ID = 'dsh-sansheng:workbench'
|
||||||
|
const RIGHT_PIN_KEY = 'sansheng.right-workbench.pinned'
|
||||||
|
|
||||||
|
function readRightWorkbenchPinned() {
|
||||||
|
try { return typeof localStorage !== 'undefined' && localStorage.getItem(RIGHT_PIN_KEY) === '1' } catch { return false }
|
||||||
|
}
|
||||||
|
|
||||||
|
function writeRightWorkbenchPinned(value) {
|
||||||
|
try {
|
||||||
|
if (typeof localStorage === 'undefined') return
|
||||||
|
if (value) localStorage.setItem(RIGHT_PIN_KEY, '1')
|
||||||
|
else localStorage.removeItem(RIGHT_PIN_KEY)
|
||||||
|
} catch { /* 忽略 */ }
|
||||||
|
}
|
||||||
|
|
||||||
function installStyles(doc, tag, text) {
|
function installStyles(doc, tag, text) {
|
||||||
if (!doc || !doc.head) return null
|
if (!doc || !doc.head) return null
|
||||||
|
|
@ -215,12 +228,29 @@ export function apply(ctx) {
|
||||||
try { return !!(tabs.get(TAB_KIND) || tabs.get(TAB_TYPE_ID)) } catch { return false }
|
try { return !!(tabs.get(TAB_KIND) || tabs.get(TAB_TYPE_ID)) } catch { return false }
|
||||||
}
|
}
|
||||||
// 桌面版右栏常是收起状态:只 openTab 不展开,用户会以为“点了没反应”。
|
// 桌面版右栏常是收起状态:只 openTab 不展开,用户会以为“点了没反应”。
|
||||||
|
function rightMountedSessionId() {
|
||||||
|
const right = get('sidebarRight')
|
||||||
|
try {
|
||||||
|
if (right && isFn(right.mounted?.getSnapshot)) return right.mounted.getSnapshot()
|
||||||
|
} catch { /* 忽略 */ }
|
||||||
|
return currentSessionId()
|
||||||
|
}
|
||||||
|
|
||||||
|
function findRightWorkbenchTab() {
|
||||||
|
const right = get('sidebarRight')
|
||||||
|
if (!right || !isFn(right.openTabs?.getSnapshot)) return null
|
||||||
|
try {
|
||||||
|
const rows = right.openTabs.getSnapshot()
|
||||||
|
const sessionId = rightMountedSessionId()
|
||||||
|
return (Array.isArray(rows) ? rows : []).find(item => item && item.kind === TAB_KIND && (!sessionId || item.sessionId === sessionId)) || null
|
||||||
|
} catch { return null }
|
||||||
|
}
|
||||||
|
|
||||||
function revealRightTab() {
|
function revealRightTab() {
|
||||||
const right = get('sidebarRight')
|
const right = get('sidebarRight')
|
||||||
if (!right) return
|
if (!right) return
|
||||||
try {
|
try {
|
||||||
const rows = isFn(right.openTabs?.getSnapshot) ? right.openTabs.getSnapshot() : []
|
const row = findRightWorkbenchTab()
|
||||||
const row = Array.isArray(rows) ? rows.find(item => item && item.kind === TAB_KIND) : null
|
|
||||||
if (row && isFn(right.focus)) right.focus(row.tabId)
|
if (row && isFn(right.focus)) right.focus(row.tabId)
|
||||||
if (isFn(right.isExpanded) && !right.isExpanded() && isFn(right.toggleExpanded)) right.toggleExpanded()
|
if (isFn(right.isExpanded) && !right.isExpanded() && isFn(right.toggleExpanded)) right.toggleExpanded()
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
@ -246,9 +276,11 @@ export function apply(ctx) {
|
||||||
const right = get('sidebarRight')
|
const right = get('sidebarRight')
|
||||||
if (!right || !isFn(right.openTab)) return false
|
if (!right || !isFn(right.openTab)) return false
|
||||||
if (!ensureRightTab()) return false
|
if (!ensureRightTab()) return false
|
||||||
try { right.openTab(TAB_KIND) } catch (error) {
|
if (!findRightWorkbenchTab()) {
|
||||||
console.warn('[sansheng] 打开右侧标签失败,改用浮动面板:', error && error.message ? error.message : error)
|
try { right.openTab(TAB_KIND) } catch (error) {
|
||||||
return false
|
console.warn('[sansheng] 打开右侧标签失败,改用浮动面板:', error && error.message ? error.message : error)
|
||||||
|
return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// 右栏(尤其 better-sidebar 的原生面)是异步落位的,不能同步看不到就当失败,
|
// 右栏(尤其 better-sidebar 的原生面)是异步落位的,不能同步看不到就当失败,
|
||||||
// 否则会误退成主区渲染、把中间聊天顶掉。这里先乐观返回,再多次补一次聚焦/展开。
|
// 否则会误退成主区渲染、把中间聊天顶掉。这里先乐观返回,再多次补一次聚焦/展开。
|
||||||
|
|
@ -317,6 +349,7 @@ export function apply(ctx) {
|
||||||
// 工作台会随宿主会话切换而重挂载;按项目记住选中的部门,避免又跳回丞相。
|
// 工作台会随宿主会话切换而重挂载;按项目记住选中的部门,避免又跳回丞相。
|
||||||
const selectedSeatCache = new Map()
|
const selectedSeatCache = new Map()
|
||||||
let lastSelectedSeatId = SEAT_IDS[0]
|
let lastSelectedSeatId = SEAT_IDS[0]
|
||||||
|
let rightWorkbenchPinned = readRightWorkbenchPinned()
|
||||||
|
|
||||||
// ---------- 工作台组件 ----------
|
// ---------- 工作台组件 ----------
|
||||||
// 工作台 v2:视觉与交互按用户提供的设计稿实现,数据全部走真实 host API。
|
// 工作台 v2:视觉与交互按用户提供的设计稿实现,数据全部走真实 host API。
|
||||||
|
|
@ -339,20 +372,47 @@ export function apply(ctx) {
|
||||||
const NAV_ORDER = 32
|
const NAV_ORDER = 32
|
||||||
|
|
||||||
function openRightWorkbench() {
|
function openRightWorkbench() {
|
||||||
return tryOpenRightTab()
|
const opened = tryOpenRightTab()
|
||||||
|
if (opened) { rightWorkbenchPinned = true; writeRightWorkbenchPinned(true) }
|
||||||
|
return opened
|
||||||
}
|
}
|
||||||
|
|
||||||
function closeRightWorkbench() {
|
function closeRightWorkbench() {
|
||||||
|
rightWorkbenchPinned = false
|
||||||
|
writeRightWorkbenchPinned(false)
|
||||||
|
const right = get('sidebarRight')
|
||||||
|
const row = findRightWorkbenchTab()
|
||||||
|
if (row && right && isFn(right.close)) {
|
||||||
|
try { right.close(row.tabId); return } catch { /* 忽略 */ }
|
||||||
|
}
|
||||||
const layout = get('layout')
|
const layout = get('layout')
|
||||||
if (layout && isFn(layout.selectPanel)) {
|
if (layout && isFn(layout.selectPanel)) {
|
||||||
try { layout.selectPanel(null); return } catch { /* 换下一个候选 */ }
|
try { layout.selectPanel(null); return } catch { /* 换下一个候选 */ }
|
||||||
}
|
}
|
||||||
const right = get('sidebarRight')
|
|
||||||
if (right && isFn(right.closeTab)) {
|
if (right && isFn(right.closeTab)) {
|
||||||
try { right.closeTab(TAB_KIND); return } catch { /* 忽略 */ }
|
try { right.closeTab(TAB_KIND); return } catch { /* 忽略 */ }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 右侧栏按会话保存布局;切换部门打开新会话后,把固定工作台重新挂到新会话。
|
||||||
|
function installRightDockPinning() {
|
||||||
|
const right = get('sidebarRight')
|
||||||
|
if (!right || !isFn(right.mounted?.subscribe)) return
|
||||||
|
const stop = right.mounted.subscribe(() => {
|
||||||
|
if (!rightWorkbenchPinned) return
|
||||||
|
for (const delay of [80, 240, 700]) {
|
||||||
|
setTimeout(() => {
|
||||||
|
if (!rightWorkbenchPinned) return
|
||||||
|
try {
|
||||||
|
if (!findRightWorkbenchTab()) openRightWorkbench()
|
||||||
|
else revealRightTab()
|
||||||
|
} catch { /* 会话尚未挂载时下次再试 */ }
|
||||||
|
}, delay)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if (isFn(ctx.effect)) ctx.effect(() => stop)
|
||||||
|
}
|
||||||
|
|
||||||
// 导航行内容(宿主只把这里当图标渲染,标签由宿主按 label() 画)。
|
// 导航行内容(宿主只把这里当图标渲染,标签由宿主按 label() 画)。
|
||||||
// 点击只开右侧工作台,并阻止冒泡,避免宿主把主区切成面板页——中央原生聊天因此保持原样。
|
// 点击只开右侧工作台,并阻止冒泡,避免宿主把主区切成面板页——中央原生聊天因此保持原样。
|
||||||
function SanshengNavButton(props) {
|
function SanshengNavButton(props) {
|
||||||
|
|
@ -468,6 +528,10 @@ export function apply(ctx) {
|
||||||
|
|
||||||
// 右侧工作台标签类型:需在 apply 时就注册(宿主按类型 id 选正文 slot)。
|
// 右侧工作台标签类型:需在 apply 时就注册(宿主按类型 id 选正文 slot)。
|
||||||
registerRightTabType()
|
registerRightTabType()
|
||||||
|
installRightDockPinning()
|
||||||
|
if (rightWorkbenchPinned) {
|
||||||
|
for (const delay of [300, 900]) setTimeout(() => { if (rightWorkbenchPinned) openRightWorkbench() }, delay)
|
||||||
|
}
|
||||||
|
|
||||||
// dsh-go 原生面板:浏览器(无 dshGo 服务)或桥未就绪时,install 内部安静退出。
|
// dsh-go 原生面板:浏览器(无 dshGo 服务)或桥未就绪时,install 内部安静退出。
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
|
|
@ -15,3 +15,11 @@ test('左侧入口使用 sidebar.panellist,位置在记忆系统与更多之
|
||||||
assert.ok(!source.includes('sidebar.session.action'), '不得再占用新会话右侧槽位')
|
assert.ok(!source.includes('sidebar.session.action'), '不得再占用新会话右侧槽位')
|
||||||
assert.ok(!source.includes('SanshengSessionAction'), '旧的新会话入口应已删除')
|
assert.ok(!source.includes('SanshengSessionAction'), '旧的新会话入口应已删除')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('右侧工作台依赖宿主 sidebarRight,并在会话切换后重新固定', async () => {
|
||||||
|
const source = await readFile(join(root, 'src/client/index.js'), 'utf8')
|
||||||
|
assert.match(source, /export const inject = \[[^\]]*'sidebarRightTabs', 'sidebarRight'/)
|
||||||
|
assert.match(source, /right\.mounted\.subscribe/)
|
||||||
|
assert.match(source, /right\.openTabs\.getSnapshot/)
|
||||||
|
assert.match(source, /right\.focus\(row\.tabId\)/)
|
||||||
|
})
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue