2022-05-26 15:18:53 +08:00
|
|
|
import {Tab} from "../layout/Tab";
|
2022-10-02 20:56:48 +08:00
|
|
|
import {Protyle} from "../protyle";
|
2022-05-26 15:18:53 +08:00
|
|
|
import {Model} from "../layout/Model";
|
|
|
|
|
import {setPadding} from "../protyle/ui/initUI";
|
2023-05-07 09:55:35 +08:00
|
|
|
/// #if !BROWSER
|
|
|
|
|
import {setModelsHash} from "../window/setHeader";
|
|
|
|
|
/// #endif
|
2022-10-05 10:12:21 +08:00
|
|
|
import {countBlockWord} from "../layout/status";
|
2023-05-18 19:27:21 +08:00
|
|
|
import {App} from "../index";
|
2024-02-18 18:27:09 +08:00
|
|
|
import {fullscreen} from "../protyle/breadcrumb/action";
|
2025-10-24 13:08:00 +08:00
|
|
|
import {fetchPost} from "../util/fetch";
|
2022-05-26 15:18:53 +08:00
|
|
|
|
|
|
|
|
export class Editor extends Model {
|
|
|
|
|
public element: HTMLElement;
|
|
|
|
|
public editor: Protyle;
|
|
|
|
|
public headElement: HTMLElement;
|
|
|
|
|
|
|
|
|
|
constructor(options: {
|
2023-05-18 19:27:21 +08:00
|
|
|
app: App,
|
2022-05-26 15:18:53 +08:00
|
|
|
tab: Tab,
|
|
|
|
|
blockId: string,
|
2023-12-09 22:58:33 +08:00
|
|
|
rootId: string,
|
2022-05-26 15:18:53 +08:00
|
|
|
mode?: TEditorMode,
|
2024-10-22 18:13:21 +08:00
|
|
|
action?: TProtyleAction[],
|
2025-04-07 20:47:48 +08:00
|
|
|
afterInitProtyle?: (editor: Protyle) => void,
|
2025-11-22 21:05:10 +08:00
|
|
|
scrollPosition?: ScrollLogicalPosition
|
2022-05-26 15:18:53 +08:00
|
|
|
}) {
|
|
|
|
|
super({
|
2023-05-18 19:27:21 +08:00
|
|
|
app: options.app,
|
2022-05-26 15:18:53 +08:00
|
|
|
id: options.tab.id,
|
|
|
|
|
});
|
|
|
|
|
if (window.siyuan.config.fileTree.openFilesUseCurrentTab) {
|
|
|
|
|
options.tab.headElement.classList.add("item--unupdate");
|
|
|
|
|
}
|
|
|
|
|
this.headElement = options.tab.headElement;
|
|
|
|
|
this.element = options.tab.panelElement;
|
|
|
|
|
this.initProtyle(options);
|
2025-10-24 13:08:00 +08:00
|
|
|
// 当文档第一次加载到页签时更新 openAt 时间
|
|
|
|
|
fetchPost("/api/storage/updateRecentDocOpenTime", {rootID: options.rootId});
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
|
|
|
|
|
2022-09-01 12:21:50 +08:00
|
|
|
private initProtyle(options: {
|
2022-05-26 15:18:53 +08:00
|
|
|
blockId: string,
|
2024-10-22 18:13:21 +08:00
|
|
|
action?: TProtyleAction[]
|
2023-12-09 22:58:33 +08:00
|
|
|
rootId: string,
|
2022-05-26 15:18:53 +08:00
|
|
|
mode?: TEditorMode,
|
2025-11-22 21:05:10 +08:00
|
|
|
scrollPosition?: ScrollLogicalPosition,
|
2025-04-07 20:47:48 +08:00
|
|
|
afterInitProtyle?: (editor: Protyle) => void,
|
2022-05-26 15:18:53 +08:00
|
|
|
}) {
|
2023-05-18 19:27:21 +08:00
|
|
|
this.editor = new Protyle(this.app, this.element, {
|
2022-09-01 12:21:50 +08:00
|
|
|
action: options.action || [],
|
2022-05-26 15:18:53 +08:00
|
|
|
blockId: options.blockId,
|
2023-12-09 12:09:46 +08:00
|
|
|
rootId: options.rootId,
|
2022-05-26 15:18:53 +08:00
|
|
|
mode: options.mode,
|
|
|
|
|
render: {
|
|
|
|
|
title: true,
|
|
|
|
|
background: true,
|
|
|
|
|
scroll: true,
|
|
|
|
|
},
|
|
|
|
|
typewriterMode: true,
|
2025-11-22 21:05:10 +08:00
|
|
|
scrollPosition: options.scrollPosition,
|
2022-05-26 15:18:53 +08:00
|
|
|
after: (editor) => {
|
|
|
|
|
if (window.siyuan.editorIsFullscreen) {
|
2024-02-18 18:27:09 +08:00
|
|
|
fullscreen(editor.protyle.element);
|
2022-05-26 15:18:53 +08:00
|
|
|
setPadding(editor.protyle);
|
|
|
|
|
}
|
2022-10-05 10:12:21 +08:00
|
|
|
countBlockWord([], editor.protyle.block.rootID);
|
2023-05-07 09:55:35 +08:00
|
|
|
/// #if !BROWSER
|
|
|
|
|
setModelsHash();
|
|
|
|
|
/// #endif
|
2025-04-04 18:33:24 +08:00
|
|
|
if (options.afterInitProtyle) {
|
|
|
|
|
options.afterInitProtyle(editor);
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
},
|
|
|
|
|
});
|
2022-06-27 22:32:15 +08:00
|
|
|
// 需在 after 回调之前,否则不会聚焦 https://github.com/siyuan-note/siyuan/issues/5303
|
2022-06-27 22:20:39 +08:00
|
|
|
this.editor.protyle.model = this;
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
|
|
|
|
}
|