-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from os2ulf/feature/OS2UOL-111
OS2UOL-111: RTE layout block component redone
- Loading branch information
Showing
2 changed files
with
22 additions
and
112 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 |
---|---|---|
@@ -1,115 +1,14 @@ | ||
<template> | ||
<div | ||
v-if="alteredData.field_text !== null" | ||
class="rte" | ||
v-html="alteredData.field_text" | ||
></div> | ||
<div v-if="props.blockData.field_text !== null" class="rte"> | ||
<BaseRte | ||
v-if="props.blockData.field_text" | ||
:content="props.blockData.field_text" | ||
/> | ||
</div> | ||
</template> | ||
|
||
<script setup> | ||
const config = useRuntimeConfig().public; | ||
const props = defineProps({ | ||
blockData: Object, | ||
}); | ||
const alteredData = { ...props.blockData }; | ||
if (alteredData.field_text !== null) { | ||
// find all img sources in the block data | ||
const imgRegex = /<img[^>]*src="([^"]*)"[^>]*>/g; | ||
const imgSrcs = alteredData.field_text.match(imgRegex); | ||
// find all data-align in the image | ||
const alignRegex = /data-align="([^"]*)"/g; | ||
const aligns = alteredData.field_text.match(alignRegex); | ||
// find all data-caption in the image | ||
const captionRegex = /<img[^>]*data-caption="([^"]*)"[^>]*>/g; | ||
const captions = alteredData.field_text.match(captionRegex); | ||
// replace all img sources with the correct url | ||
if (imgSrcs) { | ||
imgSrcs.forEach((imgSrc) => { | ||
const src = imgSrc.match(/src="([^"]*)"/)[1]; | ||
// add the correct url to the img src | ||
const newSrc = src.replace(src, config.API_BASE_URL + src); | ||
alteredData.field_text = alteredData.field_text.replace(src, newSrc); | ||
}); | ||
} | ||
// replace all data-align options with a class | ||
if (aligns) { | ||
aligns.forEach((align) => { | ||
const alignClass = align.match(/data-align="([^"]*)"/)[1]; | ||
// attach alignmed class to image | ||
alteredData.field_text = alteredData.field_text.replace( | ||
align, | ||
`class="${alignClass}"`, | ||
); | ||
}); | ||
} | ||
// if image has data-caption | ||
if (alteredData.field_text.includes('data-caption')) { | ||
const captionRegex = /<img[^>]*data-caption="([^"]*)"[^>]*>/g; | ||
const captions = alteredData.field_text.match(captionRegex); | ||
captions.forEach((caption) => { | ||
let captionAlignmentClass = ''; | ||
// if image also has a class= attribute set var to hold the value and then attach the same class to the caption text under image | ||
if (caption.includes('class=')) { | ||
const captionText = caption.match(/class="([^"]*)"/)[1]; | ||
captionAlignmentClass = captionText; | ||
} | ||
const captionText = caption.match(/data-caption="([^"]*)"/)[1]; | ||
const captionDiv = `<figcaption class="${captionAlignmentClass}">${captionText}</figcaption>`; | ||
alteredData.field_text = alteredData.field_text.replace( | ||
caption, | ||
caption + captionDiv, | ||
); | ||
}); | ||
} | ||
} | ||
</script> | ||
|
||
<style lang="postcss" scoped> | ||
/* Aligning image with deep selectors */ | ||
.rte { | ||
:deep(.text-align-center) { | ||
display: block; | ||
margin-right: auto; | ||
margin-left: auto; | ||
text-align: center; | ||
} | ||
:deep(.text-align-left) { | ||
display: block; | ||
margin-right: aut; | ||
margin-left: 0; | ||
text-align: left; | ||
} | ||
:deep(.text-align-justify) { | ||
display: block; | ||
margin-right: auto; | ||
margin-left: auto; | ||
text-align: justify; | ||
} | ||
:deep(.text-align-right) { | ||
display: block; | ||
margin-right: 0; | ||
margin-left: auto; | ||
text-align: right; | ||
} | ||
:deep(figcaption) { | ||
color: var(--color-gray-42); | ||
font-size: 0.9rem; | ||
font-style: italic; | ||
} | ||
} | ||
</style> |