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(custom): custom render prismic type #7

Merged
merged 1 commit into from
Feb 10, 2021
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
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ Use this:
<Prismic::Dom @nodes={{@myPrismicDoc.data.myRichText}} />
```

### onUnknonwnTag

Additionaly you can pass an `onUnknownTag` action to handle recieving data of a type `Prismic::Dom` can't render.

```hbs
Expand All @@ -71,6 +73,29 @@ export default class MyComponent extends Component {
}
```

### Custom Rendering

Pass a custom component name to be used to render a prismic type. For example to custom render the 'group-list-item' and 'hyperlink' types

```hbs
<Prismic::Dom
@group-list-item='my-list'
@hyperlink='my-hyperlink'
@nodes={{@myPrismicDoc.data.myRichText}}
/>
```

my-list.hbs
```hbs
<h1>My List</h2>
<ul>{{yield}}<ul>
```

my-hyperlink.hbs
```hbs
<a href={{@node.element.data.url}}>{{yield}}</a>
```


Contributing
------------------------------------------------------------------------------
Expand Down
5 changes: 4 additions & 1 deletion addon/components/prismic/children.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{{~#each @node.children as |child|~}}
<Prismic::Element @node={{child}} @onUnknownTag={{@onUnknownTag}} />
<Prismic::Element
@componentNames={{@componentNames}}
@node={{child}}
@onUnknownTag={{@onUnknownTag}} />
{{~else~}}
{{[email protected]~}}
{{~/each~}}
6 changes: 5 additions & 1 deletion addon/components/prismic/dom.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
{{@nodes}}
{{~else~}}
{{~#each this.tree.children as |child|~}}
<Prismic::Element @node={{child}} @onUnknownTag={{@onUnknownTag}} />
<Prismic::Element
@componentNames={{this.componentNames}}
@node={{child}}
@onUnknownTag={{@onUnknownTag}}
/>
{{~/each~}}
{{~/if~}}
</div>
7 changes: 7 additions & 0 deletions addon/components/prismic/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,11 @@ export default class PrismicDomComponent extends Component {
get isString() {
return typeof this.args.nodes === 'string';
}

get componentNames() {
let names = { ...this.args };
delete names.nodes;
delete names.onUnknownTag;
return names;
}
}
28 changes: 24 additions & 4 deletions addon/components/prismic/element.hbs
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
{{~#if (eq @node.type "image")~}}
{{~#if this.isCustom~}}
{{~#component this.componentName node=@node~}}
<Prismic::Children
@componentNames={{@componentNames}}
@node={{@node}}
@onUnknownTag={{@onUnknownTag}}
/>
{{~/component~}}
{{~else if (eq @node.type "image")~}}
<Prismic::Image @node={{@node}} />
{{~else if (eq @node.type "span")~}}
<Prismic::Children @node={{@node}} @onUnknownTag={{@onUnknownTag}} />
<Prismic::Children
@componentNames={{@componentNames}}
@node={{@node}}
@onUnknownTag={{@onUnknownTag}}
/>
{{~else if (eq @node.type "hyperlink")~}}
<a
href={{@node.element.data.url}}
rel="noreferrer noopener"
target={{this.target}}
><Prismic::Children @node={{@node}} @onUnknownTag={{@onUnknownTag}} /></a>
><Prismic::Children
@componentNames={{@componentNames}}
@node={{@node}}
@onUnknownTag={{@onUnknownTag}}
/></a>
{{~else~}}
{{~#let (element this.tagName) as |Tag|~}}
<Tag><Prismic::Children @node={{@node}} @onUnknownTag={{@onUnknownTag}} /></Tag>
<Tag><Prismic::Children
@componentNames={{@componentNames}}
@node={{@node}}
@onUnknownTag={{@onUnknownTag}}
/></Tag>
{{~/let~}}
{{~/if~}}
8 changes: 8 additions & 0 deletions addon/components/prismic/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,12 @@ export default class PrismicElementComponent extends Component {
get target() {
return this.args.node.element.data.value?.target || '_blank';
}

get isCustom() {
return Boolean(this.componentName);
}

get componentName() {
return this.args.componentNames?.[this.args.node.type];
}
}
1 change: 1 addition & 0 deletions tests/dummy/app/components/group-list-item.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<ul>{{~yield~}}elephant</ul>
1 change: 1 addition & 0 deletions tests/dummy/app/components/hyperlink.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<a href={{@node.element.data.url}}>{{yield}}</a>
1 change: 1 addition & 0 deletions tests/dummy/app/components/list-item.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<li>{{~yield}} bananna</li>
52 changes: 52 additions & 0 deletions tests/integration/components/prismic/dom-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,58 @@ module('Integration | Component | prismic/dom', function (hooks) {
});
});

module('custom elements', function () {
test('hyperlink', async function (assert) {
this.nodes = [
{
type: 'paragraph',
text: 'A link to somewhere',
spans: [
{
start: 2,
end: 6,
type: 'hyperlink',
data: { link_type: 'Web', url: 'https://example.org' },
},
],
},
];

await render(
hbs`<Prismic::Dom @nodes={{this.nodes}} @hyperlink='hyperlink'/>`
);
assert.equal(
cleanHtml(this),
'<div><p>A <a href="https://example.org">link</a> to somewhere</p></div>'
);
});

test('list', async function (assert) {
this.nodes = [
{ type: 'list-item', text: 'one', spans: [] },
{ type: 'list-item', text: 'two', spans: [] },
];

this.listItem = '';

await render(
hbs`<Prismic::Dom @nodes={{this.nodes}} @group-list-item='group-list-item' @list-item={{this.listItem}}/>`
);

assert.equal(
cleanHtml(this),
'<div><ul><li>one</li><li>two</li>elephant</ul></div>'
);

this.set('listItem', 'list-item');

assert.equal(
cleanHtml(this),
'<div><ul><li>one bananna</li><li>two bananna</li>elephant</ul></div>'
);
});
});

module('complex combinations', function () {
test('list', async function (assert) {
this.nodes = [
Expand Down