From 386a0927da6140423fcedaa20072fc356cc94b62 Mon Sep 17 00:00:00 2001 From: Vanessa Date: Tue, 28 Nov 2023 17:19:22 +0800 Subject: [PATCH] :art: fix https://github.com/siyuan-note/siyuan/issues/9762 --- app/src/protyle/gutter/index.ts | 9 ++-- app/src/protyle/index.ts | 57 +++++++++++++++++++++++++- app/src/protyle/wysiwyg/transaction.ts | 10 ++--- app/src/types/protyle.d.ts | 6 +++ 4 files changed, 71 insertions(+), 11 deletions(-) diff --git a/app/src/protyle/gutter/index.ts b/app/src/protyle/gutter/index.ts index 31c08f30f..ef990de42 100644 --- a/app/src/protyle/gutter/index.ts +++ b/app/src/protyle/gutter/index.ts @@ -401,8 +401,8 @@ export class Gutter { label: string, protyle: IProtyle, selectsElement: Element[], - type: string, - level?: string + type: TTurnIntoOne, + level?: TTurnIntoOneSub }) { return { icon: options.icon, @@ -419,8 +419,8 @@ export class Gutter { label: string, protyle: IProtyle, selectsElement: Element[], - type: string, - level?: number | string, + type: TTurnInto, + level?: number, isContinue?: boolean accelerator?: string }) { @@ -896,7 +896,6 @@ export class Gutter { accelerator: window.siyuan.config.keymap.editor.heading.paragraph.custom, protyle, selectsElement: [nodeElement], - level: 6, type: "Blocks2Ps", })); if (subType !== "h1") { diff --git a/app/src/protyle/index.ts b/app/src/protyle/index.ts index 2604877e2..6a7690873 100644 --- a/app/src/protyle/index.ts +++ b/app/src/protyle/index.ts @@ -14,7 +14,13 @@ import {WYSIWYG} from "./wysiwyg"; import {Toolbar} from "./toolbar"; import {Gutter} from "./gutter"; import {Breadcrumb} from "./breadcrumb"; -import {onTransaction, transaction} from "./wysiwyg/transaction"; +import { + onTransaction, + transaction, + turnsIntoOneTransaction, turnsIntoTransaction, + updateBatchTransaction, + updateTransaction +} from "./wysiwyg/transaction"; import {fetchPost} from "../util/fetch"; /// #if !MOBILE import {Title} from "./header/Title"; @@ -31,6 +37,8 @@ import {getDocByScroll} from "./scroll/saveScroll"; import {App} from "../index"; import {insertHTML} from "./util/insertHTML"; import {avRender} from "./render/av/render"; +import {focusBlock, getEditorRange} from "./util/selection"; +import {hasClosestBlock} from "./util/hasClosest"; export class Protyle { @@ -390,4 +398,51 @@ export class Protyle { public transaction(doOperations: IOperation[], undoOperations?: IOperation[]) { transaction(this.protyle, doOperations, undoOperations); } + + /** + * 多个块转换为一个块 + * @param {TTurnIntoOneSub} [subType] type 为 "BlocksMergeSuperBlock" 时必传 + */ + public turnIntoOneTransaction(selectsElement: Element[], type: TTurnIntoOne, subType?: TTurnIntoOneSub) { + turnsIntoOneTransaction({ + protyle: this.protyle, + selectsElement, + type, + level: subType + }); + } + + /** + * 多个块转换 + * @param {Element} [nodeElement] 优先使用包含 protyle-wysiwyg--select 的块,否则使用 nodeElement 单块 + * @param {number} [subType] type 为 "Blocks2Hs" 时必传 + */ + public turnIntoTransaction(nodeElement: Element, type: TTurnInto, subType?: number) { + turnsIntoTransaction({ + protyle: this.protyle, + nodeElement, + type, + level: subType, + }); + } + + public updateTransaction(id: string, newHTML: string, html: string) { + updateTransaction(this.protyle, id, newHTML, html); + } + + public updateBatchTransaction(nodeElements: Element[], cb: (e: HTMLElement) => void) { + updateBatchTransaction(nodeElements, this.protyle, cb); + } + + public getRange(element: Element) { + getEditorRange(element); + } + + public hasClosestBlock(element: Node) { + return hasClosestBlock(element); + } + + public focusBlock(element: Element, toStart = true) { + return focusBlock(element, undefined, toStart); + } } diff --git a/app/src/protyle/wysiwyg/transaction.ts b/app/src/protyle/wysiwyg/transaction.ts index bbdfd0304..b427f40c0 100644 --- a/app/src/protyle/wysiwyg/transaction.ts +++ b/app/src/protyle/wysiwyg/transaction.ts @@ -723,8 +723,8 @@ export const onTransaction = (protyle: IProtyle, operation: IOperation, isUndo: export const turnsIntoOneTransaction = (options: { protyle: IProtyle, selectsElement: Element[], - type: string, - level?: string + type: TTurnIntoOne, + level?: TTurnIntoOneSub }) => { let parentElement: Element; const id = Lute.NewNodeID(); @@ -834,8 +834,8 @@ export const turnsIntoTransaction = (options: { protyle: IProtyle, selectsElement?: Element[], nodeElement?: Element, - type: string, - level?: number | string, + type: TTurnInto, + level?: number, isContinue?: boolean, }) => { let selectsElement: Element[] = options.selectsElement; @@ -898,7 +898,7 @@ export const turnsIntoTransaction = (options: { data: item.outerHTML }); - if ((options.type === "Blocks2Ps" || options.type === "Blocks2Hs") && !options.isContinue) { + if (!options.isContinue) { // @ts-ignore item.outerHTML = options.protyle.lute[options.type](item.outerHTML, options.level); } else { diff --git a/app/src/types/protyle.d.ts b/app/src/types/protyle.d.ts index c552238a3..062be2346 100644 --- a/app/src/types/protyle.d.ts +++ b/app/src/types/protyle.d.ts @@ -8,6 +8,12 @@ interface ILuteNode { }; } +type TTurnIntoOne = "BlocksMergeSuperBlock" | "Blocks2ULs" | "Blocks2OLs" | "Blocks2TLs" | "Blocks2Blockquote" + +type TTurnIntoOneSub = "row" | "col" + +type TTurnInto = "Blocks2Ps" | "Blocks2Hs" + type ILuteRenderCallback = (node: ILuteNode, entering: boolean) => [string, number]; /** @link https://ld246.com/article/1588412297062 */