Vanessa 2025-11-22 21:05:10 +08:00
parent 67723aab28
commit c0a3635809
7 changed files with 31 additions and 14 deletions

View file

@ -23,6 +23,7 @@ export class Editor extends Model {
mode?: TEditorMode, mode?: TEditorMode,
action?: TProtyleAction[], action?: TProtyleAction[],
afterInitProtyle?: (editor: Protyle) => void, afterInitProtyle?: (editor: Protyle) => void,
scrollPosition?: ScrollLogicalPosition
}) { }) {
super({ super({
app: options.app, app: options.app,
@ -43,6 +44,7 @@ export class Editor extends Model {
action?: TProtyleAction[] action?: TProtyleAction[]
rootId: string, rootId: string,
mode?: TEditorMode, mode?: TEditorMode,
scrollPosition?: ScrollLogicalPosition,
afterInitProtyle?: (editor: Protyle) => void, afterInitProtyle?: (editor: Protyle) => void,
}) { }) {
this.editor = new Protyle(this.app, this.element, { this.editor = new Protyle(this.app, this.element, {
@ -56,6 +58,7 @@ export class Editor extends Model {
scroll: true, scroll: true,
}, },
typewriterMode: true, typewriterMode: true,
scrollPosition: options.scrollPosition,
after: (editor) => { after: (editor) => {
if (window.siyuan.editorIsFullscreen) { if (window.siyuan.editorIsFullscreen) {
fullscreen(editor.protyle.element); fullscreen(editor.protyle.element);

View file

@ -47,7 +47,7 @@ export const openFileById = async (options: {
removeCurrentTab?: boolean removeCurrentTab?: boolean
openNewTab?: boolean openNewTab?: boolean
afterOpen?: (model: Model) => void, afterOpen?: (model: Model) => void,
scrollPositon?: ScrollLogicalPosition scrollPosition?: ScrollLogicalPosition
}) => { }) => {
const response = await fetchSyncPost("/api/block/getBlockInfo", {id: options.id}); const response = await fetchSyncPost("/api/block/getBlockInfo", {id: options.id});
if (response.code === -1) { if (response.code === -1) {
@ -72,7 +72,7 @@ export const openFileById = async (options: {
removeCurrentTab: options.removeCurrentTab, removeCurrentTab: options.removeCurrentTab,
afterOpen: options.afterOpen, afterOpen: options.afterOpen,
openNewTab: options.openNewTab, openNewTab: options.openNewTab,
scrollPositon: options.scrollPositon, scrollPosition: options.scrollPosition,
}); });
}; };
@ -378,7 +378,12 @@ const switchEditor = (editor: Editor, options: IOpenFileOptions, allModels: IMod
mode: (options.action && options.action.includes(Constants.CB_GET_CONTEXT)) ? 3 : 0, mode: (options.action && options.action.includes(Constants.CB_GET_CONTEXT)) ? 3 : 0,
size: window.siyuan.config.editor.dynamicLoadBlocks, size: window.siyuan.config.editor.dynamicLoadBlocks,
}, getResponse => { }, getResponse => {
onGet({data: getResponse, protyle: editor.editor.protyle, action: options.action}); onGet({
data: getResponse,
protyle: editor.editor.protyle,
action: options.action,
scrollPosition: options.scrollPosition
});
// 大纲点击折叠标题下的内容时,需更新反链面板 // 大纲点击折叠标题下的内容时,需更新反链面板
updateBacklinkGraph(allModels, editor.editor.protyle); updateBacklinkGraph(allModels, editor.editor.protyle);
}); });
@ -388,13 +393,14 @@ const switchEditor = (editor: Editor, options: IOpenFileOptions, allModels: IMod
editor.editor.protyle.observerLoad?.disconnect(); editor.editor.protyle.observerLoad?.disconnect();
if (options.action?.includes(Constants.CB_GET_HL)) { if (options.action?.includes(Constants.CB_GET_HL)) {
highlightById(editor.editor.protyle, options.id, "start"); highlightById(editor.editor.protyle, options.id, "start");
} else if (options.action?.includes(Constants.CB_GET_FOCUS)) { }
if (options.action?.includes(Constants.CB_GET_FOCUS)) {
if (nodeElement) { if (nodeElement) {
const newRange = focusBlock(nodeElement, undefined, !options.action?.includes(Constants.CB_GET_OUTLINE)); const newRange = focusBlock(nodeElement, undefined, !options.action?.includes(Constants.CB_GET_OUTLINE));
if (newRange) { if (newRange) {
editor.editor.protyle.toolbar.range = newRange; editor.editor.protyle.toolbar.range = newRange;
} }
scrollCenter(editor.editor.protyle, (editor.editor.protyle.disabled || options.scrollPositon) ? nodeElement : null, options.scrollPositon); scrollCenter(editor.editor.protyle, (editor.editor.protyle.disabled || options.scrollPosition) ? nodeElement : null, options.scrollPosition);
editor.editor.protyle.observerLoad = new ResizeObserver(() => { editor.editor.protyle.observerLoad = new ResizeObserver(() => {
if (document.contains(nodeElement)) { if (document.contains(nodeElement)) {
scrollCenter(editor.editor.protyle); scrollCenter(editor.editor.protyle);
@ -506,6 +512,7 @@ const newTab = (options: IOpenFileOptions) => {
blockId: options.id, blockId: options.id,
rootId: options.rootID, rootId: options.rootID,
action: [Constants.CB_GET_ALL, Constants.CB_GET_FOCUS], action: [Constants.CB_GET_ALL, Constants.CB_GET_FOCUS],
scrollPosition: options.scrollPosition,
}); });
} else { } else {
editor = new Editor({ editor = new Editor({
@ -515,6 +522,7 @@ const newTab = (options: IOpenFileOptions) => {
rootId: options.rootID, rootId: options.rootID,
mode: options.mode, mode: options.mode,
action: options.action, action: options.action,
scrollPosition: options.scrollPosition,
}); });
} }
tab.addModel(editor); tab.addModel(editor);

View file

@ -177,7 +177,7 @@ export class Outline extends Model {
openFileById({ openFileById({
app: options.app, app: options.app,
id, id,
scrollPositon: "start", scrollPosition: "start",
action: zoomIn ? [Constants.CB_GET_FOCUS, Constants.CB_GET_ALL, Constants.CB_GET_HTML, Constants.CB_GET_OUTLINE] : [Constants.CB_GET_FOCUS, Constants.CB_GET_OUTLINE, Constants.CB_GET_SETID, Constants.CB_GET_CONTEXT, Constants.CB_GET_HTML], action: zoomIn ? [Constants.CB_GET_FOCUS, Constants.CB_GET_ALL, Constants.CB_GET_HTML, Constants.CB_GET_OUTLINE] : [Constants.CB_GET_FOCUS, Constants.CB_GET_OUTLINE, Constants.CB_GET_SETID, Constants.CB_GET_CONTEXT, Constants.CB_GET_HTML],
}); });
}); });
@ -931,7 +931,7 @@ export class Outline extends Model {
openFileById({ openFileById({
app: this.app, app: this.app,
id, id,
scrollPositon: "start", scrollPosition: "start",
action: zoomIn ? [Constants.CB_GET_FOCUS, Constants.CB_GET_ALL, Constants.CB_GET_HTML, Constants.CB_GET_OUTLINE] : [Constants.CB_GET_FOCUS, Constants.CB_GET_OUTLINE, Constants.CB_GET_SETID, Constants.CB_GET_CONTEXT, Constants.CB_GET_HTML], action: zoomIn ? [Constants.CB_GET_FOCUS, Constants.CB_GET_ALL, Constants.CB_GET_HTML, Constants.CB_GET_OUTLINE] : [Constants.CB_GET_FOCUS, Constants.CB_GET_OUTLINE, Constants.CB_GET_SETID, Constants.CB_GET_CONTEXT, Constants.CB_GET_HTML],
}); });
}); });

View file

@ -347,6 +347,7 @@ export class Protyle {
data: getResponse, data: getResponse,
protyle: this.protyle, protyle: this.protyle,
action: mergedOptions.action, action: mergedOptions.action,
scrollPosition: mergedOptions.scrollPosition,
afterCB: () => { afterCB: () => {
this.afterOnGet(mergedOptions); this.afterOnGet(mergedOptions);
} }

View file

@ -26,7 +26,8 @@ export const onGet = (options: {
protyle: IProtyle, protyle: IProtyle,
action?: TProtyleAction[], action?: TProtyleAction[],
scrollAttr?: IScrollAttr scrollAttr?: IScrollAttr
updateReadonly?: boolean updateReadonly?: boolean,
scrollPosition?: ScrollLogicalPosition,
afterCB?: () => void afterCB?: () => void
}) => { }) => {
if (!options.action) { if (!options.action) {
@ -96,6 +97,7 @@ export const onGet = (options: {
updateReadonly: options.updateReadonly, updateReadonly: options.updateReadonly,
isSyncing: options.data.data.isSyncing, isSyncing: options.data.data.isSyncing,
afterCB: options.afterCB, afterCB: options.afterCB,
scrollPosition: options.scrollPosition
}, options.protyle); }, options.protyle);
removeLoading(options.protyle); removeLoading(options.protyle);
return; return;
@ -122,6 +124,7 @@ export const onGet = (options: {
updateReadonly: options.updateReadonly, updateReadonly: options.updateReadonly,
isSyncing: options.data.data.isSyncing, isSyncing: options.data.data.isSyncing,
afterCB: options.afterCB, afterCB: options.afterCB,
scrollPosition: options.scrollPosition
}, options.protyle); }, options.protyle);
removeLoading(options.protyle); removeLoading(options.protyle);
}); });
@ -133,7 +136,8 @@ const setHTML = (options: {
isSyncing: boolean, isSyncing: boolean,
expand: boolean, expand: boolean,
updateReadonly?: boolean, updateReadonly?: boolean,
scrollAttr?: IScrollAttr scrollAttr?: IScrollAttr,
scrollPosition?: ScrollLogicalPosition,
afterCB?: () => void afterCB?: () => void
}, protyle: IProtyle) => { }, protyle: IProtyle) => {
if (protyle.contentElement.classList.contains("fn__none") && protyle.wysiwyg.element.innerHTML !== "") { if (protyle.contentElement.classList.contains("fn__none") && protyle.wysiwyg.element.innerHTML !== "") {
@ -256,7 +260,7 @@ const setHTML = (options: {
} }
} }
focusElementById(protyle, options.action, options.scrollAttr); focusElementById(protyle, options.action, options.scrollAttr, options.scrollPosition);
if (options.action.includes(Constants.CB_GET_SETID)) { if (options.action.includes(Constants.CB_GET_SETID)) {
// 点击大纲后,如果需要动态加载,在定位后,需要重置 block.id https://github.com/siyuan-note/siyuan/issues/4487 // 点击大纲后,如果需要动态加载,在定位后,需要重置 block.id https://github.com/siyuan-note/siyuan/issues/4487
@ -448,7 +452,7 @@ export const enableProtyle = (protyle: IProtyle) => {
hideTooltip(); hideTooltip();
}; };
const focusElementById = (protyle: IProtyle, action: string[], scrollAttr?: IScrollAttr) => { const focusElementById = (protyle: IProtyle, action: string[], scrollAttr?: IScrollAttr, scrollPosition?: ScrollLogicalPosition) => {
let focusElement: Element; let focusElement: Element;
if (scrollAttr && scrollAttr.focusId) { if (scrollAttr && scrollAttr.focusId) {
focusElement = protyle.wysiwyg.element.querySelector(`[data-node-id="${scrollAttr.focusId}"]`); focusElement = protyle.wysiwyg.element.querySelector(`[data-node-id="${scrollAttr.focusId}"]`);
@ -491,7 +495,7 @@ const focusElementById = (protyle: IProtyle, action: string[], scrollAttr?: IScr
protyle.observerLoad?.disconnect(); protyle.observerLoad?.disconnect();
if (action.includes(Constants.CB_GET_FOCUS) || action.includes(Constants.CB_GET_SCROLL) || action.includes(Constants.CB_GET_HL) || action.includes(Constants.CB_GET_FOCUSFIRST)) { if (action.includes(Constants.CB_GET_FOCUS) || action.includes(Constants.CB_GET_SCROLL) || action.includes(Constants.CB_GET_HL) || action.includes(Constants.CB_GET_FOCUSFIRST)) {
if (!hasScrollTop) { if (!hasScrollTop) {
scrollCenter(protyle, focusElement); scrollCenter(protyle, focusElement, scrollPosition);
} }
} else { } else {
return; return;
@ -503,7 +507,7 @@ const focusElementById = (protyle: IProtyle, action: string[], scrollAttr?: IScr
} }
if (action.includes(Constants.CB_GET_FOCUS) || action.includes(Constants.CB_GET_HL) || action.includes(Constants.CB_GET_FOCUSFIRST)) { if (action.includes(Constants.CB_GET_FOCUS) || action.includes(Constants.CB_GET_HL) || action.includes(Constants.CB_GET_FOCUSFIRST)) {
if (!hasScrollTop) { if (!hasScrollTop) {
scrollCenter(protyle, focusElement); scrollCenter(protyle, focusElement, scrollPosition);
} }
} }
}); });

View file

@ -663,7 +663,7 @@ interface IOpenFileOptions {
data: any, data: any,
}) => import("../layout/Model").Model, // plugin 0.8.3 历史兼容 }) => import("../layout/Model").Model, // plugin 0.8.3 历史兼容
} }
scrollPositon?: ScrollLogicalPosition, scrollPosition?: ScrollLogicalPosition,
assetPath?: string, // asset 必填 assetPath?: string, // asset 必填
fileName?: string, // file 必填 fileName?: string, // file 必填
rootIcon?: string, // 文档图标 rootIcon?: string, // 文档图标

View file

@ -446,6 +446,7 @@ interface IProtyleOptions {
expand: boolean expand: boolean
}[], }[],
action?: TProtyleAction[], action?: TProtyleAction[],
scrollPosition?: ScrollLogicalPosition,
mode?: TEditorMode, mode?: TEditorMode,
blockId?: string blockId?: string
rootId?: string rootId?: string