diff --git a/app/package.json b/app/package.json index 543c62ff9..730bd9299 100644 --- a/app/package.json +++ b/app/package.json @@ -16,6 +16,7 @@ "build:mobile": "webpack --mode production --config webpack.mobile.js", "build:desktop": "webpack --mode production --config webpack.desktop.js", "build:export": "webpack --mode production --config webpack.export.js", + "build:types": "tsc -d", "start": "NODE_ENV=development electron ./electron/main.js", "dist-appx": "ELECTRON_MIRROR=https://cnpmjs.org/mirrors/electron/ electron-builder --config electron-appx-builder.yml", "dist": "ELECTRON_MIRROR=https://cnpmjs.org/mirrors/electron/ electron-builder --config electron-builder.yml --publish=never", diff --git a/app/src/block/Panel.ts b/app/src/block/Panel.ts index ec10d0f32..fa0eeffb0 100644 --- a/app/src/block/Panel.ts +++ b/app/src/block/Panel.ts @@ -161,7 +161,7 @@ export class BlockPanel { if (!this.targetElement && typeof this.x === "undefined" && typeof this.y === "undefined") { return; } - const action = []; + const action: TProtyleAction[] = []; if (response.data.rootID !== this.nodeIds[index]) { action.push(Constants.CB_GET_ALL); } else { diff --git a/app/src/editor/index.ts b/app/src/editor/index.ts index aa255142d..91725b981 100644 --- a/app/src/editor/index.ts +++ b/app/src/editor/index.ts @@ -20,7 +20,7 @@ export class Editor extends Model { blockId: string, rootId: string, mode?: TEditorMode, - action?: string[], + action?: TProtyleAction[], }) { super({ app: options.app, @@ -36,7 +36,7 @@ export class Editor extends Model { private initProtyle(options: { blockId: string, - action?: string[] + action?: TProtyleAction[] rootId: string, mode?: TEditorMode, }) { diff --git a/app/src/mobile/editor.ts b/app/src/mobile/editor.ts index 47ded4d99..0d3bba28e 100644 --- a/app/src/mobile/editor.ts +++ b/app/src/mobile/editor.ts @@ -19,7 +19,7 @@ export const getCurrentEditor = () => { return window.siyuan.mobile.popEditor || window.siyuan.mobile.editor; }; -export const openMobileFileById = (app: App, id: string, action = [Constants.CB_GET_HL]) => { +export const openMobileFileById = (app: App, id: string, action: TProtyleAction[] = [Constants.CB_GET_HL]) => { window.siyuan.storage[Constants.LOCAL_DOCINFO] = {id}; setStorageVal(Constants.LOCAL_DOCINFO, window.siyuan.storage[Constants.LOCAL_DOCINFO]); const avPanelElement = document.querySelector(".av__panel"); @@ -52,7 +52,7 @@ export const openMobileFileById = (app: App, id: string, action = [Constants.CB_ showMessage(data.msg); return; } - const protyleOptions: IOptions = { + const protyleOptions: IProtyleOptions = { blockId: id, rootId: data.data.rootID, action, diff --git a/app/src/plugin/index.ts b/app/src/plugin/index.ts index 6a65cd60e..070567e3e 100644 --- a/app/src/plugin/index.ts +++ b/app/src/plugin/index.ts @@ -57,7 +57,7 @@ export class Plugin { /// #endif } } = {}; - private protyleOptionsValue: IOptions; + private protyleOptionsValue: IProtyleOptions; constructor(options: { app: App, @@ -373,7 +373,7 @@ export class Plugin { })); }; - set protyleOptions(options: IOptions) { + set protyleOptions(options: IProtyleOptions) { this.protyleOptionsValue = options; } diff --git a/app/src/protyle/index.ts b/app/src/protyle/index.ts index 81b455a65..24bfe15a0 100644 --- a/app/src/protyle/index.ts +++ b/app/src/protyle/index.ts @@ -51,9 +51,9 @@ export class Protyle { * @param id 要挂载 Protyle 的元素或者元素 ID。 * @param options Protyle 参数 */ - constructor(app: App, id: HTMLElement, options?: IOptions) { + constructor(app: App, id: HTMLElement, options?: IProtyleOptions) { this.version = Constants.SIYUAN_VERSION; - let pluginsOptions: IOptions = options; + let pluginsOptions: IProtyleOptions = options; app.plugins.forEach(item => { if (item.protyleOptions) { pluginsOptions = merge(pluginsOptions, item.protyleOptions); @@ -260,7 +260,7 @@ export class Protyle { } } - private getDoc(mergedOptions: IOptions) { + private getDoc(mergedOptions: IProtyleOptions) { fetchPost("/api/filetree/getDoc", { id: mergedOptions.blockId, isBacklink: mergedOptions.action.includes(Constants.CB_GET_BACKLINK), @@ -279,7 +279,7 @@ export class Protyle { }); } - private afterOnGet(mergedOptions: IOptions) { + private afterOnGet(mergedOptions: IProtyleOptions) { if (this.protyle.model) { /// #if !MOBILE if (mergedOptions.action?.includes(Constants.CB_GET_FOCUS) || mergedOptions.action?.includes(Constants.CB_GET_OPENNEW)) { diff --git a/app/src/protyle/scroll/saveScroll.ts b/app/src/protyle/scroll/saveScroll.ts index d81bd2ac2..e5e10f5b3 100644 --- a/app/src/protyle/scroll/saveScroll.ts +++ b/app/src/protyle/scroll/saveScroll.ts @@ -51,7 +51,7 @@ export const saveScroll = (protyle: IProtyle, getObject = false) => { export const getDocByScroll = (options: { protyle: IProtyle, scrollAttr?: IScrollAttr, - mergedOptions?: IOptions, + mergedOptions?: IProtyleOptions, cb?: () => void focus?: boolean, updateReadonly?: boolean diff --git a/app/src/protyle/util/Options.ts b/app/src/protyle/util/Options.ts index 020be99a8..98d128217 100644 --- a/app/src/protyle/util/Options.ts +++ b/app/src/protyle/util/Options.ts @@ -4,8 +4,8 @@ import {hintEmbed, hintRef, hintSlash, hintTag} from "../hint/extend"; import {isMobile} from "../../util/functions"; export class Options { - public options: IOptions; - private defaultOptions: IOptions = { + public options: IProtyleOptions; + private defaultOptions: IProtyleOptions = { mode: "wysiwyg", blockId: "", render: { @@ -132,11 +132,11 @@ export class Options { } }; - constructor(options: IOptions) { + constructor(options: IProtyleOptions) { this.options = options; } - public merge(): IOptions { + public merge(): IProtyleOptions { if (this.options) { if (this.options.toolbar) { this.options.toolbar = this.mergeToolbar(this.options.toolbar); diff --git a/app/src/types/index.d.ts b/app/src/types/index.d.ts index b098d8817..7bd5e7370 100644 --- a/app/src/types/index.d.ts +++ b/app/src/types/index.d.ts @@ -1,7 +1,6 @@ type TPluginDockPosition = "LeftTop" | "LeftBottom" | "RightTop" | "RightBottom" | "BottomLeft" | "BottomRight" type TDockPosition = "Left" | "Right" | "Bottom" type TWS = "main" | "filetree" | "protyle" -type TEditorMode = "preview" | "wysiwyg" type TOperation = "insert" | "update" @@ -428,17 +427,6 @@ interface ISiyuan { viewer?: Viewer } -interface IScrollAttr { - rootId: string, - startId: string, - endId: string - scrollTop: number, - focusId?: string, - focusStart?: number - focusEnd?: number - zoomInId?: string -} - interface IOperation { action: TOperation, // move, delete 不需要传 data id?: string, diff --git a/app/src/types/protyle.d.ts b/app/src/types/protyle.d.ts index bbf8f5a76..c6dc2ff62 100644 --- a/app/src/types/protyle.d.ts +++ b/app/src/types/protyle.d.ts @@ -16,8 +16,27 @@ type TTurnIntoOneSub = "row" | "col" type TTurnInto = "Blocks2Ps" | "Blocks2Hs" +type TEditorMode = "preview" | "wysiwyg" + type ILuteRenderCallback = (node: ILuteNode, entering: boolean) => [string, number]; +type TProtyleAction = "cb-get-append" | // 向下滚动加载 + "cb-get-before" | // 向上滚动加载 + "cb-get-unchangeid" | // 上下滚动,定位时不修改 blockid + "cb-get-hl" | // 高亮 + "cb-get-focus" | // 光标定位 + "cb-get-focusfirst" | // 动态定位到第一个块 + "cb-get-setid" | // 重置 blockid + "cb-get-all" | // 获取所有块 + "cb-get-backlink" | // 悬浮窗为传递型需展示上下文 + "cb-get-unundo" | // 不需要记录历史 + "cb-get-scroll" | // 滚动到指定位置,用于直接打开文档,必有 rootID + "cb-get-context" | // 包含上下文 + "cb-get-rootscroll" | // 如果为 rootID 就滚动到指定位置,必有 rootID + "cb-get-html" | // 直接渲染,不需要再 /api/block/getDocInfo,否则搜索表格无法定位 + "cb-get-history" | // 历史渲染 + "cb-get-opennew" // 编辑器只读后新建文件需为临时解锁状态 & https://github.com/siyuan-note/siyuan/issues/12197 + /** @link https://ld246.com/article/1588412297062 */ interface ILuteRender { renderDocument?: ILuteRenderCallback; @@ -313,6 +332,17 @@ interface IUpload { linkToImgCallback?(responseText: string): void; } +interface IScrollAttr { + rootId: string, + startId: string, + endId: string + scrollTop: number, + focusId?: string, + focusStart?: number + focusEnd?: number + zoomInId?: string +} + /** @link https://ld246.com/article/1549638745630#options-toolbar */ interface IMenuItem { /** 唯一标示 */ @@ -398,7 +428,7 @@ interface IHint { } /** @link https://ld246.com/article/1549638745630#options */ -interface IOptions { +interface IProtyleOptions { history?: { created?: string snapshot?: string @@ -408,7 +438,7 @@ interface IOptions { dom: string expand: boolean }[], - action?: string[], + action?: TProtyleAction[], mode?: TEditorMode, blockId: string rootId?: string @@ -484,7 +514,7 @@ interface IProtyle { title?: import("../protyle/header/Title").Title, background?: import("../protyle/header/background").Background, contentElement?: HTMLElement, - options: IOptions; + options: IProtyleOptions; lute?: Lute; toolbar?: import("../protyle/toolbar").Toolbar, preview?: import("../protyle/preview").Preview; diff --git a/app/tsconfig.json b/app/tsconfig.json index 05f6b94ca..9480e8953 100644 --- a/app/tsconfig.json +++ b/app/tsconfig.json @@ -10,7 +10,9 @@ "types": [ "./src/types", "node" - ] + ], + "declaration": true, + "emitDeclarationOnly": true, }, "include": [ "src"