refactor: use setup refactor

This commit is contained in:
xiaoxian521
2021-09-18 22:24:52 +08:00
parent 67db3cb1c3
commit 85f4917f26
18 changed files with 1139 additions and 1214 deletions

View File

@@ -1,3 +1,109 @@
<script setup lang="ts">
import { ref, unref, nextTick, onUnmounted } from "vue";
import { templateRef } from "@vueuse/core";
import flippers from "./Filpper";
let timer = ref(null);
let flipObjs = ref([]);
const flipperHour1 = templateRef<HTMLElement | null>("flipperHour1", null);
const flipperHour2 = templateRef<HTMLElement | null>("flipperHour2", null);
const flipperMinute1 = templateRef<HTMLElement | null>("flipperMinute1", null);
const flipperMinute2 = templateRef<HTMLElement | null>("flipperMinute2", null);
const flipperSecond1 = templateRef<HTMLElement | null>("flipperSecond1", null);
const flipperSecond2 = templateRef<HTMLElement | null>("flipperSecond2", null);
// 初始化数字
const init = () => {
let now = new Date();
let nowTimeStr = formatDate(new Date(now.getTime()), "hhiiss");
for (let i = 0; i < flipObjs.value.length; i++) {
flipObjs?.value[i]?.setFront(nowTimeStr[i]);
}
};
// 开始计时
const run = () => {
timer.value = setInterval(() => {
// 获取当前时间
let now = new Date();
let nowTimeStr = formatDate(new Date(now.getTime() - 1000), "hhiiss");
let nextTimeStr = formatDate(now, "hhiiss");
for (let i = 0; i < flipObjs.value.length; i++) {
if (nowTimeStr[i] === nextTimeStr[i]) {
continue;
}
flipObjs?.value[i]?.flipDown(nowTimeStr[i], nextTimeStr[i]);
}
}, 1000);
};
// 正则格式化日期
const formatDate = (date: Date, dateFormat: string) => {
/* 单独格式化年份根据y的字符数量输出年份
* 例如yyyy => 2019
yy => 19
y => 9
*/
if (/(y+)/.test(dateFormat)) {
dateFormat = dateFormat.replace(
RegExp.$1,
(date.getFullYear() + "").substr(4 - RegExp.$1.length)
);
}
// 格式化月、日、时、分、秒
let o = {
"m+": date.getMonth() + 1,
"d+": date.getDate(),
"h+": date.getHours(),
"i+": date.getMinutes(),
"s+": date.getSeconds()
};
for (let k in o) {
if (new RegExp(`(${k})`).test(dateFormat)) {
// 取出对应的值
let str = o[k] + "";
/* 根据设置的格式,输出对应的字符
* 例如: 早上8时hh => 08h => 8
* 但是,当数字>=10时无论格式为一位还是多位不做截取这是与年份格式化不一致的地方
* 例如: 下午15时hh => 15, h => 15
*/
dateFormat = dateFormat.replace(
RegExp.$1,
RegExp.$1.length === 1 ? str : padLeftZero(str)
);
}
}
return dateFormat;
};
// 日期时间补零
const padLeftZero = (str: string | any[]) => {
return ("00" + str).substr(str.length);
};
nextTick(() => {
flipObjs.value = [
unref(flipperHour1),
unref(flipperHour2),
unref(flipperMinute1),
unref(flipperMinute2),
unref(flipperSecond1),
unref(flipperSecond2)
];
init();
run();
});
onUnmounted(() => {
if (timer.value) {
clearInterval(timer.value);
timer.value = null;
}
});
</script>
<template>
<div class="flip-clock">
<flippers ref="flipperHour1" />
@@ -11,147 +117,6 @@
</div>
</template>
<script lang="ts">
import { ref, unref, nextTick, onUnmounted } from "vue";
import flippers from "./Filpper";
import { templateRef } from "@vueuse/core";
export default {
name: "Flop",
components: {
flippers
},
setup() {
let timer = ref(null);
let flipObjs = ref([]);
const flipperHour1 = templateRef<HTMLElement | null>("flipperHour1", null);
const flipperHour2 = templateRef<HTMLElement | null>("flipperHour2", null);
const flipperMinute1 = templateRef<HTMLElement | null>(
"flipperMinute1",
null
);
const flipperMinute2 = templateRef<HTMLElement | null>(
"flipperMinute2",
null
);
const flipperSecond1 = templateRef<HTMLElement | null>(
"flipperSecond1",
null
);
const flipperSecond2 = templateRef<HTMLElement | null>(
"flipperSecond2",
null
);
// 初始化数字
const init = () => {
let now = new Date();
let nowTimeStr = formatDate(new Date(now.getTime()), "hhiiss");
for (let i = 0; i < flipObjs.value.length; i++) {
flipObjs?.value[i]?.setFront(nowTimeStr[i]);
}
};
// 开始计时
const run = () => {
timer.value = setInterval(() => {
// 获取当前时间
let now = new Date();
let nowTimeStr = formatDate(new Date(now.getTime() - 1000), "hhiiss");
let nextTimeStr = formatDate(now, "hhiiss");
for (let i = 0; i < flipObjs.value.length; i++) {
if (nowTimeStr[i] === nextTimeStr[i]) {
continue;
}
flipObjs?.value[i]?.flipDown(nowTimeStr[i], nextTimeStr[i]);
}
}, 1000);
};
// 正则格式化日期
const formatDate = (date: Date, dateFormat: string) => {
/* 单独格式化年份根据y的字符数量输出年份
* 例如yyyy => 2019
yy => 19
y => 9
*/
if (/(y+)/.test(dateFormat)) {
dateFormat = dateFormat.replace(
RegExp.$1,
(date.getFullYear() + "").substr(4 - RegExp.$1.length)
);
}
// 格式化月、日、时、分、秒
let o = {
"m+": date.getMonth() + 1,
"d+": date.getDate(),
"h+": date.getHours(),
"i+": date.getMinutes(),
"s+": date.getSeconds()
};
for (let k in o) {
if (new RegExp(`(${k})`).test(dateFormat)) {
// 取出对应的值
let str = o[k] + "";
/* 根据设置的格式,输出对应的字符
* 例如: 早上8时hh => 08h => 8
* 但是,当数字>=10时无论格式为一位还是多位不做截取这是与年份格式化不一致的地方
* 例如: 下午15时hh => 15, h => 15
*/
dateFormat = dateFormat.replace(
RegExp.$1,
RegExp.$1.length === 1 ? str : padLeftZero(str)
);
}
}
return dateFormat;
};
// 日期时间补零
const padLeftZero = (str: string | any[]) => {
return ("00" + str).substr(str.length);
};
nextTick(() => {
flipObjs.value = [
unref(flipperHour1),
unref(flipperHour2),
unref(flipperMinute1),
unref(flipperMinute2),
unref(flipperSecond1),
unref(flipperSecond2)
];
init();
run();
});
onUnmounted(() => {
if (timer.value) {
clearInterval(timer.value);
timer.value = null;
}
});
return {
timer,
flipObjs,
init,
run,
formatDate,
padLeftZero,
flipperHour1,
flipperHour2,
flipperMinute1,
flipperMinute2,
flipperSecond1,
flipperSecond2
};
}
};
</script>
<style>
.flip-clock .m-flipper {
margin: 0 3px;