Skip to content

Commit

Permalink
feat: 编辑器偏好设置(可选择保存按钮点击后的行为)#35
Browse files Browse the repository at this point in the history
  • Loading branch information
Mereithhh committed Sep 6, 2022
1 parent 987a8cb commit d3907e8
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
50 changes: 50 additions & 0 deletions packages/admin/src/components/EditorProfileModal/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { ModalForm, ProFormSelect } from '@ant-design/pro-form';
import { Alert, message } from 'antd';
export default function (props: { setValue: any; value: any; trigger: any }) {
const { setValue, value, trigger } = props;
return (
<ModalForm
title="编辑器偏好设置"
trigger={trigger}
width={450}
autoFocusFirstInput
submitTimeout={3000}
initialValues={value || {}}
onFinish={async (vals) => {
setValue({ ...value, ...vals });
message.success('保存成功!');
return true;
}}
layout="horizontal"
labelCol={{ span: 6 }}
key="editForm"
>
<Alert
type="info"
message="此配置保存在浏览器存储中,切换设备需重新设置。"
style={{ marginBottom: 8 }}
></Alert>

<ProFormSelect
width="md"
required
id="afterSave"
name="afterSave"
label="保存后行为"
placeholder="请选择保存后行为,默认留在此页面"
request={async () => {
return [
{
label: '留在此页',
value: 'stay',
},
{
label: '返回之前页面',
value: 'goBack',
},
];
}}
/>
</ModalForm>
);
}
17 changes: 17 additions & 0 deletions packages/admin/src/pages/Editor/index.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Editor from '@/components/Editor';
import EditorProfileModal from '@/components/EditorProfileModal';
import PublishDraftModal from '@/components/PublishDraftModal';
import Tags from '@/components/Tags';
import UpdateModal from '@/components/UpdateModal';
Expand All @@ -13,15 +14,18 @@ import {
updateDraft,
} from '@/services/van-blog/api';
import { parseMarkdownFile, parseObjToMarkdown } from '@/services/van-blog/parseMarkdownFile';
import { useCacheState } from '@/services/van-blog/useCacheState';
import { DownOutlined } from '@ant-design/icons';
import { PageContainer } from '@ant-design/pro-layout';
import { Button, Dropdown, Input, Menu, message, Modal, Space, Tag, Upload } from 'antd';
import { useCallback, useEffect, useMemo, useState } from 'react';
import { history } from 'umi';

export default function () {
const [value, setValue] = useState('');
const [currObj, setCurrObj] = useState({});
const [loading, setLoading] = useState(true);
const [editorConfig, setEditorConfig] = useCacheState({ afterSave: 'stay' }, 'editorConfig');
const type = history.location.query?.type || 'article';
const key = useMemo(() => {
return `${type}-${history.location.query?.id || '0'}`;
Expand Down Expand Up @@ -109,6 +113,9 @@ export default function () {
message.success('保存成功!');
} else {
}
if (editorConfig.afterSave && editorConfig.afterSave == 'goBack') {
history.go(-1);
}
setLoading(false);
};

Expand Down Expand Up @@ -313,6 +320,16 @@ export default function () {
},
}
: undefined,
{
key: 'settingBtn',
label: (
<EditorProfileModal
value={editorConfig}
setValue={setEditorConfig}
trigger={<a key={'editerConfigBtn'}>偏好设置</a>}
/>
),
},
{
key: 'helpBtn',
label: '帮助文档',
Expand Down
12 changes: 12 additions & 0 deletions packages/admin/src/services/van-blog/useCacheState.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { useState } from 'react';
export const useCacheState = (init, key) => {
const k = `vanblog-admin-${key}`;
const [currValue, setCuttValue] = useState(JSON.parse(localStorage.getItem(k)) || init);
return [
currValue,
(newValue) => {
localStorage.setItem(k, JSON.stringify(newValue));
setCuttValue(newValue);
},
];
};

0 comments on commit d3907e8

Please sign in to comment.