-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cms): parse vue components in text elements (#744)
* feat: (wip) adding capability to parse vue components inside text element in sw cms * feat: (wip) adding capability to parse vue components inside text element in sw cms - refactor with html-to-vue library * feat: (wip) Added entities-parser plugin to work with html-to-vue * feat: (wip) Added entities-parser - moved 'he' dependency from default-theme to nuxt-module * feat: fixed SSR of CmsElementText.vue, added SwButton and SfLink to extraComponentsMap * feat: SfButtons from CmsElementText.vue now contains a proper SfLink.
- Loading branch information
Showing
9 changed files
with
181 additions
and
16 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 |
---|---|---|
@@ -1,35 +1,96 @@ | ||
<template> | ||
<div v-show="rawHtml" class="cms-element-text"> | ||
<div class="cms-element-text__content" v-html="rawHtml"></div> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
// import any extra components here | ||
import { SfLink } from '@storefront-ui/vue' | ||
import SwButton from '@shopware-pwa/default-theme/components/atoms/SwButton' | ||
import { renderHtml } from 'html-to-vue' | ||
export default { | ||
components: {}, | ||
name: 'CmsElementText', | ||
functional: true, | ||
props: { | ||
content: { | ||
type: Object, | ||
default: () => ({}), | ||
}, | ||
}, | ||
computed: { | ||
rawHtml() { | ||
return this.content?.data?.content | ||
}, | ||
verticalAlign() { | ||
return this.content?.config?.verticalAlign?.value | ||
}, | ||
render(h, context) { | ||
const config = { | ||
textTransformer: context.parent.$entitiesDecoder, | ||
extraComponentsMap: { | ||
link: { | ||
conditions(node) { | ||
return ( | ||
node.type === 'tag' && | ||
node.name === 'a' && | ||
!node.attrs?.class?.match(/btn\s?/) | ||
) | ||
}, | ||
renderer(node, children, createElement) { | ||
return createElement( | ||
SfLink, | ||
{ | ||
class: node.attrs?.class, | ||
attrs: { | ||
target: node.attrs?.target, | ||
}, | ||
props: { | ||
link: node.attrs?.href, | ||
}, | ||
}, | ||
[...children] | ||
) | ||
}, | ||
}, | ||
button: { | ||
conditions(node) { | ||
return ( | ||
node.type === 'tag' && | ||
node.name === 'a' && | ||
node.attrs?.class?.match(/btn\s?/) | ||
) | ||
}, | ||
renderer(node, children, createElement) { | ||
const _class = node.attrs?.class.replace('btn-secondary', 'color-secondary').replace('btn-primary', 'color-primary') | ||
return createElement( | ||
SwButton, | ||
{ | ||
class: _class, | ||
attrs: { | ||
target: node.attrs?.target, | ||
}, | ||
props: { | ||
link: node.attrs?.href, | ||
}, | ||
}, | ||
[createElement(SfLink, {}, [...children])] | ||
) | ||
}, | ||
}, | ||
}, | ||
} | ||
const rawHtml = context.props.content?.data?.content | ||
return renderHtml(rawHtml, config, h, context) | ||
}, | ||
} | ||
</script> | ||
<style lang="scss" scoped> | ||
<style lang="scss"> | ||
@import '../settings.scss'; | ||
.cms-element-text { | ||
@include sizing-mode-boxed; | ||
padding: var(--spacer-xl); | ||
} | ||
.sf-button { | ||
display: inline-block; | ||
.sf-link { | ||
text-decoration: none; | ||
} | ||
&.btn-sm { | ||
--button-padding: 0.5rem; | ||
} | ||
} | ||
</style> |
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
8 changes: 8 additions & 0 deletions
8
packages/nuxt-module/plugins/entities-parser/entities-parser.csr.js
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,8 @@ | ||
import Vue from "vue"; | ||
|
||
let decoder; | ||
Vue.prototype.$entitiesDecoder = (text) => { | ||
decoder = decoder || document.createElement("div"); | ||
decoder.innerHTML = text; | ||
return decoder.textContent; | ||
}; |
8 changes: 8 additions & 0 deletions
8
packages/nuxt-module/plugins/entities-parser/entities-parser.ssr.js
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,8 @@ | ||
import { decode } from "he"; | ||
import Vue from "vue"; | ||
|
||
Vue.prototype.$entitiesDecoder = (html) => decode(html); | ||
export default ({ app }, inject) => { | ||
// Set the function directly on the context.app object | ||
app.$entitiesDecoder = (html) => decode(html); | ||
}; |
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
3b6b5ac
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.
Successfully deployed to following URLs: