An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
import {revealTabsForTarget} from "../protyle/render/tabsRender";
import {resolveVisibleListMindmapBlock} from "../protyle/render/listMindmap/render";
import {hasClosestBlock, isInEmbedBlock} from "../protyle/util/hasClosest";
import {getEditorRange} from "../protyle/util/selection";
import {focusByRange} from "../protyle/util/selectionOffsets";
import {getStartScrollTop} from "./highlightPosition";
import {isMobile} from "./functions";
import {isUndoViewportPreserved} from "../protyle/undo/viewport";
export const bgFade = (element: Element) => {
element.classList.add("protyle-wysiwyg--hl");
setTimeout(function () {
element.classList.remove("protyle-wysiwyg--hl");
}, 1024);
};
export const highlightById = (protyle: IProtyle, id: string, position: ScrollLogicalPosition = "nearest") => {
let nodeElement: HTMLElement;
const protyleElement = protyle.wysiwyg.element;
if (!protyle.preview.element.classList.contains("fn__none")) {
// 预览定位
nodeElement = document.getElementById(id);
if (nodeElement) {
revealTabsForTarget(nodeElement, false);
protyle.preview.element.scrollTop = nodeElement.offsetTop;
bgFade(nodeElement);
}
return undefined;
}
Array.from(protyleElement.querySelectorAll(`[data-node-id="${id}"]`)).find((item: HTMLElement) => {
if (!isInEmbedBlock(item)) {
nodeElement = item;
return true;
}
});
if (nodeElement) {
const visible = resolveVisibleListMindmapBlock(nodeElement);
if (visible === null) {
return undefined;
}
if (visible) {
visible.reveal();
scrollCenter(protyle, visible.scrollElement, position);
bgFade(visible.carrier);
return visible.carrier;
}
scrollCenter(protyle, nodeElement, position);
bgFade(nodeElement);
return nodeElement;// 仅配合前进后退使用
}
if (id === protyle.block.rootID && protyle.options.render.title && protyle.title.editElement) {
bgFade(protyle.title.editElement);
return protyle.title.editElement;
}
};
export const scrollCenter = (
protyle: IProtyle,
nodeElement?: Element,
position: ScrollLogicalPosition = "nearest",
behavior: ScrollBehavior = "auto"
) => {
if (isUndoViewportPreserved(protyle.wysiwyg.element)) {
return;
}
if (nodeElement instanceof HTMLElement) {
const visible = resolveVisibleListMindmapBlock(nodeElement);
if (visible === null) {
return;
}
if (visible) {
visible.reveal();
nodeElement = visible.scrollElement;
}
}
const cellEditor = protyle.wysiwyg.element.closest(".table__cell-editor");
const cellScroll = cellEditor?.parentElement.closest<HTMLElement>(".protyle-content");
if (cellScroll && position === "nearest") {
// 单元格片段没有独立视口,只在光标离开所属文档的可视区域时滚动。
const selection = getSelection();
const range = selection.rangeCount ? selection.getRangeAt(0) : undefined;
const target = nodeElement || (range && hasClosestBlock(range.startContainer));
const caretRect = !nodeElement && range?.getBoundingClientRect();
const rect = caretRect && caretRect.height > 0 ? caretRect : target && target.getBoundingClientRect();
if (rect) {
const viewport = cellScroll.getBoundingClientRect();
const offset = rect.top < viewport.top ? rect.top - viewport.top :
rect.bottom > viewport.bottom - 16 ? rect.bottom - viewport.bottom + 16 : 0;
if (offset) {
cellScroll.scroll({top: cellScroll.scrollTop + offset, behavior});
}
}
return;
}
if (nodeElement) {
revealTabsForTarget(nodeElement);
}
if (!protyle.disabled && !nodeElement && getSelection().rangeCount > 0) {
const range = getSelection().getRangeAt(0);
const blockElement = hasClosestBlock(range.startContainer);
if (blockElement) {
// https://github.com/siyuan-note/siyuan/issues/10769
if (blockElement.classList.contains("code-block")) {
const hljsElement = blockElement.querySelector(".hljs");
const scrollLeft = hljsElement.scrollLeft;
const brElement = document.createElement("br");
// 滚动占位使用克隆 Range,避免扩展当前光标或改变文字选区。
const scrollRange = range.cloneRange();
scrollRange.insertNode(brElement);
brElement.scrollIntoView({block: position, behavior});
brElement.remove();
hljsElement.scrollLeft = scrollLeft;
return;
}
if (blockElement.classList.contains("av") && blockElement.dataset.render === "true") {
// undo 时禁止数据库滚动
if (blockElement.querySelector(".av__row--header")?.getAttribute("style")?.indexOf("transform") > -1 ||
blockElement.querySelector(".av__row--footer")?.getAttribute("style")?.indexOf("transform") > -1) {
return;
}
const activeElement = blockElement.querySelector(".av__cell--select, .av__row--select, .av__gallery-item--select");
if (activeElement) {
activeElement.scrollIntoView({block: position, behavior});
} else {
blockElement.scrollIntoView({block: position, behavior});
}
return;
}
// 撤销时 br 插入删除会导致 rang 被修改 https://github.com/siyuan-note/siyuan/issues/12679
const cloneRange = range.cloneRange();
const br2Element = document.createElement("br");
range.insertNode(br2Element);
const editorElement = protyle.contentElement;
const cursorTop = br2Element.getBoundingClientRect().top - editorElement.getBoundingClientRect().top;
// 移动端额外预留一行,避免输入文字贴近键盘工具栏。
const extraLineHeight = isMobile() ? parseFloat(getComputedStyle(br2Element).lineHeight) ||
window.siyuan.config.editor.fontSize * 1.625 : 0;
let scrollTop = 0;
if (cursorTop < 0) {
scrollTop = editorElement.scrollTop + cursorTop;
} else if (cursorTop > editorElement.clientHeight - 74) { // 74 = 移动端底部 + 段落块高度
scrollTop = editorElement.scrollTop + (cursorTop + 74 + extraLineHeight - editorElement.clientHeight);
}
if (scrollTop !== 0) {
editorElement.scroll({top: scrollTop, behavior});
}
br2Element.remove();
focusByRange(cloneRange);
return;
}
}
if (!nodeElement &&
// https://github.com/siyuan-note/siyuan/issues/11175
document.activeElement?.tagName !== "TEXTAREA" && document.activeElement?.tagName !== "INPUT") {
nodeElement = hasClosestBlock(getEditorRange(protyle.wysiwyg.element).startContainer) as HTMLElement;
}
if (!nodeElement) {
return;
}
const elementRect = nodeElement.getBoundingClientRect();
const contentRect = protyle.contentElement.getBoundingClientRect();
if (position === "start") {
const breadcrumbElement = protyle.breadcrumb?.element.parentElement;
// 使用未应用滚动平移的面包屑边界,确保顶部栏重新显示后目标块仍在可视区域内。
const offsetParentTop = breadcrumbElement?.offsetParent?.getBoundingClientRect().top ?? contentRect.top;
protyle.contentElement.scroll({
top: getStartScrollTop({
scrollTop: protyle.contentElement.scrollTop,
elementTop: elementRect.top,
contentTop: contentRect.top,
contextHeight: window.siyuan.config.editor.fontSize * 1.625 * 2 + 24,
overlay: breadcrumbElement ? {
absolute: getComputedStyle(breadcrumbElement).position === "absolute",
offsetParentTop,
offsetTop: breadcrumbElement.offsetTop,
height: breadcrumbElement.offsetHeight,
} : undefined,
}),
behavior
});
return;
}
if (position === "nearest") {
// 在可视区域内不进行滚动
if (elementRect.bottom < contentRect.top) {
protyle.contentElement.scroll({
top: protyle.contentElement.scrollTop + elementRect.top - contentRect.top,
behavior
});
} else if (elementRect.top > contentRect.bottom) {
protyle.contentElement.scroll({
top: elementRect.height > contentRect.height ? protyle.contentElement.scrollTop + elementRect.top - contentRect.top :
protyle.contentElement.scrollTop + elementRect.bottom - contentRect.bottom,
behavior
});
}
return;
}
if (position === "center") {
protyle.contentElement.scroll({
top: protyle.contentElement.scrollTop + elementRect.top - (contentRect.top + contentRect.height / 2),
behavior
});
}
};