dsh-design-studio 0.27.0 → 0.28.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -36,6 +36,9 @@
36
36
  * GET /dsh-design-studio/api/bundle?session=&key=&v=&parent= 远程看:这一屏连同样式、图片打成一份 → { html, deps }(放进沙盒 iframe;deps 改了再取)
37
37
  * POST /dsh-design-studio/api/phone 手机上看:{ session, on: true, path } 开局域网入口(给链接和二维码)、
38
38
  * { on: false } 关掉、{ follow: path } 电脑上换了一屏手机跟着跳
39
+ * POST /dsh-design-studio/api/pubshare 公网链接(实验):{ session, on: true, …整套演示的参数 } 用 cloudflared 临时通道放出整套 HTML 演示、
40
+ * { on: false } 关掉、{ peek: true } 看状态(带二维码)、{ update: true, … } 换参数、
41
+ * { download: true } 下载 cloudflared(人点了才下)、{ cancel: true } 取消下载
39
42
  *
40
43
  * 页面本身由 serve.js 开的独立端口提供(见那边的说明)。
41
44
  */
@@ -60,6 +63,7 @@ import { createTracker, parseTarget, findRule, KINDS } from './core.js'
60
63
  import { createDrafts } from './draft.js'
61
64
  import { createHistory } from './history.js'
62
65
  import { createPhone } from './phone.js'
66
+ import { createPublicShare } from './tunnel.js'
63
67
  import { qrMatrix } from './qr.js'
64
68
  import { makePdf, makePptx, jpegInfo } from './doc.js'
65
69
  import { createBrowser } from './browser.js'
@@ -979,6 +983,20 @@ export function apply(ctx) {
979
983
  }, 'dsh-design-studio: headless browser')
980
984
  const phone = createPhone({ upstream: (session) => tracker.snapshot(session).origin || '', log })
981
985
  ctx.effect(() => () => phone.stopAll(), 'dsh-design-studio: phone links')
986
+ // 公网链接(#27):只放整套 HTML 演示,每次有人打开按现在的文件打包(文件没改就用上次的)
987
+ const pub = createPublicShare({
988
+ build: (p) => {
989
+ const { pack, files } = packDesign(p.root, 0, p)
990
+ return { html: pack.html, deps: files }
991
+ },
992
+ // 这个会话换了设计(或关了):关掉,别把不在眼前的设计一直开在公网上(插件自己每 5 秒看一眼,不靠右侧栏开着)
993
+ alive: (session, p) => {
994
+ const t = tracker.stateOf(session).target
995
+ return !!(t && t.kind === 'file' && t.root === p.root)
996
+ },
997
+ log,
998
+ })
999
+ ctx.effect(() => () => { pub.stopAll(); pub.cancelTool() }, 'dsh-design-studio: public links')
982
1000
 
983
1001
  // ── 接口 ──────────────────────────────────────────────────────────────────
984
1002
 
@@ -1047,7 +1065,7 @@ export function apply(ctx) {
1047
1065
  let tweaks = null
1048
1066
  if (t && t.kind === 'file') { try { tweaks = readTweaks(t.root) } catch (e) { log(e) } }
1049
1067
  const snap = tracker.snapshot(session)
1050
- json(res, 200, { ...snap, impl: implStore.list(implRoot(snap)), strict: strictScreens(snap), phone: { on: ph.on, seen: ph.seen || 0 }, tweaks, ...annotsField(session, new URL(req.url, 'http://x').searchParams.get('an')), ...(c ? { checkReq: c.id } : {}) })
1068
+ json(res, 200, { ...snap, impl: implStore.list(implRoot(snap)), strict: strictScreens(snap), phone: { on: ph.on, seen: ph.seen || 0 }, pub: pub.status(session, { brief: true }), tweaks, ...annotsField(session, new URL(req.url, 'http://x').searchParams.get('an')), ...(c ? { checkReq: c.id } : {}) })
1051
1069
  }
1052
1070
 
1053
1071
  /** 正在看的几屏里,哪些是别人做的、没改过的(地址列表):右侧栏给它们的 iframe 加沙盒、不给剪贴板 */
@@ -1291,7 +1309,8 @@ export function apply(ctx) {
1291
1309
  // 输入框里写的相对路径按会话目录算(新会话也问 DSH 要)
1292
1310
  const target = parseTarget(body.target, await sessionCwd(session))
1293
1311
  await tracker.setTarget(session, target, { source: 'user', popup: false })
1294
- json(res, 200, { ...tracker.snapshot(session), ...annotsField(session) })
1312
+ // 换了设计,公网链接马上关(右侧栏要马上知道,不等下一次刷新)
1313
+ json(res, 200, { ...tracker.snapshot(session), ...annotsField(session), pub: pub.status(session, { brief: true }) })
1295
1314
  } catch (e) {
1296
1315
  json(res, 400, { error: e.message })
1297
1316
  }
@@ -1308,7 +1327,7 @@ export function apply(ctx) {
1308
1327
  for (const k of ['kind', 'size', 'view', 'focus']) if (typeof body[k] === 'string') patch[k] = body[k]
1309
1328
  try {
1310
1329
  await tracker.setView(session, patch)
1311
- json(res, 200, { ...tracker.snapshot(session), ...annotsField(session) })
1330
+ json(res, 200, { ...tracker.snapshot(session), ...annotsField(session), pub: pub.status(session, { brief: true }) })
1312
1331
  } catch (e) {
1313
1332
  json(res, 400, { error: e.message })
1314
1333
  }
@@ -1321,6 +1340,7 @@ export function apply(ctx) {
1321
1340
  try { body = await readBody(req) } catch (e) { return json(res, 400, { error: e.message }) }
1322
1341
  tracker.clear(String(body.session || ''))
1323
1342
  phone.stop(String(body.session || ''))
1343
+ pub.stop(String(body.session || ''))
1324
1344
  json(res, 200, tracker.snapshot(String(body.session || '')))
1325
1345
  }
1326
1346
 
@@ -1659,21 +1679,8 @@ export function apply(ctx) {
1659
1679
  saveExport(res, String(body.session || ''), body.name, format, buf)
1660
1680
  }
1661
1681
 
1662
- /**
1663
- * 整套 HTML 演示(#26):本地文件的设计,所有屏打进一个 .html(见 htmlpack.js),存进 exports/。
1664
- * 屏、版本、画幅、深浅色、外壳、调节滑块还没存的值都按右侧栏眼前的来;不截图,几秒出来
1665
- */
1666
- async function htmlPackRoute(req, res) {
1667
- if (!admit(req, res)) return
1668
- if (req.method !== 'POST') return json(res, 405, { error: 'method not allowed' })
1669
- let body
1670
- try { body = await readBody(req, 1024 * 1024) } catch (e) { return json(res, 400, { error: e.message }) }
1671
- const session = String(body.session || '')
1672
- const st = tracker.stateOf(session)
1673
- const t = st && st.target
1674
- if (!t || t.kind !== 'file') return json(res, 400, { error: 'not-file' })
1675
- if (targetMoved(res, session, body.target)) return
1676
- const v = Math.max(0, Math.floor(Number(body.v) || 0))
1682
+ /** 整套 HTML 演示的参数(导出和公网链接共用):挑掉不合法、重复的屏;画幅、深浅色、调节值按右侧栏给的。没有屏给 null */
1683
+ function packParams(body) {
1677
1684
  const okKey = (k) => typeof k === 'string' && /^[^\\]+\.html?$/i.test(k) && !k.split('/').some((p) => !p || p === '.' || p === '..')
1678
1685
  const seen = new Set()
1679
1686
  const screens = []
@@ -1682,33 +1689,62 @@ export function apply(ctx) {
1682
1689
  seen.add(s.key)
1683
1690
  screens.push({ key: s.key, label: String(s.label || s.key).slice(0, 80), top: s.top === true })
1684
1691
  }
1685
- if (!screens.length) return json(res, 400, { error: 'noScreens' })
1686
- const kind = typeof body.kind === 'string' ? body.kind : 'web'
1692
+ if (!screens.length) return null
1687
1693
  const dim = (x, d) => (Number.isFinite(+x) && +x >= 100 && +x <= 8000 ? Math.round(+x) : d)
1688
- const w = dim(body.w, 1280), h = dim(body.h, 800)
1694
+ const tw = body.tweak && typeof body.tweak === 'object' ? body.tweak : null
1695
+ const plain = (o) => Object.fromEntries(Object.entries(o && typeof o === 'object' ? o : {}).filter(([k, x]) => typeof k === 'string' && k.length < 80 && (typeof x === 'string' || typeof x === 'number' || x === null || typeof x === 'boolean')).slice(0, 200))
1696
+ return {
1697
+ screens,
1698
+ kind: typeof body.kind === 'string' ? body.kind : 'web',
1699
+ w: dim(body.w, 1280),
1700
+ h: dim(body.h, 800),
1701
+ os: body.os === 'win' ? 'win' : '',
1702
+ dark: body.dark === true,
1703
+ scheme: body.scheme === 'dark' ? 'dark' : 'light',
1704
+ tweak: tw ? { vars: plain(tw.vars), attrs: plain(tw.attrs) } : null,
1705
+ title: String(body.title || '').slice(0, 120),
1706
+ lang: body.lang === 'en' ? 'en' : 'zh',
1707
+ fit: body.fit === true,
1708
+ }
1709
+ }
1710
+
1711
+ /** 打包一个设计目录(v 是历史版本,0 = 现在的文件):deps 是读过的相对路径,files 是对应的文件(公网链接按它们判断要不要重新打包) */
1712
+ function packDesign(root, v, params) {
1689
1713
  const deps = new Set()
1714
+ const files = new Set()
1690
1715
  const read = (rel) => {
1691
1716
  deps.add(rel)
1692
- const f = bundleFile(t.root, v, rel)
1717
+ const f = bundleFile(root, v, rel)
1718
+ files.add(f || path.join(root, ...rel.split('/')))
1693
1719
  if (!f) return null
1694
1720
  const s = fs.statSync(f)
1695
1721
  return s.isFile() && s.size <= BUNDLE_MAX ? fs.readFileSync(f) : null
1696
1722
  }
1697
- const strictOf = (page) => library.strictHtml(page) || market.strictHtml(page) || (v > 0 && library.strictVersions(t.root))
1698
- const tw = body.tweak && typeof body.tweak === 'object' ? body.tweak : null
1699
- const plain = (o) => Object.fromEntries(Object.entries(o && typeof o === 'object' ? o : {}).filter(([k, x]) => typeof k === 'string' && k.length < 80 && (typeof x === 'string' || typeof x === 'number' || x === null || typeof x === 'boolean')).slice(0, 200))
1700
- let pack
1723
+ const strictOf = (page) => library.strictHtml(page) || market.strictHtml(page) || (v > 0 && library.strictVersions(root))
1724
+ const pack = buildPack({ ...params, read, strictOf })
1725
+ return { pack, deps, files: [...files] }
1726
+ }
1727
+
1728
+ /**
1729
+ * 整套 HTML 演示(#26):本地文件的设计,所有屏打进一个 .html(见 htmlpack.js),存进 exports/。
1730
+ * 屏、版本、画幅、深浅色、外壳、调节滑块还没存的值都按右侧栏眼前的来;不截图,几秒出来
1731
+ */
1732
+ async function htmlPackRoute(req, res) {
1733
+ if (!admit(req, res)) return
1734
+ if (req.method !== 'POST') return json(res, 405, { error: 'method not allowed' })
1735
+ let body
1736
+ try { body = await readBody(req, 1024 * 1024) } catch (e) { return json(res, 400, { error: e.message }) }
1737
+ const session = String(body.session || '')
1738
+ const st = tracker.stateOf(session)
1739
+ const t = st && st.target
1740
+ if (!t || t.kind !== 'file') return json(res, 400, { error: 'not-file' })
1741
+ if (targetMoved(res, session, body.target)) return
1742
+ const v = Math.max(0, Math.floor(Number(body.v) || 0))
1743
+ const params = packParams(body)
1744
+ if (!params) return json(res, 400, { error: 'noScreens' })
1745
+ let pack, deps
1701
1746
  try {
1702
- pack = buildPack({
1703
- screens, read, strictOf, kind, w, h,
1704
- os: body.os === 'win' ? 'win' : '',
1705
- dark: body.dark === true,
1706
- scheme: body.scheme === 'dark' ? 'dark' : 'light',
1707
- tweak: tw ? { vars: plain(tw.vars), attrs: plain(tw.attrs) } : null,
1708
- title: String(body.title || '').slice(0, 120),
1709
- lang: body.lang === 'en' ? 'en' : 'zh',
1710
- fit: body.fit === true,
1711
- })
1747
+ ;({ pack, deps } = packDesign(t.root, v, params))
1712
1748
  } catch (e) {
1713
1749
  return json(res, e.code ? 400 : 500, { error: e.code || e.message })
1714
1750
  }
@@ -1791,6 +1827,44 @@ export function apply(ctx) {
1791
1827
  json(res, 200, st.on ? { ...st, qr: qrMatrix(st.url).map((row) => row.map((c) => (c ? 1 : 0)).join('')) } : st)
1792
1828
  }
1793
1829
 
1830
+ /**
1831
+ * 公网链接(#27,实验):{ session, on: true, …整套演示的参数 } 开(开着就换参数),{ on: false } 关,
1832
+ * { peek: true } 只看状态(连上了带二维码)。只能右侧栏里人点开,不给模型工具
1833
+ */
1834
+ async function pubShareRoute(req, res) {
1835
+ if (!admit(req, res)) return
1836
+ if (req.method !== 'POST') return json(res, 405, { error: 'method not allowed' })
1837
+ let body
1838
+ try { body = await readBody(req, 1024 * 1024) } catch (e) { return json(res, 400, { error: e.message }) }
1839
+ const session = String(body.session || '')
1840
+ if (!session) return json(res, 400, { error: '缺 session' })
1841
+ const withQr = (x) => (x.on && x.url ? { ...x, qr: qrMatrix(x.url).map((row) => row.map((c) => (c ? 1 : 0)).join('')) } : x)
1842
+ if (body.on === false) {
1843
+ pub.stop(session)
1844
+ return json(res, 200, { ...pub.status(session), tool: pub.hasTool() })
1845
+ }
1846
+ // 看状态:顺带说本机有没有 cloudflared(没有的话右侧栏先请人下载)
1847
+ if (body.peek === true) return json(res, 200, { ...withQr(pub.status(session)), tool: pub.hasTool() })
1848
+ // 人点了「下载」:从 Cloudflare 官方 GitHub 下到插件自己的目录,进度在 peek 里;「取消」停下
1849
+ if (body.download === true || body.cancel === true) {
1850
+ if (body.cancel === true) pub.cancelTool()
1851
+ else pub.fetchTool()
1852
+ return json(res, 200, { ...pub.status(session), tool: pub.hasTool() })
1853
+ }
1854
+ const t = tracker.stateOf(session).target
1855
+ if (!t || t.kind !== 'file') return json(res, 400, { error: 'not-file' })
1856
+ if (targetMoved(res, session, body.target)) return
1857
+ const params = packParams(body)
1858
+ if (!params) return json(res, 400, { error: 'noScreens' })
1859
+ const p = { root: t.root, ...params }
1860
+ // 右侧栏换了屏、画幅、深浅色:开着就跟着换,没开不新开(开要人点)
1861
+ if (body.update === true) return json(res, 200, { ok: pub.update(session, p) })
1862
+ const r = await pub.start(session, p, { name: params.title || path.basename(t.root) })
1863
+ // 没开起来的原因(没装 cloudflared、打包不出来)start 直接给;开着的照状态给
1864
+ const now = pub.status(session)
1865
+ json(res, 200, { ...withQr(now.on ? now : { ...now, ...r }), tool: pub.hasTool() })
1866
+ }
1867
+
1794
1868
  // 设计库:我的作品、灵感、打 zip、拖 zip 进来用
1795
1869
  const inspiration = createInspiration({ log })
1796
1870
  /**
@@ -1835,7 +1909,7 @@ export function apply(ctx) {
1835
1909
  })
1836
1910
  const mk = createMarketRoutes({ admit, readBody, json, tracker, library, market, open, systemsDir: SYSTEMS_DIR, log, sessionCwd })
1837
1911
 
1838
- const routes = { works: lib.works, workthumb: lib.workthumb, scrthumb: canvasThumbs.route, pack: lib.pack, usezip: lib.usezip, ghauth: lib.ghauth, submit: lib.submit, inspire: lib.inspire, inspimg: lib.inspimg, ...mk, state, open: openRoute, view: viewRoute, close: closeRoute, history: historyRoute, restore: restoreRoute, edit: editRoute, style: styleRoute, bundle: bundleRoute, ref: refRoute, export: exportRoute, doc: docRoute, htmlpack: htmlPackRoute, reveal: revealRoute, phone: phoneRoute, check: checkRoute, locate: locateRoute, tweak: tweakRoute, systems: systemsRoute, frames: framesRoute, systhumb: systhumbRoute, project: projectRoute, impl: implRoute, implcmp: implCmpRoute, implshot: implShotRoute }
1912
+ const routes = { works: lib.works, workthumb: lib.workthumb, scrthumb: canvasThumbs.route, pack: lib.pack, usezip: lib.usezip, ghauth: lib.ghauth, submit: lib.submit, inspire: lib.inspire, inspimg: lib.inspimg, ...mk, state, open: openRoute, view: viewRoute, close: closeRoute, history: historyRoute, restore: restoreRoute, edit: editRoute, style: styleRoute, bundle: bundleRoute, ref: refRoute, export: exportRoute, doc: docRoute, htmlpack: htmlPackRoute, pubshare: pubShareRoute, reveal: revealRoute, phone: phoneRoute, check: checkRoute, locate: locateRoute, tweak: tweakRoute, systems: systemsRoute, frames: framesRoute, systhumb: systhumbRoute, project: projectRoute, impl: implRoute, implcmp: implCmpRoute, implshot: implShotRoute }
1839
1913
  for (const [key, handler] of Object.entries(routes)) {
1840
1914
  ctx.effect(() => ctx.webServer.register({
1841
1915
  kind: 'exact',
@@ -1859,6 +1933,15 @@ export function apply(ctx) {
1859
1933
  log(e)
1860
1934
  }
1861
1935
  })
1936
+ // 会话归档了:它的公网链接马上关(面板进不去了,别让链接一直开到点)。老版本 DSH 没有这个事件,就只是收不到
1937
+ ctx.on('workspace/session-archived', (req) => {
1938
+ try {
1939
+ const id = req && req.sessionId
1940
+ if (id) pub.stop(String(id))
1941
+ } catch (e) {
1942
+ log(e)
1943
+ }
1944
+ }, { global: true })
1862
1945
  // 模型边说边推的流:写大文件时参数要流好几分钟,靠它提前知道在写哪个文件(老版本 DSH 没有这个事件,就只是收不到)
1863
1946
  ctx.on('agent/assistant-stream', (e) => {
1864
1947
  try {