-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 🎸 Add feature to pin text elements
✅ Closes: #258
- Loading branch information
Showing
4 changed files
with
123 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
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,50 @@ | ||
import { SpeccerOptionsInterface } from '../../../types/speccer'; | ||
|
||
/** | ||
* Generates the content for a pin element based on the provided symbol, target element, and options. | ||
* | ||
* @param {string} symbol - The default symbol to use if no text or description is provided. | ||
* @param {HTMLElement} targetElement - The HTML element for which the content is being generated. | ||
* @param {SpeccerOptionsInterface} options - The options that define how the content should be generated. | ||
* @returns {string} The generated content for the pin element. | ||
* | ||
* @example | ||
* ```ts | ||
* const symbol = '★'; | ||
* const targetElement = document.getElementById('myElement'); | ||
* const options = { pin: { text: true } }; | ||
* const content = getContentForPin(symbol, targetElement, options); | ||
* console.log(content); | ||
* // Output: '<span class="ph-speccer title">Title Text</span>' | ||
* ``` | ||
*/ | ||
export const getContentForPin = ( | ||
symbol: string, | ||
targetElement: HTMLElement, | ||
options: SpeccerOptionsInterface | ||
): string => { | ||
const { pin } = options; | ||
|
||
if (!pin) return symbol; | ||
|
||
const { text } = pin; | ||
const _is_text = | ||
text && targetElement.getAttribute('data-speccer-title') !== null; | ||
|
||
if (!_is_text) return symbol; | ||
|
||
const _title = targetElement.getAttribute('data-speccer-title'); | ||
const _description = targetElement.getAttribute('data-speccer-description'); | ||
const _heading = | ||
targetElement.nodeName.indexOf('H') === 0 | ||
? `<span class="ph-speccer heading">${targetElement.nodeName}</span>` | ||
: ''; | ||
|
||
if (!_description && _title) | ||
return `${_heading}<span class="ph-speccer title">${_title}</span>`; | ||
|
||
if (_description && _title) | ||
return `${_heading}<span class="ph-speccer title">${_title}</span><span class="ph-speccer description">${_description.replaceAll('\\n', '<br/>')}</span>`; | ||
|
||
return symbol; | ||
}; |
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