Skip to content

Commit

Permalink
feat(time): added time compoennt close #285
Browse files Browse the repository at this point in the history
  • Loading branch information
anncwb committed Mar 12, 2021
1 parent fab7a6c commit a89eeef
Show file tree
Hide file tree
Showing 22 changed files with 313 additions and 75 deletions.
3 changes: 2 additions & 1 deletion .env.development
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ VITE_USE_MOCK = true
VITE_PUBLIC_PATH = /

# Cross-domain proxy, you can configure multiple
VITE_PROXY=[["/basic-api","http://localhost:3000"],["/upload","http://localhost:3001/upload"]]
# Please note that no line breaks
VITE_PROXY = [["/basic-api","http://localhost:3000"],["/upload","http://localhost:3001/upload"]]
# VITE_PROXY=[["/api","https://vvbin.cn/test"]]

# Delete console
Expand Down
13 changes: 3 additions & 10 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
//============= Editor ======================
//===========================================
"explorer.openEditors.visible": 0,
"editor.tabSize": 2,
"editor.renderControlCharacters": true,
"window.zoomLevel": -1,
"editor.minimap.renderCharacters": false,
"editor.minimap.maxColumn": 300,
"editor.minimap.showSlider": "always",
"editor.smoothScrolling": true,
"editor.cursorBlinking": "phase",
"editor.cursorSmoothCaretAnimation": true,
"editor.detectIndentation": false,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"diffEditor.ignoreTrimWhitespace": false,
"javascript.format.insertSpaceBeforeFunctionParenthesis": true,
"editor.formatOnPaste": true,
"editor.formatOnSave": true,
"editor.suggestSelection": "first",
"editor.trimAutoWhitespace": true,
"editor.quickSuggestions": {
Expand All @@ -39,7 +39,6 @@
"emmet.syntaxProfiles": {
"vue-html": "html",
"vue": "html",
"javascript": "javascriptreact",
"xml": {
"attr_quotes": "single"
}
Expand Down Expand Up @@ -80,7 +79,6 @@
"files.exclude": {
"**/bower_components": true,
"**/.idea": true,
"**/yarn.lock": true,
"**/tmp": true,
"**/.git": true,
"**/.svn": true,
Expand All @@ -89,7 +87,6 @@
"**/.DS_Store": true
},
"files.watcherExclude": {
// 文件监视排除
"**/.git/objects/**": true,
"**/.git/subtree-cache/**": true,
"**/.vscode/**": true,
Expand All @@ -99,10 +96,6 @@
"**/dist/**": true,
"**/yarn.lock": true
},
"files.associations": {
"*.vue": "vue",
"*.wxss": "css"
},
"stylelint.enable": true,
"stylelint.packageManager": "yarn",
// ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### ✨ Features

- 图标选择器新增 svg 模式
- 新增时间组件

### ✨ Refactor

Expand Down
5 changes: 2 additions & 3 deletions build/vite/optimizer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// TODO
import type { GetManualChunk, GetManualChunkApi } from 'rollup';
import type { GetManualChunk } from 'rollup';

//
const vendorLibs: { match: string[]; output: string }[] = [
Expand All @@ -10,8 +10,7 @@ const vendorLibs: { match: string[]; output: string }[] = [
];

// @ts-ignore
export const configManualChunk: GetManualChunk = (id: string, api: GetManualChunkApi) => {
console.log(api);
export const configManualChunk: GetManualChunk = (id: string) => {
if (/[\\/]node_modules[\\/]/.test(id)) {
const matchItem = vendorLibs.find((item) => {
const reg = new RegExp(`[\\/]node_modules[\\/]_?(${item.match.join('|')})(.*)`, 'ig');
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"vditor": "^3.8.2",
"vue": "^3.0.7",
"vue-i18n": "^9.0.0",
"vue-router": "^4.0.4",
"vue-router": "^4.0.5",
"vue-types": "^3.0.2",
"vuex": "^4.0.0",
"vuex-module-decorators": "^1.0.1",
Expand Down Expand Up @@ -118,7 +118,7 @@
"vite-plugin-style-import": "^0.8.1",
"vite-plugin-svg-icons": "^0.3.4",
"vite-plugin-theme": "^0.4.8",
"vite-plugin-windicss": "0.7.2",
"vite-plugin-windicss": "0.8.2",
"vue-eslint-parser": "^7.6.0",
"yargs": "^16.2.0"
},
Expand Down
1 change: 1 addition & 0 deletions src/components/Time/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as Time } from './src/index.vue';
109 changes: 109 additions & 0 deletions src/components/Time/src/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<template>
<span>{{ date }}</span>
</template>
<script lang="ts">
import { defineComponent, ref, watch } from 'vue';
import { useI18n } from '/@/hooks/web/useI18n';
import { useIntervalFn } from '@vueuse/core';
import { formatToDateTime, formatToDate, dateUtil } from '/@/utils/dateUtil';
import { isNumber, isObject, isString } from '/@/utils/is';
import { propTypes } from '/@/utils/propTypes';
const ONE_SECONDS = 1000;
const ONE_MINUTES = ONE_SECONDS * 60;
const ONE_HOUR = ONE_MINUTES * 60;
const ONE_DAY = ONE_HOUR * 24;
export default defineComponent({
name: 'Time',
props: {
value: propTypes.oneOfType([propTypes.number, propTypes.instanceOf(Date), propTypes.string])
.isRequired,
step: propTypes.number.def(60),
mode: propTypes.oneOf(['date', 'datetime', 'relative']).def('relative'),
},
setup(props) {
const date = ref('');
const { t } = useI18n();
useIntervalFn(setTime, props.step * ONE_SECONDS);
watch(
() => props.value,
() => {
setTime();
},
{ immediate: true }
);
function getTime() {
const { value } = props;
let time: number = 0;
if (isNumber(value)) {
const timestamp = value.toString().length > 10 ? value : value * 1000;
time = new Date(timestamp).getTime();
} else if (isString(value)) {
time = new Date(value).getTime();
} else if (isObject(value)) {
time = value.getTime();
}
return time;
}
function setTime() {
const { mode, value } = props;
const time = getTime();
if (mode === 'relative') {
date.value = getRelativeTime(time);
} else {
if (mode === 'datetime') {
date.value = formatToDateTime(value);
} else if (mode === 'date') {
date.value = formatToDate(value);
}
}
}
function getRelativeTime(timeStamp: number) {
const currentTime = new Date().getTime();
// Determine whether the incoming timestamp is earlier than the current timestamp
const isBefore = dateUtil(timeStamp).isBefore(currentTime);
let diff = currentTime - timeStamp;
if (!isBefore) {
diff = -diff;
}
let resStr = '';
let dirStr = isBefore ? t('component.time.before') : t('component.time.after');
if (diff < ONE_SECONDS) {
resStr = t('component.time.just');
// Less than or equal to 59 seconds
} else if (diff < ONE_MINUTES) {
resStr = parseInt(diff / ONE_SECONDS) + t('component.time.seconds') + dirStr;
// More than 59 seconds, less than or equal to 59 minutes and 59 seconds
} else if (diff >= ONE_MINUTES && diff < ONE_HOUR) {
resStr = Math.floor(diff / ONE_MINUTES) + t('component.time.minutes') + dirStr;
// More than 59 minutes and 59 seconds, less than or equal to 23 hours, 59 minutes and 59 seconds
} else if (diff >= ONE_HOUR && diff < ONE_DAY) {
resStr = Math.floor(diff / ONE_HOUR) + t('component.time.hours') + dirStr;
// More than 23 hours, 59 minutes and 59 seconds, less than or equal to 29 days, 59 minutes and 59 seconds
} else if (diff >= ONE_DAY && diff < 2623860000) {
resStr = Math.floor(diff / ONE_DAY) + t('component.time.days') + dirStr;
// More than 29 days, 59 minutes, 59 seconds, less than 364 days, 23 hours, 59 minutes, 59 seconds, and the incoming timestamp is earlier than the current
} else if (diff >= 2623860000 && diff <= 31567860000 && isBefore) {
resStr = dateUtil(timeStamp).format('MM-DD-HH-mm');
} else {
resStr = dateUtil(timeStamp).format('YYYY');
}
return resStr;
}
return { date };
},
});
</script>
29 changes: 16 additions & 13 deletions src/hooks/core/useContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
inject,
reactive,
readonly as defineReadonly,
defineComponent,
// defineComponent,
UnwrapRef,
} from 'vue';

Expand All @@ -29,18 +29,21 @@ export function createContext<T>(
const provideData = readonly ? defineReadonly(state) : state;
!createProvider && provide(key, native ? context : provideData);

const Provider = createProvider
? defineComponent({
name: 'Provider',
inheritAttrs: false,
setup(_, { slots }) {
provide(key, provideData);
return () => slots.default?.();
},
})
: null;

return { Provider, state };
// const Provider = createProvider
// ? defineComponent({
// name: 'Provider',
// inheritAttrs: false,
// setup(_, { slots }) {
// provide(key, provideData);
// return () => slots.default?.();
// },
// })
// : null;

return {
// Provider,
state,
};
}

export function useContext<T>(key: InjectionKey<T>, native?: boolean): T;
Expand Down
17 changes: 4 additions & 13 deletions src/layouts/default/content/index.vue
Original file line number Diff line number Diff line change
@@ -1,30 +1,21 @@
<template>
<div :class="[prefixCls, getLayoutContentMode]">
<transition name="fade">
<Loading
v-if="getOpenPageLoading"
:loading="getPageLoading"
background="rgba(240, 242, 245, 0.6)"
absolute
:class="`${prefixCls}-loading`"
/>
</transition>
<div :class="[prefixCls, getLayoutContentMode]" v-loading="getOpenPageLoading && getPageLoading">
<PageLayout />
</div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
import PageLayout from '/@/layouts/page/index.vue';
import { useDesign } from '/@/hooks/web/useDesign';
import { useRootSetting } from '/@/hooks/setting/useRootSetting';
import { useTransitionSetting } from '/@/hooks/setting/useTransitionSetting';
import PageLayout from '/@/layouts/page/index.vue';
import { useContentViewHeight } from './useContentViewHeight';
import { Loading } from '/@/components/Loading';
export default defineComponent({
name: 'LayoutContent',
components: { PageLayout, Loading },
components: { PageLayout },
setup() {
const { prefixCls } = useDesign('layout-content');
const { getOpenPageLoading } = useTransitionSetting();
Expand Down
2 changes: 2 additions & 0 deletions src/layouts/default/footer/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
<Footer :class="prefixCls" v-if="getShowLayoutFooter">
<div :class="`${prefixCls}__links`">
<a @click="openWindow(SITE_URL)">{{ t('layout.footer.onlinePreview') }}</a>

<GithubFilled @click="openWindow(GITHUB_URL)" :class="`${prefixCls}__github`" />

<a @click="openWindow(DOC_URL)">{{ t('layout.footer.onlineDocument') }}</a>
</div>
<div>Copyright &copy;2020 Vben Admin</div>
Expand Down
15 changes: 7 additions & 8 deletions src/layouts/default/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@
<Layout :class="prefixCls">
<LayoutFeatures />
<LayoutHeader fixed v-if="getShowFullHeaderRef" />
<Layout
:class="{
'ant-layout-has-sider': getIsMixSidebar,
}"
>
<Layout :class="layoutClass">
<LayoutSideBar v-if="getShowSidebar || getIsMobile" />
<Layout :class="`${prefixCls}__main`">
<Layout :class="`${prefixCls}-main`">
<LayoutMultipleHeader />
<LayoutContent />
<LayoutFooter />
Expand All @@ -18,7 +14,7 @@
</template>

<script lang="ts">
import { defineComponent } from 'vue';
import { defineComponent, computed, unref } from 'vue';
import { Layout } from 'ant-design-vue';
import { createAsyncComponent } from '/@/utils/factory/createAsyncComponent';
Expand Down Expand Up @@ -53,12 +49,15 @@
const { getShowSidebar, getIsMixSidebar } = useMenuSetting();
const layoutClass = computed(() => ({ 'ant-layout-has-sider': unref(getIsMixSidebar) }));
return {
getShowFullHeaderRef,
getShowSidebar,
prefixCls,
getIsMobile,
getIsMixSidebar,
layoutClass,
};
},
});
Expand All @@ -77,7 +76,7 @@
min-height: 100%;
}
&__main {
&-main {
margin-left: 1px;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/layouts/default/trigger/HeaderTrigger.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import { propTypes } from '/@/utils/propTypes';
export default defineComponent({
name: 'SiderTrigger',
name: 'HeaderTrigger',
components: { MenuUnfoldOutlined, MenuFoldOutlined },
props: {
theme: propTypes.oneOf(['light', 'dark']),
Expand Down
9 changes: 9 additions & 0 deletions src/locales/lang/en/component/time.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default {
before: ' ago',
after: ' after',
just: 'just now',
seconds: ' seconds',
minutes: ' minutes',
hours: ' hours',
days: ' days',
};
2 changes: 2 additions & 0 deletions src/locales/lang/en/routes/demo/comp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ export default {
upload: 'Upload',

loading: 'Loading',

time: 'Time',
};
9 changes: 9 additions & 0 deletions src/locales/lang/zh_CN/component/time.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default {
before: '前',
after: '后',
just: '刚刚',
seconds: '秒',
minutes: '分钟',
hours: '小时',
days: '天',
};
Loading

0 comments on commit a89eeef

Please sign in to comment.