Skip to content

Commit

Permalink
Merge pull request #89 from mbehzad/bugfix/71-rich-text-in-sidekick-l…
Browse files Browse the repository at this point in the history
…ibrary

Bugfix/71 rich text in sidekick library
  • Loading branch information
LoomingEcho authored May 31, 2024
2 parents 8600035 + 7b485c5 commit 06d2c3f
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 25 deletions.
2 changes: 1 addition & 1 deletion dist/banner/banner.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/banner/banner.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ Represents the constructed Element.
| ---------- | ---------- |
| `SidekickElement` | `{
dataLibraryId?: string;
innerHTML: string;
content: DocumentFragment;
href?: string;
}` |

Expand Down
8 changes: 4 additions & 4 deletions src/blocks/banner/banner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ const template = (args: TemplateArgs) => {
<div id="banner">
<div class="content">
<header>
<h1 ${getSidekickLibraryId(headline)}>${headline.innerHTML}</h1>
<p ${getSidekickLibraryId(subline)}>${subline.innerHTML}</p>
<h1 ${getSidekickLibraryId(headline)}>${headline.content}</h1>
<p ${getSidekickLibraryId(subline)}>${subline.content}</p>
</header>
${texts?.map((text) => html`<p ${getSidekickLibraryId(text)}>${text.innerHTML}</p>`)}
${texts?.map((text) => html`<div ${getSidekickLibraryId(text)}>${text.content}</div>`)}
<ul class="actions">
${buttons?.map(
(button) =>
html` <li>
<a href="${button.href}" class="button big" ${getSidekickLibraryId(button)}>${button.innerHTML}</a>
<a href="${button.href}" class="button big" ${getSidekickLibraryId(button)}>${button.content}</a>
</li>`
)}
</ul>
Expand Down
9 changes: 5 additions & 4 deletions src/helpers/sidekick/extractSidekickLibraryId.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('extractSidekickLibraryId', () => {
beforeEach(() => {
expected = {
dataLibraryId: undefined,
innerHTML: '',
content: new DocumentFragment(),
href: '',
};
});
Expand All @@ -17,15 +17,16 @@ describe('extractSidekickLibraryId', () => {
expect(result).toEqual(expected);
});

test('extracts innerHTML correctly', () => {
test('extracts content correctly', () => {
const element = document.createElement('div');
const innerHTML = '<p>Hello, World!</p>';

element.innerHTML = innerHTML;
expected.innerHTML = innerHTML;

const result = extractSidekickLibraryId(element);
expect(result).toEqual(expected);
const target = document.createElement('div');
target.append(result.content);
expect(target.innerHTML).toEqual(innerHTML);
});

test('extracts href attribute correctly for anchor elements', () => {
Expand Down
16 changes: 8 additions & 8 deletions src/helpers/sidekick/extractSidekickLibraryId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { isSidekickLibraryActive } from './isSidekickLibraryActive.ts';
*/
export type SidekickElement = {
dataLibraryId?: string;
innerHTML: string;
content: DocumentFragment;
href?: string;
};

Expand All @@ -22,12 +22,12 @@ export type SidekickElement = {
* @returns {SidekickElement} - A constructed element object.
*
* @example
* const button = extractSidekickLibraryId(document.querySelector('a'));
* const cta = extractSidekickLibraryId(document.querySelector('a'));
* <a
* href="${button.href}"
* data-library-id="${ifDefined(button.id)}"
* contenteditable="${ifDefined(button.id ? true : undefined)}">
* ${button.text}
* href="${cta.href}"
* data-library-id="${ifDefined(cta.dataLibraryId)}"
* contenteditable="${ifDefined(cta.dataLibraryId ? true : undefined)}">
* ${cta.content}
* </a>
*
* @remarks
Expand All @@ -37,12 +37,12 @@ export type SidekickElement = {
export const extractSidekickLibraryId = (element?: HTMLElement | HTMLAnchorElement | null): SidekickElement => {
const constructedElement: SidekickElement = {
dataLibraryId: undefined,
innerHTML: '',
content: new DocumentFragment(),
href: '',
};
if (!element) return constructedElement;

constructedElement.innerHTML = element.innerHTML;
constructedElement.content.append(...element.cloneNode(true).childNodes);
if (element instanceof HTMLAnchorElement && element.href !== '') {
constructedElement.href = element.href;
}
Expand Down
12 changes: 6 additions & 6 deletions types/src/helpers/sidekick/extractSidekickLibraryId.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
export type SidekickElement = {
dataLibraryId?: string;
innerHTML: string;
content: DocumentFragment;
href?: string;
};
/**
Expand All @@ -19,12 +19,12 @@ export type SidekickElement = {
* @returns {SidekickElement} - A constructed element object.
*
* @example
* const button = extractSidekickLibraryId(document.querySelector('a'));
* const cta = extractSidekickLibraryId(document.querySelector('a'));
* <a
* href="${button.href}"
* data-library-id="${ifDefined(button.id)}"
* contenteditable="${ifDefined(button.id ? true : undefined)}">
* ${button.text}
* href="${cta.href}"
* data-library-id="${ifDefined(cta.dataLibraryId)}"
* contenteditable="${ifDefined(cta.dataLibraryId ? true : undefined)}">
* ${cta.content}
* </a>
*
* @remarks
Expand Down

0 comments on commit 06d2c3f

Please sign in to comment.