改进 getSearch 函数实现 (#7089)

* 🎨 改进 `getSearch` 方法

兼容 key 为空字符串与 value 为空字符串的 URL search params

* 🎨 改进 `getSearch` 方法

* 🎨 style
This commit is contained in:
颖逸 2023-01-16 23:54:47 +08:00 committed by GitHub
parent 4f2bca7e05
commit 67743b50ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 9 additions and 19 deletions

View File

@ -300,9 +300,9 @@ export const JSONToLayout = (isStart: boolean) => {
} }
// https://github.com/siyuan-note/siyuan/pull/7086 // https://github.com/siyuan-note/siyuan/pull/7086
const openId = getSearch("id", window.location.href); const openId = getSearch("id", window.location.href);
if (openId) { if (openId !== null) {
openFileById({ openFileById({
id: getSearch("id", window.location.href), id: openId,
action: [Constants.CB_GET_FOCUS, Constants.CB_GET_CONTEXT], action: [Constants.CB_GET_FOCUS, Constants.CB_GET_CONTEXT],
zoomIn: getSearch("focus", window.location.href) === "1" zoomIn: getSearch("focus", window.location.href) === "1"
}); });

View File

@ -129,8 +129,8 @@ export const initFramework = () => {
initEditorName(); initEditorName();
if (getOpenNotebookCount() > 0) { if (getOpenNotebookCount() > 0) {
const openId = getSearch("id", window.location.href); const openId = getSearch("id", window.location.href);
if (openId) { if (openId !== null) {
openMobileFileById(getSearch("id", window.location.href), openMobileFileById(openId,
getSearch("focus", window.location.href) === "1" ? [Constants.CB_GET_ALL, Constants.CB_GET_FOCUS] : [Constants.CB_GET_FOCUS, Constants.CB_GET_CONTEXT]); getSearch("focus", window.location.href) === "1" ? [Constants.CB_GET_ALL, Constants.CB_GET_FOCUS] : [Constants.CB_GET_FOCUS, Constants.CB_GET_CONTEXT]);
} else { } else {
const localDoc = window.siyuan.storage[Constants.LOCAL_DOCINFO]; const localDoc = window.siyuan.storage[Constants.LOCAL_DOCINFO];

View File

@ -1578,7 +1578,7 @@ export class WYSIWYG {
openBy(linkAddress, "app"); openBy(linkAddress, "app");
} else { } else {
const page = getSearch("page", linkAddress); const page = getSearch("page", linkAddress);
openAsset(linkPathname, page === "" ? undefined : parseInt(page), "right"); openAsset(linkPathname, page === null ? undefined : parseInt(page), "right");
} }
} else { } else {
/// #if !BROWSER /// #if !BROWSER

View File

@ -14,20 +14,10 @@ export const getRandom = (min: number, max: number) => {
return Math.floor(Math.random() * (max - min + 1)) + min; //含最大值,含最小值 return Math.floor(Math.random() * (max - min + 1)) + min; //含最大值,含最小值
}; };
export const getSearch = (key: string, link = window.location.search) => { export const getSearch: (key: string, link?: string) => string | null = (key: string, link = window.location.search) => {
if (link.indexOf("?") === -1) { // REF https://developer.mozilla.org/zh-CN/docs/Web/API/URLSearchParams
return ""; const urlSearchParams = new URLSearchParams(link);
} return urlSearchParams.get(key);
let value = "";
const data = link.split("?")[1].split("&");
data.find(item => {
const keyValue = item.split("=");
if (keyValue[0] === key) {
value = keyValue[1];
return true;
}
});
return value;
}; };
export const isBrowser = () => { export const isBrowser = () => {