dsh-cad-studio 0.8.3 → 0.8.4

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.
Files changed (2) hide show
  1. package/index.js +83 -25
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -783,10 +783,27 @@ class FreeCadBridge {
783
783
  * otherwise only removable by editing state.json by hand — which is exactly
784
784
  * how a 28-row list happened in the first place.
785
785
  */
786
- async removeScene(id) {
786
+ async removeScene(id, opts = {}) {
787
787
  const key = String(id ?? '')
788
788
  const row = this.history.find((h) => h.id === key)
789
789
  if (!row) throw new Error(`没有这个场景:${key}(可用 ${this.history.map((h) => h.id).join(', ') || '(无)'})`)
790
+ const snapshot = JSON.parse(JSON.stringify(row))
791
+ let bytes = null
792
+ if (opts.journal !== false) {
793
+ try { bytes = await readFile(join(FreeCadBridge.dataDir, 'scenes', `${key}.json`)) } catch {}
794
+ this.journal({
795
+ kind: 'scene',
796
+ label: `delete ${key}`,
797
+ undo: async () => {
798
+ this.history = [snapshot, ...this.history.filter((h) => h.id !== key)]
799
+ if (bytes) await writeFile(join(FreeCadBridge.dataDir, 'scenes', `${key}.json`), bytes)
800
+ this.latest = { id: snapshot.id, title: snapshot.title, at: snapshot.at, scene: this.scenes.get(key) ?? null }
801
+ await this.persist()
802
+ return { scene: key }
803
+ },
804
+ redo: async () => { await this.removeScene(key, { journal: false }); return { scene: key } },
805
+ })
806
+ }
790
807
  this.history = this.history.filter((h) => h.id !== key)
791
808
  this.scenes.delete(key)
792
809
  for (const [asmId, rec] of [...this.assemblies]) if (rec.sceneId === key) this.assemblies.delete(asmId)
@@ -799,12 +816,21 @@ class FreeCadBridge {
799
816
  return { ok: true, removed: key, scenes: this.history.length, latest: this.latest ? this.latest.id : '' }
800
817
  }
801
818
 
802
- async renameScene(id, title) {
819
+ async renameScene(id, title, opts = {}) {
803
820
  const key = String(id ?? '')
804
821
  const row = this.history.find((h) => h.id === key)
805
822
  if (!row) throw new Error(`没有这个场景:${key}`)
806
823
  const name = String(title ?? '').trim()
807
824
  if (!name) throw new Error('新名字不能为空')
825
+ const old = row.title
826
+ if (opts.journal !== false && old !== name) {
827
+ this.journal({
828
+ kind: 'scene',
829
+ label: `rename ${key}`,
830
+ undo: async () => { await this.renameScene(key, old, { journal: false }); return { scene: key, title: old } },
831
+ redo: async () => { await this.renameScene(key, name, { journal: false }); return { scene: key, title: name } },
832
+ })
833
+ }
808
834
  row.title = name
809
835
  if (this.latest && this.latest.id === key) this.latest = { ...this.latest, title: name }
810
836
  await this.persist()
@@ -877,7 +903,18 @@ class FreeCadBridge {
877
903
 
878
904
  endAssemblyEdit(entry, record) {
879
905
  if (!entry) return
880
- entry.after = this.snapshotAssembly(record)
906
+ const before = entry.before
907
+ const after = this.snapshotAssembly(record)
908
+ this.journal({
909
+ kind: 'assembly',
910
+ label: entry.label,
911
+ undo: async () => this.applyAssemblySnapshot(before),
912
+ redo: async () => this.applyAssemblySnapshot(after),
913
+ })
914
+ }
915
+
916
+ /** One stack for every undoable mutation, in the order it happened. */
917
+ journal(entry) {
881
918
  this.sessionUndo.push({ at: new Date().toISOString(), ...entry })
882
919
  if (this.sessionUndo.length > 50) this.sessionUndo.shift()
883
920
  this.sessionRedo = []
@@ -892,20 +929,24 @@ class FreeCadBridge {
892
929
  return { record, published }
893
930
  }
894
931
 
895
- async undoSession() {
896
- const entry = this.sessionUndo.pop()
932
+ async undoSession(kind) {
933
+ const stack = kind ? this.sessionUndo.filter((e) => e.kind === kind) : this.sessionUndo
934
+ const entry = stack[stack.length - 1]
897
935
  if (!entry) return null
898
- const applied = await this.applyAssemblySnapshot(entry.before)
936
+ this.sessionUndo = this.sessionUndo.filter((e) => e !== entry)
937
+ const out = await entry.undo()
899
938
  this.sessionRedo.push(entry)
900
- return { label: entry.label, assembly: entry.assembly, sceneId: applied.published.id }
939
+ return { label: entry.label, kind: entry.kind, ...(out ?? {}) }
901
940
  }
902
941
 
903
- async redoSession() {
904
- const entry = this.sessionRedo.pop()
905
- if (!entry || !entry.after) return null
906
- const applied = await this.applyAssemblySnapshot(entry.after)
942
+ async redoSession(kind) {
943
+ const stack = kind ? this.sessionRedo.filter((e) => e.kind === kind) : this.sessionRedo
944
+ const entry = stack[stack.length - 1]
945
+ if (!entry) return null
946
+ this.sessionRedo = this.sessionRedo.filter((e) => e !== entry)
947
+ const out = await entry.redo()
907
948
  this.sessionUndo.push(entry)
908
- return { label: entry.label, assembly: entry.assembly, sceneId: applied.published.id }
949
+ return { label: entry.label, kind: entry.kind, ...(out ?? {}) }
909
950
  }
910
951
 
911
952
  /** Recompute instance placements from base + joints + explode, then publish. */
@@ -1714,18 +1755,17 @@ __out = {
1714
1755
  parameters: {
1715
1756
  type: 'object',
1716
1757
  properties: {
1717
- scope: { type: 'string', enum: ['auto', 'assembly', 'doc'], description: 'default auto' },
1758
+ scope: { type: 'string', enum: ['auto', 'assembly', 'scene', 'doc'], description: 'default auto' },
1718
1759
  model: { type: 'string', description: 'document name, for doc scope' },
1719
1760
  },
1720
1761
  },
1721
1762
  output: { schema: { type: 'object' }, render: (args, value) => [{ type: 'text', text: renderUndo(value) }] },
1722
1763
  async execute(args, exec) {
1723
1764
  const scope = typeof args.scope === 'string' ? args.scope : 'auto'
1724
- if (scope !== 'doc' && bridge.sessionUndo.length) {
1725
- const undone = await bridge.undoSession()
1726
- return { ok: true, scope: 'assembly', undone: undone.label, assembly: undone.assembly, sceneId: undone.sceneId, remaining: bridge.sessionUndo.length, redo: bridge.sessionRedo.length }
1727
- }
1728
- if (scope === 'assembly') return { ok: false, error: '装配层没有可撤销的操作' }
1765
+ const kind = scope === 'auto' ? undefined : scope
1766
+ const undone = await bridge.undoSession(kind)
1767
+ if (undone) return { ok: true, scope: undone.kind, undone: undone.label, assembly: undone.assembly, scene: undone.scene, model: undone.model, remaining: bridge.sessionUndo.length, redo: bridge.sessionRedo.length }
1768
+ if (scope !== 'doc') return { ok: false, error: scope === 'assembly' ? '装配层没有可撤销的操作' : scope === 'scene' ? '场景列表没有可撤销的操作' : '没有可撤销的操作' }
1729
1769
  const target = typeof args.model === 'string' && args.model ? args.model : ''
1730
1770
  if (!target) return { ok: false, error: '文档撤销需要 model 名称(可用 cad_studio_doc list 查看)' }
1731
1771
  try {
@@ -1743,18 +1783,17 @@ __out = {
1743
1783
  parameters: {
1744
1784
  type: 'object',
1745
1785
  properties: {
1746
- scope: { type: 'string', enum: ['auto', 'assembly', 'doc'], description: 'default auto' },
1786
+ scope: { type: 'string', enum: ['auto', 'assembly', 'scene', 'doc'], description: 'default auto' },
1747
1787
  model: { type: 'string', description: 'document name, for doc scope' },
1748
1788
  },
1749
1789
  },
1750
1790
  output: { schema: { type: 'object' }, render: (args, value) => [{ type: 'text', text: renderUndo(value) }] },
1751
1791
  async execute(args, exec) {
1752
1792
  const scope = typeof args.scope === 'string' ? args.scope : 'auto'
1753
- if (scope !== 'doc' && bridge.sessionRedo.length) {
1754
- const redone = await bridge.redoSession()
1755
- return { ok: true, scope: 'assembly', redone: redone.label, assembly: redone.assembly, sceneId: redone.sceneId, remaining: bridge.sessionRedo.length, undo: bridge.sessionUndo.length }
1756
- }
1757
- if (scope === 'assembly') return { ok: false, error: '装配层没有可重做的操作' }
1793
+ const kindR = scope === 'auto' ? undefined : scope
1794
+ const redone = await bridge.redoSession(kindR)
1795
+ if (redone) return { ok: true, scope: redone.kind, redone: redone.label, assembly: redone.assembly, scene: redone.scene, model: redone.model, remaining: bridge.sessionRedo.length, undo: bridge.sessionUndo.length }
1796
+ if (scope !== 'doc') return { ok: false, error: scope === 'assembly' ? '装配层没有可重做的操作' : '没有可重做的操作' }
1758
1797
  const target = typeof args.model === 'string' && args.model ? args.model : ''
1759
1798
  if (!target) return { ok: false, error: '文档重做需要 model 名称' }
1760
1799
  try {
@@ -3256,7 +3295,26 @@ __out = {"ok": True, "exported": ${JSON.stringify(target)}, "volume": round(scen
3256
3295
 
3257
3296
  if (action === 'delete') {
3258
3297
  const key = checkName(args.name)
3259
- await unlink(modelFile(key, dir)).catch(() => { throw new Error(`没有这个模型:${key}`) })
3298
+ const file = modelFile(key, dir)
3299
+ let bytes = null
3300
+ try { bytes = await readFile(file) } catch { throw new Error(`没有这个模型:${key}`) }
3301
+ const sceneRow = bridge.history.find((h) => h.title === key || (h.docKey === `model:${key}`))
3302
+ bridge.journal({
3303
+ kind: 'doc',
3304
+ label: `delete ${key}`,
3305
+ undo: async () => {
3306
+ await writeFile(file, bytes)
3307
+ const doc = await readModel(key, dir)
3308
+ try { await rebuild(doc, dir, { note: 'undo delete' }) } catch {}
3309
+ return { model: key }
3310
+ },
3311
+ redo: async () => {
3312
+ await unlink(file).catch(() => {})
3313
+ if (sceneRow) { try { await bridge.removeScene(sceneRow.id, { journal: false }) } catch {} }
3314
+ return { model: key }
3315
+ },
3316
+ })
3317
+ await unlink(file).catch(() => { throw new Error(`没有这个模型:${key}`) })
3260
3318
  return { ok: true, name: key, deleted: true, dir }
3261
3319
  }
3262
3320
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-cad-studio",
3
- "version": "0.8.3",
3
+ "version": "0.8.4",
4
4
  "type": "module",
5
5
  "description": "CAD Studio — FreeCAD 桥(给 AI 的 14 个工具)+ Web 右栏 3D 面板:建模/装配/关节运动/出图/多页签查看",
6
6
  "exports": {