@mlightcad/ribbon 0.0.1 → 0.0.3

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 (66) hide show
  1. package/README.md +41 -3
  2. package/dist/components/Ribbon.vue.d.ts +102 -0
  3. package/dist/components/RibbonCollection.vue.d.ts +41 -0
  4. package/dist/components/RibbonGroup.vue.d.ts +77 -0
  5. package/dist/components/RibbonGroupContent.vue.d.ts +39 -0
  6. package/dist/components/RibbonItemHost.vue.d.ts +52 -0
  7. package/dist/components/RibbonTab.vue.d.ts +47 -0
  8. package/dist/components/RibbonTabs.vue.d.ts +34 -0
  9. package/dist/composables/useRibbonState.d.ts +17 -0
  10. package/dist/context.d.ts +3 -0
  11. package/{src/ribbon/index.ts → dist/index.d.ts} +15 -43
  12. package/dist/items/RibbonGallery.vue.d.ts +48 -0
  13. package/dist/items/RibbonGroupButton.vue.d.ts +49 -0
  14. package/dist/items/RibbonTemplateItem.vue.d.ts +40 -0
  15. package/dist/ml-ribbon.css +2 -0
  16. package/dist/ml-ribbon.es.js +1490 -0
  17. package/dist/ml-ribbon.umd.js +1 -0
  18. package/dist/modules/RibbonBackstage.vue.d.ts +61 -0
  19. package/dist/modules/RibbonContextualTabs.vue.d.ts +41 -0
  20. package/dist/modules/RibbonFileMenu.vue.d.ts +44 -0
  21. package/dist/modules/RibbonKeyTips.vue.d.ts +39 -0
  22. package/dist/types.d.ts +130 -0
  23. package/package.json +26 -2
  24. package/.github/workflows/ci.yml +0 -110
  25. package/.vscode/extensions.json +0 -3
  26. package/AGENTS.md +0 -42
  27. package/docs/requirements.md +0 -96
  28. package/eslint.config.js +0 -41
  29. package/images/ribbon.jpg +0 -0
  30. package/index.html +0 -13
  31. package/playwright/ribbon.spec.ts +0 -6
  32. package/playwright.config.ts +0 -10
  33. package/public/favicon.svg +0 -1
  34. package/public/icons.svg +0 -24
  35. package/src/App.vue +0 -418
  36. package/src/assets/hero.png +0 -0
  37. package/src/assets/vite.svg +0 -1
  38. package/src/assets/vue.svg +0 -1
  39. package/src/components/HelloWorld.vue +0 -106
  40. package/src/main.ts +0 -9
  41. package/src/ribbon/components/Ribbon.vue +0 -844
  42. package/src/ribbon/components/RibbonCollection.vue +0 -51
  43. package/src/ribbon/components/RibbonGroup.vue +0 -327
  44. package/src/ribbon/components/RibbonGroupContent.vue +0 -61
  45. package/src/ribbon/components/RibbonItemHost.vue +0 -221
  46. package/src/ribbon/components/RibbonTab.vue +0 -44
  47. package/src/ribbon/components/RibbonTabs.vue +0 -49
  48. package/src/ribbon/composables/useRibbonState.ts +0 -272
  49. package/src/ribbon/context.ts +0 -4
  50. package/src/ribbon/items/RibbonGallery.vue +0 -78
  51. package/src/ribbon/items/RibbonGroupButton.vue +0 -85
  52. package/src/ribbon/items/RibbonTemplateItem.vue +0 -38
  53. package/src/ribbon/modules/RibbonBackstage.vue +0 -69
  54. package/src/ribbon/modules/RibbonContextualTabs.vue +0 -65
  55. package/src/ribbon/modules/RibbonFileMenu.vue +0 -88
  56. package/src/ribbon/modules/RibbonKeyTips.vue +0 -114
  57. package/src/ribbon/styles/ribbon.css +0 -925
  58. package/src/ribbon/types.ts +0 -162
  59. package/src/style.css +0 -328
  60. package/src/tests/ribbon.spec.ts +0 -830
  61. package/src/tests/setup.ts +0 -4
  62. package/tools/release.mjs +0 -107
  63. package/tsconfig.app.json +0 -14
  64. package/tsconfig.json +0 -7
  65. package/tsconfig.node.json +0 -24
  66. package/vite.config.ts +0 -38
@@ -1,844 +0,0 @@
1
- <script setup lang="ts">
2
- import { computed, markRaw, nextTick, onMounted, onUnmounted, provide, ref, toRaw, watch } from 'vue'
3
- import { ElButton, ElConfigProvider, ElIcon, ElPopover, ElSwitch, ElTooltip, useGlobalConfig } from 'element-plus'
4
- import { Grid, Menu, Operation } from '@element-plus/icons-vue'
5
- import type { Component } from 'vue'
6
- import type { ComponentSize } from 'element-plus'
7
- import { ribbonKey } from '../context'
8
- import { useRibbonState } from '../composables/useRibbonState'
9
- import type {
10
- RibbonDynamicApi,
11
- RibbonGroupModel,
12
- RibbonItemModel,
13
- RibbonLayout,
14
- RibbonLocaleTexts,
15
- RibbonTabModel,
16
- } from '../types'
17
- import MlRibbonBackstage from '../modules/RibbonBackstage.vue'
18
- import MlRibbonContextualTabs from '../modules/RibbonContextualTabs.vue'
19
- import MlRibbonFileMenu from '../modules/RibbonFileMenu.vue'
20
- import MlRibbonGroupContent from './RibbonGroupContent.vue'
21
- import MlRibbonGroup from './RibbonGroup.vue'
22
-
23
- /**
24
- * @component MlRibbon
25
- * @description
26
- * Root orchestrator for the ribbon system. It owns tab/layout/minimize state,
27
- * provides dynamic API exposure, and coordinates overflow, backstage, key tips,
28
- * contextual tabs, and item interaction events.
29
- *
30
- * @prop id - DOM/id namespace root for this ribbon instance.
31
- * @prop tabs - Ribbon tab tree with groups, collections, and items.
32
- * @prop activeTab - Controlled active tab id (`v-model:activeTab`).
33
- * @prop activeLayout - Legacy-friendly layout alias (`classic`/`simplified`).
34
- * @prop layout - Controlled layout (`v-model:layout`).
35
- * @prop size - Ribbon size delegated to Element Plus config provider.
36
- * @prop minimized - Controlled minimize state (`v-model:minimized`).
37
- * @prop hideLayoutSwitcher - Hides layout toggle button when true.
38
- * @prop showFileMenu - Enables/disables File menu tab.
39
- * @prop fileMenuItems - File menu command list.
40
- * @prop backstageItems - Backstage navigation entries.
41
- * @prop texts - Localized UI text bundle for labels/tooltips.
42
- *
43
- * @event update:activeTab - Fired when active tab changes.
44
- * @event update:layout - Fired when layout changes.
45
- * @event update:minimized - Fired when minimized changes.
46
- * @event tabChange - Fired after tab selection changes.
47
- * @event layoutChange - Fired after layout mode changes.
48
- * @event itemClick - Fired when a ribbon item is activated with `{ tabId, groupId, itemId }`.
49
- * @event overflowOpen - Fired when main overflow popover opens.
50
- * @event overflowClose - Fired when main overflow popover closes.
51
- * @event backstageOpen - Fired when backstage drawer opens.
52
- * @event backstageClose - Fired when backstage drawer closes.
53
- * @event fileMenuSelect - Fired when a file menu command is chosen.
54
- *
55
- * @example
56
- * ```vue
57
- * <MlRibbon
58
- * v-model:active-tab="activeTab"
59
- * v-model:layout="layout"
60
- * v-model:minimized="minimized"
61
- * :tabs="tabs"
62
- * :file-menu-items="fileMenuItems"
63
- * :backstage-items="backstageItems"
64
- * :texts="ribbonTexts"
65
- * @item-click="onRibbonItemClick"
66
- * />
67
- * ```
68
- */
69
- /**
70
- * Normalizes potential component definitions to non-reactive objects.
71
- * @param value Unknown schema value that may contain a component.
72
- * @returns Normalized value safe for reactive tab trees.
73
- */
74
- function normalizeComponentCandidate<T>(value: T): T {
75
- if (value == null || typeof value === 'string') return value
76
- if (typeof value === 'object' || typeof value === 'function') {
77
- return markRaw(toRaw(value as object)) as T
78
- }
79
- return value
80
- }
81
-
82
- /**
83
- * Clones and normalizes item props, including icon-like fields.
84
- * @param props Raw item props from schema.
85
- * @returns Normalized props clone.
86
- */
87
- function normalizeItemProps(props?: Record<string, unknown>): Record<string, unknown> | undefined {
88
- if (!props) return undefined
89
- const nextProps: Record<string, unknown> = { ...props }
90
- if ('icon' in nextProps) {
91
- nextProps.icon = normalizeComponentCandidate(nextProps.icon)
92
- }
93
- if (Array.isArray(nextProps.options)) {
94
- nextProps.options = nextProps.options.map((option) => {
95
- if (!option || typeof option !== 'object') return option
96
- const optionRecord = option as Record<string, unknown>
97
- if (!('icon' in optionRecord)) return optionRecord
98
- return {
99
- ...optionRecord,
100
- icon: normalizeComponentCandidate(optionRecord.icon),
101
- }
102
- })
103
- }
104
- return nextProps
105
- }
106
-
107
- /**
108
- * Clones a ribbon item and normalizes component-like fields.
109
- * @param item Source item model.
110
- * @returns Cloned item model.
111
- */
112
- function cloneItem(item: RibbonItemModel): RibbonItemModel {
113
- return {
114
- ...item,
115
- icon: normalizeComponentCandidate(item.icon),
116
- props: normalizeItemProps(item.props),
117
- }
118
- }
119
-
120
- /**
121
- * Clones a ribbon group recursively.
122
- * @param group Source group model.
123
- * @returns Cloned group model.
124
- */
125
- function cloneGroup(group: RibbonGroupModel): RibbonGroupModel {
126
- return {
127
- ...group,
128
- icon: normalizeComponentCandidate(group.icon),
129
- footerMenuItems: group.footerMenuItems?.map(cloneItem),
130
- collections: group.collections?.map((collection) => ({
131
- ...collection,
132
- items: collection.items?.map(cloneItem),
133
- })),
134
- }
135
- }
136
-
137
- /**
138
- * Clones tab schema before storing it in mutable runtime state.
139
- * @param value Source tabs array.
140
- * @returns Deep-cloned tabs array for runtime mutation.
141
- */
142
- function cloneTabs(value: RibbonTabModel[]): RibbonTabModel[] {
143
- return value.map((tab) => ({
144
- ...tab,
145
- groups: tab.groups?.map(cloneGroup),
146
- }))
147
- }
148
-
149
- const defaultRibbonTexts: Required<RibbonLocaleTexts> = {
150
- layoutSwitcherTooltip: 'Switch ribbon layout',
151
- minimizeTooltip: 'Minimize ribbon',
152
- keyTipsToggleText: 'Key Tips',
153
- overflowTriggerAriaLabel: 'Open overflow groups',
154
- groupOverflowTriggerAriaLabel: 'Open group menu',
155
- fileMenuLabel: 'File',
156
- fileMenuOpenBackstageLabel: 'Open backstage',
157
- backstageBackLabel: 'Back',
158
- backstageTitle: 'Backstage',
159
- backstageDescription: 'Manage your document and settings here.',
160
- keyTipsSequencePrefix: 'Sequence:',
161
- keyTipsEmptySequence: 'No key sequence',
162
- contextualTabDefaultTitle: 'Contextual',
163
- galleryPreviewFallback: 'Preview unavailable',
164
- }
165
-
166
- const props = withDefaults(
167
- defineProps<{
168
- id?: string
169
- tabs: RibbonTabModel[]
170
- activeTab?: string
171
- activeLayout?: RibbonLayout | 'Classic' | 'Simplified'
172
- layout?: RibbonLayout
173
- size?: ComponentSize
174
- minimized?: boolean
175
- hideLayoutSwitcher?: boolean
176
- showFileMenu?: boolean
177
- fileMenuItems?: { id: string; label: string; disabled?: boolean }[]
178
- backstageItems?: { id: string; label: string; description?: string }[]
179
- texts?: RibbonLocaleTexts
180
- }>(),
181
- {
182
- id: 'ml-ribbon-root',
183
- activeTab: '',
184
- activeLayout: undefined,
185
- layout: 'classic',
186
- size: undefined,
187
- minimized: false,
188
- hideLayoutSwitcher: false,
189
- showFileMenu: true,
190
- fileMenuItems: () => [],
191
- backstageItems: () => [],
192
- texts: () => ({}),
193
- },
194
- )
195
-
196
- const emit = defineEmits<{
197
- (e: 'update:activeTab', value: string): void
198
- (e: 'update:layout', value: RibbonLayout): void
199
- (e: 'update:minimized', value: boolean): void
200
- (e: 'tabChange', value: string): void
201
- (e: 'layoutChange', value: RibbonLayout): void
202
- (e: 'itemClick', payload: { tabId: string; groupId: string; itemId: string }): void
203
- (e: 'overflowOpen'): void
204
- (e: 'overflowClose'): void
205
- (e: 'backstageOpen'): void
206
- (e: 'backstageClose'): void
207
- (e: 'fileMenuSelect', value: string): void
208
- }>()
209
-
210
- const { context, visibleTabs } = useRibbonState(
211
- props.id,
212
- props.tabs,
213
- props.activeLayout
214
- ? String(props.activeLayout).toLowerCase() === 'simplified'
215
- ? 'simplified'
216
- : 'classic'
217
- : props.layout,
218
- props.minimized,
219
- props.activeTab || props.tabs[0]?.id || '',
220
- )
221
-
222
- provide(ribbonKey, context)
223
-
224
- watch(
225
- () => props.tabs,
226
- (tabs) => {
227
- // Keep internal mutable state in sync with controlled tabs prop.
228
- context.tabs.value = cloneTabs(tabs)
229
- if (!context.tabs.value.some((x) => x.id === context.activeTab.value)) {
230
- context.activeTab.value = context.tabs.value[0]?.id || ''
231
- }
232
- },
233
- { deep: true },
234
- )
235
-
236
- watch(() => props.layout, (value) => {
237
- if (value !== context.layout.value) context.layout.value = value
238
- })
239
- watch(() => props.activeLayout, (value) => {
240
- if (!value) return
241
- const normalized: RibbonLayout = String(value).toLowerCase() === 'simplified' ? 'simplified' : 'classic'
242
- if (normalized !== context.layout.value) context.layout.value = normalized
243
- })
244
- watch(() => props.minimized, (value) => {
245
- if (value !== context.minimized.value) context.minimized.value = value
246
- })
247
- watch(() => props.activeTab, (value) => {
248
- if (value && value !== context.activeTab.value) context.activeTab.value = value
249
- })
250
-
251
- watch(context.activeTab, (value) => {
252
- emit('update:activeTab', value)
253
- emit('tabChange', value)
254
- })
255
- watch(context.layout, (value) => {
256
- emit('update:layout', value)
257
- emit('layoutChange', value)
258
- })
259
- watch(context.minimized, (value) => emit('update:minimized', value))
260
- watch(context.overflowOpen, (value) => {
261
- if (value) emit('overflowOpen')
262
- else emit('overflowClose')
263
- })
264
- watch(context.backdropOpen, (value) => {
265
- if (value) emit('backstageOpen')
266
- else emit('backstageClose')
267
- })
268
-
269
- const activeTabModel = computed(() => visibleTabs.value.find((x) => x.id === context.activeTab.value))
270
- const visibleGroups = computed(() => activeTabModel.value?.groups?.filter((x) => x.visible !== false) ?? [])
271
- const classicPanelRef = ref<HTMLElement | null>(null)
272
- // Group ids hidden in classic mode and shown in the overflow popover.
273
- const hiddenGroupIds = ref<Set<string>>(new Set())
274
- // Cache measured widths by group id to reduce fallback width estimates on recalculation.
275
- const measuredGroupWidths = ref<Record<string, number>>({})
276
- const globalSize = useGlobalConfig('size', '')
277
- const resolvedRibbonSize = computed(() => props.size || globalSize.value || 'default')
278
- const simplifiedGroupPopoverClass = computed(
279
- () => `ml-ribbon-group-popover ml-ribbon-group-popover--size-${resolvedRibbonSize.value}`,
280
- )
281
- const classicOverflowPopoverClass = computed(
282
- () => `ml-ribbon-overflow-popover ml-ribbon-overflow-popover--size-${resolvedRibbonSize.value}`,
283
- )
284
- const ribbonTexts = computed<RibbonLocaleTexts>(() => ({
285
- layoutSwitcherTooltip: props.texts.layoutSwitcherTooltip || defaultRibbonTexts.layoutSwitcherTooltip,
286
- minimizeTooltip: props.texts.minimizeTooltip || defaultRibbonTexts.minimizeTooltip,
287
- keyTipsToggleText: props.texts.keyTipsToggleText || defaultRibbonTexts.keyTipsToggleText,
288
- overflowTriggerAriaLabel: props.texts.overflowTriggerAriaLabel || defaultRibbonTexts.overflowTriggerAriaLabel,
289
- groupOverflowTriggerAriaLabel:
290
- props.texts.groupOverflowTriggerAriaLabel || defaultRibbonTexts.groupOverflowTriggerAriaLabel,
291
- fileMenuLabel: props.texts.fileMenuLabel || defaultRibbonTexts.fileMenuLabel,
292
- fileMenuOpenBackstageLabel:
293
- props.texts.fileMenuOpenBackstageLabel || defaultRibbonTexts.fileMenuOpenBackstageLabel,
294
- backstageBackLabel: props.texts.backstageBackLabel || defaultRibbonTexts.backstageBackLabel,
295
- backstageTitle: props.texts.backstageTitle || defaultRibbonTexts.backstageTitle,
296
- backstageDescription: props.texts.backstageDescription || defaultRibbonTexts.backstageDescription,
297
- keyTipsSequencePrefix: props.texts.keyTipsSequencePrefix || defaultRibbonTexts.keyTipsSequencePrefix,
298
- keyTipsEmptySequence: props.texts.keyTipsEmptySequence || defaultRibbonTexts.keyTipsEmptySequence,
299
- contextualTabDefaultTitle: props.texts.contextualTabDefaultTitle || defaultRibbonTexts.contextualTabDefaultTitle,
300
- galleryPreviewFallback: props.texts.galleryPreviewFallback || defaultRibbonTexts.galleryPreviewFallback,
301
- }))
302
-
303
- const classicInlineGroups = computed(() =>
304
- visibleGroups.value.filter((group) => !hiddenGroupIds.value.has(group.id)),
305
- )
306
- const classicOverflowGroups = computed(() =>
307
- visibleGroups.value.filter((group) => hiddenGroupIds.value.has(group.id)),
308
- )
309
- const classicOverflowPopoverWidth = computed(() => {
310
- if (!classicOverflowGroups.value.length) return 220
311
-
312
- const widestGroupWidth = classicOverflowGroups.value.reduce((max, group) => {
313
- const width = measuredGroupWidths.value[group.id] ?? estimateGroupWidth(group)
314
- return Math.max(max, width)
315
- }, 0)
316
-
317
- // Account for popover/container paddings so group content is not clipped.
318
- const calculated = Math.ceil(widestGroupWidth + 40)
319
- return Math.max(220, Math.min(720, calculated))
320
- })
321
-
322
- /**
323
- * Builds simplified-mode popup content from a group.
324
- * @param group Source group model.
325
- * @returns Flattened group with medium-sized row items.
326
- */
327
- function toSimplifiedPanelGroup(group: RibbonGroupModel): RibbonGroupModel {
328
- const items = (group.collections ?? []).flatMap((collection) =>
329
- (collection.items ?? []).map((item) => ({
330
- ...cloneItem(item),
331
- size: 'medium' as const,
332
- })),
333
- )
334
-
335
- return {
336
- ...group,
337
- orientation: 'row',
338
- collections: [
339
- {
340
- id: `${group.id}-simplified`,
341
- layout: 'row',
342
- items,
343
- },
344
- ],
345
- }
346
- }
347
-
348
- /**
349
- * Returns overflow priority for a group.
350
- * @param group Group model.
351
- * @returns Priority where larger values are removed earlier.
352
- */
353
- function getGroupPriority(group: RibbonGroupModel): number {
354
- return group.priority ?? 100
355
- }
356
-
357
- /**
358
- * Estimates fallback width for a group when runtime measurement is unavailable.
359
- * @param group Group model.
360
- * @returns Estimated width in pixels.
361
- */
362
- function estimateGroupWidth(group: RibbonGroupModel): number {
363
- if (group.autoWidth !== false) return 140
364
- return 190
365
- }
366
-
367
- /**
368
- * Measures rendered group widths from current classic panel DOM.
369
- * @param panel Classic ribbon panel element.
370
- * @returns Width map keyed by group id.
371
- */
372
- function collectRenderedGroupWidths(panel: HTMLElement): Record<string, number> {
373
- const widths: Record<string, number> = {}
374
- const nodes = panel.querySelectorAll<HTMLElement>('.ml-ribbon-group')
375
- nodes.forEach((node) => {
376
- const groupId = node.dataset.groupId
377
- if (!groupId) return
378
- const measured = node.getBoundingClientRect().width
379
- if (measured > 0) widths[groupId] = measured
380
- })
381
- return widths
382
- }
383
-
384
- const DEFAULT_CLASSIC_OVERFLOW_SLOT_WIDTH = 40
385
-
386
- /**
387
- * Reads classic overflow slot width from CSS custom property.
388
- * @param panel Classic ribbon panel element.
389
- * @returns Overflow slot width in pixels.
390
- */
391
- function resolveClassicOverflowSlotWidth(panel: HTMLElement): number {
392
- const cssWidth = Number.parseFloat(getComputedStyle(panel).getPropertyValue('--ml-rb-overflow-slot-width'))
393
- if (Number.isFinite(cssWidth) && cssWidth > 0) return cssWidth
394
- return DEFAULT_CLASSIC_OVERFLOW_SLOT_WIDTH
395
- }
396
-
397
- /**
398
- * Recomputes which groups should move into classic overflow.
399
- */
400
- function recomputeClassicOverflow() {
401
- if (context.layout.value !== 'classic') {
402
- hiddenGroupIds.value = new Set()
403
- return
404
- }
405
- const panel = classicPanelRef.value
406
- if (!panel) return
407
-
408
- const groups = visibleGroups.value
409
- if (!groups.length) {
410
- hiddenGroupIds.value = new Set()
411
- return
412
- }
413
-
414
- const panelWidth = Math.round(panel.getBoundingClientRect().width)
415
- const overflowWidth = resolveClassicOverflowSlotWidth(panel)
416
- const renderedWidths = collectRenderedGroupWidths(panel)
417
- measuredGroupWidths.value = {
418
- ...measuredGroupWidths.value,
419
- ...renderedWidths,
420
- }
421
-
422
- const fixedGroups = groups.filter((group) => group.enableGroupOverflow === false)
423
- const overflowableGroups = groups
424
- .filter((group) => group.enableGroupOverflow !== false)
425
- .map((group, index) => ({ group, index }))
426
-
427
- const hidden = new Set<string>()
428
- const removableOrder = [...overflowableGroups].sort((a, b) => {
429
- const p = getGroupPriority(b.group) - getGroupPriority(a.group)
430
- if (p !== 0) return p
431
- return b.index - a.index
432
- })
433
-
434
- const resolveGroupWidth = (group: RibbonGroupModel): number => renderedWidths[group.id] ?? estimateGroupWidth(group)
435
-
436
- const fixedWidth = fixedGroups.reduce((sum, group) => sum + resolveGroupWidth(group), 0)
437
- const visibleOverflowableWidth = (hidden: Set<string>) =>
438
- overflowableGroups
439
- .filter(({ group }) => !hidden.has(group.id))
440
- .reduce((sum, { group }) => sum + resolveGroupWidth(group), 0)
441
-
442
- const requiredWidth = (hidden: Set<string>) =>
443
- fixedWidth + visibleOverflowableWidth(hidden) + (hidden.size > 0 ? overflowWidth : 0)
444
-
445
- let hiddenCount = 0
446
- while (requiredWidth(hidden) > panelWidth && hiddenCount < removableOrder.length) {
447
- const nextGroup = removableOrder[hiddenCount].group
448
- hidden.add(nextGroup.id)
449
- hiddenCount += 1
450
- }
451
-
452
- hiddenGroupIds.value = hidden
453
- }
454
-
455
- /**
456
- * Schedules classic overflow recomputation after DOM/layout settles.
457
- */
458
- function scheduleClassicOverflowRecompute() {
459
- nextTick(() => {
460
- if (context.layout.value !== 'classic' || context.minimized.value) {
461
- recomputeClassicOverflow()
462
- return
463
- }
464
- if (hiddenGroupIds.value.size === 0) {
465
- recomputeClassicOverflow()
466
- return
467
- }
468
- hiddenGroupIds.value = new Set()
469
- nextTick(() => {
470
- recomputeClassicOverflow()
471
- })
472
- })
473
- }
474
-
475
- /**
476
- * Selects active tab.
477
- * @param tabId Tab id to activate.
478
- */
479
- function onTabClick(tabId: string) {
480
- context.activeTab.value = tabId
481
- }
482
-
483
- /**
484
- * Toggles ribbon layout between classic and simplified.
485
- */
486
- function toggleLayout() {
487
- context.api.toggleSimplified()
488
- }
489
-
490
- /**
491
- * Toggles ribbon minimize state.
492
- */
493
- function toggleMinimize() {
494
- context.minimized.value = !context.minimized.value
495
- }
496
-
497
- /**
498
- * Forwards file menu selection event.
499
- * @param id Selected file menu command id.
500
- */
501
- function onFileMenuSelect(id: string) {
502
- emit('fileMenuSelect', id)
503
- }
504
-
505
- /**
506
- * Emits normalized item click payload with active tab context.
507
- * @param groupId Source group id.
508
- * @param itemId Source item id.
509
- */
510
- function onItemClick(groupId: string, itemId: string) {
511
- emit('itemClick', { tabId: context.activeTab.value, groupId, itemId })
512
- }
513
-
514
- /**
515
- * Returns true when keyboard event target is an editable text context.
516
- * @param target Event target.
517
- * @returns Whether keyboard shortcuts should be ignored.
518
- */
519
- function isEditableTarget(target: EventTarget | null): boolean {
520
- if (!(target instanceof HTMLElement)) return false
521
- if (target.isContentEditable) return true
522
- const tagName = target.tagName
523
- return tagName === 'INPUT' || tagName === 'TEXTAREA' || tagName === 'SELECT'
524
- }
525
-
526
- /**
527
- * Builds key-tip models for the active tab from visible, enabled items.
528
- */
529
- function syncActiveTabKeyTips() {
530
- const tab = activeTabModel.value
531
- if (!tab?.groups?.length) {
532
- context.keyTips.value = []
533
- return
534
- }
535
-
536
- const tips = tab.groups
537
- .filter((group) => group.visible !== false)
538
- .flatMap((group) =>
539
- (group.collections ?? []).flatMap((collection) =>
540
- collection.items
541
- .filter((item) => item.keyTip && item.disabled !== true)
542
- .map((item) => {
543
- const key = item.keyTip?.trim().toUpperCase()
544
- if (!key) return null
545
- return {
546
- id: `${tab.id}:${group.id}:${item.id}`,
547
- key,
548
- targetId: item.id,
549
- sequence: key.split(''),
550
- scope: 'item' as const,
551
- }
552
- })
553
- .filter((tip): tip is NonNullable<typeof tip> => Boolean(tip)),
554
- ),
555
- )
556
-
557
- context.keyTips.value = tips
558
- }
559
-
560
- /**
561
- * Resolves and emits the item click for a key-tip target id.
562
- * @param tip Matched key-tip model.
563
- */
564
- function onKeyTipActivate(tip: { targetId: string }) {
565
- const tab = activeTabModel.value
566
- if (!tab?.groups?.length) return
567
- for (const group of tab.groups) {
568
- if (group.visible === false) continue
569
- for (const collection of group.collections ?? []) {
570
- const item = collection.items.find((x) => x.id === tip.targetId && x.disabled !== true)
571
- if (!item) continue
572
- onItemClick(group.id, item.id)
573
- context.keyTipsOpen.value = false
574
- context.keyTipsSequence.value = ''
575
- return
576
- }
577
- }
578
- }
579
-
580
- /**
581
- * Resets key tip matching input sequence.
582
- */
583
- function resetKeyTipsSequence() {
584
- context.keyTipsSequence.value = ''
585
- }
586
-
587
- /**
588
- * Handles global key-tip toggle behavior (Alt to open/close).
589
- * @param event Window keyboard event.
590
- */
591
- function onWindowKeydown(event: KeyboardEvent) {
592
- if (isEditableTarget(event.target)) return
593
- if (event.altKey && !event.ctrlKey && !event.metaKey && event.key === 'Alt') {
594
- event.preventDefault()
595
- context.keyTipsOpen.value = !context.keyTipsOpen.value
596
- if (!context.keyTipsOpen.value) resetKeyTipsSequence()
597
- return
598
- }
599
- if (!context.keyTipsOpen.value) return
600
- if (event.altKey || event.ctrlKey || event.metaKey) return
601
-
602
- if (event.key === 'Escape') {
603
- event.preventDefault()
604
- context.keyTipsOpen.value = false
605
- resetKeyTipsSequence()
606
- return
607
- }
608
- if (event.key === 'Backspace') {
609
- event.preventDefault()
610
- context.keyTipsSequence.value = context.keyTipsSequence.value.slice(0, -1)
611
- return
612
- }
613
- if (/^[a-z0-9]$/i.test(event.key)) {
614
- event.preventDefault()
615
- const nextSequence = `${context.keyTipsSequence.value}${event.key.toUpperCase()}`
616
- context.keyTipsSequence.value = nextSequence
617
-
618
- const normalizedSequence = nextSequence.toLowerCase()
619
- const exactMatch = context.keyTips.value.find((tip) => tip.key.toLowerCase() === normalizedSequence)
620
- if (exactMatch) {
621
- onKeyTipActivate(exactMatch)
622
- return
623
- }
624
-
625
- const hasPrefixMatches = context.keyTips.value.some((tip) => tip.key.toLowerCase().startsWith(normalizedSequence))
626
- if (!hasPrefixMatches) resetKeyTipsSequence()
627
- }
628
- }
629
-
630
- /**
631
- * Resolves display icon for simplified group trigger.
632
- * @param group Group model.
633
- * @returns Vue icon component or `null`.
634
- */
635
- function resolveGroupIcon(group: RibbonGroupModel): Component | null {
636
- if (group.icon && typeof group.icon !== 'string') return group.icon
637
- if (group.groupIconCss) return Grid
638
- return null
639
- }
640
-
641
- watch([visibleGroups, () => context.layout.value, () => context.minimized.value], () => {
642
- scheduleClassicOverflowRecompute()
643
- })
644
- watch(
645
- [activeTabModel, () => context.tabs.value],
646
- () => {
647
- syncActiveTabKeyTips()
648
- resetKeyTipsSequence()
649
- },
650
- { deep: true, immediate: true },
651
- )
652
-
653
- watch(
654
- () => context.keyTipsOpen.value,
655
- (open) => {
656
- if (!open) resetKeyTipsSequence()
657
- },
658
- )
659
- watch(
660
- classicPanelRef,
661
- (panel, previous) => {
662
- if (previous && resizeObserver) resizeObserver.unobserve(previous)
663
- if (panel && resizeObserver) resizeObserver.observe(panel)
664
- },
665
- { flush: 'post' },
666
- )
667
-
668
- let resizeObserver: ResizeObserver | null = null
669
- onMounted(() => {
670
- window.addEventListener('keydown', onWindowKeydown)
671
- if (typeof ResizeObserver !== 'undefined') {
672
- resizeObserver = new ResizeObserver(() => {
673
- scheduleClassicOverflowRecompute()
674
- })
675
- if (classicPanelRef.value) resizeObserver.observe(classicPanelRef.value)
676
- }
677
- scheduleClassicOverflowRecompute()
678
- })
679
-
680
- onUnmounted(() => {
681
- window.removeEventListener('keydown', onWindowKeydown)
682
- resizeObserver?.disconnect()
683
- })
684
-
685
- defineExpose<RibbonDynamicApi>(context.api)
686
- </script>
687
-
688
- <template>
689
- <ElConfigProvider :size="props.size">
690
- <section
691
- class="ml-ribbon"
692
- :class="[
693
- `ml-ribbon--${context.layout.value}`,
694
- `ml-ribbon--size-${resolvedRibbonSize}`,
695
- {
696
- 'ml-ribbon--minimized': context.minimized.value,
697
- 'ml-ribbon--keytips-open': context.keyTipsOpen.value,
698
- },
699
- ]"
700
- >
701
- <header class="ml-ribbon__header">
702
- <div class="ml-ribbon__head-left">
703
- <MlRibbonFileMenu
704
- v-if="showFileMenu"
705
- :items="fileMenuItems"
706
- :label="ribbonTexts.fileMenuLabel"
707
- :open-backstage-label="ribbonTexts.fileMenuOpenBackstageLabel"
708
- @select="onFileMenuSelect"
709
- @open-backstage="context.backdropOpen.value = true"
710
- />
711
- <MlRibbonContextualTabs
712
- :tabs="visibleTabs"
713
- :active-tab="context.activeTab.value"
714
- :default-contextual-title="ribbonTexts.contextualTabDefaultTitle"
715
- @select="onTabClick"
716
- />
717
- </div>
718
- <div class="ml-ribbon__head-right">
719
- <ElTooltip v-if="!hideLayoutSwitcher" :content="ribbonTexts.layoutSwitcherTooltip">
720
- <ElButton circle :icon="Menu" @click="toggleLayout" />
721
- </ElTooltip>
722
- <ElTooltip :content="ribbonTexts.minimizeTooltip">
723
- <ElButton circle :icon="Operation" @click="toggleMinimize" />
724
- </ElTooltip>
725
- <ElSwitch
726
- inline-prompt
727
- :active-text="ribbonTexts.keyTipsToggleText"
728
- :inactive-text="ribbonTexts.keyTipsToggleText"
729
- :model-value="context.keyTipsOpen.value"
730
- @change="context.keyTipsOpen.value = !context.keyTipsOpen.value"
731
- />
732
- </div>
733
- </header>
734
-
735
- <main
736
- v-if="!context.minimized.value && activeTabModel && context.layout.value === 'classic'"
737
- class="ml-ribbon__panel"
738
- :data-active-tab="activeTabModel.id"
739
- ref="classicPanelRef"
740
- >
741
- <MlRibbonGroup
742
- v-for="group in classicInlineGroups"
743
- :id="group.id"
744
- :key="group.id"
745
- :data-group-id="group.id"
746
- :title="group.title"
747
- :icon="group.icon"
748
- :group-icon-css="group.groupIconCss"
749
- :orientation="group.orientation"
750
- :auto-width="group.autoWidth"
751
- :priority="group.priority ?? 100"
752
- :launcher="group.launcher"
753
- :show-launcher-icon="group.showLauncherIcon"
754
- :group-model="group"
755
- :overflow-trigger-aria-label="ribbonTexts.groupOverflowTriggerAriaLabel"
756
- :gallery-preview-fallback="ribbonTexts.galleryPreviewFallback"
757
- @item-click="onItemClick($event.groupId, $event.itemId)"
758
- >
759
- <MlRibbonGroupContent
760
- :group="group"
761
- :gallery-preview-fallback="ribbonTexts.galleryPreviewFallback"
762
- @item-click="onItemClick($event.groupId, $event.itemId)"
763
- />
764
- </MlRibbonGroup>
765
-
766
- <section v-if="classicOverflowGroups.length" class="ml-ribbon-group ml-ribbon-group--overflow">
767
- <div class="ml-ribbon-group__body ml-ribbon-group__body--overflow">
768
- <ElPopover
769
- trigger="click"
770
- placement="bottom-end"
771
- :width="classicOverflowPopoverWidth"
772
- :popper-class="classicOverflowPopoverClass"
773
- @show="context.overflowOpen.value = true"
774
- @hide="context.overflowOpen.value = false"
775
- >
776
- <template #reference>
777
- <button
778
- type="button"
779
- class="ml-ribbon-overflow-trigger"
780
- :aria-label="ribbonTexts.overflowTriggerAriaLabel"
781
- >
782
- <span class="ml-ribbon-overflow-trigger__dots">...</span>
783
- </button>
784
- </template>
785
- <div class="ml-ribbon-overflow-list">
786
- <section v-for="group in classicOverflowGroups" :key="group.id" class="ml-ribbon-overflow-group">
787
- <header class="ml-ribbon-overflow-group__header">{{ group.title }}</header>
788
- <MlRibbonGroupContent
789
- :group="group"
790
- :gallery-preview-fallback="ribbonTexts.galleryPreviewFallback"
791
- @item-click="onItemClick($event.groupId, $event.itemId)"
792
- />
793
- </section>
794
- </div>
795
- </ElPopover>
796
- </div>
797
- <footer class="ml-ribbon-group__footer ml-ribbon-group__footer--overflow" />
798
- </section>
799
- </main>
800
-
801
- <main
802
- v-if="!context.minimized.value && activeTabModel && context.layout.value === 'simplified'"
803
- class="ml-ribbon__panel ml-ribbon__panel--simplified"
804
- :data-active-tab="activeTabModel.id"
805
- >
806
- <ElPopover
807
- v-for="group in visibleGroups"
808
- :key="group.id"
809
- trigger="click"
810
- placement="bottom-start"
811
- :width="340"
812
- :popper-class="simplifiedGroupPopoverClass"
813
- >
814
- <template #reference>
815
- <button class="ml-ribbon-simplified-group" type="button">
816
- <ElIcon v-if="resolveGroupIcon(group)" class="ml-ribbon-simplified-group__icon">
817
- <component :is="resolveGroupIcon(group)" />
818
- </ElIcon>
819
- <span>{{ group.title }}</span>
820
- </button>
821
- </template>
822
- <div class="ml-ribbon-simplified-group__panel">
823
- <MlRibbonGroupContent
824
- :group="toSimplifiedPanelGroup(group)"
825
- :gallery-preview-fallback="ribbonTexts.galleryPreviewFallback"
826
- @item-click="onItemClick($event.groupId, $event.itemId)"
827
- />
828
- </div>
829
- </ElPopover>
830
- </main>
831
-
832
- <MlRibbonBackstage
833
- :open="context.backdropOpen.value"
834
- :items="backstageItems"
835
- :back-label="ribbonTexts.backstageBackLabel"
836
- :title="ribbonTexts.backstageTitle"
837
- :description="ribbonTexts.backstageDescription"
838
- @close="context.backdropOpen.value = false"
839
- />
840
- </section>
841
- </ElConfigProvider>
842
- </template>
843
-
844
-