diff --git a/packages/registry/news/5840.bugfix b/packages/registry/news/5840.bugfix new file mode 100644 index 0000000000..a6cf3d741d --- /dev/null +++ b/packages/registry/news/5840.bugfix @@ -0,0 +1 @@ +- Fix slots reordering function for "before" and "after" keyword @steffenri \ No newline at end of file diff --git a/packages/registry/src/index.ts b/packages/registry/src/index.ts index 9c838419fd..d71a56560a 100644 --- a/packages/registry/src/index.ts +++ b/packages/registry/src/index.ts @@ -360,11 +360,21 @@ class Config { } switch (action) { case 'after': - result.splice(targetIdx, 0, removed); - break; + if (targetIdx < origin) { + result.splice(targetIdx + 1, 0, removed); + break; + } else { + result.splice(targetIdx, 0, removed); + break; + } case 'before': - result.splice(targetIdx - 1, 0, removed); - break; + if (targetIdx > origin) { + result.splice(targetIdx - 1, 0, removed); + break; + } else { + result.splice(targetIdx, 0, removed); + break; + } case 'last': result.push(removed); break; diff --git a/packages/registry/src/registry.test.tsx b/packages/registry/src/registry.test.tsx index d69af11cdb..4763b93946 100644 --- a/packages/registry/src/registry.test.tsx +++ b/packages/registry/src/registry.test.tsx @@ -474,24 +474,24 @@ describe('Slots registry', () => { expect(config.getSlotComponents('toolbar')).toEqual(['edit', 'save']); }); - it('reorderSlotComponent - after', () => { + it('reorderSlotComponent - after (target after origin)', () => { config.registerSlotComponent({ slot: 'toolbar', - name: 'save', + name: '1', component: 'this is a toolbar save component with a true predicate', predicates: [RouteConditionTrue('/de')], }); config.registerSlotComponent({ slot: 'toolbar', - name: 'edit', + name: '2', component: 'this is a toolbar component with a false predicate', predicates: [RouteConditionFalse('/de')], }); config.registerSlotComponent({ slot: 'toolbar', - name: 'cancel', + name: '3', component: 'this is a toolbar edit component with true predicate', predicates: [ RouteConditionTrue('/folder/path'), @@ -501,7 +501,7 @@ describe('Slots registry', () => { config.registerSlotComponent({ slot: 'toolbar', - name: 'bold', + name: '4', component: 'this is a toolbar edit component with true predicate', predicates: [ RouteConditionTrue('/folder/path'), @@ -509,44 +509,34 @@ describe('Slots registry', () => { ], }); - expect(config.getSlotComponents('toolbar')).toEqual([ - 'save', - 'edit', - 'cancel', - 'bold', - ]); + expect(config.getSlotComponents('toolbar')).toEqual(['1', '2', '3', '4']); config.reorderSlotComponent({ slot: 'toolbar', - name: 'save', + name: '1', action: 'after', - target: 'cancel', + target: '3', }); - expect(config.getSlotComponents('toolbar')).toEqual([ - 'edit', - 'cancel', - 'save', - 'bold', - ]); + expect(config.getSlotComponents('toolbar')).toEqual(['2', '3', '1', '4']); }); - it('reorderSlotComponent - before', () => { + it('reorderSlotComponent - after (target before origin)', () => { config.registerSlotComponent({ slot: 'toolbar', - name: 'save', + name: '1', component: 'this is a toolbar save component with a true predicate', predicates: [RouteConditionTrue('/de')], }); config.registerSlotComponent({ slot: 'toolbar', - name: 'edit', + name: '2', component: 'this is a toolbar component with a false predicate', predicates: [RouteConditionFalse('/de')], }); config.registerSlotComponent({ slot: 'toolbar', - name: 'cancel', + name: '3', component: 'this is a toolbar edit component with true predicate', predicates: [ RouteConditionTrue('/folder/path'), @@ -556,7 +546,7 @@ describe('Slots registry', () => { config.registerSlotComponent({ slot: 'toolbar', - name: 'bold', + name: '4', component: 'this is a toolbar edit component with true predicate', predicates: [ RouteConditionTrue('/folder/path'), @@ -564,24 +554,202 @@ describe('Slots registry', () => { ], }); - expect(config.getSlotComponents('toolbar')).toEqual([ - 'save', - 'edit', - 'cancel', - 'bold', - ]); + expect(config.getSlotComponents('toolbar')).toEqual(['1', '2', '3', '4']); + // Reorder Slot with original position 1 before Slot with position 3 + // Before reordering the positions should be ["1", "2", "3", "4"] + // After the reordering the positions should be ["2", "1", "3", "4"] config.reorderSlotComponent({ slot: 'toolbar', - name: 'save', + name: '3', + action: 'after', + target: '1', + }); + + expect(config.getSlotComponents('toolbar')).toEqual(['1', '3', '2', '4']); + }); + + it('reorderSlotComponent - after (target = origin)', () => { + config.registerSlotComponent({ + slot: 'toolbar', + name: '1', + component: 'this is a toolbar save component with a true predicate', + predicates: [RouteConditionTrue('/de')], + }); + + config.registerSlotComponent({ + slot: 'toolbar', + name: '2', + component: 'this is a toolbar component with a false predicate', + predicates: [RouteConditionFalse('/de')], + }); + + config.registerSlotComponent({ + slot: 'toolbar', + name: '3', + component: 'this is a toolbar edit component with true predicate', + predicates: [ + RouteConditionTrue('/folder/path'), + ContentTypeConditionTrue(['News Item']), + ], + }); + + config.registerSlotComponent({ + slot: 'toolbar', + name: '4', + component: 'this is a toolbar edit component with true predicate', + predicates: [ + RouteConditionTrue('/folder/path'), + ContentTypeConditionTrue(['News Item']), + ], + }); + + expect(config.getSlotComponents('toolbar')).toEqual(['1', '2', '3', '4']); + // Reorder Slot with original position 1 before Slot with position 3 + // Before reordering the positions should be ["1", "2", "3", "4"] + // After the reordering the positions should be ["2", "1", "3", "4"] + config.reorderSlotComponent({ + slot: 'toolbar', + name: '3', + action: 'after', + target: '3', + }); + + expect(config.getSlotComponents('toolbar')).toEqual(['1', '2', '3', '4']); + }); + + it('reorderSlotComponent - before (target before origin)', () => { + config.registerSlotComponent({ + slot: 'toolbar', + name: '1', + component: 'this is a toolbar save component with a true predicate', + predicates: [RouteConditionTrue('/de')], + }); + + config.registerSlotComponent({ + slot: 'toolbar', + name: '2', + component: 'this is a toolbar component with a false predicate', + predicates: [RouteConditionFalse('/de')], + }); + + config.registerSlotComponent({ + slot: 'toolbar', + name: '3', + component: 'this is a toolbar edit component with true predicate', + predicates: [RouteConditionFalse('/de')], + }); + + config.registerSlotComponent({ + slot: 'toolbar', + name: '4', + component: 'this is a toolbar edit component with true predicate', + predicates: [RouteConditionFalse('/de')], + }); + + expect(config.getSlotComponents('toolbar')).toEqual(['1', '2', '3', '4']); + // Reorder Slot with original position 4 before Slot with position 3 + // Before reordering the positions should be ["1", "2", "3", "4"] + // After the reordering the positions should be ["1", "2", "4", "3 "] + config.reorderSlotComponent({ + slot: 'toolbar', + name: '3', action: 'before', - target: 'cancel', + target: '1', }); - expect(config.getSlotComponents('toolbar')).toEqual([ - 'edit', - 'save', - 'cancel', - 'bold', - ]); + + expect(config.getSlotComponents('toolbar')).toEqual(['3', '1', '2', '4']); + }); + + it('reorderSlotComponent - before (target after origin)', () => { + config.registerSlotComponent({ + slot: 'toolbar', + name: '1', + component: 'this is a toolbar save component with a true predicate', + predicates: [RouteConditionTrue('/de')], + }); + + config.registerSlotComponent({ + slot: 'toolbar', + name: '2', + component: 'this is a toolbar component with a false predicate', + predicates: [RouteConditionFalse('/de')], + }); + + config.registerSlotComponent({ + slot: 'toolbar', + name: '3', + component: 'this is a toolbar edit component with true predicate', + predicates: [ + RouteConditionTrue('/folder/path'), + ContentTypeConditionTrue(['News Item']), + ], + }); + + config.registerSlotComponent({ + slot: 'toolbar', + name: '4', + component: 'this is a toolbar edit component with true predicate', + predicates: [ + RouteConditionTrue('/folder/path'), + ContentTypeConditionTrue(['News Item']), + ], + }); + + expect(config.getSlotComponents('toolbar')).toEqual(['1', '2', '3', '4']); + // Reorder Slot with original position 1 before Slot with position 3 + // Before reordering the positions should be ["1", "2", "3", "4"] + // After the reordering the positions should be ["2", "1", "3", "4"] + config.reorderSlotComponent({ + slot: 'toolbar', + name: '1', + action: 'before', + target: '3', + }); + + expect(config.getSlotComponents('toolbar')).toEqual(['2', '1', '3', '4']); + }); + + it('reorderSlotComponent - before (target = origin)', () => { + config.registerSlotComponent({ + slot: 'toolbar', + name: '1', + component: 'this is a toolbar save component with a true predicate', + predicates: [RouteConditionTrue('/de')], + }); + + config.registerSlotComponent({ + slot: 'toolbar', + name: '2', + component: 'this is a toolbar component with a false predicate', + predicates: [RouteConditionFalse('/de')], + }); + + config.registerSlotComponent({ + slot: 'toolbar', + name: '3', + component: 'this is a toolbar edit component with true predicate', + predicates: [RouteConditionFalse('/de')], + }); + + config.registerSlotComponent({ + slot: 'toolbar', + name: '4', + component: 'this is a toolbar edit component with true predicate', + predicates: [RouteConditionFalse('/de')], + }); + + expect(config.getSlotComponents('toolbar')).toEqual(['1', '2', '3', '4']); + // Reorder Slot with original position 4 before Slot with position 3 + // Before reordering the positions should be ["1", "2", "3", "4"] + // After the reordering the positions should be ["1", "2", "4", "3 "] + config.reorderSlotComponent({ + slot: 'toolbar', + name: '3', + action: 'before', + target: '3', + }); + + expect(config.getSlotComponents('toolbar')).toEqual(['1', '2', '3', '4']); }); it('reorderSlotComponent - last', () => {