Skip to content

Commit

Permalink
将模块显示缓存到本地
Browse files Browse the repository at this point in the history
  • Loading branch information
name-jerry committed Apr 10, 2023
1 parent 8a8066a commit 64c86e5
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 28 deletions.
14 changes: 9 additions & 5 deletions components/MyTextarea/MyTextarea.vue
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,27 @@
e.preventDefault();
keydownFns.get(c)(e, area)
}
/**keys第二个值有值时在选区两侧插入keys数组中得两个字符串,第二个值无值时将替代选区内容*/
function doubleKey(e : KeyboardEvent, area : HTMLTextAreaElement, keys : string[]) {
let start = area.selectionStart;
let end = area.selectionEnd;
let text = area.value
let str1 = text.slice(0, start)
let str2 = text.slice(start, end)
let str3 = text.slice(end)
let position;
let position : number;
if (keys[1]) {
area.value = str1 + keys[0] + str2 + keys[1] + str3
text = str1 + keys[0] + str2 + keys[1] + str3
position = end + 2;
} else {
area.value = str1 + keys[0] + str3
text = str1 + keys[0] + str3
position = start + 1
}
area.selectionEnd = position
// 不设置异步会出现继续键入原值得问题
setTimeout(() => {
area.value = text
area.selectionEnd = position
})
}
/**利用div自动增高的特点,为其赋值,areatext通过css设置成同div高度*/
function autoHeight() {
Expand Down
4 changes: 2 additions & 2 deletions components/ShowMD/ShowMD.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
(e : 'update', mdText : string) : void,
(e : 'updateCoexist', isCoexist : boolean) : void
}>()
let isCoexist = ref<boolean>(false)
let isCoexist = ref<boolean>(true)
/**主体的ref对象*/
let markdownBody = ref()
/**md文档的html格式*/
Expand Down Expand Up @@ -598,7 +598,7 @@
pre {
padding: $fontSize-default;
overflow: auto;
font-size: .85em;
font-size: 1em;
line-height: 1.45;
background-color: $color-bg-pre;
border-radius: $borderRadius-default;
Expand Down
1 change: 1 addition & 0 deletions components/Tag/Tag.vue
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
uni.showModal({
editable: true,
title: "请输入名字",
content: prop.tag.title,
success: async (e) => {
if (e.confirm) {
let a : Tag = { ...prop.tag, title: e.content };
Expand Down
12 changes: 8 additions & 4 deletions pages/Article/Article.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<button class="updateBtn" v-show="!isCoexist" @tap="switchView">{{showMd?'预览':'编辑'}}</button>
<button class="saveBtn " @tap="save"
v-show="showSave||showDownload">{{showSave?'保存':showDownload?'下载':'保存'}}</button>
<button class="recoverBtn " @tap="recover" v-show="showSave&&!showMd">恢复</button>
<button class="recoverBtn " @tap="recover" v-show="showSave">恢复</button>

<picker v-if='main.artList[0]' class="pickerBtn" mode="selector" :range="main.artList" range-key="title"
:value='currentArtIndex' @change="currentArtIndex=$event.detail.value">
Expand Down Expand Up @@ -42,7 +42,7 @@
const currentArt = computed<Article>(() => main.artList[currentArtIndex.value]);
/**切换视图*/
const showMd = ref<Boolean>(false);
const isCoexist = ref<boolean>(false);
const isCoexist = ref<boolean>(true);
const showSave = ref<Boolean>(false);
const showDownload = computed<boolean>(() => main.artList[currentArtIndex.value].updateCount);
const keydownFns = new Map()
Expand Down Expand Up @@ -126,8 +126,12 @@
async function updateMdTextByIndex() {
let { title, content } = main.artList[currentArtIndex.value]
let a = history.state.current.toString().replace(/=.+$/, '=' + title)
history.replaceState(history.state, null, a);
// 获取url的检索字段
const param = new URLSearchParams(location.search)
// 设置title字段
param.set('title', title)
// 加入一条历史记录,第一个参数对象可通过新记录的history.state访问到,第二个暂时无用,第三个是用于确定新的url,?开头则只替代原来的query值,非问号开头,相对路径时(非https开头),会替代最后最后一个路径值,绝对路径时,取代全部值
history.pushState({ index: title }, null, "?" + param);
mdText.value = content
}
function print() {
Expand Down
32 changes: 20 additions & 12 deletions pages/home/home.vue
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
<script setup lang="ts">
import { reactive, onMounted, watch } from "vue";
import { reactive, onMounted, watch, watchEffect, ref } from "vue";
import { onLoad } from '@dcloudio/uni-app';
import type { Option } from '@/type';
import useMainStore from "@/stores/useMainStore"
let main = useMainStore();
const moduleShowKey = 'moduleShowKey'
// 登录判断
// 跳转时带的参数
type Query = {
needLogin ?: Boolean
uniIdRedirectUrl ?: String
}
let query : Query;
// 插入模块
let show = reactive({
let show = ref<{ [key : string] : { value : boolean, lable : string } }>({
myMemo: { value: true, lable: "计划表" },
filePicker: { value: false, lable: "上传文章" },
filePicker: { value: true, lable: "上传文章" },
login: { value: false, lable: "登录" },
swipe: { value: true, lable: "轮播图" },
swipe: { value: false, lable: "轮播图" },
set: { value: false, lable: "设置" },
})
function initShowByStorage() {
// 登录状态时
let l = uni.getStorageSync(moduleShowKey);
// 本地有值时
if (l?.set) return show.value = l;
}
// 设置
let setOptions = reactive<Option[]>([])
function swith() {
Expand All @@ -29,15 +35,15 @@
})
}
function initSet() {
for (let [k, v] of Object.entries(show)) {
for (let [k, v] of Object.entries(show.value)) {
if (k == 'set') continue;
setOptions.push({ key: k, title: v.lable })
}
setOptions.push({ key: '', title: "退出" })
}
function setSelect(option : Option) : void {
show.set.value = false;
show.value.set.value = false;
let k = option.key
if (!k) {
main.isLogin = false;
Expand All @@ -47,9 +53,9 @@
title: '请先登录',
icon: 'error'
});
show.login.value = true
show.value.login.value = true
} else {
show[k].value = true;
show.value[k].value = true;
}
}
Expand All @@ -58,17 +64,19 @@
q as Query;
if (q!.needLogin) {
query = q!;
show.login.value = true
show.value.login.value = true
}
})
// =========挂载卸载============
onMounted(() => {
initShowByStorage()
initSet()
watchEffect(() => uni.setStorageSync(moduleShowKey, show.value))
// 防刷新
watch(() => main.isLogin, async () => {
const l = main.isLogin
show.login.value = !l;
show.value.login.value = !l;
if (l && query && query.uniIdRedirectUrl) {
setTimeout(() => uni.showToast({
title: '返回之前访问页面',
Expand Down Expand Up @@ -183,7 +191,7 @@
box-sizing: border-box;
width: 80%;
max-width: 500px;
z-index: 2;
z-index: 4;
}
.set {
Expand Down
2 changes: 1 addition & 1 deletion stores/useMainStore.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineStore } from 'pinia';
import { ref, watch, } from "vue";
import { ref } from "vue";
import { Article } from "@/type"
import { checkLogin } from "@/utils/checkLogin"

Expand Down
2 changes: 1 addition & 1 deletion type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type Option = {
key ?: string,
fontIcon ?: string,
title : string
} | string;
};

interface ClientInfo {
clientLeft : number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
}
},
"web": {
"tokenExpiresIn": 7200,
"tokenExpiresIn": 86400,
"tokenExpiresThreshold": 3600,
"oauth": {
"weixin-h5": {
Expand Down Expand Up @@ -98,4 +98,4 @@
"apiSecret": ""
}
}
}
}
2 changes: 1 addition & 1 deletion unpackage/dist/build/h5/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<title>MySite</title>
<!--preload-links-->
<!--app-context-->
<script type="module" crossorigin src="/assets/index-32b37a27.js"></script>
<script type="module" crossorigin src="/assets/index-cef708af.js"></script>
<link rel="stylesheet" href="/assets/index-f8fadadc.css">
</head>
<body>
Expand Down

0 comments on commit 64c86e5

Please sign in to comment.