Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(editor): Use segments/graphemes when creating the compact sidebar entries #9776

Merged
merged 2 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions packages/design-system/src/components/N8nMenuItem/MenuItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ import N8nIcon from '../N8nIcon';
import ConditionalRouterLink from '../ConditionalRouterLink';
import type { IMenuItem } from '../../types';
import { doesMenuItemMatchCurrentRoute } from './routerUtil';
import { getInitials } from './labelUtil';

interface MenuItemProps {
item: IMenuItem;
Expand Down Expand Up @@ -149,16 +150,6 @@ const isItemActive = (item: IMenuItem): boolean => {
Array.isArray(item.children) && item.children.some((child) => isActive(child));
return isActive(item) || hasActiveChild;
};

const getInitials = (label: string): string => {
const words = label.split(' ');

if (words.length === 1) {
return words[0].substring(0, 2);
} else {
return words[0].charAt(0) + words[1].charAt(0);
}
};
</script>

<style module lang="scss">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { getInitials } from '../labelUtil';

describe('labelUtil.getInitials', () => {
it.each([
['', ''],

// simple words
['Hello', 'He'],
['Hello World', 'HW'],
['H', 'H'],

// multiple spaces
['Double Space', 'DS'],
[' ', ''],

// simple emoji
['👋 Hello', '👋H'],
['👋Hello', '👋H'],
['Hello 👋', 'H👋'],
['Hello👋', 'He'],

// combined emojis
['1️⃣ 1️⃣', '1️⃣1️⃣'],
['1️⃣', '1️⃣'],
['👩‍⚕️D 👩‍⚕️D', '👩‍⚕️👩‍⚕️'],
])('turns "%s" into "%s"', (input, output) => {
expect(getInitials(input)).toBe(output);
});
});
25 changes: 25 additions & 0 deletions packages/design-system/src/components/N8nMenuItem/labelUtil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export const getInitials = (label: string): string => {
const words = label
.split(' ')
.filter((word) => word !== '')
.map((word) => [...new Intl.Segmenter().segment(word)]);

if (words.length === 0) {
return '';
} else if (words.length === 1) {
// first two segments of the first word
return (
words
.at(0)
?.slice(0, 2)
.map((grapheme) => grapheme.segment)
.join('') ?? ''
);
} else {
// first segment ok the first two words
return words
.slice(0, 2)
.map((word) => word.at(0)?.segment ?? '')
.join('');
}
};
Loading