4 changed files with 120 additions and 112 deletions
-
3src/components/blogs/header/NavigateMenu.vue
-
41src/components/blogs/leftsite/CataloGue.vue
-
18src/stores/index.ts
-
94src/views/blog/ContentDetail.vue
@ -1,11 +1,42 @@ |
|||||
<template> |
<template> |
||||
|
<div> |
||||
|
<div class="catalogue" v-for="anchor in titles" :style="{ padding: `4px 0 4px ${anchor.indent * 20}px` }" |
||||
|
@click="handleAnchorClick(anchor)" :key="anchor.lineIndex"> |
||||
|
<a style="cursor: pointer">{{ anchor.title }}</a> |
||||
|
</div> |
||||
|
</div> |
||||
|
</template> |
||||
|
|
||||
</template> |
|
||||
|
<script setup lang="ts"> |
||||
|
import { computed } from 'vue'; |
||||
|
import { useContentStore } from '@/stores/index'; |
||||
|
|
||||
<script setup lang='ts'> |
|
||||
|
const store = useContentStore(); |
||||
|
|
||||
</script> |
|
||||
|
const titles = computed(() => store.titles); |
||||
|
|
||||
<style> |
|
||||
|
const handleAnchorClick = (anchor: { title: string; lineIndex: string }) => { |
||||
|
const heading = document.querySelector(`[data-v-md-line="${anchor.lineIndex}"]`); |
||||
|
|
||||
|
if (heading) { |
||||
|
heading.scrollIntoView({ behavior: 'smooth', block: 'start' }); |
||||
|
} else { |
||||
|
const observer = new MutationObserver((mutations, obs) => { |
||||
|
const heading = document.querySelector(`[data-v-md-line="${anchor.lineIndex}"]`); |
||||
|
if (heading) { |
||||
|
heading.scrollIntoView({ behavior: 'smooth', block: 'start' }); |
||||
|
obs.disconnect(); |
||||
|
} |
||||
|
}); |
||||
|
const container = document.querySelector('#content-container') as HTMLElement; |
||||
|
observer.observe(container, { childList: true, subtree: true }); |
||||
|
} |
||||
|
}; |
||||
|
</script> |
||||
|
|
||||
|
<style scoped> |
||||
|
.catalogue { |
||||
|
cursor: pointer; |
||||
|
} |
||||
|
</style> |
||||
|
|
||||
</style> |
|
@ -1,100 +1,74 @@ |
|||||
<template> |
<template> |
||||
|
<div class="contentDetail"> |
||||
<div :id="containerId"> |
<div :id="containerId"> |
||||
<div v-for="anchor in titles" :style="{ padding: `4px 0 4px ${anchor.indent * 20}px` }" |
|
||||
@click="handleAnchorClick(anchor)" :key="anchor.lineIndex"> |
|
||||
<a style="cursor: pointer">{{ anchor.title }}</a> |
|
||||
</div> |
|
||||
<a-card> |
<a-card> |
||||
<v-md-preview v-if="text.content" :text="text.content" v-bind="previewProps" /> |
|
||||
|
<v-md-preview v-if="store.content.text" :text="store.content.text" v-bind="previewProps" /> |
||||
</a-card> |
</a-card> |
||||
</div> |
</div> |
||||
</template> |
|
||||
|
</div> |
||||
|
</template> |
||||
|
|
||||
<script setup lang="ts"> |
|
||||
import { ref, onMounted, nextTick } from 'vue'; |
|
||||
import { useRoute } from 'vue-router'; |
|
||||
import { get } from '@/tools/request'; |
|
||||
|
<script setup lang="ts"> |
||||
|
import { ref, onMounted, nextTick } from 'vue'; |
||||
|
import { useRoute } from 'vue-router'; |
||||
|
import { get } from '@/tools/request'; |
||||
|
import { useContentStore } from '@/stores'; |
||||
|
|
||||
const route = useRoute(); |
|
||||
|
const route = useRoute(); |
||||
|
const store = useContentStore(); |
||||
|
|
||||
const props = defineProps<{ |
|
||||
|
const props = defineProps<{ |
||||
apiUrl: string; |
apiUrl: string; |
||||
contentField: string; |
contentField: string; |
||||
containerId: string; |
containerId: string; |
||||
}>(); |
|
||||
|
|
||||
const text = ref({ content: "" }); |
|
||||
|
}>(); |
||||
|
|
||||
const titles = ref<{ title: string; lineIndex: string; indent: number }[]>([]); |
|
||||
|
|
||||
// Props for v-md-preview component |
|
||||
const previewProps = { |
|
||||
|
// Props for v-md-preview component |
||||
|
const previewProps = { |
||||
leftToolbar: "undo redo clear | h bold italic strikethrough quote | ul ol table hr | link image code | save | tip | emoji" |
leftToolbar: "undo redo clear | h bold italic strikethrough quote | ul ol table hr | link image code | save | tip | emoji" |
||||
}; |
|
||||
|
}; |
||||
|
|
||||
// Fetch content |
|
||||
const fetchContent = async () => { |
|
||||
|
// Fetch content |
||||
|
const fetchContent = async () => { |
||||
try { |
try { |
||||
const response = await get(`${props.apiUrl}/${route.params.id}`); |
const response = await get(`${props.apiUrl}/${route.params.id}`); |
||||
if (response.data.data) { |
if (response.data.data) { |
||||
text.value.content = response.data.data[props.contentField]; |
|
||||
|
store.setContent(response.data.data[props.contentField]); |
||||
} |
} |
||||
} catch (error) { |
} catch (error) { |
||||
console.error("Error fetching content:", error); |
console.error("Error fetching content:", error); |
||||
} |
} |
||||
}; |
|
||||
|
}; |
||||
|
|
||||
// Process headings after content is fetched |
|
||||
const processHeadings = async () => { |
|
||||
await nextTick(); |
|
||||
|
// Process headings after content is fetched |
||||
|
const processHeadings = async () => { |
||||
const anchors = document.querySelectorAll(`#${props.containerId} h1, #${props.containerId} h2, #${props.containerId} h3, #${props.containerId} h4, #${props.containerId} h5, #${props.containerId} h6`); |
const anchors = document.querySelectorAll(`#${props.containerId} h1, #${props.containerId} h2, #${props.containerId} h3, #${props.containerId} h4, #${props.containerId} h5, #${props.containerId} h6`); |
||||
const titlesArray = Array.from(anchors).filter((title: any) => !!title.innerText.trim()); |
const titlesArray = Array.from(anchors).filter((title: any) => !!title.innerText.trim()); |
||||
|
|
||||
if (!titlesArray.length) { |
if (!titlesArray.length) { |
||||
titles.value = []; |
|
||||
|
store.setTitles([]); |
||||
return; |
return; |
||||
} |
} |
||||
|
|
||||
const hTags = Array.from(new Set(titlesArray.map((title: any) => title.tagName))).sort(); |
const hTags = Array.from(new Set(titlesArray.map((title: any) => title.tagName))).sort(); |
||||
|
|
||||
titles.value = titlesArray.map((el: any) => ({ |
|
||||
|
store.setTitles(titlesArray.map((el: any) => ({ |
||||
title: el.innerText, |
title: el.innerText, |
||||
lineIndex: el.getAttribute('data-v-md-line') || '', |
lineIndex: el.getAttribute('data-v-md-line') || '', |
||||
indent: hTags.indexOf(el.tagName), |
indent: hTags.indexOf(el.tagName), |
||||
})); |
|
||||
}; |
|
||||
|
|
||||
// Handle anchor click |
|
||||
const handleAnchorClick = (anchor: { title: string; lineIndex: string }) => { |
|
||||
const heading = document.querySelector(`#${props.containerId} [data-v-md-line="${anchor.lineIndex}"]`); |
|
||||
|
}))); |
||||
|
}; |
||||
|
|
||||
if (heading) { |
|
||||
heading.scrollIntoView({ behavior: 'smooth', block: 'start' }); |
|
||||
} else { |
|
||||
// Use MutationObserver to wait for the target element to be available |
|
||||
const observer = new MutationObserver((mutations, obs) => { |
|
||||
const heading = document.querySelector(`#${props.containerId} [data-v-md-line="${anchor.lineIndex}"]`); |
|
||||
if (heading) { |
|
||||
heading.scrollIntoView({ behavior: 'smooth', block: 'start' }); |
|
||||
obs.disconnect(); // Stop observing once the target element is found |
|
||||
} |
|
||||
}); |
|
||||
const container = document.querySelector(`#${props.containerId}`) as HTMLElement; |
|
||||
observer.observe(container, { childList: true, subtree: true }); |
|
||||
} |
|
||||
}; |
|
||||
|
|
||||
// Lifecycle hook to fetch content and process headings |
|
||||
onMounted(async () => { |
|
||||
|
// Lifecycle hook to fetch content and process headings |
||||
|
onMounted(async () => { |
||||
await fetchContent(); |
await fetchContent(); |
||||
await processHeadings(); |
await processHeadings(); |
||||
}); |
|
||||
</script> |
|
||||
|
}); |
||||
|
</script> |
||||
|
|
||||
<style scoped> |
|
||||
#blogDetail, #diaryDetail { |
|
||||
|
<style scoped> |
||||
|
.contentDetail { |
||||
width: 45%; |
width: 45%; |
||||
margin: 0 24px; |
margin: 0 24px; |
||||
} |
|
||||
</style> |
|
||||
|
|
||||
|
} |
||||
|
</style> |
Write
Preview
Loading…
Cancel
Save
Reference in new issue