web: 修复快捷键设置bug和esc键逻辑; 修复超大章节列表造成的卡顿

bug: 第一点击目录时没有跳转到合适位置
This commit is contained in:
Xwite 2023-05-10 12:41:31 +08:00
parent a3be54238b
commit ec9c2fec08
5 changed files with 64 additions and 84 deletions

View File

@ -10,6 +10,7 @@ export {}
declare module '@vue/runtime-core' { declare module '@vue/runtime-core' {
export interface GlobalComponents { export interface GlobalComponents {
BookItems: typeof import('./components/BookItems.vue')['default'] BookItems: typeof import('./components/BookItems.vue')['default']
CatalogItem: typeof import('./components/CatalogItem.vue')['default']
ChapterContent: typeof import('./components/ChapterContent.vue')['default'] ChapterContent: typeof import('./components/ChapterContent.vue')['default']
ElButton: typeof import('element-plus/es')['ElButton'] ElButton: typeof import('element-plus/es')['ElButton']
ElCheckbox: typeof import('element-plus/es')['ElCheckbox'] ElCheckbox: typeof import('element-plus/es')['ElCheckbox']

View File

@ -44,9 +44,8 @@ const calculateWordCount = (paragraph) => {
return paragraph.replaceAll(imgPattern, imagePlaceHolder).length; return paragraph.replaceAll(imgPattern, imagePlaceHolder).length;
}; };
const wordCounts = computed(() => { const wordCounts = computed(() => {
return Array.from(props.contents, content => calculateWordCount(content)); return Array.from(props.contents, (content) => calculateWordCount(content));
}); });
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>

View File

@ -1,64 +1,55 @@
<template> <template>
<div class="cata-wrapper" :style="popupTheme"> <div class="cata-wrapper" :style="popupTheme">
<div class="title">目录</div> <div class="title">目录</div>
<div <virtual-list
class="data-wrapper" style="height: 300px; overflow: auto"
ref="cataData"
:class="{ night: isNight, day: !isNight }" :class="{ night: isNight, day: !isNight }"
> ref="virtualListRef"
<div class="cata"> :data-key="'index'"
<div :start="index"
class="log" wrap-class="data-wrapper"
v-for="(note, index) in catalog" item-class="cata"
:class="{ selected: isSelected(index) }" :data-sources="catalog"
:key="note.durChapterIndex" :data-component="CatalogItem"
@click="gotoChapter(note)" :estimate-size="40"
ref="cata" :extra-props="{ gotoChapter }"
> />
<div class="log-text">
{{ note.title }}
</div>
</div>
</div>
</div>
</div> </div>
</template> </template>
<script setup> <script setup>
import VirtualList from "vue3-virtual-scroll-list";
import jump from "../plugins/jump"; import jump from "../plugins/jump";
import settings from "../plugins/config"; import settings from "../plugins/config";
import "../assets/fonts/popfont.css"; import "../assets/fonts/popfont.css";
import CatalogItem from "./CatalogItem.vue";
const store = useBookStore(); const store = useBookStore();
const isNight = ref(false); const isNight = computed(() => theme.value == 6);
const { index } = toRefs(store.readingBook);
const { catalog, popCataVisible } = storeToRefs(store); const { catalog, popCataVisible } = storeToRefs(store);
const theme = computed(() => { const theme = computed(() => {
return store.config.theme; return store.config.theme;
}); });
const popupTheme = computed(() => { const popupTheme = computed(() => {
return { return {
background: settings.themes[theme.value].popup, background: settings.themes[theme.value].popup,
}; };
}); });
watchEffect(() => {
isNight.value = theme.value == 6;
});
const cata = ref(); const index = computed({
const cataData = ref(); get: () => store.readingBook.index,
watch(popCataVisible, () => { set: (value) => (store.readingBook.index = value),
});
const virtualListRef = ref();
watch(popCataVisible, (visible) => {
if (!visible) return;
nextTick(() => { nextTick(() => {
let wrapper = cataData.value; virtualListRef.value.scrollToIndex(index.value);
jump(cata.value[index.value], { container: wrapper, duration: 0 });
}); });
}); });
const isSelected = (idx) => {
return idx == index.value;
};
const emit = defineEmits(["getContent"]); const emit = defineEmits(["getContent"]);
const gotoChapter = (note) => { const gotoChapter = (note) => {
index.value = catalog.value.indexOf(note); index.value = catalog.value.indexOf(note);
@ -83,55 +74,35 @@ const gotoChapter = (note) => {
width: fit-content; width: fit-content;
border-bottom: 1px solid #ed4259; border-bottom: 1px solid #ed4259;
} }
:deep(.data-wrapper) {
.data-wrapper { display: flex;
height: 300px; flex-direction: row;
overflow: auto; flex-wrap: wrap;
justify-content: space-between;
.cata { .cata {
display: flex; width: 50%;
flex-direction: row; height: 40px;
flex-wrap: wrap; cursor: pointer;
justify-content: space-between; font: 16px / 40px PingFangSC-Regular, HelveticaNeue-Light,
"Helvetica Neue Light", "Microsoft YaHei", sans-serif;
.selected {
color: #eb4259;
}
.log {
width: 50%;
height: 40px;
cursor: pointer;
float: left;
font: 16px / 40px PingFangSC-Regular, HelveticaNeue-Light,
"Helvetica Neue Light", "Microsoft YaHei", sans-serif;
.log-text {
margin-right: 26px;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
}
} }
} }
.night { .night {
:deep(.log) { :deep(.cata) {
border-bottom: 1px solid #666; border-bottom: 1px solid #666;
} }
} }
.day { .day {
:deep(.log) { :deep(.cata) {
border-bottom: 1px solid #f2f2f2; border-bottom: 1px solid #f2f2f2;
} }
} }
} }
@media screen and (max-width: 500px) { @media screen and (max-width: 500px) {
.cata-wrapper .data-wrapper .cata .log { :deep(.cata) {
width: 100%; width: 100% !important;
} }
} }
</style> </style>

View File

@ -221,21 +221,31 @@ const recordKeyDowning = ref(false);
const recordKeyDownIndex = ref(-1); const recordKeyDownIndex = ref(-1);
const stopRecordKeyDown = () => { const stopRecordKeyDown = () => {
if (!recordKeyDowning.value) {
hotkeysDialogVisible.value = false;
}
recordKeyDowning.value = false; recordKeyDowning.value = false;
}; };
watch(hotkeysDialogVisible, (visibale) => { watch(
if (!visibale) return hotkeys.unbind("*"); hotkeysDialogVisible,
hotkeys.unbind(); (visibale) => {
/**监听按键 */ if (!visibale) return hotkeys.unbind("*");
hotkeys("*", (event) => { hotkeys.unbind();
event.preventDefault(); /**监听按键 */
if (recordKeyDowning.value && recordKeyDownIndex.value > -1) hotkeys("*", (event) => {
buttons.value[recordKeyDownIndex.value].hotKeys = event.preventDefault();
// @ts-ignore let pressedKeys = hotkeys.getPressedKeyString();
hotkeys.getPressedKeyString(); if (pressedKeys.length == 1 && pressedKeys[0] == "esc") {
}); //esc
}); return;
}
if (recordKeyDowning.value && recordKeyDownIndex.value > -1)
buttons.value[recordKeyDownIndex.value].hotKeys = pressedKeys;
});
},
{ immediate: true }
);
const recordKeyDown = (index) => { const recordKeyDown = (index) => {
recordKeyDowning.value = true; recordKeyDowning.value = true;

View File

@ -148,7 +148,6 @@ const {
config, config,
readingBook, readingBook,
} = storeToRefs(store); } = storeToRefs(store);
const chapterPos = computed({ const chapterPos = computed({
get: () => readingBook.value.chapterPos, get: () => readingBook.value.chapterPos,
set: (value) => (readingBook.value.chapterPos = value), set: (value) => (readingBook.value.chapterPos = value),
@ -293,9 +292,9 @@ const toChapterPos = (pos) => {
let wordCount = 0; let wordCount = 0;
if (chapter.value.length != 1) return; if (chapter.value.length != 1) return;
for (let element of chapter.value[0].children) { for (let element of chapter.value[0].children) {
wordCount += parseInt(element.getAttribute("wordCount")) + 1;// wordCount += parseInt(element.getAttribute("wordCount")) + 1; //
if (wordCount - 1 >= pos) { if (wordCount - 1 >= pos) {
// //
jump(element, { jump(element, {
duration: 0, duration: 0,
}); });