This commit is contained in:
Vanessa 2022-12-31 22:14:42 +08:00
parent 7688733364
commit 4ee566cbd9
30 changed files with 153 additions and 163 deletions

View file

@ -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: `<div class="b3-dialog__content">
@ -35,14 +35,14 @@ export const saveExport = (option: { type: string, id: string }) => {
${window.siyuan.languages.exportPDF4}
</div>
<span class="fn__space"></span>
<input id="removeAssets" class="b3-switch" type="checkbox" ${localData === "true" ? "checked" : ""}>
<input id="removeAssets" class="b3-switch" type="checkbox" ${localData.removeAssets ? "checked" : ""}>
</label>
<label class="fn__flex b3-label">
<div class="fn__flex-1">
${window.siyuan.languages.exportPDF6}
</div>
<span class="fn__space"></span>
<input id="mergeSubdocs" class="b3-switch" type="checkbox" ${localData === "true" ? "checked" : ""}>
<input id="mergeSubdocs" class="b3-switch" type="checkbox" ${localData.mergeSubdocs ? "checked" : ""}>
</label>
</div>
<div class="b3-dialog__action">
@ -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 = "";

View file

@ -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();

View file

@ -556,7 +556,7 @@ ${unicode2Emoji(emoji.unicode, true)}</button>`;
}
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

View file

@ -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) {

View file

@ -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 = `<div style="margin-bottom: 2px" class="fn__flex">
${window.siyuan.languages.lastUsed}<span class="fn__space"></span>
@ -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";

View file

@ -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);

View file

@ -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();
}

View file

@ -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 `<div data-type="NodeCodeBlock" class="code-block" data-node-id="${Lute.NewNodeID()}"><div class="protyle-action"><span class="protyle-action--first protyle-action__language" contenteditable="false">${localStorage.getItem(Constants.LOCAL_CODELANG) || ""}</span><span class="fn__flex-1"></span><span class="protyle-icon protyle-icon--first protyle-action__copy"><svg><use xlink:href="#iconCopy"></use></svg></span><span class="protyle-icon protyle-icon--last protyle-action__menu"><svg><use xlink:href="#iconMore"></use></svg></span></div><div contenteditable="true" spellcheck="${window.siyuan.config.editor.spellcheck}">${code.replace(/&/g, "&amp;").replace(/</g, "&lt;")}<wbr></div><div class="protyle-attr" contenteditable="false">${Constants.ZWSP}</div></div>`;
return `<div data-type="NodeCodeBlock" class="code-block" data-node-id="${Lute.NewNodeID()}"><div class="protyle-action"><span class="protyle-action--first protyle-action__language" contenteditable="false">${window.siyuan.storage[Constants.LOCAL_CODELANG]}</span><span class="fn__flex-1"></span><span class="protyle-icon protyle-icon--first protyle-action__copy"><svg><use xlink:href="#iconCopy"></use></svg></span><span class="protyle-icon protyle-icon--last protyle-action__menu"><svg><use xlink:href="#iconMore"></use></svg></span></div><div contenteditable="true" spellcheck="${window.siyuan.config.editor.spellcheck}">${code.replace(/&/g, "&amp;").replace(/</g, "&lt;")}<wbr></div><div class="protyle-attr" contenteditable="false">${Constants.ZWSP}</div></div>`;
} else {
return code;
}

View file

@ -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 {

View file

@ -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) {

View file

@ -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 + "<wbr>\n```";
editElement.innerHTML = "```" + window.siyuan.storage[Constants.LOCAL_CODELANG] + "\n" + editElement.textContent + "<wbr>\n```";
const newHTML = protyle.lute.SpinBlockDOM(nodeElement.outerHTML);
nodeElement.outerHTML = newHTML;
const newNodeElement = protyle.wysiwyg.element.querySelector(`[data-node-id="${id}"]`);