Skip to content

Commit 5c0db09

Browse files
authored
fix: [2367] Sort widgets with order 0 first, matching the editor (#2368)
1 parent 96f0179 commit 5c0db09

2 files changed

Lines changed: 46 additions & 10 deletions

File tree

‎test/store/ui.spec.js‎

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,22 +96,50 @@ describe('store: ui getters', function () {
9696
orderOf(widgets, 'g1').should.eql(['a'])
9797
})
9898

99-
it('sinks widgets with an unset/zero order to the end', function () {
99+
it('sorts a widget with order 0 first, like the editor layout manager', function () {
100100
const widgets = [
101-
{ id: 'a', type: 'ui-text', props: { group: 'g1' }, layout: { order: 0 } },
102-
{ id: 'b', type: 'ui-text', props: { group: 'g1' }, layout: { order: 2 } },
101+
{ id: 'a', type: 'ui-text', props: { group: 'g1' }, layout: { order: 2 } },
102+
{ id: 'b', type: 'ui-text', props: { group: 'g1' }, layout: { order: 0 } },
103103
{ id: 'c', type: 'ui-text', props: { group: 'g1' }, layout: { order: 1 } }
104104
]
105-
orderOf(widgets).should.eql(['c', 'b', 'a'])
105+
orderOf(widgets).should.eql(['b', 'c', 'a'])
106106
})
107107

108-
it('sinks a subflow with an unset/zero order to the end', function () {
108+
it('sorts a string "0" order the same as a numeric 0 (#2367)', function () {
109+
const widgets = [
110+
{ id: 'a', type: 'ui-text', props: { group: 'g1' }, layout: { order: '2' } },
111+
{ id: 'b', type: 'ui-text', props: { group: 'g1' }, layout: { order: 0 } },
112+
{ id: 'c', type: 'ui-text', props: { group: 'g1' }, layout: { order: '1' } },
113+
{ id: 'd', type: 'ui-text', props: { group: 'g1' }, layout: { order: '0' } }
114+
]
115+
orderOf(widgets).should.eql(['b', 'd', 'c', 'a'])
116+
})
117+
118+
it('sorts subflow widgets with a mix of numeric and string orders (#2367)', function () {
119+
const widgets = [
120+
{ id: 'zero', type: 'ui-text', props: { group: 'g1', subflow: { id: 'sf1', order: 1 } }, layout: { order: 0 } },
121+
{ id: 'two', type: 'ui-text', props: { group: 'g1', subflow: { id: 'sf1', order: 1 } }, layout: { order: '2' } },
122+
{ id: 'one', type: 'ui-text', props: { group: 'g1', subflow: { id: 'sf1', order: 1 } }, layout: { order: '1' } },
123+
{ id: 'three', type: 'ui-text', props: { group: 'g1', subflow: { id: 'sf1', order: 1 } }, layout: { order: '3' } }
124+
]
125+
orderOf(widgets).should.eql(['zero', 'one', 'two', 'three'])
126+
})
127+
128+
it('sorts a subflow with order 0 first', function () {
109129
const widgets = [
110130
{ id: 'a', type: 'ui-text', props: { group: 'g1' }, layout: { order: 1 } },
111131
{ id: 'b', type: 'ui-text', props: { group: 'g1' }, layout: { order: 2 } },
112132
{ id: 's', type: 'ui-template', props: { group: 'g1', templateScope: 'local', subflow: { id: 'sf1', order: 0 } }, layout: { order: 1 } }
113133
]
114-
orderOf(widgets).should.eql(['a', 'b', 's'])
134+
orderOf(widgets).should.eql(['s', 'a', 'b'])
135+
})
136+
137+
it('sinks a widget with no order to the end', function () {
138+
const widgets = [
139+
{ id: 'a', type: 'ui-text', props: { group: 'g1' }, layout: {} },
140+
{ id: 'b', type: 'ui-text', props: { group: 'g1' }, layout: { order: 0 } }
141+
]
142+
orderOf(widgets).should.eql(['b', 'a'])
115143
})
116144
})
117145

‎ui/src/store/ui.mjs‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ const state = () => ({
3838
widgets: null
3939
})
4040

41+
function sortOrder (order) {
42+
const value = Number(order ?? NaN)
43+
if (isNaN(value)) {
44+
return Number.MAX_SAFE_INTEGER
45+
}
46+
return value
47+
}
48+
4149
// getters
4250
const getters = {
4351
id: (state) => {
@@ -153,15 +161,15 @@ const getters = {
153161
if (subflowId) {
154162
if (!subflowUnits[subflowId]) {
155163
subflowUnits[subflowId] = {
156-
order: w.props.subflow.order || Number.MAX_SAFE_INTEGER,
164+
order: sortOrder(w.props.subflow.order),
157165
widgets: []
158166
}
159167
units.push(subflowUnits[subflowId])
160168
}
161169
subflowUnits[subflowId].widgets.push(w)
162170
} else {
163171
units.push({
164-
order: w.layout?.order || Number.MAX_SAFE_INTEGER,
172+
order: sortOrder(w.layout?.order),
165173
widgets: [w]
166174
})
167175
}
@@ -171,8 +179,8 @@ const getters = {
171179
units.forEach((unit) => {
172180
if (unit.widgets.length > 1) {
173181
unit.widgets.sort((a, b) => {
174-
const aOrder = a.layout?.order || Number.MAX_SAFE_INTEGER
175-
const bOrder = b.layout?.order || Number.MAX_SAFE_INTEGER
182+
const aOrder = sortOrder(a.layout?.order)
183+
const bOrder = sortOrder(b.layout?.order)
176184
return aOrder - bOrder
177185
})
178186
}

0 commit comments

Comments
 (0)