Skip to content

Commit

Permalink
feat: 添加滚动逻辑 添加依赖 优化结构
Browse files Browse the repository at this point in the history
  • Loading branch information
YasinChan committed Dec 18, 2023
1 parent e78c834 commit 08f4709
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 54 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
"format": "prettier --write src/"
},
"dependencies": {
"@vueuse/core": "^10.7.0",
"axios": "^1.4.0",
"js-cookie": "^3.0.5",
"lodash": "^4.17.21",
"pinia": "^2.1.4",
"pinyin-pro": "^3.15.4",
"vue": "^3.3.4",
Expand Down
49 changes: 48 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions src/assets/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
}

body {
font-family: Inter, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu,
Cantarell, 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
font-family: Quotes, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
Expand Down
37 changes: 25 additions & 12 deletions src/components/WordInput.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script setup lang="ts">
import { ref, onMounted, reactive, nextTick, watch } from 'vue';
import { KEY_CODE_ENUM } from '@/config/key';
import { useScroll } from '@vueuse/core';
type SentenceArrItem = {
id: number;
Expand All @@ -9,16 +10,20 @@ type SentenceArrItem = {
isWrong: boolean;
};
const whiteList = ['']; // 白名单
const LINE_HEIGHT = 70;
const el = ref<HTMLElement | null>(null);
const { x, y } = useScroll(el, { behavior: 'smooth' });
const whiteList = ['', '', '}', ']', '']; // 白名单
const inputAreaRef = ref<HTMLElement | null>(null);
const props = defineProps<{
sentence: string;
quote: string;
}>();
const state = reactive({
currentAreaHeight: LINE_HEIGHT,
isComposing: false,
inputText: '',
sentenceArr: [] as SentenceArrItem[]
quoteArr: [] as SentenceArrItem[]
});
onMounted(async () => {
Expand All @@ -28,14 +33,14 @@ onMounted(async () => {
});
watch(
() => props.sentence,
() => props.quote,
(val) => {
state.inputText = '';
if (inputAreaRef.value) {
inputAreaRef.value.innerHTML = '';
inputAreaRef.value.focus();
}
state.sentenceArr = val.split('').map((item, index) => {
state.quoteArr = val.split('').map((item, index) => {
return {
id: index,
word: item,
Expand All @@ -53,7 +58,7 @@ watch(
() => state.inputText,
(newVal) => {
const inputTextArr = newVal.split('');
state.sentenceArr.forEach((item, index) => {
state.quoteArr.forEach((item, index) => {
item.isInput = false;
item.isWrong = false;
if (inputTextArr[index]) {
Expand All @@ -65,13 +70,19 @@ watch(
}
}
});
console.log('----------', 'state.sentenceArr', state.sentenceArr, '----------cyy log');
}
);
function handlerInput(text: string) {
console.log('----------', 'text', text, '----------cyy log');
state.inputText = text;
const currentHeight = inputAreaRef.value?.offsetHeight;
console.log(currentHeight);
if (currentHeight && currentHeight > Math.max(state.currentAreaHeight, LINE_HEIGHT * 2)) {
y.value += LINE_HEIGHT;
} else if (currentHeight && currentHeight < Math.min(state.currentAreaHeight, LINE_HEIGHT * 2)) {
y.value -= LINE_HEIGHT;
}
state.currentAreaHeight = currentHeight || 70;
}
function pasteEvent(e: ClipboardEvent) {
Expand Down Expand Up @@ -105,10 +116,10 @@ function compositionEndEvent(e: CompositionEvent) {
</script>

<template>
<div class="y-word-input">
<div class="y-word-input__sentence">
<div class="y-word-input" ref="el">
<div class="y-word-input__quote">
<span
v-for="item in state.sentenceArr"
v-for="item in state.quoteArr"
:class="[item.isWrong ? 'is-wrong' : '', item.isInput ? 'is-input' : '']"
:key="item.id"
>{{ item.word }}</span
Expand All @@ -132,8 +143,10 @@ function compositionEndEvent(e: CompositionEvent) {
.y-word-input {
position: relative;
user-select: none;
height: 280px;
overflow: hidden;
}
.y-word-input__sentence {
.y-word-input__quote {
line-height: 70px;
user-select: none;
color: $gray-04;
Expand Down
2 changes: 1 addition & 1 deletion src/components/key/SingleKey.vue
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ $unit: 40px;
}
}
.y-single-key--half {
height: $unit / 2;
height: calc($unit / 2);
margin: 1px 0;
}
.y-single-key--active {
Expand Down
53 changes: 30 additions & 23 deletions src/files/Sentence.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
[
{
"title": "《背影》节选",
"author": "朱自清",
"type": "散文",
"length": "long",
"content": "我说道,“爸爸,你走吧。”他望车外看了看,说,“我买几个橘子去。你就在此地,不要走动。”我看那边月台的栅栏外有几个卖东西的等着顾客。走到那边月台,须穿过铁道,须跳下去又爬上去。父亲是一个胖子,走过去自然要费事些。我本来要去的,他不肯,只好让他去。我看见他戴着黑布小帽,穿着黑布大马褂,深青布棉袍,蹒跚地走到铁道边,慢慢探身下去,尚不大难。可是他穿过铁道,要爬上那边月台,就不容易了。他用两手攀着上面,两脚再向上缩;他肥胖的身子向左微倾,显出努力的样子。这时我看见他的背影,我的泪很快地流下来了。我赶紧拭干了泪,怕他看见,也怕别人看见。我再向外看时,他已抱了朱红的橘子望回走了。过铁道时,他先将橘子散放在地上,自己慢慢爬下,再抱起橘子走。到这边时,我赶紧去搀他。他和我走到车上,将橘子一股脑儿放在我的皮大衣上。于是扑扑衣上的泥土,心里很轻松似的,过一会说,“我走了;到那边来信!”我望着他走出去。他走了几步,回过头看见我,说,“进去吧,里边没人。”等他的背影混入来来往往的人里,再找不着了,我便进来坐下,我的眼泪又来了。"
},
{
"title": "《我与地坛》节选",
"author": "史铁生",
"type": "散文",
"length": "long",
"content": "如果以一天中的时间来对应四季,当然春天是早晨,夏天是中午,秋天是黄昏,冬天是夜晚。如果以乐器来对应四季,我想春天应该是小号,夏天是定音鼓,秋天是大提琴,冬天是圆号和长笛。要是以这园子里的声响来对应四季呢?那么,春天是祭坛上空漂浮着的鸽子的哨音,夏天是冗长的蝉歌和杨树叶子哗啦啦地对蝉歌的取笑,秋天是古殿檐头的风铃响,冬天是啄木鸟随意而空旷的啄木声。以园中的景物对应四季,春天是一径时而苍白时而黑润的小路,时而明朗时而阴晦的天上摇荡着串串杨花;夏天是一条条耀眼而灼人的石凳,或阴凉而爬满了青苔的石阶,阶下有果皮,阶上有半张被坐皱的报纸;秋天是一座青铜的大钟,在园子的西北角上曾丢弃着一座很大的铜钟,铜钟与这园子一般年纪,浑身挂满绿锈,文字已不清晰;冬天,是林中空地上几只羽毛蓬松的老麻雀。以心绪对应四季呢?春天是卧病的季节,否则人们不易发觉春天的残忍与渴望;夏天,情人们应该在这个季节里失恋,不然就似乎对不起爱情;秋天是从外面买一棵盆花回家的时候,把花搁在阔别了的家中,并且打开窗户把阳光也放进屋里,慢慢回忆慢慢整理一些发过霉的东西;冬天伴着火炉和书,一遍遍坚定不死的决心,写一些并不发出的信。还可以用艺术形式对应四季,这样春天就是一幅画,夏天是一部长篇小说,秋天是一首短歌或诗,冬天是一群雕塑。以梦呢?以梦对应四季呢?春天是树尖上的呼喊,夏天是呼喊中的细雨,秋天是细雨中的土地,冬天是干净的土地上的一只孤零的烟斗。"
},
{
"title": "《关雎》",
"author": "佚名",
"type": "诗歌",
"length": "medium",
"content": "关关雎鸠,在河之洲。窈窕淑女,君子好逑。参差荇菜,左右流之。窈窕淑女,寤寐求之。求之不得,寤寐思服。悠哉悠哉,辗转反侧。参差荇菜,左右采之。窈窕淑女,琴瑟友之。参差荇菜,左右芼之。窈窕淑女,钟鼓乐之。"
}
]
{
"long": [
{
"title": "《背影》节选",
"author": "朱自清",
"type": "散文",
"length": "long",
"content": "我说道,“爸爸,你走吧。”他望车外看了看,说,“我买几个橘子去。你就在此地,不要走动。”我看那边月台的栅栏外有几个卖东西的等着顾客。走到那边月台,须穿过铁道,须跳下去又爬上去。父亲是一个胖子,走过去自然要费事些。我本来要去的,他不肯,只好让他去。我看见他戴着黑布小帽,穿着黑布大马褂,深青布棉袍,蹒跚地走到铁道边,慢慢探身下去,尚不大难。可是他穿过铁道,要爬上那边月台,就不容易了。他用两手攀着上面,两脚再向上缩;他肥胖的身子向左微倾,显出努力的样子。这时我看见他的背影,我的泪很快地流下来了。我赶紧拭干了泪,怕他看见,也怕别人看见。我再向外看时,他已抱了朱红的橘子望回走了。过铁道时,他先将橘子散放在地上,自己慢慢爬下,再抱起橘子走。到这边时,我赶紧去搀他。他和我走到车上,将橘子一股脑儿放在我的皮大衣上。于是扑扑衣上的泥土,心里很轻松似的,过一会说,“我走了;到那边来信!”我望着他走出去。他走了几步,回过头看见我,说,“进去吧,里边没人。”等他的背影混入来来往往的人里,再找不着了,我便进来坐下,我的眼泪又来了。"
},
{
"title": "《我与地坛》节选",
"author": "史铁生",
"type": "散文",
"length": "long",
"content": "如果以一天中的时间来对应四季,当然春天是早晨,夏天是中午,秋天是黄昏,冬天是夜晚。如果以乐器来对应四季,我想春天应该是小号,夏天是定音鼓,秋天是大提琴,冬天是圆号和长笛。要是以这园子里的声响来对应四季呢?那么,春天是祭坛上空漂浮着的鸽子的哨音,夏天是冗长的蝉歌和杨树叶子哗啦啦地对蝉歌的取笑,秋天是古殿檐头的风铃响,冬天是啄木鸟随意而空旷的啄木声。以园中的景物对应四季,春天是一径时而苍白时而黑润的小路,时而明朗时而阴晦的天上摇荡着串串杨花;夏天是一条条耀眼而灼人的石凳,或阴凉而爬满了青苔的石阶,阶下有果皮,阶上有半张被坐皱的报纸;秋天是一座青铜的大钟,在园子的西北角上曾丢弃着一座很大的铜钟,铜钟与这园子一般年纪,浑身挂满绿锈,文字已不清晰;冬天,是林中空地上几只羽毛蓬松的老麻雀。以心绪对应四季呢?春天是卧病的季节,否则人们不易发觉春天的残忍与渴望;夏天,情人们应该在这个季节里失恋,不然就似乎对不起爱情;秋天是从外面买一棵盆花回家的时候,把花搁在阔别了的家中,并且打开窗户把阳光也放进屋里,慢慢回忆慢慢整理一些发过霉的东西;冬天伴着火炉和书,一遍遍坚定不死的决心,写一些并不发出的信。还可以用艺术形式对应四季,这样春天就是一幅画,夏天是一部长篇小说,秋天是一首短歌或诗,冬天是一群雕塑。以梦呢?以梦对应四季呢?春天是树尖上的呼喊,夏天是呼喊中的细雨,秋天是细雨中的土地,冬天是干净的土地上的一只孤零的烟斗。"
}
],
"medium": [

],
"short": [
{
"title": "《关雎》",
"author": "佚名",
"type": "诗歌",
"length": "medium",
"content": "关关雎鸠,在河之洲。窈窕淑女,君子好逑。参差荇菜,左右流之。窈窕淑女,寤寐求之。求之不得,寤寐思服。悠哉悠哉,辗转反侧。参差荇菜,左右采之。窈窕淑女,琴瑟友之。参差荇菜,左右芼之。窈窕淑女,钟鼓乐之。"
}
]
}
70 changes: 66 additions & 4 deletions src/view/QuoteLimit.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,75 @@
<script setup lang="ts">
import YPinyin from '@/components/Pinyin.vue';
import { useConfigStore } from '@/store/config';
import { reactive } from 'vue';
import flatten from 'lodash/flatten';

Check failure on line 3 in src/view/QuoteLimit.vue

View workflow job for this annotation

GitHub Actions / build

Could not find a declaration file for module 'lodash/flatten'. '/home/runner/work/typing/typing/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/flatten.js' implicitly has an 'any' type.
// components
import WordInput from '@/components/WordInput.vue';
// stores
import { storeToRefs } from 'pinia';
const useConfig = useConfigStore();
import { useConfigStore } from '@/store/config';
// files
import Sentence from '@/files/Sentence.json';
const useConfig = useConfigStore();
const { currentFont } = storeToRefs(useConfig);
const state = reactive({
quote: {} as any,
lastIndex: -1
});
state.quote = getRandomNonRepeatingElement(Object.values(Sentence));
function refresh() {
state.quote = getRandomNonRepeatingElement(Object.values(Sentence));
}
function getRandomNonRepeatingElement(arr: any[]) {
arr = flatten(arr);
if (arr.length === 0) {
return null;
}
if (arr.length === 1) {
return arr[0];
}
let randomIndex: number = state.lastIndex;
while (randomIndex === state.lastIndex) {
randomIndex = Math.floor(Math.random() * arr.length);
}
state.lastIndex = randomIndex;
return arr[randomIndex];
}
</script>
<template>
<main :class="'y-font--' + currentFont">
<y-pinyin words="句子模式"></y-pinyin>
<div class="y-quote-limit__refresh" @click="refresh">随机</div>

<WordInput :quote="state.quote?.content"></WordInput>
<div class="y-quote-limit__info">
——
<span class="y-quote-limit__info-title">
{{ state.quote?.title }}
</span>
-
<span class="y-quote-limit__info-author">
{{ state.quote?.author }}
</span>
</div>
</main>
</template>
<style lang="scss">
.y-quote-limit__refresh {
cursor: pointer;
}
.y-quote-limit__info {
margin-top: 30px;
text-align: right;
color: $gray-02;
}
</style>
Loading

0 comments on commit 08f4709

Please sign in to comment.