Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
09a3ca4680 | ||
|
|
09ad92972c |
No files matched your search
@@ -9,6 +9,7 @@
|
||||
* mermaid libraries arrive as lazy chunks).
|
||||
*/
|
||||
import { createElement, Fragment } from 'react'
|
||||
import { OfficeView } from './office-view.tsx'
|
||||
import { createRoot, type Root } from 'react-dom/client'
|
||||
import type { Context } from '../context-types.ts'
|
||||
import { createSidebarStore } from './state.ts'
|
||||
@@ -82,11 +83,62 @@ export const inject = ['slots', 'sessions', 'locale', 'modules', 'connection', '
|
||||
* per-tab scope (Sidebar.tsx) catches viewer/editor crashes first; this root
|
||||
* boundary stays as the last resort for Workbench/shell errors.
|
||||
*/
|
||||
/**
|
||||
* 宿主左栏的「取消固定」状态:localStorage `dsh-desktop.sidebar.panel-pins`
|
||||
* 形如 `{ unpinned: string[] }`。未被固定的入口会从左栏隐藏,收进宿主自带的
|
||||
* 「更多」菜单(`sidebar.panellist.actions` → entry-customization)。
|
||||
*
|
||||
* 我们在**首次见到该键**时写入默认折叠清单(插件市场 / 上下文洞察 / 任务看板),
|
||||
* 让左栏只留常用入口;用户之后手动 pin / unpin 的选择不再被覆盖。
|
||||
* `plugins`(宿主内置的插件管理入口)是固定项,不可折叠。
|
||||
*/
|
||||
const PANEL_PIN_KEY = 'dsh-desktop.sidebar.panel-pins'
|
||||
const DEFAULT_COLLAPSED_PANELS = ['dsh-market', 'dsh-context', 'task-board', 'forge-review']
|
||||
const PANEL_PIN_MARK_KEY = 'dsh-apes-sidebar:collapsed-defaults'
|
||||
|
||||
/**
|
||||
* 写入默认折叠清单(仅当用户从未做过 pin 选择、或默认集合升级时补齐缺项)。
|
||||
*
|
||||
* 宿主的 pin store 在它自己的模块初始化时读取该键,我们的 bundle 加载更晚,
|
||||
* 因此写入后刷新一次页面让折叠生效;标记键记录我们已写过的默认集合,
|
||||
* 之后用户手动 pin / unpin 的结果不再被改动。
|
||||
*/
|
||||
function ensureDefaultCollapsedPanels(): void {
|
||||
if (typeof localStorage === 'undefined') return
|
||||
try {
|
||||
const mark = localStorage.getItem(PANEL_PIN_MARK_KEY)
|
||||
if (mark === DEFAULT_COLLAPSED_PANELS.join(',')) return
|
||||
const raw = localStorage.getItem(PANEL_PIN_KEY)
|
||||
const current = raw === null ? [] : (JSON.parse(raw)?.unpinned ?? [])
|
||||
if (!Array.isArray(current)) return
|
||||
// 只补齐缺项:用户手动 pin 回来的入口,若不在默认集合里则保持不变。
|
||||
const merged = [...current]
|
||||
for (const id of DEFAULT_COLLAPSED_PANELS) if (!merged.includes(id)) merged.push(id)
|
||||
localStorage.setItem(PANEL_PIN_KEY, JSON.stringify({ unpinned: merged }))
|
||||
localStorage.setItem(PANEL_PIN_MARK_KEY, DEFAULT_COLLAPSED_PANELS.join(','))
|
||||
console.info('[dsh-apes-sidebar] 已写入默认折叠的侧栏入口,刷新后生效')
|
||||
location.reload()
|
||||
} catch {
|
||||
// 存储不可用(隐私模式 / 配额满)时保持宿主默认,不阻断插件装载。
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Client plugin body.
|
||||
* @param ctx - the client cordis context (slots, sessions).
|
||||
*/
|
||||
export function apply(ctx: Context): void {
|
||||
ensureDefaultCollapsedPanels()
|
||||
// The ApeSi session view: the current session's Office document list, placed
|
||||
// AFTER the host's own views (上下文 order 20 / 编队 order 30 → ours is 40).
|
||||
// The host renders every `conversation.view` registration into the session
|
||||
// header tab strip, and passes the session it renders.
|
||||
ctx.slots.inject('conversation.view', () =>
|
||||
ctx.slots.register(
|
||||
{ name: 'conversation.view', id: 'apesi-office', order: 40, label: () => 'Office' },
|
||||
(viewProps: { sessionId?: string }) => createElement(OfficeView, { ...viewProps, ctx }),
|
||||
),
|
||||
)
|
||||
// The sidebar follows the DSH i18n system: attach the locale service so
|
||||
// the module-level t()/isZh() resolve the Host-backed language preference
|
||||
// (and switch live — the Sidebar root subscribes to it), and register the
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
/**
|
||||
* The ApeSi session view: the current session's Office document list.
|
||||
*
|
||||
* The list is served by the `dsh-apesi-dshoffice` bridge (`POST /office/api/files`);
|
||||
* a click opens the file in DSH's NATIVE right sidebar, where the preview and the
|
||||
* editor already live (the bridge moved them there in v0.7). This view only lists
|
||||
* and hands over — it never renders an editor of its own.
|
||||
*
|
||||
* Registered as a `conversation.view`, so it appears in the session header tab
|
||||
* strip next to the host's own views (对话 / 上下文 / 编队 / 轨迹 …). The host
|
||||
* passes the session it renders; `order` places it after the context/fleet
|
||||
* views (20 / 30) instead of at the front of the strip.
|
||||
*/
|
||||
|
||||
import { createElement, useEffect, useState, type ReactNode } from 'react'
|
||||
import { addressBaseOf } from './address-base.ts'
|
||||
import { hostRouteUrl } from './host-route-url.ts'
|
||||
import { fileAddressFor } from './resource-address.ts'
|
||||
import type { Context } from '../context-types.ts'
|
||||
|
||||
/** One row of the Office list, as the bridge returns it. */
|
||||
interface OfficeFile {
|
||||
name?: string
|
||||
path?: string
|
||||
size?: number
|
||||
updatedAt?: string
|
||||
}
|
||||
|
||||
/** The view's props: the host's session plus our own client context. */
|
||||
export interface OfficeViewProps {
|
||||
sessionId?: string
|
||||
ctx: Context
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the file list out of the bridge's answer.
|
||||
*
|
||||
* The bridge answers `{ items, files }`: `items` is the flat document list this
|
||||
* view renders, `files` the directory tree its own page uses. A bridge that is
|
||||
* not installed answers with an error object — that reads as an empty list.
|
||||
*/
|
||||
function normalizeList(payload: unknown): OfficeFile[] {
|
||||
if (payload === null || typeof payload !== 'object') return []
|
||||
const data = payload as { items?: unknown; files?: unknown }
|
||||
const raw = Array.isArray(data.items) ? data.items : Array.isArray(data.files) ? data.files : []
|
||||
return raw.filter((entry): entry is OfficeFile => entry !== null && typeof entry === 'object')
|
||||
}
|
||||
|
||||
/** The row's display name: the file name, or the tail of its path. */
|
||||
function displayName(file: OfficeFile): string {
|
||||
if (typeof file.name === 'string' && file.name !== '') return file.name
|
||||
if (typeof file.path === 'string') {
|
||||
const parts = file.path.split(/[\\/]/).filter(part => part !== '')
|
||||
return parts[parts.length - 1] ?? file.path
|
||||
}
|
||||
return '(unnamed)'
|
||||
}
|
||||
|
||||
/**
|
||||
* The session's Office documents.
|
||||
*
|
||||
* @param props - the host's session id and our client context.
|
||||
* @returns the list view (loading / empty / rows).
|
||||
*/
|
||||
export function OfficeView(props: OfficeViewProps): ReactNode {
|
||||
const { sessionId, ctx } = props
|
||||
const [files, setFiles] = useState<OfficeFile[] | null>(null)
|
||||
const [failed, setFailed] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
if (sessionId === undefined) {
|
||||
setFiles([])
|
||||
return
|
||||
}
|
||||
let cancelled = false
|
||||
fetch(hostRouteUrl('office/api/files').href, {
|
||||
method: 'POST',
|
||||
headers: { 'content-type': 'application/json' },
|
||||
body: JSON.stringify({ sessionId }),
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(payload => {
|
||||
if (cancelled) return
|
||||
setFiles(normalizeList(payload))
|
||||
setFailed(false)
|
||||
})
|
||||
.catch(() => {
|
||||
if (cancelled) return
|
||||
// The bridge is absent or unreachable: the view stays, empty.
|
||||
setFiles([])
|
||||
setFailed(true)
|
||||
})
|
||||
return () => {
|
||||
cancelled = true
|
||||
}
|
||||
}, [sessionId])
|
||||
|
||||
/** Open the file in the native right sidebar (preview / edit live there). */
|
||||
function open(file: OfficeFile): void {
|
||||
if (sessionId === undefined || typeof file.path !== 'string') return
|
||||
const surface = ctx.get('sidebarRight') as
|
||||
| { openResource?: (input: { sessionId: string; address: string; revealIfOpened: boolean }) => void }
|
||||
| undefined
|
||||
const address = fileAddressFor(sessionId, addressBaseOf(ctx, sessionId), file.path)
|
||||
surface?.openResource?.({ sessionId, address, revealIfOpened: true })
|
||||
}
|
||||
|
||||
if (files === null) {
|
||||
return createElement('div', { className: 'apesi-office-view', 'data-apesi-office': 'loading' }, '加载中…')
|
||||
}
|
||||
if (files.length === 0) {
|
||||
return createElement(
|
||||
'div',
|
||||
{ className: 'apesi-office-view', 'data-apesi-office': failed ? 'failed' : 'empty' },
|
||||
failed ? 'Office 桥接不可用(dsh-apesi-dshoffice 未安装)' : '本会话暂无 Office 文档',
|
||||
)
|
||||
}
|
||||
return createElement(
|
||||
'div',
|
||||
{ className: 'apesi-office-view', 'data-apesi-office': 'list' },
|
||||
files.map((file, index) =>
|
||||
createElement(
|
||||
'button',
|
||||
{
|
||||
key: `${file.path ?? ''}:${index}`,
|
||||
type: 'button',
|
||||
className: 'apesi-office-row',
|
||||
onClick: () => open(file),
|
||||
},
|
||||
createElement('span', { className: 'apesi-office-name' }, displayName(file)),
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user