This commit is contained in:
Vanessa 2023-05-10 20:18:40 +08:00
parent 78cba22910
commit e0e803d743
21 changed files with 135 additions and 71 deletions

View file

@ -503,7 +503,7 @@ export class Asset extends Model {
/// #if !BROWSER /// #if !BROWSER
setModelsHash(); setModelsHash();
/// #endif /// #endif
}, Constants.TIMEOUT_BLOCKLOAD); }, Constants.TIMEOUT_LOAD);
/// #endif /// #endif
} }
} }

View file

@ -85,10 +85,9 @@ export abstract class Constants {
// timeout // timeout
public static readonly TIMEOUT_DBLCLICK = 190; public static readonly TIMEOUT_DBLCLICK = 190;
public static readonly TIMEOUT_SEARCH = 300;
public static readonly TIMEOUT_INPUT = 256; public static readonly TIMEOUT_INPUT = 256;
public static readonly TIMEOUT_BLOCKLOAD = 300; public static readonly TIMEOUT_LOAD = 300;
public static readonly TIMEOUT_TRANSITION = 150; public static readonly TIMEOUT_TRANSITION = 300;
// id // id
public static readonly HELP_PATH = { public static readonly HELP_PATH = {

View file

@ -434,48 +434,56 @@ const newTab = (options: IOpenFileOptions) => {
return tab; return tab;
}; };
export const updatePanelByEditor = (protyle?: IProtyle, focus = true, pushBackStack = false, reload = false) => { export const updatePanelByEditor = (options: {
protyle?: IProtyle,
focus: boolean,
pushBackStack: boolean,
reload: boolean,
resize: boolean
}) => {
let title = window.siyuan.languages.siyuanNote; let title = window.siyuan.languages.siyuanNote;
if (protyle && protyle.path) { if (options.protyle && options.protyle.path) {
// https://ld246.com/article/1637636106054/comment/1641485541929#comments // https://ld246.com/article/1637636106054/comment/1641485541929#comments
if (protyle.element.classList.contains("fn__none") || if (options.protyle.element.classList.contains("fn__none") ||
(!hasClosestByClassName(protyle.element, "layout__wnd--active") && (!hasClosestByClassName(options.protyle.element, "layout__wnd--active") &&
document.querySelector(".layout__wnd--active") // https://github.com/siyuan-note/siyuan/issues/4414 document.querySelector(".layout__wnd--active") // https://github.com/siyuan-note/siyuan/issues/4414
) )
) { ) {
return; return;
} }
title = protyle.title.editElement.textContent; title = options.protyle.title.editElement.textContent;
resize(protyle); if (options.resize) {
resize(options.protyle);
}
if (focus) { if (focus) {
if (protyle.toolbar.range) { if (options.protyle.toolbar.range) {
focusByRange(protyle.toolbar.range); focusByRange(options.protyle.toolbar.range);
countSelectWord(protyle.toolbar.range, protyle.block.rootID); countSelectWord(options.protyle.toolbar.range, options.protyle.block.rootID);
if (pushBackStack && protyle.preview.element.classList.contains("fn__none")) { if (options.pushBackStack && options.protyle.preview.element.classList.contains("fn__none")) {
pushBack(protyle, protyle.toolbar.range); pushBack(options.protyle, options.protyle.toolbar.range);
} }
} else { } else {
focusBlock(protyle.wysiwyg.element.firstElementChild); focusBlock(options.protyle.wysiwyg.element.firstElementChild);
if (pushBackStack && protyle.preview.element.classList.contains("fn__none")) { if (options.pushBackStack && options.protyle.preview.element.classList.contains("fn__none")) {
pushBack(protyle, undefined, protyle.wysiwyg.element.firstElementChild); pushBack(options.protyle, undefined, options.protyle.wysiwyg.element.firstElementChild);
} }
countBlockWord([], protyle.block.rootID); countBlockWord([], options.protyle.block.rootID);
} }
} }
if (window.siyuan.config.fileTree.alwaysSelectOpenedFile && protyle) { if (window.siyuan.config.fileTree.alwaysSelectOpenedFile && options.protyle) {
const fileModel = getDockByType("file")?.data.file; const fileModel = getDockByType("file")?.data.file;
if (fileModel instanceof Files) { if (fileModel instanceof Files) {
const target = fileModel.element.querySelector(`li[data-path="${protyle.path}"]`); const target = fileModel.element.querySelector(`li[data-path="${options.protyle.path}"]`);
if (!target || (target && !target.classList.contains("b3-list-item--focus"))) { if (!target || (target && !target.classList.contains("b3-list-item--focus"))) {
fileModel.selectItem(protyle.notebookId, protyle.path); fileModel.selectItem(options.protyle.notebookId, options.protyle.path);
} }
} }
} }
} }
// 切换页签或关闭所有页签时,需更新对应的面板 // 切换页签或关闭所有页签时,需更新对应的面板
const models = getAllModels(); const models = getAllModels();
updateOutline(models, protyle, reload); updateOutline(models, options.protyle, options.reload);
updateBacklinkGraph(models, protyle); updateBacklinkGraph(models, options.protyle);
setTitle(title); setTitle(title);
}; };

View file

@ -102,7 +102,7 @@ export class Tab {
(event.clientX < 0 || event.clientY < 0 || event.clientX > window.innerWidth || event.clientY > window.innerHeight)) { (event.clientX < 0 || event.clientY < 0 || event.clientX > window.innerWidth || event.clientY > window.innerHeight)) {
openNewWindow(this); openNewWindow(this);
} }
}, Constants.TIMEOUT_BLOCKLOAD); // 等待主进程发送关闭消息 }, Constants.TIMEOUT_LOAD); // 等待主进程发送关闭消息
/// #endif /// #endif
window.siyuan.dragElement = undefined; window.siyuan.dragElement = undefined;
if (event.dataTransfer.dropEffect === "none") { if (event.dataTransfer.dropEffect === "none") {

View file

@ -484,10 +484,22 @@ export class Wnd {
} }
// focusin 触发前layout__wnd--active 和 tab 已设置,需在调用里面更新 // focusin 触发前layout__wnd--active 和 tab 已设置,需在调用里面更新
if (update) { if (update) {
updatePanelByEditor(currentTab.model.editor.protyle, true, pushBack); updatePanelByEditor({
protyle: currentTab.model.editor.protyle,
focus: true,
pushBackStack: pushBack,
reload: false,
resize: true,
});
} }
} else { } else {
updatePanelByEditor(undefined, false); updatePanelByEditor({
protyle: undefined,
focus: false,
pushBackStack: false,
reload: false,
resize: true,
});
} }
} }
@ -685,12 +697,24 @@ export class Wnd {
// 关闭分屏页签后光标消失 // 关闭分屏页签后光标消失
const editors = getAllModels().editor; const editors = getAllModels().editor;
if (editors.length === 0) { if (editors.length === 0) {
updatePanelByEditor(); updatePanelByEditor({
protyle: undefined,
focus: true,
pushBackStack: false,
reload: false,
resize: true,
});
} else { } else {
editors.forEach(item => { editors.forEach(item => {
if (!item.element.classList.contains("fn__none")) { if (!item.element.classList.contains("fn__none")) {
setPanelFocus(item.parent.parent.headersElement.parentElement.parentElement); setPanelFocus(item.parent.parent.headersElement.parentElement.parentElement);
updatePanelByEditor(item.editor.protyle, true, true); updatePanelByEditor({
protyle: item.editor.protyle,
focus: true,
pushBackStack: true,
reload: false,
resize: true,
});
return; return;
} }
}); });
@ -719,7 +743,7 @@ export class Wnd {
item.headElement.setAttribute("style", "max-width: 0px;"); item.headElement.setAttribute("style", "max-width: 0px;");
setTimeout(() => { setTimeout(() => {
item.headElement.remove(); item.headElement.remove();
}, Constants.TIMEOUT_TRANSITION); }, 200);
} else { } else {
item.headElement.remove(); item.headElement.remove();
} }

View file

@ -641,6 +641,6 @@ export class Backlink extends Model {
setTimeout(() => { setTimeout(() => {
this.tree.element.scrollTop = this.status[this.blockId].scrollTop; this.tree.element.scrollTop = this.status[this.blockId].scrollTop;
this.mTree.element.scrollTop = this.status[this.blockId].mScrollTop; this.mTree.element.scrollTop = this.status[this.blockId].mScrollTop;
}, Constants.TIMEOUT_BLOCKLOAD); }, Constants.TIMEOUT_LOAD);
} }
} }

View file

@ -400,7 +400,7 @@ export const JSONToLayout = (app: App, isStart: boolean) => {
action: idZoomIn.isZoomIn ? [Constants.CB_GET_ALL, Constants.CB_GET_FOCUS] : [Constants.CB_GET_FOCUS, Constants.CB_GET_CONTEXT], action: idZoomIn.isZoomIn ? [Constants.CB_GET_ALL, Constants.CB_GET_FOCUS] : [Constants.CB_GET_FOCUS, Constants.CB_GET_CONTEXT],
zoomIn: idZoomIn.isZoomIn zoomIn: idZoomIn.isZoomIn
}); });
}, Constants.TIMEOUT_BLOCKLOAD); }, Constants.TIMEOUT_LOAD);
} }
}; };

View file

@ -242,7 +242,7 @@ const updateSearchResult = (config: ISearchOption, element: Element) => {
} }
}); });
} }
}, Constants.TIMEOUT_SEARCH); }, Constants.TIMEOUT_INPUT);
}; };
const initSearchEvent = (element: Element, config: ISearchOption) => { const initSearchEvent = (element: Element, config: ISearchOption) => {

View file

@ -69,7 +69,7 @@ export const exportImage = (id: string) => {
}); });
}); });
}); });
}, Constants.TIMEOUT_TRANSITION); }, Constants.TIMEOUT_LOAD);
}); });
const previewElement = exportDialog.element.querySelector("#preview") as HTMLElement; const previewElement = exportDialog.element.querySelector("#preview") as HTMLElement;
const foldElement = (exportDialog.element.querySelector("#keepFold") as HTMLInputElement); const foldElement = (exportDialog.element.querySelector("#keepFold") as HTMLInputElement);

View file

@ -60,7 +60,13 @@ export class Title {
this.editElement.addEventListener("click", () => { this.editElement.addEventListener("click", () => {
if (protyle.model) { if (protyle.model) {
setPanelFocus(protyle.model.element.parentElement.parentElement); setPanelFocus(protyle.model.element.parentElement.parentElement);
updatePanelByEditor(protyle, false); updatePanelByEditor({
protyle: protyle,
focus: false,
pushBackStack: false,
reload: false,
resize: false,
});
} }
protyle.toolbar?.element.classList.add("fn__none"); protyle.toolbar?.element.classList.add("fn__none");
}); });

View file

@ -116,7 +116,13 @@ export class Protyle {
/// #if !MOBILE /// #if !MOBILE
if (data.cmd === "heading2doc") { if (data.cmd === "heading2doc") {
// 文档标题互转后,需更新大纲 // 文档标题互转后,需更新大纲
updatePanelByEditor(this.protyle, false, false, true); updatePanelByEditor({
protyle: this.protyle,
focus: false,
pushBackStack: false,
reload: true,
resize: false
});
} }
/// #endif /// #endif
} }
@ -200,7 +206,13 @@ export class Protyle {
if (mergedOptions.action?.includes(Constants.CB_GET_FOCUS)) { if (mergedOptions.action?.includes(Constants.CB_GET_FOCUS)) {
setPanelFocus(this.protyle.model.element.parentElement.parentElement); setPanelFocus(this.protyle.model.element.parentElement.parentElement);
} }
updatePanelByEditor(this.protyle, false); updatePanelByEditor({
protyle: this.protyle,
focus: false,
pushBackStack: false,
reload: false,
resize: false
});
/// #endif /// #endif
} }
@ -217,7 +229,13 @@ export class Protyle {
return; return;
} }
setPanelFocus(this.protyle.model.element.parentElement.parentElement); setPanelFocus(this.protyle.model.element.parentElement.parentElement);
updatePanelByEditor(this.protyle, false); updatePanelByEditor({
protyle: this.protyle,
focus: false,
pushBackStack: false,
reload: false,
resize: false,
});
} else { } else {
// 悬浮层应移除其余面板高亮,否则按键会被面板监听到 // 悬浮层应移除其余面板高亮,否则按键会被面板监听到
document.querySelectorAll(".layout__tab--active").forEach(item => { document.querySelectorAll(".layout__tab--active").forEach(item => {

View file

@ -73,7 +73,7 @@ const initMermaid = (mermaidElements: Element[]) => {
renderElement.textContent = Lute.UnEscapeHTMLStr(item.getAttribute("data-content")); renderElement.textContent = Lute.UnEscapeHTMLStr(item.getAttribute("data-content"));
setTimeout(() => { setTimeout(() => {
mermaid.init(undefined, renderElement); mermaid.init(undefined, renderElement);
}, Constants.TIMEOUT_BLOCKLOAD * index); }, Constants.TIMEOUT_LOAD * index);
item.setAttribute("data-render", "true"); item.setAttribute("data-render", "true");
renderElement.setAttribute("contenteditable", "false"); renderElement.setAttribute("contenteditable", "false");
if (!item.textContent.endsWith(Constants.ZWSP)) { if (!item.textContent.endsWith(Constants.ZWSP)) {

View file

@ -55,7 +55,7 @@ export const scrollEvent = (protyle: IProtyle, element: HTMLElement) => {
inputElement.value = response.data; inputElement.value = response.data;
protyle.scroll.element.setAttribute("aria-label", `Blocks ${response.data}/${protyle.block.blockCount}`); protyle.scroll.element.setAttribute("aria-label", `Blocks ${response.data}/${protyle.block.blockCount}`);
}); });
}, Constants.TIMEOUT_BLOCKLOAD); }, Constants.TIMEOUT_LOAD);
} }
if (protyle.wysiwyg.element.getAttribute("data-top") || protyle.block.showAll || if (protyle.wysiwyg.element.getAttribute("data-top") || protyle.block.showAll ||
(protyle.scroll && protyle.scroll.element.classList.contains("fn__none")) || !protyle.scroll || (protyle.scroll && protyle.scroll.element.classList.contains("fn__none")) || !protyle.scroll ||

View file

@ -988,7 +988,7 @@ export class Toolbar {
} }
setTimeout(() => { setTimeout(() => {
addScript("stage/protyle/js/html2canvas.min.js?v=1.4.1", "protyleHtml2canvas").then(() => { addScript("stage/protyle/js/html2canvas.min.js?v=1.4.1", "protyleHtml2canvas").then(() => {
window.html2canvas(renderElement).then((canvas) => { window.html2canvas(renderElement, {useCORS: true}).then((canvas) => {
canvas.toBlob((blob: Blob) => { canvas.toBlob((blob: Blob) => {
const formData = new FormData(); const formData = new FormData();
formData.append("file", blob); formData.append("file", blob);
@ -1000,7 +1000,7 @@ export class Toolbar {
}); });
}); });
}); });
}, Constants.TIMEOUT_TRANSITION); }, Constants.TIMEOUT_LOAD);
}; };
headerElement.addEventListener("mousedown", (event: MouseEvent) => { headerElement.addEventListener("mousedown", (event: MouseEvent) => {
if (hasClosestByClassName(event.target as HTMLElement, "block__icon")) { if (hasClosestByClassName(event.target as HTMLElement, "block__icon")) {

View file

@ -78,7 +78,7 @@ export const addLoading = (protyle: IProtyle) => {
if (protyle.element.getAttribute("data-loading") !== "finished") { if (protyle.element.getAttribute("data-loading") !== "finished") {
protyle.element.insertAdjacentHTML("beforeend", '<div style="background-color: var(--b3-theme-background)" class="fn__loading wysiwygLoading"><img width="48px" src="/stage/loading-pure.svg"></div>'); protyle.element.insertAdjacentHTML("beforeend", '<div style="background-color: var(--b3-theme-background)" class="fn__loading wysiwygLoading"><img width="48px" src="/stage/loading-pure.svg"></div>');
} }
}, Constants.TIMEOUT_BLOCKLOAD); }, Constants.TIMEOUT_LOAD);
}; };
export const removeLoading = (protyle: IProtyle) => { export const removeLoading = (protyle: IProtyle) => {

View file

@ -855,13 +855,19 @@ export const dropEvent = (protyle: IProtyle, editorElement: HTMLElement) => {
onGet(getResponse, protyle); onGet(getResponse, protyle);
/// #if !MOBILE /// #if !MOBILE
// 文档标题互转后,需更新大纲 // 文档标题互转后,需更新大纲
updatePanelByEditor(protyle, false, false, true); updatePanelByEditor({
protyle,
focus: false,
pushBackStack: false,
reload: true,
resize: false,
});
/// #endif /// #endif
// 文档标题互转后,编辑区会跳转到开头 https://github.com/siyuan-note/siyuan/issues/2939 // 文档标题互转后,编辑区会跳转到开头 https://github.com/siyuan-note/siyuan/issues/2939
setTimeout(() => { setTimeout(() => {
protyle.contentElement.scrollTop = scrollTop; protyle.contentElement.scrollTop = scrollTop;
protyle.scroll.lastScrollTop = scrollTop - 1; protyle.scroll.lastScrollTop = scrollTop - 1;
}, Constants.TIMEOUT_BLOCKLOAD); }, Constants.TIMEOUT_LOAD);
}); });
targetElement.classList.remove("dragover__bottom", "dragover__top"); targetElement.classList.remove("dragover__bottom", "dragover__top");
} else if (!window.siyuan.dragElement && (event.dataTransfer.types[0] === "Files" || event.dataTransfer.types.includes("text/html"))) { } else if (!window.siyuan.dragElement && (event.dataTransfer.types[0] === "Files" || event.dataTransfer.types.includes("text/html"))) {

View file

@ -51,7 +51,7 @@ export const insertHTML = (html: string, protyle: IProtyle, isBlock = false,
updateTransaction(protyle, id, blockElement.outerHTML, oldHTML); updateTransaction(protyle, id, blockElement.outerHTML, oldHTML);
setTimeout(() => { setTimeout(() => {
scrollCenter(protyle, blockElement, false, "smooth"); scrollCenter(protyle, blockElement, false, "smooth");
}, Constants.TIMEOUT_BLOCKLOAD); }, Constants.TIMEOUT_LOAD);
return; return;
} }

View file

@ -220,7 +220,7 @@ const setHTML = (options: {
// 减少抖动 https://ld246.com/article/1654263598088 // 减少抖动 https://ld246.com/article/1654263598088
setTimeout(() => { setTimeout(() => {
focusElement.scrollIntoView(); focusElement.scrollIntoView();
}, Constants.TIMEOUT_BLOCKLOAD); }, Constants.TIMEOUT_LOAD);
} else { } else {
focusBlock(protyle.wysiwyg.element.firstElementChild); focusBlock(protyle.wysiwyg.element.firstElementChild);
/// #if !MOBILE /// #if !MOBILE

View file

@ -1,33 +1,36 @@
import {hideElements} from "../ui/hideElements"; import {hideElements} from "../ui/hideElements";
import {setPadding} from "../ui/initUI"; import {setPadding} from "../ui/initUI";
import {hasClosestBlock} from "./hasClosest"; import {hasClosestBlock} from "./hasClosest";
import {Constants} from "../../constants";
export const resize = (protyle: IProtyle) => { export const resize = (protyle: IProtyle) => {
hideElements(["gutter"], protyle); hideElements(["gutter"], protyle);
setPadding(protyle); setPadding(protyle);
if (typeof echarts !== "undefined") { setTimeout(() => {
protyle.wysiwyg.element.querySelectorAll('[data-subtype="echarts"], [data-subtype="mindmap"]').forEach((chartItem: HTMLElement) => { if (typeof echarts !== "undefined") {
const chartInstance = echarts.getInstanceById(chartItem.firstElementChild.nextElementSibling.getAttribute("_echarts_instance_")); protyle.wysiwyg.element.querySelectorAll('[data-subtype="echarts"], [data-subtype="mindmap"]').forEach((chartItem: HTMLElement) => {
if (chartInstance) { const chartInstance = echarts.getInstanceById(chartItem.firstElementChild.nextElementSibling.getAttribute("_echarts_instance_"));
chartInstance.resize(); if (chartInstance) {
chartInstance.resize();
}
});
}
// 保持光标位置不变 https://ld246.com/article/1673704873983/comment/1673765814595#comments
if (protyle.toolbar.range) {
let rangeRect = protyle.toolbar.range.getBoundingClientRect();
if (rangeRect.height === 0) {
const blockElement = hasClosestBlock(protyle.toolbar.range.startContainer);
if (blockElement) {
rangeRect = blockElement.getBoundingClientRect();
}
} }
}); if (rangeRect.height === 0) {
} return;
// 保持光标位置不变 https://ld246.com/article/1673704873983/comment/1673765814595#comments }
if (protyle.toolbar.range) { const protyleRect = protyle.element.getBoundingClientRect();
let rangeRect = protyle.toolbar.range.getBoundingClientRect(); if (protyleRect.top + 30 > rangeRect.top || protyleRect.bottom < rangeRect.bottom) {
if (rangeRect.height === 0) { protyle.toolbar.range.startContainer.parentElement.scrollIntoView(protyleRect.top > rangeRect.top);
const blockElement = hasClosestBlock(protyle.toolbar.range.startContainer);
if (blockElement) {
rangeRect = blockElement.getBoundingClientRect();
} }
} }
if (rangeRect.height === 0) { }, Constants.TIMEOUT_TRANSITION); // 等待 setPadding 动画结束
return;
}
const protyleRect = protyle.element.getBoundingClientRect();
if (protyleRect.top + 30 > rangeRect.top || protyleRect.bottom < rangeRect.bottom) {
protyle.toolbar.range.startContainer.parentElement.scrollIntoView(protyleRect.top > rangeRect.top);
}
}
}; };

View file

@ -1054,7 +1054,7 @@ const inputEvent = (element: Element, config: ISearchOption, inputTimeout: numbe
loadingElement.classList.add("fn__none"); loadingElement.classList.add("fn__none");
}); });
} }
}, Constants.TIMEOUT_SEARCH); }, Constants.TIMEOUT_INPUT);
return inputTimeout; return inputTimeout;
}; };

View file

@ -151,7 +151,7 @@ const focusStack = async (stack: IBackStack) => {
setTimeout(() => { setTimeout(() => {
// 图片、视频等加载完成后再定位 // 图片、视频等加载完成后再定位
scrollCenter(stack.protyle, blockElement, true); scrollCenter(stack.protyle, blockElement, true);
}, Constants.TIMEOUT_BLOCKLOAD); }, Constants.TIMEOUT_LOAD);
}); });
return true; return true;
} }