jvs-ppt-ui 0.0.34 → 0.0.36

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.
@@ -1349,7 +1349,12 @@ function applyPresetTheme(preset: PresetTheme) {
1349
1349
  el.textColor = preset.fontColor
1350
1350
  el.color = preset.fontColor
1351
1351
  if (el.content) {
1352
- el.content = el.content.replace(/color\s*:\s*[^;"]+;?/gi, '').replace(/(?:--ppt-)?font-family\s*:\s*[^;"]+;?/gi, '')
1352
+ el.content = el.content
1353
+ .replace(/color\s*:\s*[^;"]+;?/gi, '')
1354
+ .replace(/(?:--ppt-)?font-family\s*:\s*[^;"]+;?/gi, '')
1355
+ .replace(/\s*style="\s*;?\s*"/gi, '')
1356
+ .replace(/style="\s+/gi, 'style="')
1357
+ .replace(/\s+"/gi, '"')
1353
1358
  }
1354
1359
  }
1355
1360
  if (el.type === 'shape') {
@@ -1597,7 +1602,12 @@ export function recolorElementsWithRecipe(elements: any[], recipe: StyleRecipe)
1597
1602
  if (cfg.letterSpacing !== undefined) el.style.letterSpacing = `${cfg.letterSpacing}px`
1598
1603
 
1599
1604
  if (el.content) {
1600
- el.content = el.content.replace(/color\s*:\s*[^;"]+;?/gi, '').replace(/(?:--ppt-)?font-family\s*:\s*[^;"]+;?/gi, '')
1605
+ el.content = el.content
1606
+ .replace(/color\s*:\s*[^;"]+;?/gi, '')
1607
+ .replace(/(?:--ppt-)?font-family\s*:\s*[^;"]+;?/gi, '')
1608
+ .replace(/\s*style="\s*;?\s*"/gi, '')
1609
+ .replace(/style="\s+/gi, 'style="')
1610
+ .replace(/\s+"/gi, '"')
1601
1611
  }
1602
1612
 
1603
1613
  // 处理超链接元素或 <a> 标签的链接颜色与下划线 (linkUnderline)
@@ -1659,20 +1669,34 @@ export function recolorElementsWithRecipe(elements: any[], recipe: StyleRecipe)
1659
1669
  el.style.textColor = textColor
1660
1670
  if (el.text && typeof el.text === 'object') {
1661
1671
  el.text.defaultColor = textColor
1672
+ if (typeof el.text.content === 'string' && el.text.content.includes('<')) {
1673
+ const div = document.createElement('div')
1674
+ div.innerHTML = el.text.content
1675
+ div.querySelectorAll('div, span, p, font, td, th').forEach((child: any) => {
1676
+ child.style.color = textColor
1677
+ child.style.removeProperty('font-family')
1678
+ child.style.removeProperty('--ppt-font-family')
1679
+ })
1680
+ el.text.content = div.innerHTML
1681
+ }
1662
1682
  }
1663
1683
  if (el.text && typeof el.text === 'string' && el.text.includes('<')) {
1664
1684
  const div = document.createElement('div')
1665
1685
  div.innerHTML = el.text
1666
- div.querySelectorAll('span, p, font, td, th').forEach((child: any) => {
1686
+ div.querySelectorAll('div, span, p, font, td, th').forEach((child: any) => {
1667
1687
  child.style.color = textColor
1688
+ child.style.removeProperty('font-family')
1689
+ child.style.removeProperty('--ppt-font-family')
1668
1690
  })
1669
1691
  el.text = div.innerHTML
1670
1692
  }
1671
1693
  if (el.content && typeof el.content === 'string' && el.content.includes('<')) {
1672
1694
  const div = document.createElement('div')
1673
1695
  div.innerHTML = el.content
1674
- div.querySelectorAll('span, p, font, td, th').forEach((child: any) => {
1696
+ div.querySelectorAll('div, span, p, font, td, th').forEach((child: any) => {
1675
1697
  child.style.color = textColor
1698
+ child.style.removeProperty('font-family')
1699
+ child.style.removeProperty('--ppt-font-family')
1676
1700
  })
1677
1701
  el.content = div.innerHTML
1678
1702
  }
@@ -2000,6 +2024,9 @@ function applyStyleRecipeToSlides(
2000
2024
  return
2001
2025
  }
2002
2026
 
2027
+ // 在修改幻灯片与母版前,先将此前尚未落盘的防抖编辑(如刚输入的文字、刚插入的幻灯片)独立落盘,保证本次主题切换是单一原子操作
2028
+ flushPendingHistorySnapshot()
2029
+
2003
2030
  let targetSlides: Slide[] = []
2004
2031
  if (effectiveMode === 'all') {
2005
2032
  targetSlides = slides.value
@@ -2094,9 +2121,11 @@ function applyStyleRecipeToSlides(
2094
2121
  globalThemeColor.value = [...recipe.styleColors]
2095
2122
  }
2096
2123
 
2097
- // 同 tick 多页变更 + 急切快照:非 single 模式涉及多页,显式全量标脏(字符串比对会自动去重)
2098
- if (effectiveMode !== 'single') markAllPagesDirty()
2099
- addHistorySnapshot()
2124
+ // 同 tick 多页与母版变更 + 急切快照:single 模式同样可能锁定其余幻灯片默认主题并同步当前母版,显式全量标脏(字符串比对会自动去重)
2125
+ markAllPagesDirty()
2126
+ for (let i = 0; i < slideMasters.value.length; i++) dirtyMasterIndexes.add(i)
2127
+ addHistorySnapshot(true)
2128
+ syncDOMWithRestoredSlides()
2100
2129
  if (effectiveMode === 'all') {
2101
2130
  notify(`已应用主题「${recipe.name}」到全部幻灯片与母版`)
2102
2131
  } else if (effectiveMode === 'single') {
@@ -2385,6 +2414,7 @@ const slides = ref<Slide[]>([
2385
2414
  background: '#ffffff',
2386
2415
  bgType: 'color',
2387
2416
  elements: [],
2417
+ animations: [],
2388
2418
  notes: '',
2389
2419
  masterId: 'master-default',
2390
2420
  layoutId: 'layout-title'
@@ -2402,14 +2432,16 @@ watch(activeIndex, (newIdx) => {
2402
2432
  if (!selectedSlideIndexes.value.includes(newIdx)) {
2403
2433
  selectedSlideIndexes.value = [newIdx]
2404
2434
  }
2435
+ if (isUndoingRedoing.value) return
2405
2436
  const curSlide = slides.value[newIdx]
2406
2437
  if (curSlide?.themeId && !isMasterEditingView.value) {
2407
2438
  const resolved = resolveSlideMasterAndLayout(curSlide)
2408
2439
  const master = slideMasters.value.find(m => m.id === resolved.masterId) || slideMasters.value[0]
2409
- if (master && !(master as any).isImported && (master as any).themeId !== curSlide.themeId) {
2410
- const targetRecipe = getActiveSlideThemeRecipe(curSlide)
2411
- if (targetRecipe) {
2412
- applyStyleRecipeToMaster(targetRecipe, 'single', master.id, true)
2440
+ const matchedRecipe = builtinStyleRecipes.find(r => r.id === curSlide.themeId)
2441
+ if (master && !(master as any).isImported && matchedRecipe && (master as any).themeId !== curSlide.themeId) {
2442
+ const hasOtherSlidesOnMaster = slides.value.some((s, idx) => idx !== newIdx && (s.masterId || 'master-default') === master.id && s.themeId !== curSlide.themeId)
2443
+ if (!hasOtherSlidesOnMaster) {
2444
+ applyStyleRecipeToMaster(matchedRecipe, 'single', master.id, true)
2413
2445
  }
2414
2446
  }
2415
2447
  }
@@ -2787,10 +2819,11 @@ function replacePlaceholderElement(
2787
2819
 
2788
2820
  const currentSlideAnimations = computed(() => {
2789
2821
  if (!activeSlide.value) return []
2790
- if (!activeSlide.value.animations) activeSlide.value.animations = []
2822
+ const slideAnims = activeSlide.value.animations
2823
+ if (!Array.isArray(slideAnims) || slideAnims.length === 0) return []
2791
2824
 
2792
2825
  const elIds = activeSlide.value.elements.map(el => el.id)
2793
- return activeSlide.value.animations.filter(animation => elIds.includes(animation.elId))
2826
+ return slideAnims.filter(animation => elIds.includes(animation.elId))
2794
2827
  })
2795
2828
 
2796
2829
  interface FormatedAnimation {
@@ -2804,10 +2837,11 @@ function isTriggerAnimation(animation: PPTAnimation): boolean {
2804
2837
 
2805
2838
  const formatedAnimations = computed(() => {
2806
2839
  if (!activeSlide.value) return []
2807
- if (!activeSlide.value.animations) activeSlide.value.animations = []
2840
+ const slideAnims = activeSlide.value.animations
2841
+ if (!Array.isArray(slideAnims) || slideAnims.length === 0) return []
2808
2842
 
2809
2843
  const elIds = activeSlide.value.elements.map(el => el.id)
2810
- const animations = activeSlide.value.animations.filter(animation => elIds.includes(animation.elId) && !isTriggerAnimation(animation))
2844
+ const animations = slideAnims.filter(animation => elIds.includes(animation.elId) && !isTriggerAnimation(animation))
2811
2845
 
2812
2846
  const formatted: FormatedAnimation[] = []
2813
2847
  for (const animation of animations) {
@@ -3002,11 +3036,37 @@ function notify(msg: string) {
3002
3036
  }
3003
3037
 
3004
3038
  // 历史栈按页存 JSON 字符串:每条快照是 string[](每页一条),未变页的字符串引用跨快照共享,
3005
- // 编辑只序列化脏页、撤销只解析变更页;每条快照对应的激活页仍记在平行数组中,翻页只改一个数字
3039
+ // 编辑只序列化脏页、撤销只解析变更页;每条快照对应的激活页、母版快照与全局主题状态记在平行数组中
3040
+ interface HistoryGlobalThemeState {
3041
+ fontFamily: string
3042
+ fontColor: string
3043
+ bgColor: string
3044
+ themeColor: string[]
3045
+ }
3046
+
3006
3047
  const historyStack = ref<string[][]>([])
3007
3048
  const historyIndex = ref(-1)
3008
3049
  const isUndoingRedoing = ref(false)
3009
3050
  let historyActiveIndexes: number[] = []
3051
+ let historyMasterSnapshots: string[][] = []
3052
+ let historyGlobalStates: HistoryGlobalThemeState[] = []
3053
+
3054
+ function captureGlobalThemeState(): HistoryGlobalThemeState {
3055
+ return {
3056
+ fontFamily: globalFontFamily.value,
3057
+ fontColor: globalFontColor.value,
3058
+ bgColor: globalBgColor.value,
3059
+ themeColor: Array.isArray(globalThemeColor.value) ? [...globalThemeColor.value] : []
3060
+ }
3061
+ }
3062
+
3063
+ function restoreGlobalThemeState(state?: HistoryGlobalThemeState) {
3064
+ if (!state) return
3065
+ globalFontFamily.value = state.fontFamily
3066
+ globalFontColor.value = state.fontColor
3067
+ globalBgColor.value = state.bgColor
3068
+ globalThemeColor.value = [...state.themeColor]
3069
+ }
3010
3070
 
3011
3071
  const masterHistoryStack = ref<string[][]>([])
3012
3072
  const masterHistoryIndex = ref(-1)
@@ -3117,13 +3177,16 @@ function initHistory() {
3117
3177
 
3118
3178
  currentPageStrs = slides.value.map(serializePage)
3119
3179
  currentPageIds = slides.value.map(s => s && s.id)
3180
+ currentMasterStrs = slideMasters.value.map(serializePage)
3181
+ currentMasterIds = slideMasters.value.map(m => m && m.id)
3182
+
3120
3183
  historyStack.value = [currentPageStrs.slice()]
3121
3184
  historyActiveIndexes = [activeIndex.value]
3185
+ historyMasterSnapshots = [currentMasterStrs.slice()]
3186
+ historyGlobalStates = [captureGlobalThemeState()]
3122
3187
  historyIndex.value = 0
3123
3188
 
3124
3189
  // 母版历史独立初始化(导入/重置演示文稿时同样清空)
3125
- currentMasterStrs = slideMasters.value.map(serializePage)
3126
- currentMasterIds = slideMasters.value.map(m => m && m.id)
3127
3190
  masterHistoryStack.value = [currentMasterStrs.slice()]
3128
3191
  masterHistoryActiveMids = [activeMasterId.value || '']
3129
3192
  masterHistoryActiveLids = [activeLayoutId.value || '']
@@ -3131,31 +3194,65 @@ function initHistory() {
3131
3194
  activeSlideVersion.value++
3132
3195
  }
3133
3196
 
3134
- function syncActiveEditingTextToElement() {
3135
- if (typeof document === 'undefined' || !slides.value) return
3197
+ function normalizeComparableHtml(html: string): string {
3198
+ if (!html) return ''
3199
+ return html
3200
+ .replace(/;?\s*--ppt-font-family\s*:\s*[^;"]+;?/gi, '')
3201
+ .replace(/\s*style="\s*;?\s*"/gi, '')
3202
+ .replace(/style="\s+/gi, 'style="')
3203
+ .replace(/\s+"/gi, '"')
3204
+ .trim()
3205
+ }
3206
+
3207
+ function syncActiveEditingTextToElement(): boolean {
3208
+ if (typeof document === 'undefined' || !slides.value) return false
3136
3209
  const curSlide = slides.value[activeIndex.value]
3137
- if (!curSlide || !curSlide.elements) return
3138
- curSlide.elements.forEach((el: any) => {
3139
- if (el && (el.type === 'text' || el.type === 'shape')) {
3140
- const container = document.getElementById(`editable-${el.id}`)
3141
- const isEditingContainer = Boolean(
3142
- container &&
3143
- (container.getAttribute('contenteditable') === 'true' ||
3144
- (document.activeElement && container.contains(document.activeElement)))
3145
- )
3146
- if (container && isEditingContainer && (!el.content || !el.content.includes('<table'))) {
3147
- const htmlVal = container.innerHTML
3148
- if (el.type === 'shape') {
3149
- // 空 DOM 抓到 '' 时不能给无文本 shape 制造 text:"" 新键:
3150
- // 语义等价但字符串不等,会被当作变更推入幽灵快照,导致 undo 永远撤不到底
3151
- const isEmptyNoop = htmlVal === '' && !el.text
3152
- if (!isEmptyNoop && el.text !== htmlVal) el.text = htmlVal
3153
- } else if (el.content !== htmlVal) {
3154
- el.content = htmlVal
3210
+ if (!curSlide || !curSlide.elements) return false
3211
+ let syncedAny = false
3212
+ const syncList = (elements: any[]) => {
3213
+ elements.forEach((el: any) => {
3214
+ if (!el) return
3215
+ if (el.type === 'group' && Array.isArray(el.elements)) {
3216
+ syncList(el.elements)
3217
+ return
3218
+ }
3219
+ if (el.type === 'text' || el.type === 'shape') {
3220
+ const container = document.getElementById(`editable-${el.id}`)
3221
+ const isEditingContainer = Boolean(
3222
+ container &&
3223
+ (container.getAttribute('contenteditable') === 'true' ||
3224
+ (document.activeElement && (container === document.activeElement || container.contains(document.activeElement))))
3225
+ )
3226
+ if (container && isEditingContainer && (!el.content || !el.content.includes('<table'))) {
3227
+ const htmlVal = container.innerHTML
3228
+ if (htmlVal.includes('color: rgb(153, 153, 153)') && htmlVal.includes('user-select: none')) {
3229
+ return
3230
+ }
3231
+ if (el.type === 'shape') {
3232
+ // 空 DOM 抓到 '' 时不能给无文本 shape 制造 text:"" 新键:
3233
+ // 语义等价但字符串不等,会被当作变更推入幽灵快照,导致 undo 永远撤不到底
3234
+ const isEmptyNoop = htmlVal === '' && !el.text
3235
+ if (!isEmptyNoop) {
3236
+ if (el.text && typeof el.text === 'object') {
3237
+ if (normalizeComparableHtml(el.text.content || '') !== normalizeComparableHtml(htmlVal)) {
3238
+ el.text.content = htmlVal
3239
+ syncedAny = true
3240
+ }
3241
+ } else if (normalizeComparableHtml(typeof el.text === 'string' ? el.text : '') !== normalizeComparableHtml(htmlVal)) {
3242
+ el.text = htmlVal
3243
+ syncedAny = true
3244
+ }
3245
+ }
3246
+ } else if (normalizeComparableHtml(el.content || '') !== normalizeComparableHtml(htmlVal)) {
3247
+ el.content = htmlVal
3248
+ syncedAny = true
3249
+ }
3155
3250
  }
3156
3251
  }
3157
- }
3158
- })
3252
+ })
3253
+ }
3254
+ syncList(curSlide.elements)
3255
+ return syncedAny
3159
3256
  }
3160
3257
 
3161
3258
  function syncDOMWithRestoredSlides() {
@@ -3163,27 +3260,73 @@ function syncDOMWithRestoredSlides() {
3163
3260
  if (typeof document === 'undefined' || !slides.value) return
3164
3261
  const curSlide = slides.value[activeIndex.value]
3165
3262
  if (!curSlide || !curSlide.elements) return
3166
- curSlide.elements.forEach((el: any) => {
3167
- if (el && (el.type === 'text' || el.type === 'shape')) {
3168
- const container = document.getElementById(`editable-${el.id}`)
3169
- if (container && (!el.content || !el.content.includes('<table'))) {
3170
- const freshVal = el.type === 'shape' ? (el.text || '') : (el.content || '')
3171
- container.innerHTML = freshVal
3263
+ const syncList = (elements: any[]) => {
3264
+ elements.forEach((el: any) => {
3265
+ if (!el) return
3266
+ if (el.type === 'group' && Array.isArray(el.elements)) {
3267
+ syncList(el.elements)
3268
+ return
3172
3269
  }
3173
- }
3174
- })
3270
+ if (el.type === 'text' || el.type === 'shape' || el.type === 'table') {
3271
+ const container = document.getElementById(`editable-${el.id}`)
3272
+ if (!container || el.latex) return
3273
+ if (el.type === 'shape') {
3274
+ let rawText = ''
3275
+ if (typeof el.text === 'string') rawText = el.text
3276
+ else if (el.text && typeof el.text === 'object') rawText = el.text.content || el.text.text || ''
3277
+ if (rawText && (/^[Mm]\s*[-+]?\d/.test(rawText.trim()) || rawText === 'rect' || rawText === 'circle' || rawText === 'triangle')) {
3278
+ rawText = ''
3279
+ }
3280
+ const freshVal = normalizeHtmlFontFamily(rawText)
3281
+ if (container.innerHTML !== freshVal) container.innerHTML = freshVal
3282
+ } else {
3283
+ const freshVal = normalizeHtmlFontFamily(el.content || '')
3284
+ if (container.innerHTML !== freshVal) container.innerHTML = freshVal
3285
+ }
3286
+ }
3287
+ })
3288
+ }
3289
+ syncList(curSlide.elements)
3175
3290
  })
3176
3291
  }
3177
3292
 
3178
- function addHistorySnapshot() {
3293
+ let isPostSnapshotMicrotask = false
3294
+ let isPostMasterSnapshotMicrotask = false
3295
+
3296
+ function markPostSnapshotMicrotask() {
3297
+ isPostSnapshotMicrotask = true
3298
+ queueMicrotask(() => {
3299
+ isPostSnapshotMicrotask = false
3300
+ })
3301
+ }
3302
+
3303
+ function markPostMasterSnapshotMicrotask() {
3304
+ isPostMasterSnapshotMicrotask = true
3305
+ queueMicrotask(() => {
3306
+ isPostMasterSnapshotMicrotask = false
3307
+ })
3308
+ }
3309
+
3310
+ function addHistorySnapshot(skipDomSync = false) {
3179
3311
  if (isUndoingRedoing.value || pauseHistory.value) return
3180
- syncActiveEditingTextToElement()
3312
+ if (historySnapshotTimer) {
3313
+ clearTimeout(historySnapshotTimer)
3314
+ historySnapshotTimer = null
3315
+ }
3316
+ if (historyIdleCallbackId && typeof window !== 'undefined' && 'cancelIdleCallback' in window) {
3317
+ window.cancelIdleCallback(historyIdleCallbackId)
3318
+ historyIdleCallbackId = null
3319
+ }
3320
+ if (!skipDomSync) {
3321
+ syncActiveEditingTextToElement()
3322
+ }
3181
3323
  // 连接线吸附是 flush:'post' 的深度 watcher,在手动快照之后才执行:若快照先落盘,
3182
3324
  // 吸附结果会被防抖 watcher 再推一条幽灵条目(同一操作两条历史,撤销第一步看似无效;
3183
3325
  // 还原旧条目还会让吸附反复重推导,undo 前的 flush 不断推新条目,撤销指针原地打转)。
3184
3326
  // 序列化前先就地收敛一次(拖拽 stopDrag 里显式调 syncSlideConnections 同理),幂等无副作用。
3185
3327
  settleActiveSlideConnections()
3186
3328
  ensurePageWatchers()
3329
+ ensureMasterWatchers()
3187
3330
 
3188
3331
  const __perfT0 = performance.now()
3189
3332
  const n = slides.value.length
@@ -3212,6 +3355,34 @@ function addHistorySnapshot() {
3212
3355
  currentPageStrs.length = n
3213
3356
  dirtyPageIndexes.clear()
3214
3357
 
3358
+ // 同步检测普通视图下关联的母版变更(例如单页/整册切换主题、插入带独立母版的模板页)
3359
+ const mn = slideMasters.value.length
3360
+ const masterStructural = mn !== currentMasterStrs.length || masterWatchers.length !== mn
3361
+ const masterCandidates: Set<number> = new Set(masterStructural ? undefined : dirtyMasterIndexes)
3362
+ if (!masterStructural) {
3363
+ for (let i = 0; i < mn; i++) {
3364
+ if (slideMasters.value[i]?.id !== currentMasterIds[i]) masterCandidates.add(i)
3365
+ }
3366
+ } else {
3367
+ for (let i = 0; i < mn; i++) masterCandidates.add(i)
3368
+ changed = true
3369
+ }
3370
+ for (const i of masterCandidates) {
3371
+ const ms = serializePage(slideMasters.value[i])
3372
+ if (ms !== currentMasterStrs[i]) {
3373
+ currentMasterStrs[i] = ms
3374
+ changed = true
3375
+ }
3376
+ }
3377
+ currentMasterStrs.length = mn
3378
+ currentMasterIds = slideMasters.value.map(m => m && m.id)
3379
+ if (!isMasterEditingView.value) {
3380
+ dirtyMasterIndexes.clear()
3381
+ }
3382
+
3383
+ // 抑制当前同步 tick 内已经被快照吸收的变更在下一个 microtask (pre-flush watcher) 中再次把页面/母版标脏
3384
+ markPostSnapshotMicrotask()
3385
+
3215
3386
  if (!changed) {
3216
3387
  if (import.meta.env.DEV) console.debug(`[perf:addSnapshot-dedup] ${(performance.now() - __perfT0).toFixed(1)}ms pages=${slides.value.length} dirty=${candidates.size}`)
3217
3388
  return
@@ -3220,15 +3391,21 @@ function addHistorySnapshot() {
3220
3391
  if (historyIndex.value >= 0) {
3221
3392
  historyStack.value = historyStack.value.slice(0, historyIndex.value + 1)
3222
3393
  historyActiveIndexes = historyActiveIndexes.slice(0, historyIndex.value + 1)
3394
+ historyMasterSnapshots = historyMasterSnapshots.slice(0, historyIndex.value + 1)
3395
+ historyGlobalStates = historyGlobalStates.slice(0, historyIndex.value + 1)
3223
3396
  }
3224
3397
 
3225
- // 未变页的字符串引用跨快照共享(string 不可变,零拷贝)
3398
+ // 未变页/未变母版的字符串引用跨快照共享(string 不可变,零拷贝)
3226
3399
  historyStack.value.push(currentPageStrs.slice())
3227
3400
  historyActiveIndexes.push(activeIndex.value)
3401
+ historyMasterSnapshots.push(currentMasterStrs.slice())
3402
+ historyGlobalStates.push(captureGlobalThemeState())
3228
3403
 
3229
3404
  if (historyStack.value.length > 15) {
3230
3405
  historyStack.value.shift()
3231
3406
  historyActiveIndexes.shift()
3407
+ historyMasterSnapshots.shift()
3408
+ historyGlobalStates.shift()
3232
3409
  }
3233
3410
 
3234
3411
  historyIndex.value = historyStack.value.length - 1
@@ -3246,6 +3423,14 @@ function addHistorySnapshot() {
3246
3423
 
3247
3424
  function addMasterHistorySnapshot() {
3248
3425
  if (isUndoingRedoing.value || pauseHistory.value) return
3426
+ if (masterSnapshotTimer) {
3427
+ clearTimeout(masterSnapshotTimer)
3428
+ masterSnapshotTimer = null
3429
+ }
3430
+ if (masterIdleCallbackId && typeof window !== 'undefined' && 'cancelIdleCallback' in window) {
3431
+ window.cancelIdleCallback(masterIdleCallbackId)
3432
+ masterIdleCallbackId = null
3433
+ }
3249
3434
  ensureMasterWatchers()
3250
3435
 
3251
3436
  const n = slideMasters.value.length
@@ -3271,6 +3456,7 @@ function addMasterHistorySnapshot() {
3271
3456
  }
3272
3457
  currentMasterStrs.length = n
3273
3458
  dirtyMasterIndexes.clear()
3459
+ markPostMasterSnapshotMicrotask()
3274
3460
  if (!changed) return
3275
3461
 
3276
3462
  masterHistoryStack.value = masterHistoryStack.value.slice(0, masterHistoryIndex.value + 1)
@@ -3302,10 +3488,11 @@ function scheduleHistorySnapshot() {
3302
3488
  }
3303
3489
 
3304
3490
  historySnapshotTimer = setTimeout(() => {
3491
+ historySnapshotTimer = null
3305
3492
  if (typeof window !== 'undefined' && 'requestIdleCallback' in window) {
3306
3493
  historyIdleCallbackId = window.requestIdleCallback(() => {
3307
- addHistorySnapshot()
3308
3494
  historyIdleCallbackId = null
3495
+ addHistorySnapshot()
3309
3496
  }, { timeout: 800 })
3310
3497
  } else {
3311
3498
  addHistorySnapshot()
@@ -3328,8 +3515,9 @@ function ensurePageWatchers() {
3328
3515
  // undo/redo 整体替换数组会触发全部页 watcher(deep 回调不比较新旧值),全是假阳性;
3329
3516
  // 此窗口内还原路径已同步 currentPageStrs,无需标脏。watcher 回调先于 nextTick 复位执行,守卫可靠
3330
3517
  if (isUndoingRedoing.value) return
3331
- dirtyPageIndexes.add(i)
3332
3518
  slidesRevision.value++
3519
+ if (isPostSnapshotMicrotask) return
3520
+ dirtyPageIndexes.add(i)
3333
3521
  scheduleHistorySnapshot()
3334
3522
  }, { deep: true }))
3335
3523
  }
@@ -3337,8 +3525,10 @@ function ensurePageWatchers() {
3337
3525
 
3338
3526
  // 长度哨兵:纯尾部追加(末页后 addSlide)不触发任何 index getter,必须靠它补注册 watcher
3339
3527
  watch(() => slides.value.length, () => {
3528
+ if (isUndoingRedoing.value) return
3340
3529
  ensurePageWatchers()
3341
3530
  slidesRevision.value++
3531
+ if (isPostSnapshotMicrotask) return
3342
3532
  scheduleHistorySnapshot()
3343
3533
  })
3344
3534
 
@@ -3361,10 +3551,11 @@ function scheduleMasterHistorySnapshot() {
3361
3551
  }
3362
3552
 
3363
3553
  masterSnapshotTimer = setTimeout(() => {
3554
+ masterSnapshotTimer = null
3364
3555
  if (typeof window !== 'undefined' && 'requestIdleCallback' in window) {
3365
3556
  masterIdleCallbackId = window.requestIdleCallback(() => {
3366
- addMasterHistorySnapshot()
3367
3557
  masterIdleCallbackId = null
3558
+ addMasterHistorySnapshot()
3368
3559
  }, { timeout: 800 })
3369
3560
  } else {
3370
3561
  addMasterHistorySnapshot()
@@ -3379,17 +3570,31 @@ function ensureMasterWatchers() {
3379
3570
  const i = masterWatchers.length
3380
3571
  masterWatchers.push(watch(() => slideMasters.value[i], () => {
3381
3572
  if (isUndoingRedoing.value) return
3382
- dirtyMasterIndexes.add(i)
3383
3573
  slidesRevision.value++
3384
- scheduleMasterHistorySnapshot()
3574
+ if (isMasterEditingView.value) {
3575
+ if (isPostMasterSnapshotMicrotask) return
3576
+ dirtyMasterIndexes.add(i)
3577
+ scheduleMasterHistorySnapshot()
3578
+ } else {
3579
+ if (isPostSnapshotMicrotask) return
3580
+ dirtyMasterIndexes.add(i)
3581
+ scheduleHistorySnapshot()
3582
+ }
3385
3583
  }, { deep: true }))
3386
3584
  }
3387
3585
  }
3388
3586
 
3389
3587
  watch(() => slideMasters.value.length, () => {
3588
+ if (isUndoingRedoing.value) return
3390
3589
  ensureMasterWatchers()
3391
3590
  slidesRevision.value++
3392
- scheduleMasterHistorySnapshot()
3591
+ if (isMasterEditingView.value) {
3592
+ if (isPostMasterSnapshotMicrotask) return
3593
+ scheduleMasterHistorySnapshot()
3594
+ } else {
3595
+ if (isPostSnapshotMicrotask) return
3596
+ scheduleHistorySnapshot()
3597
+ }
3393
3598
  })
3394
3599
 
3395
3600
  // 切换母版/版式只更新栈顶快照的激活状态(与 activeIndex 同理),不产生新历史记录
@@ -3404,6 +3609,7 @@ watch([activeMasterId, activeLayoutId], ([mid, lid]) => {
3404
3609
  // 立即落盘尚在防抖中的快照:undo/redo 前调用,
3405
3610
  // 避免编辑后 350ms 内撤销导致该步编辑被定时器取消而丢失
3406
3611
  function flushPendingHistorySnapshot() {
3612
+ const hadPendingTimer = Boolean(historySnapshotTimer || historyIdleCallbackId)
3407
3613
  if (historySnapshotTimer) {
3408
3614
  clearTimeout(historySnapshotTimer)
3409
3615
  historySnapshotTimer = null
@@ -3412,10 +3618,14 @@ function flushPendingHistorySnapshot() {
3412
3618
  window.cancelIdleCallback(historyIdleCallbackId)
3413
3619
  historyIdleCallbackId = null
3414
3620
  }
3415
- addHistorySnapshot()
3621
+ const syncedFromDom = syncActiveEditingTextToElement()
3622
+ if (syncedFromDom || hadPendingTimer || dirtyPageIndexes.size > 0 || dirtyMasterIndexes.size > 0) {
3623
+ addHistorySnapshot(true)
3624
+ }
3416
3625
  }
3417
3626
 
3418
3627
  function flushPendingMasterHistorySnapshot() {
3628
+ const hadPendingTimer = Boolean(masterSnapshotTimer || masterIdleCallbackId)
3419
3629
  if (masterSnapshotTimer) {
3420
3630
  clearTimeout(masterSnapshotTimer)
3421
3631
  masterSnapshotTimer = null
@@ -3424,7 +3634,9 @@ function flushPendingMasterHistorySnapshot() {
3424
3634
  window.cancelIdleCallback(masterIdleCallbackId)
3425
3635
  masterIdleCallbackId = null
3426
3636
  }
3427
- addMasterHistorySnapshot()
3637
+ if (hadPendingTimer || dirtyMasterIndexes.size > 0) {
3638
+ addMasterHistorySnapshot()
3639
+ }
3428
3640
  }
3429
3641
 
3430
3642
  // Initialize history stack
@@ -3470,6 +3682,8 @@ function undo() {
3470
3682
  activeLayoutId.value = masterHistoryActiveLids[masterHistoryIndex.value]
3471
3683
  selectedElementId.value = null
3472
3684
  selectedElementIds.value = []
3685
+ syncDOMWithRestoredSlides()
3686
+ activeSlideVersion.value++
3473
3687
  if (import.meta.env.DEV) console.debug(`[perf:undo-master] patch ${(performance.now() - __perfT0).toFixed(1)}ms masters=${slideMasters.value.length}`)
3474
3688
  notify('已撤销')
3475
3689
  nextTick(() => {
@@ -3490,6 +3704,13 @@ function undo() {
3490
3704
  slides.value = applyHistoryEntry<Slide>(entry, currentPageStrs, slides.value).list
3491
3705
  currentPageStrs = entry.slice()
3492
3706
  currentPageIds = slides.value.map(s => s && s.id)
3707
+ const masterEntry = historyMasterSnapshots[historyIndex.value]
3708
+ if (masterEntry) {
3709
+ slideMasters.value = applyHistoryEntry<SlideMaster>(masterEntry, currentMasterStrs, slideMasters.value).list
3710
+ currentMasterStrs = masterEntry.slice()
3711
+ currentMasterIds = slideMasters.value.map(m => m && m.id)
3712
+ }
3713
+ restoreGlobalThemeState(historyGlobalStates[historyIndex.value])
3493
3714
  activeIndex.value = historyActiveIndexes[historyIndex.value]
3494
3715
  selectedElementId.value = null
3495
3716
  selectedElementIds.value = []
@@ -3542,6 +3763,13 @@ function redo() {
3542
3763
  slides.value = applyHistoryEntry<Slide>(entry, currentPageStrs, slides.value).list
3543
3764
  currentPageStrs = entry.slice()
3544
3765
  currentPageIds = slides.value.map(s => s && s.id)
3766
+ const masterEntry = historyMasterSnapshots[historyIndex.value]
3767
+ if (masterEntry) {
3768
+ slideMasters.value = applyHistoryEntry<SlideMaster>(masterEntry, currentMasterStrs, slideMasters.value).list
3769
+ currentMasterStrs = masterEntry.slice()
3770
+ currentMasterIds = slideMasters.value.map(m => m && m.id)
3771
+ }
3772
+ restoreGlobalThemeState(historyGlobalStates[historyIndex.value])
3545
3773
  activeIndex.value = historyActiveIndexes[historyIndex.value]
3546
3774
  selectedElementId.value = null
3547
3775
  selectedElementIds.value = []
@@ -4061,6 +4289,12 @@ function pasteElementsData(elementsArray: any[], options?: { sourceWidth?: numbe
4061
4289
  updateGroupBounds(newEl)
4062
4290
  }
4063
4291
 
4292
+ const maxOrder = (activeSlide.value!.elements || []).reduce((max: number, item: any, idx: number) => {
4293
+ const o = typeof item?.order === 'number' ? item.order : (idx + 1)
4294
+ return o > max ? o : max
4295
+ }, 0)
4296
+ newEl.order = maxOrder + 1
4297
+
4064
4298
  activeSlide.value!.elements.push(newEl)
4065
4299
  pastedIds.push(newEl.id)
4066
4300
  })
@@ -6384,7 +6618,17 @@ function extractPatternFromFill(el: any): string | undefined {
6384
6618
  if (!pattern || typeof pattern !== 'string') return undefined
6385
6619
  const str = pattern.trim()
6386
6620
  if (!str) return undefined
6387
- if (str.startsWith('data:') || str.startsWith('http:') || str.startsWith('https:') || str.startsWith('blob:') || str.startsWith('url(')) {
6621
+ if (
6622
+ str.startsWith('data:') ||
6623
+ str.startsWith('http:') ||
6624
+ str.startsWith('https:') ||
6625
+ str.startsWith('blob:') ||
6626
+ str.startsWith('url(') ||
6627
+ str.startsWith('/') ||
6628
+ str.startsWith('./') ||
6629
+ str.startsWith('../') ||
6630
+ /\.(png|jpe?g|gif|webp|svg|bmp|avif|ico)([?#].*)?$/i.test(str)
6631
+ ) {
6388
6632
  return str
6389
6633
  }
6390
6634
  return `data:image/png;base64,${str}`
@@ -7728,12 +7972,15 @@ function preprocessImportedSlides(slides: any[]) {
7728
7972
  slide.bgType = bgInfo.bgType
7729
7973
  if (!slide.elements) continue
7730
7974
 
7731
- // 导入时按 PPT 原始 order 排序一次并规范化,确保页面元素数组本身就是最权威的层级顺序
7732
- slide.elements.sort((a: any, b: any) => {
7733
- const orderA = a?.order !== undefined ? a.order : (parseFloat(a?.style?.zIndex) || 0)
7734
- const orderB = b?.order !== undefined ? b.order : (parseFloat(b?.style?.zIndex) || 0)
7735
- return orderA - orderB
7736
- })
7975
+ // 导入时仅对原始 PPTX 解析结构(尚无 style.left)按 order 排序,已由编辑器导出的页面元素数组本身即为权威层级顺序
7976
+ const isEditorExportedSlide = slide.elements.every((el: any) => el && el.style && el.style.left !== undefined)
7977
+ if (!isEditorExportedSlide) {
7978
+ slide.elements.sort((a: any, b: any) => {
7979
+ const orderA = a?.order !== undefined ? a.order : (parseFloat(a?.style?.zIndex) || 0)
7980
+ const orderB = b?.order !== undefined ? b.order : (parseFloat(b?.style?.zIndex) || 0)
7981
+ return orderA - orderB
7982
+ })
7983
+ }
7737
7984
  slide.elements.forEach((el: any, idx: number) => {
7738
7985
  if (el) el.order = idx + 1
7739
7986
  })
@@ -9420,12 +9667,12 @@ export function useSlides() {
9420
9667
  }
9421
9668
  }
9422
9669
 
9423
- const curRecipe = getActiveSlideThemeRecipe(curSlide)
9424
- if (curRecipe && activeMasterId.value && curMaster && curSlide.themeId && curSlide.themeId !== (curMaster as any).themeId) {
9425
- applyStyleRecipeToMaster(curRecipe, 'single', activeMasterId.value)
9426
- } else if (curMaster && curRecipe) {
9427
- ;(curMaster as any).themeId = curRecipe.id
9428
- ;(curMaster as any).themeName = curRecipe.name
9670
+ const matchedRecipe = curSlide.themeId ? builtinStyleRecipes.find(r => r.id === curSlide.themeId) : null
9671
+ if (matchedRecipe && activeMasterId.value && curMaster && !(curMaster as any).isImported && curSlide.themeId !== (curMaster as any).themeId) {
9672
+ applyStyleRecipeToMaster(matchedRecipe, 'single', activeMasterId.value)
9673
+ } else if (curMaster && matchedRecipe && !(curMaster as any).isImported) {
9674
+ ;(curMaster as any).themeId = matchedRecipe.id
9675
+ ;(curMaster as any).themeName = matchedRecipe.name
9429
9676
  }
9430
9677
  }
9431
9678
 
@@ -9840,6 +10087,7 @@ export function useSlides() {
9840
10087
  canRedo,
9841
10088
  historyStack,
9842
10089
  historyIndex,
10090
+ isUndoingRedoing,
9843
10091
  pauseHistory,
9844
10092
  isCanvasDragging,
9845
10093
  activeSlideVersion,