-
Notifications
You must be signed in to change notification settings - Fork 8
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
chore(site): create lorem-ipsum
custom dummy text element
#2040
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,91 @@ | ||
import {ESLBaseElement} from '@exadel/esl/modules/esl-base-element/core'; | ||
import {attr} from '@exadel/esl/modules/esl-utils/decorators'; | ||
|
||
const WORDS = [ | ||
'lorem', 'ipsum', 'dolor', 'sit', 'amet', | ||
'consectetur', 'adipiscing', 'elit', 'curabitur', | ||
'ultrices', 'et', 'mi', 'suscipit', 'eget', 'vulputate', 'ante', | ||
'proin', 'vel', 'pretium', 'enim', 'vivamus', 'venenatis', 'eu', | ||
'urna', 'tempor', 'blandit', 'nullam', 'pellentesque', 'metus', | ||
'rhoncus', 'mauris', 'mollis', 'neque', 'sed', 'tincidunt', 'tellus', | ||
'nunc', 'ac', 'nulla', 'ut', 'purus', 'etiam', 'id', 'dui', 'justo', | ||
'sapien', 'scelerisque', 'viverra', 'ligula', 'aenean', 'porta', | ||
'condimentum', 'nibh', 'dictum', 'congue', 'odio', 'facilisis', | ||
'finibus', 'mattis', 'vehicula', 'lacinia', 'risus', 'placerat', | ||
'augue', 'fringilla', 'at', 'facilisi', 'arcu', 'diam', 'laoreet' | ||
]; | ||
|
||
export class ESLDemoLoremIpsum extends ESLBaseElement { | ||
static override readonly is = 'lorem-ipsum'; | ||
static override readonly observedAttributes = ['paragraphs', 'words']; | ||
|
||
@attr({parser: parseInt, defaultValue: 3}) public paragraphs: number; | ||
@attr({parser: parseInt, defaultValue: NaN}) public words: number; | ||
|
||
public rerender(): void { | ||
const paragraphs = isNaN(this.paragraphs) ? 3 : this.paragraphs; | ||
const words = isNaN(this.words) ? paragraphs * 100 : this.words; | ||
this.style.display = 'contents'; | ||
this.innerHTML = ESLDemoLoremIpsum.buildParagraphs(words); | ||
} | ||
|
||
protected override connectedCallback(): void { | ||
super.connectedCallback(); | ||
this.rerender(); | ||
} | ||
protected override disconnectedCallback(): void { | ||
this.innerHTML = ''; | ||
this.style.display = ''; | ||
super.disconnectedCallback(); | ||
} | ||
protected override attributeChangedCallback(name: string, oldValue: string, newValue: string): void { | ||
super.attributeChangedCallback(name, oldValue, newValue); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it need to call the empty method? |
||
if (name === 'paragraphs' || name === 'words') this.rerender(); | ||
} | ||
|
||
// Static API | ||
/** Capitalize first word in sentence */ | ||
protected static capitalize(s: string): string { | ||
return s.charAt(0).toUpperCase() + s.slice(1); | ||
} | ||
|
||
/** | ||
* Build a dummy string text. | ||
* @param count - number of words in string | ||
*/ | ||
public static buildString(count: number): string { | ||
const words = []; | ||
for (let i = 0; i < count; i++) { | ||
words.push(WORDS[Math.floor(Math.random() * WORDS.length)]); | ||
} | ||
return words.join(' '); | ||
} | ||
|
||
/** | ||
* Build a dummy paragraph. | ||
* @param words - number of words in paragraph | ||
*/ | ||
public static buildParagraph(words: number = 100): string { | ||
const sentences = []; | ||
while (words > 0) { | ||
const sentence = this.buildString(Math.min(10, words)); | ||
sentences.push(this.capitalize(sentence) + '.'); | ||
words -= 10; | ||
} | ||
return `<p>${sentences.join(' ')}</p>`; | ||
} | ||
|
||
/** | ||
* Build a dummy text. | ||
* @param words - number of words in text | ||
*/ | ||
public static buildParagraphs(words: number = 100): string { | ||
const result = []; | ||
while (words > 0) { | ||
const paragraph = this.buildParagraph(Math.min(100, words)); | ||
result.push(paragraph); | ||
words -= 100; | ||
} | ||
return result.join(''); | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess "dictionary" is a more appropriate name.
And it seems to me that if users could import the dictionaries they need and use them, then this would be a very cool feature. Moreover, they could make their own dictionaries and use them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great idea! I'll take a look how to make this setting available