|
1 | 1 | <template> |
2 | | - <section |
3 | | - v-html="content" |
4 | | - ref="codeSection" |
5 | | - class="latest-posts-single-post-content text-black" |
6 | | - /> |
| 2 | + <div class="post-content-wrapper"> |
| 3 | + <section |
| 4 | + v-html="content" |
| 5 | + ref="codeSection" |
| 6 | + class="latest-posts-single-post-content text-black" |
| 7 | + /> |
| 8 | + |
| 9 | + <ImageModal |
| 10 | + :visible="isModalVisible" |
| 11 | + :image-url="selectedImageUrl" |
| 12 | + @close="closeModal" |
| 13 | + /> |
| 14 | + </div> |
7 | 15 | </template> |
8 | 16 |
|
9 | 17 | <script> |
10 | | -import hljs from 'highlight.js'; // Import Highlight.js |
11 | | -import 'highlight.js/styles/atom-one-dark-reasonable.css'; // Import the desired style |
| 18 | +import hljs from 'highlight.js'; |
| 19 | +import 'highlight.js/styles/atom-one-dark-reasonable.css'; |
| 20 | +import ImageModal from '../ImageModal.vue'; |
12 | 21 |
|
13 | 22 | export default { |
14 | 23 | name: 'PostContent', |
| 24 | + components: { |
| 25 | + ImageModal, // Register the modal component |
| 26 | + }, |
15 | 27 | props: { |
16 | 28 | content: { |
17 | 29 | type: String, |
18 | 30 | default: 'lorem ipsum' |
19 | 31 | }, |
20 | 32 | }, |
| 33 | + data() { |
| 34 | + return { |
| 35 | + isModalVisible: false, |
| 36 | + selectedImageUrl: '', |
| 37 | + }; |
| 38 | + }, |
21 | 39 | methods: { |
| 40 | + // Existing method for code highlighting |
22 | 41 | highlightCode() { |
| 42 | + // It's important to check if the ref exists before querying the DOM |
| 43 | + if (!this.$refs.codeSection) return; |
23 | 44 |
|
24 | | - // Target only the code blocks in the section |
25 | 45 | const codeBlocks = this.$refs.codeSection.querySelectorAll('pre code'); |
26 | 46 | codeBlocks.forEach((block) => { |
27 | | - hljs.highlightBlock(block); // Highlight the code block |
| 47 | + hljs.highlightBlock(block); |
28 | 48 | }); |
29 | 49 | }, |
| 50 | +
|
| 51 | + // Attach click listeners to images |
| 52 | + attachImageClickEvent() { |
| 53 | + // It's important to check if the ref exists before querying the DOM |
| 54 | + if (!this.$refs.codeSection) return; |
| 55 | +
|
| 56 | + const images = this.$refs.codeSection.querySelectorAll('img'); |
| 57 | +
|
| 58 | + images.forEach(img => { |
| 59 | + // Prevent attaching the listener multiple times on component updates |
| 60 | + if (img.dataset.clickAttached) return; |
| 61 | +
|
| 62 | + img.style.cursor = 'zoom-in'; // Visual cue for the user |
| 63 | +
|
| 64 | + // 1. Define the handler function |
| 65 | + const clickHandler = (event) => { |
| 66 | + event.preventDefault(); // Prevent default link behavior if the image is wrapped in an <a> tag |
| 67 | + this.openModal(img.src); |
| 68 | + }; |
| 69 | +
|
| 70 | + // 2. Attach the native DOM event listener |
| 71 | + img.addEventListener('click', clickHandler); |
| 72 | +
|
| 73 | + // 3. Mark the image as handled |
| 74 | + img.dataset.clickAttached = 'true'; |
| 75 | + }); |
| 76 | + }, |
| 77 | +
|
| 78 | + // 🆕 NEW METHOD: Modal controls |
| 79 | + openModal(url) { |
| 80 | + this.selectedImageUrl = url; |
| 81 | + this.isModalVisible = true; |
| 82 | + }, |
| 83 | + closeModal() { |
| 84 | + this.isModalVisible = false; |
| 85 | + this.selectedImageUrl = ''; |
| 86 | + }, |
30 | 87 | }, |
31 | | - mounted() { |
32 | 88 |
|
33 | | - // Apply syntax highlighting when mounted |
| 89 | + mounted() { |
| 90 | + // Apply syntax highlighting |
34 | 91 | this.highlightCode(); |
| 92 | + // 🆕 Attach image click event after initial render |
| 93 | + this.attachImageClickEvent(); |
35 | 94 | }, |
36 | 95 |
|
| 96 | +
|
37 | 97 | } |
38 | 98 | </script> |
39 | 99 |
|
|
0 commit comments