Skip to content

Commit 3cd24c4

Browse files
authored
Chore (toolbox): improved shortcuts visibility when tool exports array of toolbox items (#2846)
* toolbox items logic improved * typo * lint fix * logic improved * make displaySecondaryLabel true by default * eslint fix * added testcase * updated changelog * typo * lint fix
1 parent eb7ffcb commit 3cd24c4

File tree

4 files changed

+106
-5
lines changed

4 files changed

+106
-5
lines changed

docs/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- `New` - Inline tools (those with `isReadOnlySupported` specified) can now be used in read-only mode
66
- `Fix` - Fix selection of first block in read-only initialization with "autofocus=true"
77
- `Fix` - Incorrect caret position after blocks merging in Safari
8+
- `Fix` - Several toolbox items exported by the one tool have the same shortcut displayed in toolbox
89

910
### 2.30.6
1011

src/components/ui/toolbox.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,23 +308,23 @@ export default class Toolbox extends EventsDispatcher<ToolboxEventMap> {
308308
/**
309309
* Maps tool data to popover item structure
310310
*/
311-
const toPopoverItem = (toolboxItem: ToolboxConfigEntry, tool: BlockToolAdapter): PopoverItemParams => {
311+
const toPopoverItem = (toolboxItem: ToolboxConfigEntry, tool: BlockToolAdapter, displaySecondaryLabel = true): PopoverItemParams => {
312312
return {
313313
icon: toolboxItem.icon,
314314
title: I18n.t(I18nInternalNS.toolNames, toolboxItem.title || _.capitalize(tool.name)),
315315
name: tool.name,
316316
onActivate: (): void => {
317317
this.toolButtonActivated(tool.name, toolboxItem.data);
318318
},
319-
secondaryLabel: tool.shortcut ? _.beautifyShortcut(tool.shortcut) : '',
319+
secondaryLabel: (tool.shortcut && displaySecondaryLabel) ? _.beautifyShortcut(tool.shortcut) : '',
320320
};
321321
};
322322

323323
return this.toolsToBeDisplayed
324324
.reduce<PopoverItemParams[]>((result, tool) => {
325325
if (Array.isArray(tool.toolbox)) {
326-
tool.toolbox.forEach(item => {
327-
result.push(toPopoverItem(item, tool));
326+
tool.toolbox.forEach((item, index) => {
327+
result.push(toPopoverItem(item, tool, index === 0));
328328
});
329329
} else if (tool.toolbox !== undefined) {
330330
result.push(toPopoverItem(tool.toolbox, tool));

test/cypress/tests/ui/toolbox.cy.ts

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,5 +114,105 @@ describe('Toolbox', function () {
114114
expect(blocks[1].type).to.eq('nonConvertableTool');
115115
});
116116
});
117+
118+
it('should display shortcut only for the first toolbox item if tool exports toolbox with several items', function () {
119+
/**
120+
* Mock of Tool with conversionConfig
121+
*/
122+
class ToolWithSeveralToolboxItems extends ToolMock {
123+
/**
124+
* Specify toolbox with several items related to one tool
125+
*/
126+
public static get toolbox(): ToolboxConfig {
127+
return [
128+
{
129+
icon: '',
130+
title: 'first tool',
131+
},
132+
{
133+
icon: '',
134+
title: 'second tool',
135+
},
136+
];
137+
}
138+
}
139+
140+
cy.createEditor({
141+
tools: {
142+
severalToolboxItemsTool: {
143+
class: ToolWithSeveralToolboxItems,
144+
shortcut: 'CMD+SHIFT+L',
145+
},
146+
},
147+
});
148+
149+
cy.get('[data-cy=editorjs]')
150+
.find('.ce-paragraph')
151+
.click()
152+
.type('Some text')
153+
.type('/'); // call a shortcut for toolbox
154+
155+
/**
156+
* Secondary title (shortcut) should exist for first toolbox item of the tool
157+
*/
158+
/* eslint-disable-next-line cypress/require-data-selectors */
159+
cy.get('.ce-popover')
160+
.find('.ce-popover-item[data-item-name="severalToolboxItemsTool"]')
161+
.first()
162+
.find('.ce-popover-item__secondary-title')
163+
.should('exist');
164+
165+
/**
166+
* Secondary title (shortcut) should not exist for second toolbox item of the same tool
167+
*/
168+
/* eslint-disable-next-line cypress/require-data-selectors */
169+
cy.get('.ce-popover')
170+
.find('.ce-popover-item[data-item-name="severalToolboxItemsTool"]')
171+
.eq(1)
172+
.find('.ce-popover-item__secondary-title')
173+
.should('not.exist');
174+
});
175+
176+
it('should display shortcut for the item if tool exports toolbox as an one item object', function () {
177+
/**
178+
* Mock of Tool with conversionConfig
179+
*/
180+
class ToolWithOneToolboxItems extends ToolMock {
181+
/**
182+
* Specify toolbox with several items related to one tool
183+
*/
184+
public static get toolbox(): ToolboxConfig {
185+
return {
186+
icon: '',
187+
title: 'tool',
188+
};
189+
}
190+
}
191+
192+
cy.createEditor({
193+
tools: {
194+
oneToolboxItemTool: {
195+
class: ToolWithOneToolboxItems,
196+
shortcut: 'CMD+SHIFT+L',
197+
},
198+
},
199+
});
200+
201+
cy.get('[data-cy=editorjs]')
202+
.find('.ce-paragraph')
203+
.click()
204+
.type('Some text')
205+
.type('/'); // call a shortcut for toolbox
206+
207+
/**
208+
* Secondary title (shortcut) should exist for toolbox item of the tool
209+
*/
210+
/* eslint-disable-next-line cypress/require-data-selectors */
211+
cy.get('.ce-popover')
212+
.find('.ce-popover-item[data-item-name="oneToolboxItemTool"]')
213+
.first()
214+
.find('.ce-popover-item__secondary-title')
215+
.should('exist');
216+
});
117217
});
118218
});

types/tools/tool-settings.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export interface ToolboxConfigEntry {
2222
icon?: string;
2323

2424
/**
25-
* May contain overrides for tool default config
25+
* May contain overrides for tool default data
2626
*/
2727
data?: BlockToolData
2828
}

0 commit comments

Comments
 (0)