Skip to content

Commit

Permalink
feat: config 增加 template
Browse files Browse the repository at this point in the history
  • Loading branch information
visiky committed Sep 29, 2022
1 parent 820107a commit aad0337
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 9 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"cross-fetch": "^3.1.4",
"gatsby-plugin-google-gtag": "^4.23.0",
"lodash-es": "^4.17.21",
"query-string": "^7.1.1",
"react": "^17.0.1",
"react-color": "^2.19.3",
"react-dnd": "^14.0.2",
Expand Down
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

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

52 changes: 43 additions & 9 deletions src/components/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useCallback, useState, useEffect, useRef } from 'react';
import { Button, Affix, Upload, Spin, message, Alert, Modal } from 'antd';
import type { RcFile } from 'antd/lib/upload';
import _ from 'lodash-es';
import qs from 'query-string';
import { getLanguage, getLocale } from '@/locale';
import { useModeSwitcher } from '@/hooks/useModeSwitcher';
import { getDefaultTitleNameMap } from '@/datas/constant';
Expand All @@ -28,24 +29,50 @@ export const Page: React.FC = () => {
const query = getSearchObj();
const [config, setConfig] = useState<ResumeConfig>();
const [loading, updateLoading] = useState<boolean>(true);
const [template, updateTemplate] = useState<string>('template1');
const [theme, setTheme] = useState<ThemeConfig>({
color: '#2f5785',
tagColor: '#8bc34a',
});

useEffect(() => {
const {
pathname,
hash: currentHash,
search: currentSearch,
} = window.location;
const hash = currentHash === '#/' ? '' : currentHash;
const searchObj = qs.parse(currentSearch);
if (!searchObj.template) {
const search = qs.stringify({
template: config?.template || 'template1',
...qs.parse(currentSearch),
});

window.location.href = `${pathname}?${search}${hash}`;
}
}, [config]);

const updateTemplate = (value: string) => {
const {
pathname,
hash: currentHash,
search: currentSearch,
} = window.location;
const hash = currentHash === '#/' ? '' : currentHash;
const search = qs.stringify({
...qs.parse(currentSearch),
template: value,
});

window.location.href = `${pathname}?${search}${hash}`;
};

const changeConfig = (v: Partial<ResumeConfig>) => {
setConfig(
_.assign({}, { titleNameMap: getDefaultTitleNameMap({ i18n }) }, v)
);
};

useEffect(() => {
if (query.template) {
updateTemplate(query.template as string);
}
}, []);

useEffect(() => {
const user = (query.user || '') as string;
const branch = (query.branch || 'master') as string;
Expand Down Expand Up @@ -215,7 +242,11 @@ export const Page: React.FC = () => {
)}
<div className="page">
{config && (
<Resume value={config} theme={theme} template={template} />
<Resume
value={config}
theme={theme}
template={query.template || 'template1'}
/>
)}
{mode === 'edit' && (
<React.Fragment>
Expand All @@ -226,7 +257,7 @@ export const Page: React.FC = () => {
onValueChange={onConfigChange}
theme={theme}
onThemeChange={onThemeChange}
template={template}
template={query.template || 'template1'}
onTemplateChange={updateTemplate}
/>
<Button type="primary" onClick={copyConfig}>
Expand All @@ -244,6 +275,9 @@ export const Page: React.FC = () => {
{i18n.get('导入配置')}
</Button>
</Upload>
<Button type="primary" onClick={() => window.print()}>
{i18n.get('PDF 下载')}
</Button>
</Button.Group>
</Affix>
<div
Expand Down
2 changes: 2 additions & 0 deletions src/components/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ export type ResumeConfig = {
locales?: {
[key: string]: ResumeConfig;
};

template?: string;
};

/**
Expand Down

0 comments on commit aad0337

Please sign in to comment.