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

feat(editor): Add truncate directive #10842

Merged
merged 4 commits into from
Sep 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
1 change: 1 addition & 0 deletions packages/design-system/src/directives/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { n8nTruncate } from './n8n-truncate';
76 changes: 76 additions & 0 deletions packages/design-system/src/directives/n8n-truncate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { render } from '@testing-library/vue';
import { n8nTruncate } from './n8n-truncate';

describe('Directive n8n-truncate', () => {
it('should truncate text to 30 chars by default', async () => {
const { html } = render(
{
props: {
text: {
type: String,
},
},
template: '<div v-n8n-truncate>{{text}}</div>',
},
{
props: {
text: 'This is a very long text that should be truncated',
},
global: {
directives: {
n8nTruncate,
},
},
},
);
expect(html()).toBe('<div>This is a very long text that...</div>');
});

it('should truncate text to 30 chars in case of wrong argument', async () => {
const { html } = render(
{
props: {
text: {
type: String,
},
},
template: '<div v-n8n-truncate:ab>{{text}}</div>',
},
{
props: {
text: 'This is a very long text that should be truncated',
},
global: {
directives: {
n8nTruncate,
},
},
},
);
expect(html()).toBe('<div>This is a very long text that...</div>');
});

it('should truncate text to given length', async () => {
const { html } = render(
{
props: {
text: {
type: String,
},
},
template: '<div v-n8n-truncate:25>{{text}}</div>',
},
{
props: {
text: 'This is a very long text that should be truncated',
},
global: {
directives: {
n8nTruncate,
},
},
},
);
expect(html()).toBe('<div>This is a very long text...</div>');
});
});
26 changes: 26 additions & 0 deletions packages/design-system/src/directives/n8n-truncate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { DirectiveBinding, ObjectDirective } from 'vue';
import { truncate } from '../utils/string';

/**
* Custom directive `n8nTruncate` to truncate text content of an HTML element.
*
* Usage:
* In your Vue template, use the directive `v-n8n-truncate` with an argument to specify the length to truncate to.
*
* Example:
* <p v-n8n-truncate:10>Some long text that will be truncated</p>
*
* This will truncate the text content of the paragraph to 10 characters.
*
* Hint: Do not use it on components
* https://vuejs.org/guide/reusability/custom-directives#usage-on-components
*/

export const n8nTruncate: ObjectDirective = {
cstuncsik marked this conversation as resolved.
Show resolved Hide resolved
mounted(el: HTMLElement, binding: DirectiveBinding) {
el.textContent = truncate(el.textContent ?? '', Number(binding.arg) || undefined);
},
updated(el: HTMLElement, binding: DirectiveBinding) {
el.textContent = truncate(el.textContent ?? '', Number(binding.arg) || undefined);
},
};
1 change: 1 addition & 0 deletions packages/design-system/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export * from './components';
export * from './plugin';
export * from './types';
export * from './utils';
export * from './directives';
export { locale };
5 changes: 5 additions & 0 deletions packages/design-system/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Component, Plugin } from 'vue';
import * as components from './components';
import * as directives from './directives';

export interface N8nPluginOptions {}

Expand All @@ -8,5 +9,9 @@ export const N8nPlugin: Plugin<N8nPluginOptions> = {
for (const [name, component] of Object.entries(components)) {
app.component(name, component as unknown as Component);
}

for (const [name, directive] of Object.entries(directives)) {
app.directive(name, directive);
}
},
};
17 changes: 17 additions & 0 deletions packages/design-system/src/utils/string.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { truncate } from './string';

describe('Utils string', () => {
describe('truncate', () => {
it('should truncate text to 30 chars by default', () => {
expect(truncate('This is a very long text that should be truncated')).toBe(
'This is a very long text that...',
);
});

it('should truncate text to given length', () => {
expect(truncate('This is a very long text that should be truncated', 25)).toBe(
'This is a very long text...',
);
});
});
});
2 changes: 2 additions & 0 deletions packages/design-system/src/utils/string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const truncate = (text: string, length = 30): string =>
text.length > length ? text.slice(0, length).trim() + '...' : text;
Loading