diff --git a/app/src/asset/index.ts b/app/src/asset/index.ts index 72bc46cb8..24cafd54d 100644 --- a/app/src/asset/index.ts +++ b/app/src/asset/index.ts @@ -424,13 +424,8 @@ export class Asset extends Model {
`; - const localPDF = JSON.parse(localStorage.getItem(Constants.LOCAL_PDFTHEME) || "{}"); - let pdfTheme; - if (window.siyuan.config.appearance.mode === 0) { - pdfTheme = localPDF.light || "light"; - } else { - pdfTheme = localPDF.dark || "dark"; - } + const localPDF = window.siyuan.storage[Constants.LOCAL_PDFTHEME] + const pdfTheme = window.siyuan.config.appearance.mode === 0 ? localPDF.light : localPDF.dark; const darkElement = this.element.querySelector("#pdfDark"); const lightElement = this.element.querySelector("#pdfLight"); if (pdfTheme === "dark") { @@ -448,7 +443,6 @@ export class Asset extends Model { localPDF.dark = "light"; } this.element.firstElementChild.classList.remove("pdf__outer--dark"); - localStorage.setItem(Constants.LOCAL_PDFTHEME, JSON.stringify(localPDF)); lightElement.classList.add("toggled"); darkElement.classList.remove("toggled"); }); @@ -459,7 +453,6 @@ export class Asset extends Model { localPDF.dark = "dark"; } this.element.firstElementChild.classList.add("pdf__outer--dark"); - localStorage.setItem(Constants.LOCAL_PDFTHEME, JSON.stringify(localPDF)); lightElement.classList.remove("toggled"); darkElement.classList.add("toggled"); }); diff --git a/app/src/card/makeCard.ts b/app/src/card/makeCard.ts index 68410acba..e2b62707f 100644 --- a/app/src/card/makeCard.ts +++ b/app/src/card/makeCard.ts @@ -78,8 +78,8 @@ export const makeCard = (nodeElement: Element[]) => { focusByRange(range); } }); - dialog.element.setAttribute("data-key", "makeCard") - dialog.element.style.zIndex = "199" + dialog.element.setAttribute("data-key", "makeCard"); + dialog.element.style.zIndex = "199"; dialog.element.addEventListener("click", (event) => { let target = event.target as HTMLElement; while (target && !target.isSameNode(dialog.element)) { @@ -224,7 +224,7 @@ const viewCards = (deckID: string, title: string) => { if (response.data.pageCount > 1) { nextElement.removeAttribute("disabled"); } - dialog.element.style.zIndex = "200" + dialog.element.style.zIndex = "200"; dialog.element.addEventListener("click", (event) => { let target = event.target as HTMLElement; while (target && !dialog.element.isSameNode(target)) { diff --git a/app/src/config/bazaar.ts b/app/src/config/bazaar.ts index 354f40ef4..c5a948c26 100644 --- a/app/src/config/bazaar.ts +++ b/app/src/config/bazaar.ts @@ -15,19 +15,7 @@ import {isBrowser} from "../util/functions"; export const bazaar = { element: undefined as Element, genHTML() { - const localSortString = localStorage.getItem(Constants.LOCAL_BAZAAR); - let localSort; - if (!localSortString) { - localSort = { - theme: "0", - template: "0", - icon: "0", - widget: "0", - }; - localStorage.setItem(Constants.LOCAL_BAZAAR, JSON.stringify(localSort)); - } else { - localSort = JSON.parse(localSortString); - } + const localSort = window.siyuan.storage[Constants.LOCAL_BAZAAR]; const loadingHTML = `
`; return `
@@ -609,7 +597,7 @@ export const bazaar = { }); } else { // sort - const localSort = JSON.parse(localStorage.getItem(Constants.LOCAL_BAZAAR)); + const localSort = window.siyuan.storage[Constants.LOCAL_BAZAAR]; const panelElement = selectElement.parentElement.parentElement; let html = ""; if (selectElement.value === "0") { // 更新时间降序 @@ -638,7 +626,6 @@ export const bazaar = { }); } localSort[selectElement.parentElement.parentElement.getAttribute("data-type")] = selectElement.value; - localStorage.setItem(Constants.LOCAL_BAZAAR, JSON.stringify(localSort)); panelElement.querySelector(".b3-cards").innerHTML = html; } }); @@ -667,7 +654,7 @@ export const bazaar = { bazaar.data[bazaarType] = response.data.packages; element.innerHTML = `
${html}
`; - const localSort = JSON.parse(localStorage.getItem(Constants.LOCAL_BAZAAR)); + const localSort = window.siyuan.storage[Constants.LOCAL_BAZAAR]; if (localSort[bazaarType.replace("s", "")] === "1") { html = ""; Array.from(element.querySelectorAll(".b3-card")).sort((a, b) => { diff --git a/app/src/constants.ts b/app/src/constants.ts index ce134069e..848de3946 100644 --- a/app/src/constants.ts +++ b/app/src/constants.ts @@ -59,11 +59,11 @@ export abstract class Constants { // localstorage public static readonly LOCAL_SEARCHEDATA = "local-searchedata"; - public static readonly LOCAL_SEARCHEKEYS = "local-searchekeys"; // "keys", "col", "row", "replaceKeys", "layout" + public static readonly LOCAL_SEARCHEKEYS = "local-searchekeys"; public static readonly LOCAL_DOCINFO = "local-docinfo"; // only mobile - public static readonly LOCAL_DAILYNOTEID = "local-dailynoteid"; - public static readonly LOCAL_HISTORYNOTEID = "local-historynoteid"; - public static readonly LOCAL_CODELANG = "local-codelang"; + public static readonly LOCAL_DAILYNOTEID = "local-dailynoteid"; // string + public static readonly LOCAL_HISTORYNOTEID = "local-historynoteid"; // string + public static readonly LOCAL_CODELANG = "local-codelang"; // string public static readonly LOCAL_FONTSTYLES = "local-fontstyles"; public static readonly LOCAL_EXPORTPDF = "local-exportpdf"; public static readonly LOCAL_EXPORTWORD = "local-exportword"; diff --git a/app/src/history/history.ts b/app/src/history/history.ts index 08261e755..3ee7badc9 100644 --- a/app/src/history/history.ts +++ b/app/src/history/history.ts @@ -25,7 +25,7 @@ const renderDoc = (element: HTMLElement, currentPage: number) => { const opElement = element.querySelector('.b3-select[data-type="opselect"]') as HTMLSelectElement; const typeElement = element.querySelector('.b3-select[data-type="typeselect"]') as HTMLSelectElement; const notebookElement = element.querySelector('.b3-select[data-type="notebookselect"]') as HTMLSelectElement; - localStorage.setItem(Constants.LOCAL_HISTORYNOTEID, notebookElement.value); + window.siyuan.storage[Constants.LOCAL_HISTORYNOTEID] = notebookElement.value; const docElement = element.querySelector('.history__text[data-type="docPanel"]'); const assetElement = element.querySelector('.history__text[data-type="assetPanel"]'); const mdElement = element.querySelector('.history__text[data-type="mdPanel"]') as HTMLTextAreaElement; @@ -211,7 +211,7 @@ export const openHistory = () => { return; } - const currentNotebookId = localStorage.getItem(Constants.LOCAL_HISTORYNOTEID); + const currentNotebookId = window.siyuan.storage[Constants.LOCAL_HISTORYNOTEID]; let notebookSelectHTML = ""; window.siyuan.notebooks.forEach((item) => { if (!item.closed) { diff --git a/app/src/index.ts b/app/src/index.ts index b2544ecaf..72727a9e9 100644 --- a/app/src/index.ts +++ b/app/src/index.ts @@ -122,9 +122,9 @@ class App { }), menus: new Menus() }; - setLocalStorage(); fetchPost("/api/system/getConf", {}, response => { window.siyuan.config = response.data.conf; + setLocalStorage(); fetchGet(`/appearance/langs/${window.siyuan.config.appearance.lang}.json?v=${Constants.SIYUAN_VERSION}`, (lauguages) => { window.siyuan.languages = lauguages; bootSync(); diff --git a/app/src/menus/protyle.ts b/app/src/menus/protyle.ts index d97b00221..8f7b98c56 100644 --- a/app/src/menus/protyle.ts +++ b/app/src/menus/protyle.ts @@ -455,10 +455,10 @@ export const zoomOut = (protyle: IProtyle, id: string, focusId?: string, isPushB } } if (window.siyuan.mobileEditor) { - localStorage.setItem(Constants.LOCAL_DOCINFO, JSON.stringify({ + window.siyuan.storage[Constants.LOCAL_DOCINFO] = { id, action: id === protyle.block.rootID ? [Constants.CB_GET_HL, Constants.CB_GET_CONTEXT] : [Constants.CB_GET_ALL] - })); + }; if (isPushBack) { pushBack(); } diff --git a/app/src/mobile/editor.ts b/app/src/mobile/editor.ts index 2e5240406..f3a01ff99 100644 --- a/app/src/mobile/editor.ts +++ b/app/src/mobile/editor.ts @@ -14,7 +14,7 @@ import {hideElements} from "../protyle/ui/hideElements"; import {pushBack} from "./util/MobileBackFoward"; export const openMobileFileById = (id: string, action = [Constants.CB_GET_HL]) => { - localStorage.setItem(Constants.LOCAL_DOCINFO, JSON.stringify({id, action})); + window.siyuan.storage[Constants.LOCAL_DOCINFO] = {id, action}; if (window.siyuan.mobileEditor) { hideElements(["toolbar", "hint", "util"], window.siyuan.mobileEditor.protyle); if (window.siyuan.mobileEditor.protyle.contentElement.classList.contains("fn__none")) { diff --git a/app/src/mobile/index.ts b/app/src/mobile/index.ts index 7e3b054d0..132a55ae8 100644 --- a/app/src/mobile/index.ts +++ b/app/src/mobile/index.ts @@ -44,10 +44,10 @@ class App { window.siyuan.menus.menu.remove(); } }); - setLocalStorage(); fetchPost("/api/system/getConf", {}, confResponse => { confResponse.data.conf.keymap = Constants.SIYUAN_KEYMAP; window.siyuan.config = confResponse.data.conf; + setLocalStorage(); fetchGet(`/appearance/langs/${window.siyuan.config.appearance.lang}.json?v=${Constants.SIYUAN_VERSION}`, (lauguages) => { window.siyuan.languages = lauguages; document.title = window.siyuan.languages.siyuanNote; diff --git a/app/src/mobile/util/MobileBackFoward.ts b/app/src/mobile/util/MobileBackFoward.ts index 352a68d0b..77bcb87e3 100644 --- a/app/src/mobile/util/MobileBackFoward.ts +++ b/app/src/mobile/util/MobileBackFoward.ts @@ -12,10 +12,10 @@ const forwardStack: IBackStack[] = []; const focusStack = (backStack: IBackStack) => { const protyle = window.siyuan.mobileEditor.protyle; - localStorage.setItem(Constants.LOCAL_DOCINFO, JSON.stringify({ + window.siyuan.storage[Constants.LOCAL_DOCINFO] = { id: backStack.id, action: backStack.callback, - })); + }; hideElements(["toolbar", "hint", "util"], window.siyuan.mobileEditor.protyle); if (protyle.contentElement.classList.contains("fn__none")) { setEditMode(protyle, "wysiwyg"); diff --git a/app/src/mobile/util/initFramework.ts b/app/src/mobile/util/initFramework.ts index 8633364a9..f66d9114b 100644 --- a/app/src/mobile/util/initFramework.ts +++ b/app/src/mobile/util/initFramework.ts @@ -127,7 +127,7 @@ export const initFramework = () => { }); initEditorName(); if (getOpenNotebookCount() > 0) { - const localDoc = JSON.parse(localStorage.getItem(Constants.LOCAL_DOCINFO) || '{"id": ""}'); + const localDoc = window.siyuan.storage[Constants.LOCAL_DOCINFO]; fetchPost("/api/block/checkBlockExist", {id: localDoc.id}, existResponse => { if (existResponse.data) { openMobileFileById(localDoc.id, localDoc.action); diff --git a/app/src/mobile/util/menu.ts b/app/src/mobile/util/menu.ts index 72cd1b5df..50616c783 100644 --- a/app/src/mobile/util/menu.ts +++ b/app/src/mobile/util/menu.ts @@ -1,5 +1,5 @@ import {fetchPost} from "../../util/fetch"; -import {getEventName, openByMobile, writeText} from "../../protyle/util/compatibility"; +import {exportLocalStorage, getEventName, openByMobile, writeText} from "../../protyle/util/compatibility"; import {popSearch} from "./search"; import {initAppearance} from "../settings/appearance"; import {closePanel} from "./closePanel"; @@ -172,7 +172,9 @@ ${accountHTML} event.stopPropagation(); break; } else if (target.id === "menuSafeQuit") { - exitSiYuan(); + exportLocalStorage(() => { + exitSiYuan(); + }); event.preventDefault(); event.stopPropagation(); break; @@ -439,8 +441,10 @@ ${accountHTML} event.stopPropagation(); break; } else if (target.id === "menuLock") { - fetchPost("/api/system/logoutAuth", {}, () => { - window.location.href = "/"; + exportLocalStorage(() => { + fetchPost("/api/system/logoutAuth", {}, () => { + window.location.href = "/"; + }); }); event.preventDefault(); event.stopPropagation(); @@ -457,6 +461,7 @@ ${accountHTML} event.stopPropagation(); break; } else if (target.id === "menuSyncNow") { + exportLocalStorage(); syncGuide(); event.preventDefault(); event.stopPropagation(); diff --git a/app/src/mobile/util/search.ts b/app/src/mobile/util/search.ts index d0bb5266b..ac310663c 100644 --- a/app/src/mobile/util/search.ts +++ b/app/src/mobile/util/search.ts @@ -37,17 +37,14 @@ export const toolbarSearchEvent = () => { onRecentBlocks(response.data.blocks, response.data.matchedRootCount,response.data.matchedBlockCount); }); } - const localData = JSON.parse(localStorage.getItem(Constants.LOCAL_SEARCHEDATA) || "{}"); - localData.k = inputElement.value; - localStorage.setItem(Constants.LOCAL_SEARCHEDATA, JSON.stringify(localData)); + window.siyuan.storage[Constants.LOCAL_SEARCHEDATA].k = inputElement.value; }, Constants.TIMEOUT_SEARCH); }; const initToolbarSearch = () => { const inputElement = document.getElementById("toolbarSearch") as HTMLInputElement; inputElement.focus(); - const localData = JSON.parse(localStorage.getItem(Constants.LOCAL_SEARCHEDATA) || "{}"); - inputElement.value = localData.k || ""; + inputElement.value = window.siyuan.storage[Constants.LOCAL_SEARCHEDATA].k; inputElement.addEventListener("compositionend", (event: InputEvent) => { if (event && event.isComposing) { return; diff --git a/app/src/protyle/export/index.ts b/app/src/protyle/export/index.ts index ddad3b7bd..18dd21345 100644 --- a/app/src/protyle/export/index.ts +++ b/app/src/protyle/export/index.ts @@ -26,7 +26,7 @@ export const saveExport = (option: { type: string, id: string }) => { renderPDF(option.id); } } else if (option.type === "word") { - const localData = localStorage.getItem(Constants.LOCAL_EXPORTWORD); + const localData = window.siyuan.storage[Constants.LOCAL_EXPORTWORD]; const wordDialog = new Dialog({ title: "Word " + window.siyuan.languages.config, content: `
@@ -35,14 +35,14 @@ export const saveExport = (option: { type: string, id: string }) => { ${window.siyuan.languages.exportPDF4}
- +
@@ -58,7 +58,6 @@ export const saveExport = (option: { type: string, id: string }) => { btnsElement[1].addEventListener("click", () => { const removeAssets = (wordDialog.element.querySelector("#removeAssets") as HTMLInputElement).checked; const mergeSubdocs = (wordDialog.element.querySelector("#mergeSubdocs") as HTMLInputElement).checked; - localStorage.setItem(Constants.LOCAL_EXPORTWORD, JSON.stringify({removeAssets, mergeSubdocs})); getExportPath(option, removeAssets, mergeSubdocs); wordDialog.destroy(); }); @@ -71,15 +70,7 @@ export const saveExport = (option: { type: string, id: string }) => { /// #if !BROWSER let originalZoomFactor = 1; const renderPDF = (id: string) => { - const localData = JSON.parse(localStorage.getItem(Constants.LOCAL_EXPORTPDF) || JSON.stringify({ - landscape: false, - marginType: "0", - scale: 1, - pageSize: "A4", - removeAssets: true, - keepFold: false, - mergeSubdocs: false, - })); + const localData = window.siyuan.storage[Constants.LOCAL_EXPORTPDF]; const servePath = window.location.protocol + "//" + window.location.host; const isDefault = (window.siyuan.config.appearance.mode === 1 && window.siyuan.config.appearance.themeDark === "midnight") || (window.siyuan.config.appearance.mode === 0 && window.siyuan.config.appearance.themeLight === "daylight"); let themeStyle = ""; diff --git a/app/src/protyle/gutter/index.ts b/app/src/protyle/gutter/index.ts index 532f4b107..5f6c71370 100644 --- a/app/src/protyle/gutter/index.ts +++ b/app/src/protyle/gutter/index.ts @@ -591,7 +591,7 @@ export class Gutter { click() { let html = ""; selectsElement.forEach(item => { - item.querySelectorAll('[spellcheck]').forEach(editItem => { + item.querySelectorAll("[spellcheck]").forEach(editItem => { const cloneNode = editItem.cloneNode(true) as HTMLElement; cloneNode.querySelectorAll('[data-type="backslash"]').forEach(slashItem => { slashItem.firstElementChild.remove(); @@ -980,7 +980,7 @@ export class Gutter { accelerator: window.siyuan.config.keymap.editor.general.copyPlainText.custom, click() { let text = ""; - nodeElement.querySelectorAll('[spellcheck]').forEach(item => { + nodeElement.querySelectorAll("[spellcheck]").forEach(item => { const cloneNode = item.cloneNode(true) as HTMLElement; cloneNode.querySelectorAll('[data-type="backslash"]').forEach(slashItem => { slashItem.firstElementChild.remove(); diff --git a/app/src/protyle/hint/index.ts b/app/src/protyle/hint/index.ts index e5667faec..506ad8d95 100644 --- a/app/src/protyle/hint/index.ts +++ b/app/src/protyle/hint/index.ts @@ -556,7 +556,7 @@ ${unicode2Emoji(emoji.unicode, true)}`; } let textContent = value; if (value === "```") { - textContent = value + (localStorage.getItem(Constants.LOCAL_CODELANG) || "") + Lute.Caret + "\n```"; + textContent = value + window.siyuan.storage[Constants.LOCAL_CODELANG] + Lute.Caret + "\n```"; } const editableElement = getContenteditableElement(nodeElement); if (value === "![]()") { // https://github.com/siyuan-note/siyuan/issues/4586 1 diff --git a/app/src/protyle/markdown/highlightRender.ts b/app/src/protyle/markdown/highlightRender.ts index f323c57c7..8dd1f729b 100644 --- a/app/src/protyle/markdown/highlightRender.ts +++ b/app/src/protyle/markdown/highlightRender.ts @@ -9,7 +9,7 @@ export const highlightRender = (element: Element, cdn = Constants.PROTYLE_CDN) = let isPreview = false; if (element.classList.contains("code-block")) { // 编辑器内代码块编辑渲染 - codeElements = element.querySelectorAll('[spellcheck]'); + codeElements = element.querySelectorAll("[spellcheck]"); } else { if (element.classList.contains("item__readme")) { // bazaar reademe @@ -22,7 +22,7 @@ export const highlightRender = (element: Element, cdn = Constants.PROTYLE_CDN) = codeElements = element.querySelectorAll(".code-block code"); isPreview = true; } else { - codeElements = element.querySelectorAll('.code-block [spellcheck]'); + codeElements = element.querySelectorAll(".code-block [spellcheck]"); } } if (codeElements.length === 0) { diff --git a/app/src/protyle/toolbar/Font.ts b/app/src/protyle/toolbar/Font.ts index 27696b305..b22cd2341 100644 --- a/app/src/protyle/toolbar/Font.ts +++ b/app/src/protyle/toolbar/Font.ts @@ -46,7 +46,7 @@ export const fontMenu = (protyle: IProtyle) => { const element = document.createElement("div"); element.classList.add("protyle-font"); let lastColorHTML = ""; - const lastFonts = JSON.parse(localStorage.getItem(Constants.LOCAL_FONTSTYLES) || "[]"); + const lastFonts = window.siyuan.storage[Constants.LOCAL_FONTSTYLES]; if (lastFonts.length > 0) { lastColorHTML = `
${window.siyuan.languages.lastUsed} @@ -136,14 +136,13 @@ export const fontMenu = (protyle: IProtyle) => { }; export const fontEvent = (protyle: IProtyle, type?: string, color?: string) => { - let localFontStyles = JSON.parse(localStorage.getItem(Constants.LOCAL_FONTSTYLES) || "[]"); + let localFontStyles = window.siyuan.storage[Constants.LOCAL_FONTSTYLES]; if (type) { localFontStyles.splice(0, 0, `${type}${Constants.ZWSP}${color}`); localFontStyles = [...new Set(localFontStyles)]; if (localFontStyles.length > 8) { localFontStyles.splice(7, 1); } - localStorage.setItem(Constants.LOCAL_FONTSTYLES, JSON.stringify(localFontStyles)); } else { if (localFontStyles.length === 0) { type = "color"; diff --git a/app/src/protyle/toolbar/index.ts b/app/src/protyle/toolbar/index.ts index ee09d8553..577d1bbf7 100644 --- a/app/src/protyle/toolbar/index.ts +++ b/app/src/protyle/toolbar/index.ts @@ -1162,7 +1162,7 @@ export class Toolbar { if (event.key === "Enter") { const activeText = this.subElement.querySelector(".b3-list-item--focus").textContent; languageElement.textContent = activeText === window.siyuan.languages.clear ? "" : activeText; - localStorage.setItem(Constants.LOCAL_CODELANG, languageElement.textContent); + window.siyuan.storage[Constants.LOCAL_CODELANG] = languageElement.textContent; const editElement = getContenteditableElement(nodeElement); const lineNumber = nodeElement.getAttribute("linenumber"); if (lineNumber === "true" || (lineNumber !== "false" && window.siyuan.config.editor.codeSyntaxHighlightLineNum)) { @@ -1226,7 +1226,7 @@ export class Toolbar { return; } languageElement.textContent = listElement.textContent === window.siyuan.languages.clear ? "" : listElement.textContent; - localStorage.setItem(Constants.LOCAL_CODELANG, languageElement.textContent); + window.siyuan.storage[Constants.LOCAL_CODELANG] = languageElement.textContent; const nodeElement = hasClosestBlock(languageElement); if (nodeElement) { const editElement = getContenteditableElement(nodeElement); diff --git a/app/src/protyle/util/compatibility.ts b/app/src/protyle/util/compatibility.ts index eff473675..d758fd51e 100644 --- a/app/src/protyle/util/compatibility.ts +++ b/app/src/protyle/util/compatibility.ts @@ -141,21 +141,79 @@ export const hotKey2Electron = (key: string) => { export const setLocalStorage = () => { fetchPost("/api/storage/getLocalStorage", undefined, (response) => { - if (response.data) { - localStorage.clear(); - Object.keys(response.data).forEach(item => { - if (item !== "setItem" && item !== "removeItem") { - localStorage.setItem(item, response.data[item]); + window.siyuan.storage = response.data; + // 历史数据迁移 + const defaultStorage: any = {}; + defaultStorage[Constants.LOCAL_SEARCHEKEYS] = { + keys: [], + replaceKeys: [], + col: "", + row: "", + layout: 0 + }; + defaultStorage[Constants.LOCAL_PDFTHEME] = {light: "light", dark: "dark"}; + defaultStorage[Constants.LOCAL_BAZAAR] = { + theme: "0", + template: "0", + icon: "0", + widget: "0", + }; + defaultStorage[Constants.LOCAL_EXPORTWORD] = {removeAssets: false, mergeSubdocs: false}; + defaultStorage[Constants.LOCAL_EXPORTPDF] = { + landscape: false, + marginType: "0", + scale: 1, + pageSize: "A4", + removeAssets: true, + keepFold: false, + mergeSubdocs: false, + }; + defaultStorage[Constants.LOCAL_DOCINFO] = { + id: "", + action: [] + }; + defaultStorage[Constants.LOCAL_FONTSTYLES] = []; + defaultStorage[Constants.LOCAL_SEARCHEDATA] = { + sort: 0, + group: 0, + hasReplace: false, + method: 0, + hPath: "", + idPath: [], + k: "", + r: "", + types: { + document: window.siyuan.config.search.document, + heading: window.siyuan.config.search.heading, + list: window.siyuan.config.search.list, + listItem: window.siyuan.config.search.listItem, + codeBlock: window.siyuan.config.search.codeBlock, + htmlBlock: window.siyuan.config.search.htmlBlock, + mathBlock: window.siyuan.config.search.mathBlock, + table: window.siyuan.config.search.table, + blockquote: window.siyuan.config.search.blockquote, + superBlock: window.siyuan.config.search.superBlock, + paragraph: window.siyuan.config.search.paragraph, + } + }; + + [Constants.LOCAL_SEARCHEKEYS, Constants.LOCAL_PDFTHEME, Constants.LOCAL_BAZAAR, Constants.LOCAL_EXPORTWORD, + Constants.LOCAL_EXPORTPDF, Constants.LOCAL_DOCINFO, Constants.LOCAL_FONTSTYLES, Constants.LOCAL_SEARCHEDATA].forEach((key) => { + if (typeof response.data[key] === "string") { + try { + window.siyuan.storage[key] = Object.assign(defaultStorage[key], JSON.parse(response.data[key])); + } catch (e) { + window.siyuan.storage[key] = defaultStorage[key]; } - }); - } else { - exportLocalStorage(); - } + } else if (typeof response.data[key] === "undefined") { + window.siyuan.storage[key] = defaultStorage[key]; + } + }); }); }; export const exportLocalStorage = (cb?: () => void) => { - fetchPost("/api/storage/setLocalStorage", {val: localStorage}, () => { + fetchPost("/api/storage/setLocalStorage", {val: window.siyuan.storage}, () => { if (cb) { cb(); } diff --git a/app/src/protyle/util/processCode.ts b/app/src/protyle/util/processCode.ts index 40fa74061..63616dea2 100644 --- a/app/src/protyle/util/processCode.ts +++ b/app/src/protyle/util/processCode.ts @@ -36,7 +36,7 @@ export const processPasteCode = (html: string, text: string) => { if (isCode) { const code = text || html; if (/\n/.test(code) || pres.length === 1) { - return `
${localStorage.getItem(Constants.LOCAL_CODELANG) || ""}
${code.replace(/&/g, "&").replace(/
${Constants.ZWSP}
`; + return `
${window.siyuan.storage[Constants.LOCAL_CODELANG]}
${code.replace(/&/g, "&").replace(/
${Constants.ZWSP}
`; } else { return code; } diff --git a/app/src/protyle/wysiwyg/enter.ts b/app/src/protyle/wysiwyg/enter.ts index f637257cd..ad8a7f97e 100644 --- a/app/src/protyle/wysiwyg/enter.ts +++ b/app/src/protyle/wysiwyg/enter.ts @@ -244,10 +244,10 @@ export const enter = (blockElement: HTMLElement, range: Range, protyle: IProtyle blockElement = protyle.wysiwyg.element.querySelector(`[data-node-id="${blockElement.getAttribute("data-node-id")}"]`); const languageElement = blockElement.querySelector(".protyle-action__language"); if (languageElement) { - if (localStorage.getItem(Constants.LOCAL_CODELANG) && languageElement.textContent === "") { - languageElement.textContent = localStorage.getItem(Constants.LOCAL_CODELANG); + if (window.siyuan.storage[Constants.LOCAL_CODELANG] && languageElement.textContent === "") { + languageElement.textContent = window.siyuan.storage[Constants.LOCAL_CODELANG]; } else { - localStorage.setItem(Constants.LOCAL_CODELANG, languageElement.textContent); + window.siyuan.storage[Constants.LOCAL_CODELANG] = languageElement.textContent; } highlightRender(blockElement); } else { diff --git a/app/src/protyle/wysiwyg/input.ts b/app/src/protyle/wysiwyg/input.ts index 5c3fc63bc..f18b2c68f 100644 --- a/app/src/protyle/wysiwyg/input.ts +++ b/app/src/protyle/wysiwyg/input.ts @@ -170,8 +170,8 @@ export const input = async (protyle: IProtyle, blockElement: HTMLElement, range: if (realType === "NodeCodeBlock") { const languageElement = realElement.querySelector(".protyle-action__language"); if (languageElement) { - if (localStorage.getItem(Constants.LOCAL_CODELANG) && languageElement.textContent === "") { - languageElement.textContent = localStorage.getItem(Constants.LOCAL_CODELANG); + if (window.siyuan.storage[Constants.LOCAL_CODELANG] && languageElement.textContent === "") { + languageElement.textContent = window.siyuan.storage[Constants.LOCAL_CODELANG]; } highlightRender(realElement); } else if (tempElement.content.childElementCount === 1) { diff --git a/app/src/protyle/wysiwyg/keydown.ts b/app/src/protyle/wysiwyg/keydown.ts index ad3b43001..74277abb9 100644 --- a/app/src/protyle/wysiwyg/keydown.ts +++ b/app/src/protyle/wysiwyg/keydown.ts @@ -1248,7 +1248,7 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => { const id = nodeElement.getAttribute("data-node-id"); const html = nodeElement.outerHTML; const editElement = getContenteditableElement(nodeElement); - editElement.innerHTML = "```" + (localStorage.getItem(Constants.LOCAL_CODELANG) || "") + "\n" + editElement.textContent + "\n```"; + editElement.innerHTML = "```" + window.siyuan.storage[Constants.LOCAL_CODELANG] + "\n" + editElement.textContent + "\n```"; const newHTML = protyle.lute.SpinBlockDOM(nodeElement.outerHTML); nodeElement.outerHTML = newHTML; const newNodeElement = protyle.wysiwyg.element.querySelector(`[data-node-id="${id}"]`); diff --git a/app/src/search/spread.ts b/app/src/search/spread.ts index 15b84cbda..99f89d220 100644 --- a/app/src/search/spread.ts +++ b/app/src/search/spread.ts @@ -45,22 +45,7 @@ export const openSearch = async (hotkey: string, key?: string, notebookId?: stri if (exitDialog) { return; } - const localData = JSON.parse(localStorage.getItem(Constants.LOCAL_SEARCHEDATA) || "{}"); - if (!localData.types) { - localData.types = { - document: window.siyuan.config.search.document, - heading: window.siyuan.config.search.heading, - list: window.siyuan.config.search.list, - listItem: window.siyuan.config.search.listItem, - codeBlock: window.siyuan.config.search.codeBlock, - htmlBlock: window.siyuan.config.search.htmlBlock, - mathBlock: window.siyuan.config.search.mathBlock, - table: window.siyuan.config.search.table, - blockquote: window.siyuan.config.search.blockquote, - superBlock: window.siyuan.config.search.superBlock, - paragraph: window.siyuan.config.search.paragraph, - }; - } + const localData = window.siyuan.storage[Constants.LOCAL_SEARCHEDATA]; let hPath = ""; let idPath: string[] = []; if (notebookId) { @@ -75,8 +60,8 @@ export const openSearch = async (hotkey: string, key?: string, notebookId?: stri idPath[0] = pathPosix().join(idPath[0], searchPath); } } else if (window.siyuan.config.keymap.general.globalSearch.custom === hotkey) { - hPath = localData.hPath || ""; - idPath = localData.idPath || []; + hPath = localData.hPath; + idPath = localData.idPath; // 历史原因,2.5.2 之前为 string https://github.com/siyuan-note/siyuan/issues/6902 if (typeof idPath === "string") { idPath = [idPath]; @@ -103,13 +88,13 @@ export const openSearch = async (hotkey: string, key?: string, notebookId?: stri dialog.element.setAttribute("data-key", hotkey); const edit = genSearch({ k: key || localData.k, - r: localData.r || "", + r: localData.r, hasReplace: hotkey === window.siyuan.config.keymap.general.replace.custom, - method: localData.method || 0, + method: localData.method, hPath, idPath, - group: localData.group || 0, - sort: localData.sort || 0, + group: localData.group, + sort: localData.sort, types: localData.types }, dialog.element.querySelector(".b3-dialog__container").lastElementChild, () => { dialog.destroy(); diff --git a/app/src/search/util.ts b/app/src/search/util.ts index 1cf5f737a..4a0c85897 100644 --- a/app/src/search/util.ts +++ b/app/src/search/util.ts @@ -20,15 +20,12 @@ import {Dialog} from "../dialog"; import {hasClosestByClassName} from "../protyle/util/hasClosest"; const saveKeyList = (type: "keys" | "replaceKeys", value: string) => { - const searchKeys = JSON.parse(localStorage.getItem(Constants.LOCAL_SEARCHEKEYS) || "{}"); - let list: string[] = searchKeys[type] || []; + let list: string[] = window.siyuan.storage[Constants.LOCAL_SEARCHEKEYS][type]; list.splice(0, 0, value); list = Array.from(new Set(list)); if (list.length > window.siyuan.config.search.limit) { list.splice(window.siyuan.config.search.limit, list.length - window.siyuan.config.search.limit); } - searchKeys[type] = list; - localStorage.setItem(Constants.LOCAL_SEARCHEKEYS, JSON.stringify(searchKeys)); }; export const openGlobalSearch = (text: string, replace: boolean) => { @@ -52,33 +49,18 @@ export const openGlobalSearch = (text: string, replace: boolean) => { icon: "iconSearch", title: window.siyuan.languages.search, callback(tab) { - const localData = JSON.parse(localStorage.getItem(Constants.LOCAL_SEARCHEDATA) || "{}"); - if (!localData.types) { - localData.types = { - document: window.siyuan.config.search.document, - heading: window.siyuan.config.search.heading, - list: window.siyuan.config.search.list, - listItem: window.siyuan.config.search.listItem, - codeBlock: window.siyuan.config.search.codeBlock, - htmlBlock: window.siyuan.config.search.htmlBlock, - mathBlock: window.siyuan.config.search.mathBlock, - table: window.siyuan.config.search.table, - blockquote: window.siyuan.config.search.blockquote, - superBlock: window.siyuan.config.search.superBlock, - paragraph: window.siyuan.config.search.paragraph, - }; - } + const localData = window.siyuan.storage[Constants.LOCAL_SEARCHEDATA]; const asset = new Search({ tab, config: { k: text, r: "", hasReplace: false, - method: localData.method || 0, + method: localData.method, hPath: "", idPath: [], - group: localData.group || 0, - sort: localData.sort || 0, + group: localData.group, + sort: localData.sort, types: localData.types } }); @@ -109,7 +91,7 @@ export const genSearch = (config: ISearchOption, element: Element, closeCB?: () enableIncludeChild = true; } }); - const data = JSON.parse(localStorage.getItem(Constants.LOCAL_SEARCHEKEYS) || "{}"); + const data = window.siyuan.storage[Constants.LOCAL_SEARCHEKEYS]; element.innerHTML = `
@@ -225,7 +207,7 @@ export const genSearch = (config: ISearchOption, element: Element, closeCB?: () const documentSelf = document; const nextElement = dragElement.nextElementSibling as HTMLElement; const previousElement = dragElement.previousElementSibling as HTMLElement; - const direction = JSON.parse(localStorage.getItem(Constants.LOCAL_SEARCHEKEYS) || "{}").layout === 1 ? "lr" : "tb"; + const direction = window.siyuan.storage[Constants.LOCAL_SEARCHEKEYS].layout === 1 ? "lr" : "tb"; const x = event[direction === "lr" ? "clientX" : "clientY"]; const previousSize = direction === "lr" ? previousElement.clientWidth : previousElement.clientHeight; const nextSize = direction === "lr" ? nextElement.clientWidth : nextElement.clientHeight; @@ -250,9 +232,7 @@ export const genSearch = (config: ISearchOption, element: Element, closeCB?: () documentSelf.ondragstart = null; documentSelf.onselectstart = null; documentSelf.onselect = null; - const json = JSON.parse(localStorage.getItem(Constants.LOCAL_SEARCHEKEYS) || "{}"); - json[direction === "lr" ? "col" : "row"] = nextElement[direction === "lr" ? "clientWidth" : "clientHeight"] + "px"; - localStorage.setItem(Constants.LOCAL_SEARCHEKEYS, JSON.stringify(json)); + window.siyuan.storage[Constants.LOCAL_SEARCHEKEYS][direction === "lr" ? "col" : "row"] = nextElement[direction === "lr" ? "clientWidth" : "clientHeight"] + "px"; if (direction === "lr") { setPadding(edit.protyle); } @@ -403,7 +383,7 @@ export const genSearch = (config: ISearchOption, element: Element, closeCB?: () event.preventDefault(); break; } else if (target.id === "searchHistoryBtn") { - const list = JSON.parse(localStorage.getItem(Constants.LOCAL_SEARCHEKEYS) || "{}"); + const list = window.siyuan.storage[Constants.LOCAL_SEARCHEKEYS]; if (!list.keys || list.keys.length === 0) { return; } @@ -423,7 +403,7 @@ export const genSearch = (config: ISearchOption, element: Element, closeCB?: () event.preventDefault(); return; } else if (target.id === "replaceHistoryBtn") { - const list = JSON.parse(localStorage.getItem(Constants.LOCAL_SEARCHEKEYS) || "{}"); + const list = window.siyuan.storage[Constants.LOCAL_SEARCHEKEYS]; if (!list.replaceKeys || list.replaceKeys.length === 0) { return; } @@ -704,10 +684,7 @@ const addConfigMoreMenu = async (config: ISearchOption, edit: Protyle, element: } }] }).element); - const localData = JSON.parse(localStorage.getItem(Constants.LOCAL_SEARCHEKEYS) || "{}"); - if (typeof localData.layout === "undefined") { - localData.layout = 0; - } + const localData = window.siyuan.storage[Constants.LOCAL_SEARCHEKEYS]; window.siyuan.menus.menu.append(new MenuItem({ label: window.siyuan.languages.layout, type: "submenu", @@ -725,7 +702,6 @@ const addConfigMoreMenu = async (config: ISearchOption, edit: Protyle, element: } setPadding(edit.protyle); localData.layout = 0; - localStorage.setItem(Constants.LOCAL_SEARCHEKEYS, JSON.stringify(localData)); } }, { label: window.siyuan.languages.leftRightLayout, @@ -741,7 +717,6 @@ const addConfigMoreMenu = async (config: ISearchOption, edit: Protyle, element: } setPadding(edit.protyle); localData.layout = 1; - localStorage.setItem(Constants.LOCAL_SEARCHEKEYS, JSON.stringify(localData)); } }] }).element); @@ -892,9 +867,8 @@ const updateConfig = (element: Element, item: ISearchOption, config: ISearchOpti } (element.querySelector("#searchInput") as HTMLInputElement).value = item.k; (element.querySelector("#replaceInput") as HTMLInputElement).value = item.r; - Object.assign(config, item); + window.siyuan.storage[Constants.LOCAL_SEARCHEDATA] = Object.assign({}, config, item); inputEvent(element, config, undefined, edit); - localStorage.setItem(Constants.LOCAL_SEARCHEDATA, JSON.stringify(config)); window.siyuan.menus.menu.remove(); }; diff --git a/app/src/types/index.d.ts b/app/src/types/index.d.ts index 1e4fd7f61..d44252642 100644 --- a/app/src/types/index.d.ts +++ b/app/src/types/index.d.ts @@ -57,6 +57,7 @@ interface ICard { name: string size: number } + interface ISearchOption { name?: string sort: number, // 0:按块类型(默认),1:按创建时间升序,2:按创建时间降序,3:按更新时间升序,4:按更新时间降序,5:按内容顺序(仅在按文档分组时),6:按相关度升序,7:按相关度降序 @@ -149,6 +150,7 @@ interface INotebook { } interface ISiyuan { + storage?: { [key: string]: any }, printWin?: import("electron").BrowserWindow transactionsTimeout?: number, transactions?: { diff --git a/app/src/util/assets.ts b/app/src/util/assets.ts index 4d489757e..f1fa7b854 100644 --- a/app/src/util/assets.ts +++ b/app/src/util/assets.ts @@ -53,13 +53,8 @@ export const loadAssets = (data: IAppearance) => { getAllModels().graph.forEach(item => { item.searchGraph(false); }); - const localPDF = JSON.parse(localStorage.getItem(Constants.LOCAL_PDFTHEME) || "{}"); - let pdfTheme: string; - if (window.siyuan.config.appearance.mode === 0) { - pdfTheme = localPDF.light || "light"; - } else { - pdfTheme = localPDF.dark || "dark"; - } + const pdfTheme = window.siyuan.config.appearance.mode === 0 ? window.siyuan.storage[Constants.LOCAL_PDFTHEME].light : + window.siyuan.storage[Constants.LOCAL_PDFTHEME].dark; document.querySelectorAll(".pdf__outer").forEach(item => { const darkElement = item.querySelector("#pdfDark"); const lightElement = item.querySelector("#pdfLight"); diff --git a/app/src/util/mount.ts b/app/src/util/mount.ts index 4de1d5bbd..022a8bb5e 100644 --- a/app/src/util/mount.ts +++ b/app/src/util/mount.ts @@ -34,7 +34,7 @@ export const newDailyNote = () => { }); return; } - const localNotebookId = localStorage.getItem(Constants.LOCAL_DAILYNOTEID); + const localNotebookId = window.siyuan.storage[Constants.LOCAL_DAILYNOTEID]; if (localNotebookId && getNotebookName(localNotebookId) && !isMobile()) { fetchPost("/api/filetree/createDailyNote", { notebook: localNotebookId, @@ -66,7 +66,7 @@ export const newDailyNote = () => { }); btnsElement[1].addEventListener("click", () => { const notebook = selectElement.value; - localStorage.setItem(Constants.LOCAL_DAILYNOTEID, notebook); + window.siyuan.storage[Constants.LOCAL_DAILYNOTEID] = notebook; fetchPost("/api/filetree/createDailyNote", { notebook, app: Constants.SIYUAN_APPID, diff --git a/app/src/util/onGetConfig.ts b/app/src/util/onGetConfig.ts index ca16e149f..e9183c8b1 100644 --- a/app/src/util/onGetConfig.ts +++ b/app/src/util/onGetConfig.ts @@ -292,7 +292,7 @@ const initBar = () => { notebook: item.id, app: Constants.SIYUAN_APPID, }); - localStorage.setItem(Constants.LOCAL_DAILYNOTEID, item.id); + window.siyuan.storage[Constants.LOCAL_DAILYNOTEID] = item.id; } }).element); } @@ -380,11 +380,15 @@ const initWindow = () => { return; } const msgId = showMessage(window.siyuan.languages.exporting, -1); - localStorage.setItem(Constants.LOCAL_EXPORTPDF, JSON.stringify(Object.assign(ipcData.pdfOptions, { + window.siyuan.storage[Constants.LOCAL_EXPORTPDF] = { removeAssets: ipcData.removeAssets, keepFold: ipcData.keepFold, mergeSubdocs: ipcData.mergeSubdocs, - }))); + landscape: ipcData.pdfOptions.landscape, + marginType: ipcData.pdfOptions.marginType, + pageSize: ipcData.pdfOptions.pageSize, + scale: ipcData.pdfOptions.scale, + }; try { if (window.siyuan.config.export.addFooter) { ipcData.pdfOptions.displayHeaderFooter = true;