-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(editor): Add truncate directive (#10842)
- Loading branch information
Showing
7 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { n8nTruncate } from './n8n-truncate'; |
76 changes: 76 additions & 0 deletions
76
packages/design-system/src/directives/n8n-truncate.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = { | ||
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); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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...', | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |