@fluentui/priority-overflow 9.0.0-beta.1 → 9.0.0-beta.3
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.json +58 -1
- package/CHANGELOG.md +21 -2
- package/dist/index.d.ts +13 -0
- package/lib/debounce.js.map +1 -1
- package/lib/overflowManager.js +36 -4
- package/lib/overflowManager.js.map +1 -1
- package/lib/priorityQueue.js +3 -2
- package/lib/priorityQueue.js.map +1 -1
- package/lib/types.js.map +1 -1
- package/lib-commonjs/debounce.js.map +1 -1
- package/lib-commonjs/index.js.map +1 -1
- package/lib-commonjs/overflowManager.js +36 -4
- package/lib-commonjs/overflowManager.js.map +1 -1
- package/lib-commonjs/priorityQueue.js +3 -2
- package/lib-commonjs/priorityQueue.js.map +1 -1
- package/package.json +2 -2
- package/dist/tsdoc-metadata.json +0 -11
package/CHANGELOG.json
CHANGED
@@ -2,7 +2,64 @@
|
|
2
2
|
"name": "@fluentui/priority-overflow",
|
3
3
|
"entries": [
|
4
4
|
{
|
5
|
-
"date": "
|
5
|
+
"date": "Thu, 13 Oct 2022 10:59:18 GMT",
|
6
|
+
"tag": "@fluentui/priority-overflow_v9.0.0-beta.3",
|
7
|
+
"version": "9.0.0-beta.3",
|
8
|
+
"comments": {
|
9
|
+
"prerelease": [
|
10
|
+
{
|
11
|
+
"author": "lingfangao@hotmail.com",
|
12
|
+
"package": "@fluentui/priority-overflow",
|
13
|
+
"commit": "6440417cb5db157acfb33d9f2c93de9bf7493791",
|
14
|
+
"comment": "feat: Adds API to register overflow menus for better available space calculation"
|
15
|
+
},
|
16
|
+
{
|
17
|
+
"author": "lingfangao@hotmail.com",
|
18
|
+
"package": "@fluentui/priority-overflow",
|
19
|
+
"commit": "d59683655d4d2a3775df4a2b41a09504cddd72ad",
|
20
|
+
"comment": "new overflow items should only be enqueued while observing"
|
21
|
+
}
|
22
|
+
]
|
23
|
+
}
|
24
|
+
},
|
25
|
+
{
|
26
|
+
"date": "Thu, 15 Sep 2022 09:49:45 GMT",
|
27
|
+
"tag": "@fluentui/priority-overflow_v9.0.0-beta.2",
|
28
|
+
"version": "9.0.0-beta.2",
|
29
|
+
"comments": {
|
30
|
+
"none": [
|
31
|
+
{
|
32
|
+
"author": "martinhochel@microsoft.com",
|
33
|
+
"package": "@fluentui/priority-overflow",
|
34
|
+
"commit": "e6cf183695d6d67a24e038c49a876224e5ed35e5",
|
35
|
+
"comment": "chore: update package scaffold"
|
36
|
+
},
|
37
|
+
{
|
38
|
+
"author": "martinhochel@microsoft.com",
|
39
|
+
"package": "@fluentui/priority-overflow",
|
40
|
+
"commit": "16aa65dcae8f75c6a221225fd0eb43800650ac66",
|
41
|
+
"comment": "chore(priority-flow): re-generate api.md"
|
42
|
+
}
|
43
|
+
]
|
44
|
+
}
|
45
|
+
},
|
46
|
+
{
|
47
|
+
"date": "Tue, 28 Jun 2022 15:14:10 GMT",
|
48
|
+
"tag": "@fluentui/priority-overflow_v9.0.0-beta.2",
|
49
|
+
"version": "9.0.0-beta.2",
|
50
|
+
"comments": {
|
51
|
+
"prerelease": [
|
52
|
+
{
|
53
|
+
"author": "lingfangao@hotmail.com",
|
54
|
+
"package": "@fluentui/priority-overflow",
|
55
|
+
"commit": "2da4428ae7d6e464c538b7ed10e425ce0e531144",
|
56
|
+
"comment": "chore: Mark internal APIs with @internal"
|
57
|
+
}
|
58
|
+
]
|
59
|
+
}
|
60
|
+
},
|
61
|
+
{
|
62
|
+
"date": "Mon, 23 May 2022 12:13:58 GMT",
|
6
63
|
"tag": "@fluentui/priority-overflow_v9.0.0-beta.1",
|
7
64
|
"version": "9.0.0-beta.1",
|
8
65
|
"comments": {
|
package/CHANGELOG.md
CHANGED
@@ -1,12 +1,31 @@
|
|
1
1
|
# Change Log - @fluentui/priority-overflow
|
2
2
|
|
3
|
-
This log was last generated on
|
3
|
+
This log was last generated on Thu, 13 Oct 2022 10:59:18 GMT and should not be manually modified.
|
4
4
|
|
5
5
|
<!-- Start content -->
|
6
6
|
|
7
|
+
## [9.0.0-beta.3](https://github.com/microsoft/fluentui/tree/@fluentui/priority-overflow_v9.0.0-beta.3)
|
8
|
+
|
9
|
+
Thu, 13 Oct 2022 10:59:18 GMT
|
10
|
+
[Compare changes](https://github.com/microsoft/fluentui/compare/@fluentui/priority-overflow_v9.0.0-beta.2..@fluentui/priority-overflow_v9.0.0-beta.3)
|
11
|
+
|
12
|
+
### Changes
|
13
|
+
|
14
|
+
- feat: Adds API to register overflow menus for better available space calculation ([PR #25091](https://github.com/microsoft/fluentui/pull/25091) by lingfangao@hotmail.com)
|
15
|
+
- new overflow items should only be enqueued while observing ([PR #25122](https://github.com/microsoft/fluentui/pull/25122) by lingfangao@hotmail.com)
|
16
|
+
|
17
|
+
## [9.0.0-beta.2](https://github.com/microsoft/fluentui/tree/@fluentui/priority-overflow_v9.0.0-beta.2)
|
18
|
+
|
19
|
+
Tue, 28 Jun 2022 15:14:10 GMT
|
20
|
+
[Compare changes](https://github.com/microsoft/fluentui/compare/@fluentui/priority-overflow_v9.0.0-beta.1..@fluentui/priority-overflow_v9.0.0-beta.2)
|
21
|
+
|
22
|
+
### Changes
|
23
|
+
|
24
|
+
- chore: Mark internal APIs with @internal ([PR #23689](https://github.com/microsoft/fluentui/pull/23689) by lingfangao@hotmail.com)
|
25
|
+
|
7
26
|
## [9.0.0-beta.1](https://github.com/microsoft/fluentui/tree/@fluentui/priority-overflow_v9.0.0-beta.1)
|
8
27
|
|
9
|
-
Mon, 23 May 2022 12:
|
28
|
+
Mon, 23 May 2022 12:13:58 GMT
|
10
29
|
|
11
30
|
### Changes
|
12
31
|
|
package/dist/index.d.ts
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
/**
|
2
|
+
* @internal
|
2
3
|
* @returns overflow manager instance
|
3
4
|
*/
|
4
5
|
export declare function createOverflowManager(): OverflowManager;
|
@@ -79,6 +80,9 @@ export declare interface OverflowItemEntry {
|
|
79
80
|
groupId?: string;
|
80
81
|
}
|
81
82
|
|
83
|
+
/**
|
84
|
+
* @internal
|
85
|
+
*/
|
82
86
|
export declare interface OverflowManager {
|
83
87
|
/**
|
84
88
|
* Starts observing the container and managing the overflow state
|
@@ -104,6 +108,15 @@ export declare interface OverflowManager {
|
|
104
108
|
* Manually update the overflow sync
|
105
109
|
*/
|
106
110
|
forceUpdate: () => void;
|
111
|
+
/**
|
112
|
+
* Adds an element that opens an overflow menu. This is used to calculate
|
113
|
+
* available space and check if additional items need to overflow
|
114
|
+
*/
|
115
|
+
addOverflowMenu: (element: HTMLElement) => void;
|
116
|
+
/**
|
117
|
+
* Unsets the overflow menu element
|
118
|
+
*/
|
119
|
+
removeOverflowMenu: () => void;
|
107
120
|
}
|
108
121
|
|
109
122
|
export { }
|
package/lib/debounce.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["debounce.ts"],"names":[],"mappings":"AAAA;;;;;AAKG;AACH,OAAM,SAAU,QAAV,CAAmB,EAAnB,EAA+B;
|
1
|
+
{"version":3,"sources":["debounce.ts"],"names":[],"mappings":"AAAA;;;;;AAKG;AACH,OAAM,SAAU,QAAV,CAAmB,EAAnB,EAA+B;EACnC,IAAI,OAAJ;EACA,OAAO,MAAK;IACV,IAAI,CAAC,OAAL,EAAc;MACZ,OAAO,GAAG,IAAV;MACA,cAAc,CAAC,MAAK;QAClB;QACA;QACA,OAAO,GAAG,KAAV;QACA,EAAE;MACH,CALa,CAAd;IAMD;EACF,CAVD;AAWD","sourcesContent":["/**\n * Microtask debouncer\n * https://developer.mozilla.org/en-US/docs/Web/API/HTML_DOM_API/Microtask_guide\n * @param fn - Function to debounce\n * @returns debounced function\n */\nexport function debounce(fn: Function) {\n let pending: boolean;\n return () => {\n if (!pending) {\n pending = true;\n queueMicrotask(() => {\n // Need to set pending to `false` before the debounced function is run.\n // React can actually interrupt the function while it's running!\n pending = false;\n fn();\n });\n }\n };\n}\n"],"sourceRoot":"../src/"}
|
package/lib/overflowManager.js
CHANGED
@@ -1,11 +1,14 @@
|
|
1
1
|
import { debounce } from './debounce';
|
2
2
|
import { createPriorityQueue } from './priorityQueue';
|
3
3
|
/**
|
4
|
+
* @internal
|
4
5
|
* @returns overflow manager instance
|
5
6
|
*/
|
6
7
|
|
7
8
|
export function createOverflowManager() {
|
8
9
|
let container;
|
10
|
+
let overflowMenu;
|
11
|
+
let observing = false;
|
9
12
|
const options = {
|
10
13
|
padding: 10,
|
11
14
|
overflowAxis: 'horizontal',
|
@@ -118,8 +121,9 @@ export function createOverflowManager() {
|
|
118
121
|
const processOverflowItems = availableSize => {
|
119
122
|
if (!container) {
|
120
123
|
return;
|
121
|
-
}
|
124
|
+
}
|
122
125
|
|
126
|
+
const overflowMenuOffset = overflowMenu ? getOffsetSize(overflowMenu) : 0; // Snapshot of the visible/invisible state to compare for updates
|
123
127
|
|
124
128
|
const visibleTop = visibleItemQueue.peek();
|
125
129
|
const invisibleTop = invisibleItemQueue.peek();
|
@@ -140,6 +144,10 @@ export function createOverflowManager() {
|
|
140
144
|
}
|
141
145
|
|
142
146
|
currentWidth -= makeItemInvisible();
|
147
|
+
}
|
148
|
+
|
149
|
+
if (invisibleItemQueue.size() > 0 && currentWidth + overflowMenuOffset > availableSize) {
|
150
|
+
makeItemInvisible();
|
143
151
|
} // only update when the state of visible/invisible items has changed
|
144
152
|
|
145
153
|
|
@@ -161,17 +169,27 @@ export function createOverflowManager() {
|
|
161
169
|
|
162
170
|
const observe = (observedContainer, userOptions) => {
|
163
171
|
Object.assign(options, userOptions);
|
172
|
+
observing = true;
|
173
|
+
Object.values(overflowItems).forEach(item => visibleItemQueue.enqueue(item.id));
|
164
174
|
container = observedContainer;
|
165
175
|
resizeObserver.observe(container);
|
166
176
|
};
|
167
177
|
|
168
178
|
const disconnect = () => {
|
179
|
+
observing = false;
|
169
180
|
resizeObserver.disconnect();
|
170
181
|
};
|
171
182
|
|
172
183
|
const addItem = item => {
|
173
|
-
overflowItems[item.id]
|
174
|
-
|
184
|
+
if (overflowItems[item.id]) {
|
185
|
+
return;
|
186
|
+
}
|
187
|
+
|
188
|
+
overflowItems[item.id] = item; // some options can affect priority which are only set on `observe`
|
189
|
+
|
190
|
+
if (observing) {
|
191
|
+
visibleItemQueue.enqueue(item.id);
|
192
|
+
}
|
175
193
|
|
176
194
|
if (item.groupId) {
|
177
195
|
if (!overflowGroups[item.groupId]) {
|
@@ -187,7 +205,19 @@ export function createOverflowManager() {
|
|
187
205
|
update();
|
188
206
|
};
|
189
207
|
|
208
|
+
const addOverflowMenu = el => {
|
209
|
+
overflowMenu = el;
|
210
|
+
};
|
211
|
+
|
212
|
+
const removeOverflowMenu = () => {
|
213
|
+
overflowMenu = undefined;
|
214
|
+
};
|
215
|
+
|
190
216
|
const removeItem = itemId => {
|
217
|
+
if (!overflowItems[itemId]) {
|
218
|
+
return;
|
219
|
+
}
|
220
|
+
|
191
221
|
const item = overflowItems[itemId];
|
192
222
|
visibleItemQueue.remove(itemId);
|
193
223
|
invisibleItemQueue.remove(itemId);
|
@@ -207,7 +237,9 @@ export function createOverflowManager() {
|
|
207
237
|
forceUpdate,
|
208
238
|
observe,
|
209
239
|
removeItem,
|
210
|
-
update
|
240
|
+
update,
|
241
|
+
addOverflowMenu,
|
242
|
+
removeOverflowMenu
|
211
243
|
};
|
212
244
|
}
|
213
245
|
//# sourceMappingURL=overflowManager.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["overflowManager.ts"],"names":[],"mappings":"AAAA,SAAS,QAAT,QAAyB,YAAzB;AACA,SAAS,mBAAT,QAAoC,iBAApC;AAGA;;AAEG;;AACH,OAAM,SAAU,qBAAV,GAA+B;AACnC,MAAI,SAAJ;AACA,QAAM,OAAO,GAA6B;AACxC,IAAA,OAAO,EAAE,EAD+B;AAExC,IAAA,YAAY,EAAE,YAF0B;AAGxC,IAAA,iBAAiB,EAAE,KAHqB;AAIxC,IAAA,cAAc,EAAE,CAJwB;AAKxC,IAAA,sBAAsB,EAAE,MAAM,SALU;AAMxC,IAAA,gBAAgB,EAAE,MAAM;AANgB,GAA1C;AASA,QAAM,a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,MAA9C,CAAqD,IAAI,CAAC,EAA1D;AACD;;AAED,WAAO,aAAa,CAAC,MAAD,CAApB;AACA,IAAA,MAAM;AACP,GAZD;;AAcA,SAAO;AACL,IAAA,OADK;AAEL,IAAA,UAFK;AAGL,IAAA,WAHK;AAIL,IAAA,OAJK;AAKL,IAAA,UALK;AAML,IAAA;AANK,GAAP;AAQD","sourcesContent":["import { debounce } from './debounce';\nimport { createPriorityQueue } from './priorityQueue';\nimport type { OverflowGroupState, OverflowItemEntry, OverflowManager, ObserveOptions } from './types';\n\n/**\n * @returns overflow manager instance\n */\nexport function createOverflowManager(): OverflowManager {\n let container: HTMLElement | undefined;\n const options: Required<ObserveOptions> = {\n padding: 10,\n overflowAxis: 'horizontal',\n overflowDirection: 'end',\n minimumVisible: 0,\n onUpdateItemVisibility: () => undefined,\n onUpdateOverflow: () => undefined,\n };\n\n const overflowItems: Record<string, OverflowItemEntry> = {};\n const overflowGroups: Record<string, { visibleItemIds: Set<string>; invisibleItemIds: Set<string> }> = {};\n const resizeObserver = new ResizeObserver(entries => {\n if (!entries[0] || !container) {\n return;\n }\n\n update();\n });\n\n const invisibleItemQueue = createPriorityQueue<string>((a, b) => {\n const itemA = overflowItems[a];\n const itemB = overflowItems[b];\n // Higher priority at the top of the queue\n const priority = itemB.priority - itemA.priority;\n if (priority !== 0) {\n return priority;\n }\n\n const positionStatusBit =\n options.overflowDirection === 'end' ? Node.DOCUMENT_POSITION_FOLLOWING : Node.DOCUMENT_POSITION_PRECEDING;\n\n // equal priority, use DOM order\n // eslint-disable-next-line no-bitwise\n return itemA.element.compareDocumentPosition(itemB.element) & positionStatusBit ? -1 : 1;\n });\n\n const visibleItemQueue = createPriorityQueue<string>((a, b) => {\n const itemA = overflowItems[a];\n const itemB = overflowItems[b];\n // Lower priority at the top of the queue\n const priority = itemA.priority - itemB.priority;\n\n if (priority !== 0) {\n return priority;\n }\n\n const positionStatusBit =\n options.overflowDirection === 'end' ? Node.DOCUMENT_POSITION_PRECEDING : Node.DOCUMENT_POSITION_FOLLOWING;\n\n // equal priority, use DOM order\n // eslint-disable-next-line no-bitwise\n return itemA.element.compareDocumentPosition(itemB.element) & positionStatusBit ? -1 : 1;\n });\n\n const getOffsetSize = (el: HTMLElement) => {\n return options.overflowAxis === 'horizontal' ? el.offsetWidth : el.offsetHeight;\n };\n\n const makeItemVisible = () => {\n const nextVisible = invisibleItemQueue.dequeue();\n visibleItemQueue.enqueue(nextVisible);\n\n const item = overflowItems[nextVisible];\n options.onUpdateItemVisibility({ item, visible: true });\n if (item.groupId) {\n overflowGroups[item.groupId].invisibleItemIds.delete(item.id);\n overflowGroups[item.groupId].visibleItemIds.add(item.id);\n }\n\n return getOffsetSize(item.element);\n };\n\n const makeItemInvisible = () => {\n const nextInvisible = visibleItemQueue.dequeue();\n invisibleItemQueue.enqueue(nextInvisible);\n\n const item = overflowItems[nextInvisible];\n const width = getOffsetSize(item.element);\n options.onUpdateItemVisibility({ item, visible: false });\n if (item.groupId) {\n overflowGroups[item.groupId].visibleItemIds.delete(item.id);\n overflowGroups[item.groupId].invisibleItemIds.add(item.id);\n }\n\n return width;\n };\n\n const dispatchOverflowUpdate = () => {\n const visibleItemIds = visibleItemQueue.all();\n const invisibleItemIds = invisibleItemQueue.all();\n\n const visibleItems = visibleItemIds.map(itemId => overflowItems[itemId]);\n const invisibleItems = invisibleItemIds.map(itemId => overflowItems[itemId]);\n\n const groupVisibility: Record<string, OverflowGroupState> = {};\n Object.entries(overflowGroups).forEach(([groupId, groupState]) => {\n if (groupState.invisibleItemIds.size && groupState.visibleItemIds.size) {\n groupVisibility[groupId] = 'overflow';\n } else if (groupState.visibleItemIds.size === 0) {\n groupVisibility[groupId] = 'hidden';\n } else {\n groupVisibility[groupId] = 'visible';\n }\n });\n\n options.onUpdateOverflow({ visibleItems, invisibleItems, groupVisibility });\n };\n\n const processOverflowItems = (availableSize: number) => {\n if (!container) {\n return;\n }\n\n // Snapshot of the visible/invisible state to compare for updates\n const visibleTop = visibleItemQueue.peek();\n const invisibleTop = invisibleItemQueue.peek();\n\n const visibleItemIds = visibleItemQueue.all();\n let currentWidth = visibleItemIds.reduce((sum, visibleItemId) => {\n const child = overflowItems[visibleItemId].element;\n return sum + getOffsetSize(child);\n }, 0);\n\n // Add items until available width is filled\n while (currentWidth < availableSize && invisibleItemQueue.size() > 0) {\n currentWidth += makeItemVisible();\n }\n // Remove items until there's no more overlap\n while (currentWidth > availableSize && visibleItemQueue.size() > 0) {\n if (visibleItemQueue.size() === options.minimumVisible) {\n break;\n }\n currentWidth -= makeItemInvisible();\n }\n\n // only update when the state of visible/invisible items has changed\n if (visibleItemQueue.peek() !== visibleTop || invisibleItemQueue.peek() !== invisibleTop) {\n dispatchOverflowUpdate();\n }\n };\n\n const forceUpdate: OverflowManager['forceUpdate'] = () => {\n if (!container) {\n return;\n }\n\n const availableSize = getOffsetSize(container) - options.padding;\n processOverflowItems(availableSize);\n };\n\n const update: OverflowManager['update'] = debounce(forceUpdate);\n\n const observe: OverflowManager['observe'] = (observedContainer, userOptions) => {\n Object.assign(options, userOptions);\n container = observedContainer;\n resizeObserver.observe(container);\n };\n\n const disconnect: OverflowManager['disconnect'] = () => {\n resizeObserver.disconnect();\n };\n\n const addItem: OverflowManager['addItem'] = item => {\n overflowItems[item.id] = item;\n visibleItemQueue.enqueue(item.id);\n\n if (item.groupId) {\n if (!overflowGroups[item.groupId]) {\n overflowGroups[item.groupId] = {\n visibleItemIds: new Set<string>(),\n invisibleItemIds: new Set<string>(),\n };\n }\n\n overflowGroups[item.groupId].visibleItemIds.add(item.id);\n }\n\n update();\n };\n\n const removeItem: OverflowManager['removeItem'] = itemId => {\n const item = overflowItems[itemId];\n visibleItemQueue.remove(itemId);\n invisibleItemQueue.remove(itemId);\n\n if (item.groupId) {\n overflowGroups[item.groupId].visibleItemIds.delete(item.id);\n overflowGroups[item.groupId].invisibleItemIds.delete(item.id);\n }\n\n delete overflowItems[itemId];\n update();\n };\n\n return {\n addItem,\n disconnect,\n force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IAED,IAAI,IAAI,CAAC,OAAT,EAAkB;MAChB,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,OAAN,CAAnB,EAAmC;QACjC,cAAc,CAAC,IAAI,CAAC,OAAN,CAAd,GAA+B;UAC7B,cAAc,EAAE,IAAI,GAAJ,EADa;UAE7B,gBAAgB,EAAE,IAAI,GAAJ;QAFW,CAA/B;MAID;;MAED,cAAc,CAAC,IAAI,CAAC,OAAN,CAAd,CAA6B,cAA7B,CAA4C,GAA5C,CAAgD,IAAI,CAAC,EAArD;IACD;;IAED,MAAM;EACP,CAxBD;;EA0BA,MAAM,eAAe,GAAuC,EAAE,IAAG;IAC/D,YAAY,GAAG,EAAf;EACD,CAFD;;EAIA,MAAM,kBAAkB,GAA0C,MAAK;IACrE,YAAY,GAAG,SAAf;EACD,CAFD;;EAIA,MAAM,UAAU,GAAkC,MAAM,IAAG;IACzD,IAAI,CAAC,aAAa,CAAC,MAAD,CAAlB,EAA4B;MAC1B;IACD;;IAED,MAAM,IAAI,GAAG,aAAa,CAAC,MAAD,CAA1B;IACA,gBAAgB,CAAC,MAAjB,CAAwB,MAAxB;IACA,kBAAkB,CAAC,MAAnB,CAA0B,MAA1B;;IAEA,IAAI,IAAI,CAAC,OAAT,EAAkB;MAChB,cAAc,CAAC,IAAI,CAAC,OAAN,CAAd,CAA6B,cAA7B,CAA4C,MAA5C,CAAmD,IAAI,CAAC,EAAxD;MACA,cAAc,CAAC,IAAI,CAAC,OAAN,CAAd,CAA6B,gBAA7B,CAA8C,MAA9C,CAAqD,IAAI,CAAC,EAA1D;IACD;;IAED,OAAO,aAAa,CAAC,MAAD,CAApB;IACA,MAAM;EACP,CAhBD;;EAkBA,OAAO;IACL,OADK;IAEL,UAFK;IAGL,WAHK;IAIL,OAJK;IAKL,UALK;IAML,MANK;IAOL,eAPK;IAQL;EARK,CAAP;AAUD","sourcesContent":["import { debounce } from './debounce';\nimport { createPriorityQueue } from './priorityQueue';\nimport type { OverflowGroupState, OverflowItemEntry, OverflowManager, ObserveOptions } from './types';\n\n/**\n * @internal\n * @returns overflow manager instance\n */\nexport function createOverflowManager(): OverflowManager {\n let container: HTMLElement | undefined;\n let overflowMenu: HTMLElement | undefined;\n let observing = false;\n const options: Required<ObserveOptions> = {\n padding: 10,\n overflowAxis: 'horizontal',\n overflowDirection: 'end',\n minimumVisible: 0,\n onUpdateItemVisibility: () => undefined,\n onUpdateOverflow: () => undefined,\n };\n\n const overflowItems: Record<string, OverflowItemEntry> = {};\n const overflowGroups: Record<string, { visibleItemIds: Set<string>; invisibleItemIds: Set<string> }> = {};\n const resizeObserver = new ResizeObserver(entries => {\n if (!entries[0] || !container) {\n return;\n }\n\n update();\n });\n\n const invisibleItemQueue = createPriorityQueue<string>((a, b) => {\n const itemA = overflowItems[a];\n const itemB = overflowItems[b];\n // Higher priority at the top of the queue\n const priority = itemB.priority - itemA.priority;\n if (priority !== 0) {\n return priority;\n }\n\n const positionStatusBit =\n options.overflowDirection === 'end' ? Node.DOCUMENT_POSITION_FOLLOWING : Node.DOCUMENT_POSITION_PRECEDING;\n\n // equal priority, use DOM order\n // eslint-disable-next-line no-bitwise\n return itemA.element.compareDocumentPosition(itemB.element) & positionStatusBit ? -1 : 1;\n });\n\n const visibleItemQueue = createPriorityQueue<string>((a, b) => {\n const itemA = overflowItems[a];\n const itemB = overflowItems[b];\n // Lower priority at the top of the queue\n const priority = itemA.priority - itemB.priority;\n\n if (priority !== 0) {\n return priority;\n }\n\n const positionStatusBit =\n options.overflowDirection === 'end' ? Node.DOCUMENT_POSITION_PRECEDING : Node.DOCUMENT_POSITION_FOLLOWING;\n\n // equal priority, use DOM order\n // eslint-disable-next-line no-bitwise\n return itemA.element.compareDocumentPosition(itemB.element) & positionStatusBit ? -1 : 1;\n });\n\n const getOffsetSize = (el: HTMLElement) => {\n return options.overflowAxis === 'horizontal' ? el.offsetWidth : el.offsetHeight;\n };\n\n const makeItemVisible = () => {\n const nextVisible = invisibleItemQueue.dequeue();\n visibleItemQueue.enqueue(nextVisible);\n\n const item = overflowItems[nextVisible];\n options.onUpdateItemVisibility({ item, visible: true });\n if (item.groupId) {\n overflowGroups[item.groupId].invisibleItemIds.delete(item.id);\n overflowGroups[item.groupId].visibleItemIds.add(item.id);\n }\n\n return getOffsetSize(item.element);\n };\n\n const makeItemInvisible = () => {\n const nextInvisible = visibleItemQueue.dequeue();\n invisibleItemQueue.enqueue(nextInvisible);\n\n const item = overflowItems[nextInvisible];\n const width = getOffsetSize(item.element);\n options.onUpdateItemVisibility({ item, visible: false });\n if (item.groupId) {\n overflowGroups[item.groupId].visibleItemIds.delete(item.id);\n overflowGroups[item.groupId].invisibleItemIds.add(item.id);\n }\n\n return width;\n };\n\n const dispatchOverflowUpdate = () => {\n const visibleItemIds = visibleItemQueue.all();\n const invisibleItemIds = invisibleItemQueue.all();\n\n const visibleItems = visibleItemIds.map(itemId => overflowItems[itemId]);\n const invisibleItems = invisibleItemIds.map(itemId => overflowItems[itemId]);\n\n const groupVisibility: Record<string, OverflowGroupState> = {};\n Object.entries(overflowGroups).forEach(([groupId, groupState]) => {\n if (groupState.invisibleItemIds.size && groupState.visibleItemIds.size) {\n groupVisibility[groupId] = 'overflow';\n } else if (groupState.visibleItemIds.size === 0) {\n groupVisibility[groupId] = 'hidden';\n } else {\n groupVisibility[groupId] = 'visible';\n }\n });\n\n options.onUpdateOverflow({ visibleItems, invisibleItems, groupVisibility });\n };\n\n const processOverflowItems = (availableSize: number) => {\n if (!container) {\n return;\n }\n\n const overflowMenuOffset = overflowMenu ? getOffsetSize(overflowMenu) : 0;\n\n // Snapshot of the visible/invisible state to compare for updates\n const visibleTop = visibleItemQueue.peek();\n const invisibleTop = invisibleItemQueue.peek();\n\n const visibleItemIds = visibleItemQueue.all();\n let currentWidth = visibleItemIds.reduce((sum, visibleItemId) => {\n const child = overflowItems[visibleItemId].element;\n return sum + getOffsetSize(child);\n }, 0);\n\n // Add items until available width is filled\n while (currentWidth < availableSize && invisibleItemQueue.size() > 0) {\n currentWidth += makeItemVisible();\n }\n // Remove items until there's no more overlap\n while (currentWidth > availableSize && visibleItemQueue.size() > 0) {\n if (visibleItemQueue.size() === options.minimumVisible) {\n break;\n }\n currentWidth -= makeItemInvisible();\n }\n\n if (invisibleItemQueue.size() > 0 && currentWidth + overflowMenuOffset > availableSize) {\n makeItemInvisible();\n }\n\n // only update when the state of visible/invisible items has changed\n if (visibleItemQueue.peek() !== visibleTop || invisibleItemQueue.peek() !== invisibleTop) {\n dispatchOverflowUpdate();\n }\n };\n\n const forceUpdate: OverflowManager['forceUpdate'] = () => {\n if (!container) {\n return;\n }\n\n const availableSize = getOffsetSize(container) - options.padding;\n processOverflowItems(availableSize);\n };\n\n const update: OverflowManager['update'] = debounce(forceUpdate);\n\n const observe: OverflowManager['observe'] = (observedContainer, userOptions) => {\n Object.assign(options, userOptions);\n observing = true;\n Object.values(overflowItems).forEach(item => visibleItemQueue.enqueue(item.id));\n\n container = observedContainer;\n resizeObserver.observe(container);\n };\n\n const disconnect: OverflowManager['disconnect'] = () => {\n observing = false;\n resizeObserver.disconnect();\n };\n\n const addItem: OverflowManager['addItem'] = item => {\n if (overflowItems[item.id]) {\n return;\n }\n\n overflowItems[item.id] = item;\n\n // some options can affect priority which are only set on `observe`\n if (observing) {\n visibleItemQueue.enqueue(item.id);\n }\n\n if (item.groupId) {\n if (!overflowGroups[item.groupId]) {\n overflowGroups[item.groupId] = {\n visibleItemIds: new Set<string>(),\n invisibleItemIds: new Set<string>(),\n };\n }\n\n overflowGroups[item.groupId].visibleItemIds.add(item.id);\n }\n\n update();\n };\n\n const addOverflowMenu: OverflowManager['addOverflowMenu'] = el => {\n overflowMenu = el;\n };\n\n const removeOverflowMenu: OverflowManager['removeOverflowMenu'] = () => {\n overflowMenu = undefined;\n };\n\n const removeItem: OverflowManager['removeItem'] = itemId => {\n if (!overflowItems[itemId]) {\n return;\n }\n\n const item = overflowItems[itemId];\n visibleItemQueue.remove(itemId);\n invisibleItemQueue.remove(itemId);\n\n if (item.groupId) {\n overflowGroups[item.groupId].visibleItemIds.delete(item.id);\n overflowGroups[item.groupId].invisibleItemIds.delete(item.id);\n }\n\n delete overflowItems[itemId];\n update();\n };\n\n return {\n addItem,\n disconnect,\n forceUpdate,\n observe,\n removeItem,\n update,\n addOverflowMenu,\n removeOverflowMenu,\n };\n}\n"],"sourceRoot":"../src/"}
|
package/lib/priorityQueue.js
CHANGED
@@ -75,13 +75,14 @@ export function createPriorityQueue(compare) {
|
|
75
75
|
};
|
76
76
|
|
77
77
|
const contains = item => {
|
78
|
-
|
78
|
+
const index = arr.indexOf(item);
|
79
|
+
return index >= 0 && index < size;
|
79
80
|
};
|
80
81
|
|
81
82
|
const remove = item => {
|
82
83
|
const i = arr.indexOf(item);
|
83
84
|
|
84
|
-
if (i === -1) {
|
85
|
+
if (i === -1 || i >= size) {
|
85
86
|
return;
|
86
87
|
}
|
87
88
|
|
package/lib/priorityQueue.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["priorityQueue.ts"],"names":[],"mappings":"AAaA;;;AAGG;AACH,OAAM,SAAU,mBAAV,CAAiC,OAAjC,EAAmE;
|
1
|
+
{"version":3,"sources":["priorityQueue.ts"],"names":[],"mappings":"AAaA;;;AAGG;AACH,OAAM,SAAU,mBAAV,CAAiC,OAAjC,EAAmE;EACvE,MAAM,GAAG,GAAQ,EAAjB;EACA,IAAI,IAAI,GAAG,CAAX;;EAEA,MAAM,IAAI,GAAI,CAAD,IAAc;IACzB,OAAO,IAAI,CAAJ,GAAQ,CAAf;EACD,CAFD;;EAIA,MAAM,KAAK,GAAI,CAAD,IAAc;IAC1B,OAAO,IAAI,CAAJ,GAAQ,CAAf;EACD,CAFD;;EAIA,MAAM,MAAM,GAAI,CAAD,IAAc;IAC3B,OAAO,IAAI,CAAC,KAAL,CAAW,CAAC,CAAC,GAAG,CAAL,IAAU,CAArB,CAAP;EACD,CAFD;;EAIA,MAAM,IAAI,GAAG,CAAC,CAAD,EAAY,CAAZ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ue<T> {\n all: () => T[];\n clear: () => void;\n contains: (item: T) => boolean;\n dequeue: () => T;\n enqueue: (item: T) => void;\n peek: () => T | null;\n remove: (item: T) => void;\n size: () => number;\n}\n\n/**\n * @param compare - comparison function for items\n * @returns Priority queue implemented with a min heap\n */\nexport function createPriorityQueue<T>(compare: PriorityQueueCompareFn<T>): PriorityQueue<T> {\n const arr: T[] = [];\n let size = 0;\n\n const left = (i: number) => {\n return 2 * i + 1;\n };\n\n const right = (i: number) => {\n return 2 * i + 2;\n };\n\n const parent = (i: number) => {\n return Math.floor((i - 1) / 2);\n };\n\n const swap = (a: number, b: number) => {\n const tmp = arr[a];\n arr[a] = arr[b];\n arr[b] = tmp;\n };\n\n const heapify = (i: number) => {\n let smallest = i;\n const l = left(i);\n const r = right(i);\n\n if (l < size && compare(arr[l], arr[smallest]) < 0) {\n smallest = l;\n }\n\n if (r < size && compare(arr[r], arr[smallest]) < 0) {\n smallest = r;\n }\n\n if (smallest !== i) {\n swap(smallest, i);\n heapify(smallest);\n }\n };\n\n const dequeue = () => {\n if (size === 0) {\n throw new Error('Priority queue empty');\n }\n\n const res = arr[0];\n arr[0] = arr[--size];\n heapify(0);\n\n return res;\n };\n\n const peek = () => {\n if (size === 0) {\n return null;\n }\n\n return arr[0];\n };\n\n const enqueue = (item: T) => {\n arr[size++] = item;\n let i = size - 1;\n let p = parent(i);\n while (i > 0 && compare(arr[p], arr[i]) > 0) {\n swap(p, i);\n i = p;\n p = parent(i);\n }\n };\n\n const contains = (item: T) => {\n const index = arr.indexOf(item);\n return index >= 0 && index < size;\n };\n\n const remove = (item: T) => {\n const i = arr.indexOf(item);\n\n if (i === -1 || i >= size) {\n return;\n }\n\n arr[i] = arr[--size];\n heapify(i);\n };\n\n const clear = () => {\n size = 0;\n };\n\n const all = () => {\n return arr.slice(0, size);\n };\n\n return {\n all,\n clear,\n contains,\n dequeue,\n enqueue,\n peek,\n remove,\n size: () => size,\n };\n}\n"],"sourceRoot":"../src/"}
|
package/lib/types.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"../src/","sources":["types.ts"],"names":[],"mappings":"","sourcesContent":["export type OverflowDirection = 'start' | 'end';\nexport type OverflowAxis = 'horizontal' | 'vertical';\nexport type OverflowGroupState = 'visible' | 'hidden' | 'overflow';\nexport interface OverflowItemEntry {\n /**\n * HTML element that will be disappear when overflowed\n */\n element: HTMLElement;\n /**\n * Lower priority items are invisible first when the container is overflowed\n * @default 0\n */\n priority: number;\n /**\n * Specific id, used to track visibility and provide updates to consumers\n */\n id: string;\n\n groupId?: string;\n}\n\n/**\n * signature similar to standard event listeners, but typed to handle the custom event\n */\nexport type OnUpdateOverflow = (data: OverflowEventPayload) => void;\n\nexport type OnUpdateItemVisibility = (data: OnUpdateItemVisibilityPayload) => void;\n\n/**\n * Payload of the custom DOM event for overflow updates\n */\nexport interface OverflowEventPayload {\n visibleItems: OverflowItemEntry[];\n invisibleItems: OverflowItemEntry[];\n groupVisibility: Record<string, OverflowGroupState>;\n}\n\nexport interface OnUpdateItemVisibilityPayload {\n item: OverflowItemEntry;\n visible: boolean;\n}\n\nexport interface ObserveOptions {\n /**\n * Padding (in px) at the end of the container before overflow occurs\n * Useful to account for extra elements (i.e. dropdown menu)\n * or to account for any kinds of margins between items which are hard to measure with JS\n * @default 10\n */\n padding?: number;\n /**\n * Direction where items are removed when overflow occurs\n * @default end\n */\n overflowDirection?: OverflowDirection;\n\n /**\n * Horizontal or vertical overflow\n * @default horizontal\n */\n overflowAxis?: OverflowAxis;\n\n /**\n * The minimum number of visible items\n */\n minimumVisible?: number;\n\n /**\n * Callback when item visibility is updated\n */\n onUpdateItemVisibility: OnUpdateItemVisibility;\n\n /**\n * Callback when item visibility is updated\n */\n onUpdateOverflow: OnUpdateOverflow;\n}\n\nexport interface OverflowManager {\n /**\n * Starts observing the container and managing the overflow state\n */\n observe: (container: HTMLElement, options: ObserveOptions) => void;\n /**\n * Stops observing the container\n */\n disconnect: () => void;\n /**\n * Add overflow items\n */\n addItem: (items: OverflowItemEntry) => void;\n /**\n * Remove overflow item\n */\n removeItem: (itemId: string) => void;\n /**\n * Manually update the overflow, updates are batched and async\n */\n update: () => void;\n /**\n * Manually update the overflow sync\n */\n forceUpdate: () => void;\n}\n"]}
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"../src/","sources":["types.ts"],"names":[],"mappings":"","sourcesContent":["export type OverflowDirection = 'start' | 'end';\nexport type OverflowAxis = 'horizontal' | 'vertical';\nexport type OverflowGroupState = 'visible' | 'hidden' | 'overflow';\nexport interface OverflowItemEntry {\n /**\n * HTML element that will be disappear when overflowed\n */\n element: HTMLElement;\n /**\n * Lower priority items are invisible first when the container is overflowed\n * @default 0\n */\n priority: number;\n /**\n * Specific id, used to track visibility and provide updates to consumers\n */\n id: string;\n\n groupId?: string;\n}\n\n/**\n * signature similar to standard event listeners, but typed to handle the custom event\n */\nexport type OnUpdateOverflow = (data: OverflowEventPayload) => void;\n\nexport type OnUpdateItemVisibility = (data: OnUpdateItemVisibilityPayload) => void;\n\n/**\n * Payload of the custom DOM event for overflow updates\n */\nexport interface OverflowEventPayload {\n visibleItems: OverflowItemEntry[];\n invisibleItems: OverflowItemEntry[];\n groupVisibility: Record<string, OverflowGroupState>;\n}\n\nexport interface OnUpdateItemVisibilityPayload {\n item: OverflowItemEntry;\n visible: boolean;\n}\n\nexport interface ObserveOptions {\n /**\n * Padding (in px) at the end of the container before overflow occurs\n * Useful to account for extra elements (i.e. dropdown menu)\n * or to account for any kinds of margins between items which are hard to measure with JS\n * @default 10\n */\n padding?: number;\n /**\n * Direction where items are removed when overflow occurs\n * @default end\n */\n overflowDirection?: OverflowDirection;\n\n /**\n * Horizontal or vertical overflow\n * @default horizontal\n */\n overflowAxis?: OverflowAxis;\n\n /**\n * The minimum number of visible items\n */\n minimumVisible?: number;\n\n /**\n * Callback when item visibility is updated\n */\n onUpdateItemVisibility: OnUpdateItemVisibility;\n\n /**\n * Callback when item visibility is updated\n */\n onUpdateOverflow: OnUpdateOverflow;\n}\n\n/**\n * @internal\n */\nexport interface OverflowManager {\n /**\n * Starts observing the container and managing the overflow state\n */\n observe: (container: HTMLElement, options: ObserveOptions) => void;\n /**\n * Stops observing the container\n */\n disconnect: () => void;\n /**\n * Add overflow items\n */\n addItem: (items: OverflowItemEntry) => void;\n /**\n * Remove overflow item\n */\n removeItem: (itemId: string) => void;\n /**\n * Manually update the overflow, updates are batched and async\n */\n update: () => void;\n /**\n * Manually update the overflow sync\n */\n forceUpdate: () => void;\n\n /**\n * Adds an element that opens an overflow menu. This is used to calculate\n * available space and check if additional items need to overflow\n */\n addOverflowMenu: (element: HTMLElement) => void;\n\n /**\n * Unsets the overflow menu element\n */\n removeOverflowMenu: () => void;\n}\n"]}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["debounce.ts"],"names":[],"mappings":";;;;;;AAAA;;;;;AAKG;;AACH,SAAgB,QAAhB,CAAyB,EAAzB,EAAqC;
|
1
|
+
{"version":3,"sources":["debounce.ts"],"names":[],"mappings":";;;;;;AAAA;;;;;AAKG;;AACH,SAAgB,QAAhB,CAAyB,EAAzB,EAAqC;EACnC,IAAI,OAAJ;EACA,OAAO,MAAK;IACV,IAAI,CAAC,OAAL,EAAc;MACZ,OAAO,GAAG,IAAV;MACA,cAAc,CAAC,MAAK;QAClB;QACA;QACA,OAAO,GAAG,KAAV;QACA,EAAE;MACH,CALa,CAAd;IAMD;EACF,CAVD;AAWD;;AAbD,OAAA,CAAA,QAAA,GAAA,QAAA","sourcesContent":["/**\n * Microtask debouncer\n * https://developer.mozilla.org/en-US/docs/Web/API/HTML_DOM_API/Microtask_guide\n * @param fn - Function to debounce\n * @returns debounced function\n */\nexport function debounce(fn: Function) {\n let pending: boolean;\n return () => {\n if (!pending) {\n pending = true;\n queueMicrotask(() => {\n // Need to set pending to `false` before the debounced function is run.\n // React can actually interrupt the function while it's running!\n pending = false;\n fn();\n });\n }\n };\n}\n"],"sourceRoot":"../src/"}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["index.ts"],"names":[],"mappings":";;;;;;;AAAA,IAAA,iBAAA,gBAAA,OAAA,CAAA,mBAAA,CAAA;;AAAS,MAAA,CAAA,cAAA,CAAA,OAAA,EAAA,uBAAA,EAAA;
|
1
|
+
{"version":3,"sources":["index.ts"],"names":[],"mappings":";;;;;;;AAAA,IAAA,iBAAA,gBAAA,OAAA,CAAA,mBAAA,CAAA;;AAAS,MAAA,CAAA,cAAA,CAAA,OAAA,EAAA,uBAAA,EAAA;EAAA,UAAA,EAAA,IAAA;EAAA,GAAA,EAAA,YAAA;IAAA,OAAA,iBAAA,CAAA,qBAAA;EAAqB;AAArB,CAAA","sourcesContent":["export { createOverflowManager } from './overflowManager';\nexport type {\n ObserveOptions,\n OnUpdateItemVisibility,\n OnUpdateItemVisibilityPayload,\n OnUpdateOverflow,\n OverflowAxis,\n OverflowDirection,\n OverflowEventPayload,\n OverflowGroupState,\n OverflowItemEntry,\n OverflowManager,\n} from './types';\n"],"sourceRoot":"../src/"}
|
@@ -9,12 +9,15 @@ const debounce_1 = /*#__PURE__*/require("./debounce");
|
|
9
9
|
|
10
10
|
const priorityQueue_1 = /*#__PURE__*/require("./priorityQueue");
|
11
11
|
/**
|
12
|
+
* @internal
|
12
13
|
* @returns overflow manager instance
|
13
14
|
*/
|
14
15
|
|
15
16
|
|
16
17
|
function createOverflowManager() {
|
17
18
|
let container;
|
19
|
+
let overflowMenu;
|
20
|
+
let observing = false;
|
18
21
|
const options = {
|
19
22
|
padding: 10,
|
20
23
|
overflowAxis: 'horizontal',
|
@@ -127,8 +130,9 @@ function createOverflowManager() {
|
|
127
130
|
const processOverflowItems = availableSize => {
|
128
131
|
if (!container) {
|
129
132
|
return;
|
130
|
-
}
|
133
|
+
}
|
131
134
|
|
135
|
+
const overflowMenuOffset = overflowMenu ? getOffsetSize(overflowMenu) : 0; // Snapshot of the visible/invisible state to compare for updates
|
132
136
|
|
133
137
|
const visibleTop = visibleItemQueue.peek();
|
134
138
|
const invisibleTop = invisibleItemQueue.peek();
|
@@ -149,6 +153,10 @@ function createOverflowManager() {
|
|
149
153
|
}
|
150
154
|
|
151
155
|
currentWidth -= makeItemInvisible();
|
156
|
+
}
|
157
|
+
|
158
|
+
if (invisibleItemQueue.size() > 0 && currentWidth + overflowMenuOffset > availableSize) {
|
159
|
+
makeItemInvisible();
|
152
160
|
} // only update when the state of visible/invisible items has changed
|
153
161
|
|
154
162
|
|
@@ -170,17 +178,27 @@ function createOverflowManager() {
|
|
170
178
|
|
171
179
|
const observe = (observedContainer, userOptions) => {
|
172
180
|
Object.assign(options, userOptions);
|
181
|
+
observing = true;
|
182
|
+
Object.values(overflowItems).forEach(item => visibleItemQueue.enqueue(item.id));
|
173
183
|
container = observedContainer;
|
174
184
|
resizeObserver.observe(container);
|
175
185
|
};
|
176
186
|
|
177
187
|
const disconnect = () => {
|
188
|
+
observing = false;
|
178
189
|
resizeObserver.disconnect();
|
179
190
|
};
|
180
191
|
|
181
192
|
const addItem = item => {
|
182
|
-
overflowItems[item.id]
|
183
|
-
|
193
|
+
if (overflowItems[item.id]) {
|
194
|
+
return;
|
195
|
+
}
|
196
|
+
|
197
|
+
overflowItems[item.id] = item; // some options can affect priority which are only set on `observe`
|
198
|
+
|
199
|
+
if (observing) {
|
200
|
+
visibleItemQueue.enqueue(item.id);
|
201
|
+
}
|
184
202
|
|
185
203
|
if (item.groupId) {
|
186
204
|
if (!overflowGroups[item.groupId]) {
|
@@ -196,7 +214,19 @@ function createOverflowManager() {
|
|
196
214
|
update();
|
197
215
|
};
|
198
216
|
|
217
|
+
const addOverflowMenu = el => {
|
218
|
+
overflowMenu = el;
|
219
|
+
};
|
220
|
+
|
221
|
+
const removeOverflowMenu = () => {
|
222
|
+
overflowMenu = undefined;
|
223
|
+
};
|
224
|
+
|
199
225
|
const removeItem = itemId => {
|
226
|
+
if (!overflowItems[itemId]) {
|
227
|
+
return;
|
228
|
+
}
|
229
|
+
|
200
230
|
const item = overflowItems[itemId];
|
201
231
|
visibleItemQueue.remove(itemId);
|
202
232
|
invisibleItemQueue.remove(itemId);
|
@@ -216,7 +246,9 @@ function createOverflowManager() {
|
|
216
246
|
forceUpdate,
|
217
247
|
observe,
|
218
248
|
removeItem,
|
219
|
-
update
|
249
|
+
update,
|
250
|
+
addOverflowMenu,
|
251
|
+
removeOverflowMenu
|
220
252
|
};
|
221
253
|
}
|
222
254
|
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["overflowManager.ts"],"names":[],"mappings":";;;;;;;AAAA,MAAA,UAAA,gBAAA,OAAA,CAAA,YAAA,CAAA;;AACA,MAAA,eAAA,gBAAA,OAAA,CAAA,iBAAA,CAAA;AAGA;;AAEG;;;AACH,SAAgB,qBAAhB,GAAqC;AACnC,MAAI,SAAJ;AACA,QAAM,OAAO,GAA6B;AACxC,IAAA,OAAO,EAAE,EAD+B;AAExC,IAAA,YAAY,EAAE,YAF0B;AAGxC,IAAA,iBAAiB,EAAE,KAHqB;AAIxC,IAAA,cAAc,EAAE,CAJwB;AAKxC,IAAA,sBAAsB,EAAE,MAAM,SALU;AAMxC,IAAA,gBAAgB,EAAE,MAAM;AANgB,GAA1C;AASA,QAAM,aAAa,GAAsC,EAAzD;AACA,QAAM,cAAc,GAAmF,EAAvG;AACA,QAAM,cAAc,GAAG,IAAI,cAAJ,CAAmB,OAAO,IAAG;AAClD,QAAI,CAAC,OAAO,CAAC,CAAD,CAAR,IAAe,CAAC,SAApB,EAA+B;AAC7B;AACD;;AAED,IAAA,MAAM;AACP,GANsB,CAAvB;AAQA,QAAM,kBAAkB,GAAG,eAAA,CAAA,mBAAA,CAA4B,CAAC,CAAD,EAAI,CAAJ,KAAS;AAC9D,UAAM,KAAK,GAAG,aAAa,CAAC,CAAD,CAA3B;AACA,UAAM,KAAK,GAAG,aAAa,CAAC,CAAD,CAA3B,CAF8D,CAG9D;;AACA,UAAM,QAAQ,GAAG,KAAK,CAAC,QAAN,GAAiB,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,EAA9B;;AAEA,QAAI,IAAI,CAAC,OAAT,EAAkB;AAChB,UAAI,CAAC,cAAc,CAAC,IAAI,CAAC,OAAN,CAAnB,EAAmC;AACjC,QAAA,cAAc,CAAC,IAAI,CAAC,OAAN,CAAd,GAA+B;AAC7B,UAAA,cAAc,EAAE,IAAI,GAAJ,EADa;AAE7B,UAAA,gBAAgB,EAAE,IAAI,GAAJ;AAFW,SAA/B;AAID;;AAED,MAAA,cAAc,CAAC,IAAI,CAAC,OAAN,CAAd,CAA6B,cAA7B,CAA4C,GAA5C,CAAgD,IAAI,CAAC,EAArD;AACD;;AAED,IAAA,MAAM;AACP,GAhBD;;AAkBA,QAAM,UAAU,GAAkC,MAAM,IAAG;AACzD,UAAM,IAAI,GAAG,aAAa,CAAC,MAAD,CAA1B;AACA,IAAA,gBAAgB,CAAC,MAAjB,CAAwB,MAAxB;AACA,IAAA,kBAAkB,CAAC,MAAnB,CAA0B,MAA1B;;AAEA,QAAI,IAAI,CAAC,OAAT,EAAkB;AAChB,MAAA,cAAc,CAAC,IAAI,CAAC,OAAN,CAAd,CAA6B,cAA7B,CAA4C,MAA5C,CAAmD,IAAI,CAAC,EAAxD;AACA,MAAA,cAAc,CAAC,IAAI,CAAC,OAAN,CAAd,CAA6B,gBAA7B,CAA8C,MAA9C,CAAqD,IAAI,CAAC,EAA1D;AACD;;AAED,WAAO,aAAa,CAAC,MAAD,CAApB;AACA,IAAA,MAAM;AACP,GAZD;;AAcA,SAAO;AACL,IAAA,OADK;AAEL,IAAA,UAFK;AAGL,IAAA,WAHK;AAIL,IAAA,OAJK;AAKL,IAAA,UALK;AAML,IAAA;AANK,GAAP;AAQD;;AA5MD,OAAA,CAAA,qBAAA,GAAA,qBAAA","sourcesContent":["import { debounce } from './debounce';\nimport { createPriorityQueue } from './priorityQueue';\nimport type { OverflowGroupState, OverflowItemEntry, OverflowManager, ObserveOptions } from './types';\n\n/**\n * @returns overflow manager instance\n */\nexport function createOverflowManager(): OverflowManager {\n let container: HTMLElement | undefined;\n const options: Required<ObserveOptions> = {\n padding: 10,\n overflowAxis: 'horizontal',\n overflowDirection: 'end',\n minimumVisible: 0,\n onUpdateItemVisibility: () => undefined,\n onUpdateOverflow: () => undefined,\n };\n\n const overflowItems: Record<string, OverflowItemEntry> = {};\n const overflowGroups: Record<string, { visibleItemIds: Set<string>; invisibleItemIds: Set<string> }> = {};\n const resizeObserver = new ResizeObserver(entries => {\n if (!entries[0] || !container) {\n return;\n }\n\n update();\n });\n\n const invisibleItemQueue = createPriorityQueue<string>((a, b) => {\n const itemA = overflowItems[a];\n const itemB = overflowItems[b];\n // Higher priority at the top of the queue\n const priority = itemB.priority - itemA.priority;\n if (priority !== 0) {\n return priority;\n }\n\n const positionStatusBit =\n options.overflowDirection === 'end' ? Node.DOCUMENT_POSITION_FOLLOWING : Node.DOCUMENT_POSITION_PRECEDING;\n\n // equal priority, use DOM order\n // eslint-disable-next-line no-bitwise\n return itemA.element.compareDocumentPosition(itemB.element) & positionStatusBit ? -1 : 1;\n });\n\n const visibleItemQueue = createPriorityQueue<string>((a, b) => {\n const itemA = overflowItems[a];\n const itemB = overflowItems[b];\n // Lower priority at the top of the queue\n const priority = itemA.priority - itemB.priority;\n\n if (priority !== 0) {\n return priority;\n }\n\n const positionStatusBit =\n options.overflowDirection === 'end' ? Node.DOCUMENT_POSITION_PRECEDING : Node.DOCUMENT_POSITION_FOLLOWING;\n\n // equal priority, use DOM order\n // eslint-disable-next-line no-bitwise\n return itemA.element.compareDocumentPosition(itemB.element) & positionStatusBit ? -1 : 1;\n });\n\n const getOffsetSize = (el: HTMLElement) => {\n return options.overflowAxis === 'horizontal' ? el.offsetWidth : el.offsetHeight;\n };\n\n const makeItemVisible = () => {\n const nextVisible = invisibleItemQueue.dequeue();\n visibleItemQueue.enqueue(nextVisible);\n\n const item = overflowItems[nextVisible];\n options.onUpdateItemVisibility({ item, visible: true });\n if (item.groupId) {\n overflowGroups[item.groupId].invisibleItemIds.delete(item.id);\n overflowGroups[item.groupId].visibleItemIds.add(item.id);\n }\n\n return getOffsetSize(item.element);\n };\n\n const makeItemInvisible = () => {\n const nextInvisible = visibleItemQueue.dequeue();\n invisibleItemQueue.enqueue(nextInvisible);\n\n const item = overflowItems[nextInvisible];\n const width = getOffsetSize(item.element);\n options.onUpdateItemVisibility({ item, visible: false });\n if (item.groupId) {\n overflowGroups[item.groupId].visibleItemIds.delete(item.id);\n overflowGroups[item.groupId].invisibleItemIds.add(item.id);\n }\n\n return width;\n };\n\n const dispatchOverflowUpdate = () => {\n const visibleItemIds = visibleItemQueue.all();\n const invisibleItemIds = invisibleItemQueue.all();\n\n const visibleItems = visibleItemIds.map(itemId => overflowItems[itemId]);\n const invisibleItems = invisibleItemIds.map(itemId => overflowItems[itemId]);\n\n const groupVisibility: Record<string, OverflowGroupState> = {};\n Object.entries(overflowGroups).forEach(([groupId, groupState]) => {\n if (groupState.invisibleItemIds.size && groupState.visibleItemIds.size) {\n groupVisibility[groupId] = 'overflow';\n } else if (groupState.visibleItemIds.size === 0) {\n groupVisibility[groupId] = 'hidden';\n } else {\n groupVisibility[groupId] = 'visible';\n }\n });\n\n options.onUpdateOverflow({ visibleItems, invisibleItems, groupVisibility });\n };\n\n const processOverflowItems = (availableSize: number) => {\n if (!container) {\n return;\n }\n\n // Snapshot of the visible/invisible state to compare for updates\n const visibleTop = visibleItemQueue.peek();\n const invisibleTop = invisibleItemQueue.peek();\n\n const visibleItemIds = visibleItemQueue.all();\n let currentWidth = visibleItemIds.reduce((sum, visibleItemId) => {\n const child = overflowItems[visibleItemId].element;\n return sum + getOffsetSize(child);\n }, 0);\n\n // Add items until available width is filled\n while (currentWidth < availableSize && invisibleItemQueue.size() > 0) {\n currentWidth += makeItemVisible();\n }\n // Remove items until there's no more overlap\n while (currentWidth > availableSize && visibleItemQueue.size() > 0) {\n if (visibleItemQueue.size() === options.minimumVisible) {\n break;\n }\n currentWidth -= makeItemInvisible();\n }\n\n // only update when the state of visible/invisible items has changed\n if (visibleItemQueue.peek() !== visibleTop || invisibleItemQueue.peek() !== invisibleTop) {\n dispatchOverflowUpdate();\n }\n };\n\n const forceUpdate: OverflowManager['forceUpdate'] = () => {\n if (!container) {\n return;\n }\n\n const availableSize = getOffsetSize(container) - options.padding;\n processOverflowItems(availableSize);\n };\n\n const update: OverflowManager['update'] = debounce(forceUpdate);\n\n const observe: OverflowManager['observe'] = (observedContainer, userOptions) => {\n Object.assign(options, userOptions);\n container = observedContainer;\n resizeObserver.observe(container);\n };\n\n const disconnect: OverflowManager['disconnect'] = () => {\n resizeObserver.disconnect();\n };\n\n const addItem: OverflowManager['addItem'] = item => {\n overflowItems[item.id] = item;\n visibleItemQueue.enqueue(item.id);\n\n if (item.groupId) {\n if (!overflowGroups[item.groupId]) {\n overflowGroups[item.groupId] = {\n visibleItemIds: new Set<string>(),\n invisibleItemIds: new Set<string>(),\n };\n }\n\n overflowGroups[item.groupId].visibleItemIds.add(item.id);\n }\n\n update();\n };\n\n const removeItem: OverflowManager['removeItem'] = itemId => {\n const item = overflowItems[itemId];\n visibleItemQueue.remove(itemId);\n invisibleItemQueue.remove(itemId);\n\n if (item.groupId) {\n overflowGroups[item.groupId].visibleItemIds.delete(item.id);\n overflowGroups[item.groupId].invisibleItemIds.delete(item.id);\n }\n\n delete overflowItems[itemId];\n update();\n };\n\n return {\n addItem,\n disconnect,\n forceUpdate,\n observe,\n removeItem,\n update,\n };\n}\n"],"sourceRoot":"../src/"}
|
1
|
+
{"version":3,"sources":["overflowManager.ts"],"names":[],"mappings":";;;;;;;AAAA,MAAA,UAAA,gBAAA,OAAA,CAAA,YAAA,CAAA;;AACA,MAAA,eAAA,gBAAA,OAAA,CAAA,iBAAA,CAAA;AAGA;;;AAGG;;;AACH,SAAgB,qBAAhB,GAAqC;EACnC,IAAI,SAAJ;EACA,IAAI,YAAJ;EACA,IAAI,SAAS,GAAG,KAAhB;EACA,MAAM,OAAO,GAA6B;IACxC,OAAO,EAAE,EAD+B;IAExC,YAAY,EAAE,YAF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AI,CAAC,EAArD;IACD;;IAED,MAAM;EACP,CAxBD;;EA0BA,MAAM,eAAe,GAAuC,EAAE,IAAG;IAC/D,YAAY,GAAG,EAAf;EACD,CAFD;;EAIA,MAAM,kBAAkB,GAA0C,MAAK;IACrE,YAAY,GAAG,SAAf;EACD,CAFD;;EAIA,MAAM,UAAU,GAAkC,MAAM,IAAG;IACzD,IAAI,CAAC,aAAa,CAAC,MAAD,CAAlB,EAA4B;MAC1B;IACD;;IAED,MAAM,IAAI,GAAG,aAAa,CAAC,MAAD,CAA1B;IACA,gBAAgB,CAAC,MAAjB,CAAwB,MAAxB;IACA,kBAAkB,CAAC,MAAnB,CAA0B,MAA1B;;IAEA,IAAI,IAAI,CAAC,OAAT,EAAkB;MAChB,cAAc,CAAC,IAAI,CAAC,OAAN,CAAd,CAA6B,cAA7B,CAA4C,MAA5C,CAAmD,IAAI,CAAC,EAAxD;MACA,cAAc,CAAC,IAAI,CAAC,OAAN,CAAd,CAA6B,gBAA7B,CAA8C,MAA9C,CAAqD,IAAI,CAAC,EAA1D;IACD;;IAED,OAAO,aAAa,CAAC,MAAD,CAApB;IACA,MAAM;EACP,CAhBD;;EAkBA,OAAO;IACL,OADK;IAEL,UAFK;IAGL,WAHK;IAIL,OAJK;IAKL,UALK;IAML,MANK;IAOL,eAPK;IAQL;EARK,CAAP;AAUD;;AA9OD,OAAA,CAAA,qBAAA,GAAA,qBAAA","sourcesContent":["import { debounce } from './debounce';\nimport { createPriorityQueue } from './priorityQueue';\nimport type { OverflowGroupState, OverflowItemEntry, OverflowManager, ObserveOptions } from './types';\n\n/**\n * @internal\n * @returns overflow manager instance\n */\nexport function createOverflowManager(): OverflowManager {\n let container: HTMLElement | undefined;\n let overflowMenu: HTMLElement | undefined;\n let observing = false;\n const options: Required<ObserveOptions> = {\n padding: 10,\n overflowAxis: 'horizontal',\n overflowDirection: 'end',\n minimumVisible: 0,\n onUpdateItemVisibility: () => undefined,\n onUpdateOverflow: () => undefined,\n };\n\n const overflowItems: Record<string, OverflowItemEntry> = {};\n const overflowGroups: Record<string, { visibleItemIds: Set<string>; invisibleItemIds: Set<string> }> = {};\n const resizeObserver = new ResizeObserver(entries => {\n if (!entries[0] || !container) {\n return;\n }\n\n update();\n });\n\n const invisibleItemQueue = createPriorityQueue<string>((a, b) => {\n const itemA = overflowItems[a];\n const itemB = overflowItems[b];\n // Higher priority at the top of the queue\n const priority = itemB.priority - itemA.priority;\n if (priority !== 0) {\n return priority;\n }\n\n const positionStatusBit =\n options.overflowDirection === 'end' ? Node.DOCUMENT_POSITION_FOLLOWING : Node.DOCUMENT_POSITION_PRECEDING;\n\n // equal priority, use DOM order\n // eslint-disable-next-line no-bitwise\n return itemA.element.compareDocumentPosition(itemB.element) & positionStatusBit ? -1 : 1;\n });\n\n const visibleItemQueue = createPriorityQueue<string>((a, b) => {\n const itemA = overflowItems[a];\n const itemB = overflowItems[b];\n // Lower priority at the top of the queue\n const priority = itemA.priority - itemB.priority;\n\n if (priority !== 0) {\n return priority;\n }\n\n const positionStatusBit =\n options.overflowDirection === 'end' ? Node.DOCUMENT_POSITION_PRECEDING : Node.DOCUMENT_POSITION_FOLLOWING;\n\n // equal priority, use DOM order\n // eslint-disable-next-line no-bitwise\n return itemA.element.compareDocumentPosition(itemB.element) & positionStatusBit ? -1 : 1;\n });\n\n const getOffsetSize = (el: HTMLElement) => {\n return options.overflowAxis === 'horizontal' ? el.offsetWidth : el.offsetHeight;\n };\n\n const makeItemVisible = () => {\n const nextVisible = invisibleItemQueue.dequeue();\n visibleItemQueue.enqueue(nextVisible);\n\n const item = overflowItems[nextVisible];\n options.onUpdateItemVisibility({ item, visible: true });\n if (item.groupId) {\n overflowGroups[item.groupId].invisibleItemIds.delete(item.id);\n overflowGroups[item.groupId].visibleItemIds.add(item.id);\n }\n\n return getOffsetSize(item.element);\n };\n\n const makeItemInvisible = () => {\n const nextInvisible = visibleItemQueue.dequeue();\n invisibleItemQueue.enqueue(nextInvisible);\n\n const item = overflowItems[nextInvisible];\n const width = getOffsetSize(item.element);\n options.onUpdateItemVisibility({ item, visible: false });\n if (item.groupId) {\n overflowGroups[item.groupId].visibleItemIds.delete(item.id);\n overflowGroups[item.groupId].invisibleItemIds.add(item.id);\n }\n\n return width;\n };\n\n const dispatchOverflowUpdate = () => {\n const visibleItemIds = visibleItemQueue.all();\n const invisibleItemIds = invisibleItemQueue.all();\n\n const visibleItems = visibleItemIds.map(itemId => overflowItems[itemId]);\n const invisibleItems = invisibleItemIds.map(itemId => overflowItems[itemId]);\n\n const groupVisibility: Record<string, OverflowGroupState> = {};\n Object.entries(overflowGroups).forEach(([groupId, groupState]) => {\n if (groupState.invisibleItemIds.size && groupState.visibleItemIds.size) {\n groupVisibility[groupId] = 'overflow';\n } else if (groupState.visibleItemIds.size === 0) {\n groupVisibility[groupId] = 'hidden';\n } else {\n groupVisibility[groupId] = 'visible';\n }\n });\n\n options.onUpdateOverflow({ visibleItems, invisibleItems, groupVisibility });\n };\n\n const processOverflowItems = (availableSize: number) => {\n if (!container) {\n return;\n }\n\n const overflowMenuOffset = overflowMenu ? getOffsetSize(overflowMenu) : 0;\n\n // Snapshot of the visible/invisible state to compare for updates\n const visibleTop = visibleItemQueue.peek();\n const invisibleTop = invisibleItemQueue.peek();\n\n const visibleItemIds = visibleItemQueue.all();\n let currentWidth = visibleItemIds.reduce((sum, visibleItemId) => {\n const child = overflowItems[visibleItemId].element;\n return sum + getOffsetSize(child);\n }, 0);\n\n // Add items until available width is filled\n while (currentWidth < availableSize && invisibleItemQueue.size() > 0) {\n currentWidth += makeItemVisible();\n }\n // Remove items until there's no more overlap\n while (currentWidth > availableSize && visibleItemQueue.size() > 0) {\n if (visibleItemQueue.size() === options.minimumVisible) {\n break;\n }\n currentWidth -= makeItemInvisible();\n }\n\n if (invisibleItemQueue.size() > 0 && currentWidth + overflowMenuOffset > availableSize) {\n makeItemInvisible();\n }\n\n // only update when the state of visible/invisible items has changed\n if (visibleItemQueue.peek() !== visibleTop || invisibleItemQueue.peek() !== invisibleTop) {\n dispatchOverflowUpdate();\n }\n };\n\n const forceUpdate: OverflowManager['forceUpdate'] = () => {\n if (!container) {\n return;\n }\n\n const availableSize = getOffsetSize(container) - options.padding;\n processOverflowItems(availableSize);\n };\n\n const update: OverflowManager['update'] = debounce(forceUpdate);\n\n const observe: OverflowManager['observe'] = (observedContainer, userOptions) => {\n Object.assign(options, userOptions);\n observing = true;\n Object.values(overflowItems).forEach(item => visibleItemQueue.enqueue(item.id));\n\n container = observedContainer;\n resizeObserver.observe(container);\n };\n\n const disconnect: OverflowManager['disconnect'] = () => {\n observing = false;\n resizeObserver.disconnect();\n };\n\n const addItem: OverflowManager['addItem'] = item => {\n if (overflowItems[item.id]) {\n return;\n }\n\n overflowItems[item.id] = item;\n\n // some options can affect priority which are only set on `observe`\n if (observing) {\n visibleItemQueue.enqueue(item.id);\n }\n\n if (item.groupId) {\n if (!overflowGroups[item.groupId]) {\n overflowGroups[item.groupId] = {\n visibleItemIds: new Set<string>(),\n invisibleItemIds: new Set<string>(),\n };\n }\n\n overflowGroups[item.groupId].visibleItemIds.add(item.id);\n }\n\n update();\n };\n\n const addOverflowMenu: OverflowManager['addOverflowMenu'] = el => {\n overflowMenu = el;\n };\n\n const removeOverflowMenu: OverflowManager['removeOverflowMenu'] = () => {\n overflowMenu = undefined;\n };\n\n const removeItem: OverflowManager['removeItem'] = itemId => {\n if (!overflowItems[itemId]) {\n return;\n }\n\n const item = overflowItems[itemId];\n visibleItemQueue.remove(itemId);\n invisibleItemQueue.remove(itemId);\n\n if (item.groupId) {\n overflowGroups[item.groupId].visibleItemIds.delete(item.id);\n overflowGroups[item.groupId].invisibleItemIds.delete(item.id);\n }\n\n delete overflowItems[itemId];\n update();\n };\n\n return {\n addItem,\n disconnect,\n forceUpdate,\n observe,\n removeItem,\n update,\n addOverflowMenu,\n removeOverflowMenu,\n };\n}\n"],"sourceRoot":"../src/"}
|
@@ -82,13 +82,14 @@ function createPriorityQueue(compare) {
|
|
82
82
|
};
|
83
83
|
|
84
84
|
const contains = item => {
|
85
|
-
|
85
|
+
const index = arr.indexOf(item);
|
86
|
+
return index >= 0 && index < size;
|
86
87
|
};
|
87
88
|
|
88
89
|
const remove = item => {
|
89
90
|
const i = arr.indexOf(item);
|
90
91
|
|
91
|
-
if (i === -1) {
|
92
|
+
if (i === -1 || i >= size) {
|
92
93
|
return;
|
93
94
|
}
|
94
95
|
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["priorityQueue.ts"],"names":[],"mappings":";;;;;;AAaA;;;AAGG;;AACH,SAAgB,mBAAhB,CAAuC,OAAvC,EAAyE;
|
1
|
+
{"version":3,"sources":["priorityQueue.ts"],"names":[],"mappings":";;;;;;AAaA;;;AAGG;;AACH,SAAgB,mBAAhB,CAAuC,OAAvC,EAAyE;EACvE,MAAM,GAAG,GAAQ,EAAjB;EACA,IAAI,IAAI,GAAG,CAAX;;EAEA,MAAM,IAAI,GAAI,CAAD,IAAc;IACzB,OAAO,IAAI,CAAJ,GAAQ,CAAf;EACD,CAFD;;EAIA,MAAM,KAAK,GAAI,CAAD,IAAc;IAC1B,OAAO,IAAI,CAAJ,GAAQ,CAAf;EACD,CAFD;;EAIA,MAAM,MAAM,GAAI,CAAD,IAAc;IAC3B,OAAO,IAAI,CAAC,KAAL,CAAW,CAAC,CAAC,GAAG,CAAL,IAAU,CAArB,CAAP;EACD,CAFD;;EAIA,MAAM,IAAI,GAAG,CAAC,CAAD,EAAY,CAAZ,KAAyB;IACpC,MAAM,GAAG,GAAG,GAAG,CAAC,CAAD,CAAf;IACA,GAAG,CAAC,CAAD,CAAH,GAAS,GAAG,CAAC,CAAD,CAAZ;IACA,GAAG,CAAC,CAAD,CAAH,GAAS,GAAT;EACD,CAJD;;EAMA,MAAM,OAAO,GAAI,CAAD,IAAc;IAC5B,IAAI,QAAQ,GAAG,CAAf;IACA,MAAM,CAAC,GAAG,IAAI,CAAC,CAAD,CAAd;IACA,MAAM,CAAC,GAAG,KAAK,CAAC,CA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CAAJ,CAAJ;MACA,CAAC,GAAG,CAAJ;MACA,CAAC,GAAG,MAAM,CAAC,CAAD,CAAV;IACD;EACF,CATD;;EAWA,MAAM,QAAQ,GAAI,IAAD,IAAY;IAC3B,MAAM,KAAK,GAAG,GAAG,CAAC,OAAJ,CAAY,IAAZ,CAAd;IACA,OAAO,KAAK,IAAI,CAAT,IAAc,KAAK,GAAG,IAA7B;EACD,CAHD;;EAKA,MAAM,MAAM,GAAI,IAAD,IAAY;IACzB,MAAM,CAAC,GAAG,GAAG,CAAC,OAAJ,CAAY,IAAZ,CAAV;;IAEA,IAAI,CAAC,KAAK,CAAC,CAAP,IAAY,CAAC,IAAI,IAArB,EAA2B;MACzB;IACD;;IAED,GAAG,CAAC,CAAD,CAAH,GAAS,GAAG,CAAC,EAAE,IAAH,CAAZ;IACA,OAAO,CAAC,CAAD,CAAP;EACD,CATD;;EAWA,MAAM,KAAK,GAAG,MAAK;IACjB,IAAI,GAAG,CAAP;EACD,CAFD;;EAIA,MAAM,GAAG,GAAG,MAAK;IACf,OAAO,GAAG,CAAC,KAAJ,CAAU,CAAV,EAAa,IAAb,CAAP;EACD,CAFD;;EAIA,OAAO;IACL,GADK;IAEL,KAFK;IAGL,QAHK;IAIL,OAJK;IAKL,OALK;IAML,IANK;IAOL,MAPK;IAQL,IAAI,EAAE,MAAM;EARP,CAAP;AAUD;;AA1GD,OAAA,CAAA,mBAAA,GAAA,mBAAA","sourcesContent":["export type PriorityQueueCompareFn<T> = (a: T, b: T) => number;\n\nexport interface PriorityQueue<T> {\n all: () => T[];\n clear: () => void;\n contains: (item: T) => boolean;\n dequeue: () => T;\n enqueue: (item: T) => void;\n peek: () => T | null;\n remove: (item: T) => void;\n size: () => number;\n}\n\n/**\n * @param compare - comparison function for items\n * @returns Priority queue implemented with a min heap\n */\nexport function createPriorityQueue<T>(compare: PriorityQueueCompareFn<T>): PriorityQueue<T> {\n const arr: T[] = [];\n let size = 0;\n\n const left = (i: number) => {\n return 2 * i + 1;\n };\n\n const right = (i: number) => {\n return 2 * i + 2;\n };\n\n const parent = (i: number) => {\n return Math.floor((i - 1) / 2);\n };\n\n const swap = (a: number, b: number) => {\n const tmp = arr[a];\n arr[a] = arr[b];\n arr[b] = tmp;\n };\n\n const heapify = (i: number) => {\n let smallest = i;\n const l = left(i);\n const r = right(i);\n\n if (l < size && compare(arr[l], arr[smallest]) < 0) {\n smallest = l;\n }\n\n if (r < size && compare(arr[r], arr[smallest]) < 0) {\n smallest = r;\n }\n\n if (smallest !== i) {\n swap(smallest, i);\n heapify(smallest);\n }\n };\n\n const dequeue = () => {\n if (size === 0) {\n throw new Error('Priority queue empty');\n }\n\n const res = arr[0];\n arr[0] = arr[--size];\n heapify(0);\n\n return res;\n };\n\n const peek = () => {\n if (size === 0) {\n return null;\n }\n\n return arr[0];\n };\n\n const enqueue = (item: T) => {\n arr[size++] = item;\n let i = size - 1;\n let p = parent(i);\n while (i > 0 && compare(arr[p], arr[i]) > 0) {\n swap(p, i);\n i = p;\n p = parent(i);\n }\n };\n\n const contains = (item: T) => {\n const index = arr.indexOf(item);\n return index >= 0 && index < size;\n };\n\n const remove = (item: T) => {\n const i = arr.indexOf(item);\n\n if (i === -1 || i >= size) {\n return;\n }\n\n arr[i] = arr[--size];\n heapify(i);\n };\n\n const clear = () => {\n size = 0;\n };\n\n const all = () => {\n return arr.slice(0, size);\n };\n\n return {\n all,\n clear,\n contains,\n dequeue,\n enqueue,\n peek,\n remove,\n size: () => size,\n };\n}\n"],"sourceRoot":"../src/"}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@fluentui/priority-overflow",
|
3
|
-
"version": "9.0.0-beta.
|
3
|
+
"version": "9.0.0-beta.3",
|
4
4
|
"description": "Vanilla JS utilities to implement overflow menus",
|
5
5
|
"main": "lib-commonjs/index.js",
|
6
6
|
"module": "lib/index.js",
|
@@ -20,7 +20,7 @@
|
|
20
20
|
"lint": "just-scripts lint",
|
21
21
|
"test": "jest --passWithNoTests",
|
22
22
|
"docs": "api-extractor run --config=config/api-extractor.local.json --local",
|
23
|
-
"build:local": "tsc -p ./tsconfig.lib.json --module esnext --emitDeclarationOnly && node ../../../scripts/typescript/normalize-import --output ./dist/packages/react-components/priority-overflow/src && yarn docs",
|
23
|
+
"build:local": "tsc -p ./tsconfig.lib.json --module esnext --emitDeclarationOnly && node ../../../scripts/typescript/normalize-import --output ./dist/types/packages/react-components/priority-overflow/src && yarn docs",
|
24
24
|
"type-check": "tsc -b tsconfig.json"
|
25
25
|
},
|
26
26
|
"devDependencies": {
|
package/dist/tsdoc-metadata.json
DELETED
@@ -1,11 +0,0 @@
|
|
1
|
-
// This file is read by tools that parse documentation comments conforming to the TSDoc standard.
|
2
|
-
// It should be published with your NPM package. It should not be tracked by Git.
|
3
|
-
{
|
4
|
-
"tsdocVersion": "0.12",
|
5
|
-
"toolPackages": [
|
6
|
-
{
|
7
|
-
"packageName": "@microsoft/api-extractor",
|
8
|
-
"packageVersion": "7.18.1"
|
9
|
-
}
|
10
|
-
]
|
11
|
-
}
|