From 67743b50ba1db454b6547e986959b1636a650fa2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A2=96=E9=80=B8?= <49649786+Zuoqiu-Yingyi@users.noreply.github.com> Date: Mon, 16 Jan 2023 23:54:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=20`getSearch`=20=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E5=AE=9E=E7=8E=B0=20(#7089)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * :art: 改进 `getSearch` 方法 兼容 key 为空字符串与 value 为空字符串的 URL search params * :art: 改进 `getSearch` 方法 * :art: style --- app/src/layout/util.ts | 4 ++-- app/src/mobile/util/initFramework.ts | 4 ++-- app/src/protyle/wysiwyg/index.ts | 2 +- app/src/util/functions.ts | 18 ++++-------------- 4 files changed, 9 insertions(+), 19 deletions(-) diff --git a/app/src/layout/util.ts b/app/src/layout/util.ts index 65ba82f14..678960536 100644 --- a/app/src/layout/util.ts +++ b/app/src/layout/util.ts @@ -300,9 +300,9 @@ export const JSONToLayout = (isStart: boolean) => { } // https://github.com/siyuan-note/siyuan/pull/7086 const openId = getSearch("id", window.location.href); - if (openId) { + if (openId !== null) { openFileById({ - id: getSearch("id", window.location.href), + id: openId, action: [Constants.CB_GET_FOCUS, Constants.CB_GET_CONTEXT], zoomIn: getSearch("focus", window.location.href) === "1" }); diff --git a/app/src/mobile/util/initFramework.ts b/app/src/mobile/util/initFramework.ts index 927d43aa4..982fb7ba6 100644 --- a/app/src/mobile/util/initFramework.ts +++ b/app/src/mobile/util/initFramework.ts @@ -129,8 +129,8 @@ export const initFramework = () => { initEditorName(); if (getOpenNotebookCount() > 0) { const openId = getSearch("id", window.location.href); - if (openId) { - openMobileFileById(getSearch("id", window.location.href), + if (openId !== null) { + openMobileFileById(openId, getSearch("focus", window.location.href) === "1" ? [Constants.CB_GET_ALL, Constants.CB_GET_FOCUS] : [Constants.CB_GET_FOCUS, Constants.CB_GET_CONTEXT]); } else { const localDoc = window.siyuan.storage[Constants.LOCAL_DOCINFO]; diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.ts index c52f92693..c8cb90a86 100644 --- a/app/src/protyle/wysiwyg/index.ts +++ b/app/src/protyle/wysiwyg/index.ts @@ -1578,7 +1578,7 @@ export class WYSIWYG { openBy(linkAddress, "app"); } else { const page = getSearch("page", linkAddress); - openAsset(linkPathname, page === "" ? undefined : parseInt(page), "right"); + openAsset(linkPathname, page === null ? undefined : parseInt(page), "right"); } } else { /// #if !BROWSER diff --git a/app/src/util/functions.ts b/app/src/util/functions.ts index c93031ca3..f7a437fb1 100644 --- a/app/src/util/functions.ts +++ b/app/src/util/functions.ts @@ -14,20 +14,10 @@ export const getRandom = (min: number, max: number) => { return Math.floor(Math.random() * (max - min + 1)) + min; //含最大值,含最小值 }; -export const getSearch = (key: string, link = window.location.search) => { - if (link.indexOf("?") === -1) { - return ""; - } - 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 getSearch: (key: string, link?: string) => string | null = (key: string, link = window.location.search) => { + // REF https://developer.mozilla.org/zh-CN/docs/Web/API/URLSearchParams + const urlSearchParams = new URLSearchParams(link); + return urlSearchParams.get(key); }; export const isBrowser = () => {