Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:sparkles: Support converting non-contiguous selected blocks https://github.com/siyuan-note/siyuan/issues/18349

Daniel68a6f4dc0a74
diff --git a/app/src/protyle/gutter/index.ts b/app/src/protyle/gutter/index.ts
index 441e220f3e..c997081c1b 100644
--- a/app/src/protyle/gutter/index.ts
+++ b/app/src/protyle/gutter/index.ts
@@ -26,6 +26,7 @@ import {
import {
transaction,
turnListsRecursively,
+ turnsIntoGroupsTransaction,
turnsIntoOneTransaction,
turnsIntoTransaction,
turnsOneInto,
@@ -40,6 +41,7 @@ import {blockRender} from "../render/blockRender";
import {
getContenteditableElement,
getEmbedChildOperationContext,
+ getNextBlockSibling,
getParentBlock,
getTopAloneElement,
isNotEditBlock
@@ -825,6 +827,26 @@ export class Gutter {
};
}
+ private turnsIntoGroups(options: {
+ menuId?: string,
+ accelerator?: string,
+ icon?: string,
+ label: string,
+ protyle: IProtyle,
+ selectsElementGroups: Element[][],
+ type: Exclude<TTurnIntoOne, "BlocksMergeSuperBlock">,
+ }) {
+ return {
+ id: options.menuId,
+ icon: options.icon,
+ label: options.label,
+ accelerator: options.accelerator,
+ click() {
+ turnsIntoGroupsTransaction(options);
+ }
+ };
+ }
+
private turnsInto(options: {
menuId?: string,
icon?: string,
@@ -862,68 +884,67 @@ export class Gutter {
public renderMultipleMenu(protyle: IProtyle, selectsElement: Element[]) {
let isList = false;
- let isContinue = false;
- selectsElement.find((item, index) => {
+ const selectsElementGroups: Element[][] = [];
+ selectsElement.forEach((item) => {
if (item.classList.contains("li")) {
isList = true;
- return true;
}
- if (item.nextElementSibling && selectsElement[index + 1] &&
- item.nextElementSibling === selectsElement[index + 1]) {
- isContinue = true;
- } else if (index !== selectsElement.length - 1) {
- isContinue = false;
- return true;
+ const currentGroup = selectsElementGroups[selectsElementGroups.length - 1];
+ const previousElement = currentGroup?.[currentGroup.length - 1];
+ if (previousElement && previousElement.parentElement === item.parentElement &&
+ getNextBlockSibling(previousElement) === item) {
+ currentGroup.push(item);
+ } else {
+ selectsElementGroups.push([item]);
}
});
+ const isContinue = selectsElementGroups.length === 1;
if (!isList && !protyle.disabled) {
const turnIntoSubmenu: IMenu[] = [];
- if (isContinue) {
- turnIntoSubmenu.push(this.turnsIntoOne({
- menuId: "list",
- icon: "iconList",
- label: window.siyuan.languages.list,
- protyle,
- accelerator: window.siyuan.config.keymap.editor.insert.list.custom,
- selectsElement,
- type: "Blocks2ULs"
- }));
- turnIntoSubmenu.push(this.turnsIntoOne({
- menuId: "orderedList",
- icon: "iconOrderedList",
- label: window.siyuan.languages["ordered-list"],
- accelerator: window.siyuan.config.keymap.editor.insert["ordered-list"].custom,
- protyle,
- selectsElement,
- type: "Blocks2OLs"
- }));
- turnIntoSubmenu.push(this.turnsIntoOne({
- menuId: "check",
- icon: "iconCheck",
- label: window.siyuan.languages.check,
- accelerator: window.siyuan.config.keymap.editor.insert.check.custom,
- protyle,
- selectsElement,
- type: "Blocks2TLs"
- }));
- turnIntoSubmenu.push(this.turnsIntoOne({
- menuId: "quote",
- icon: "iconQuote",
- label: window.siyuan.languages.quote,
- accelerator: window.siyuan.config.keymap.editor.insert.quote.custom,
- protyle,
- selectsElement,
- type: "Blocks2Blockquote"
- }));
- turnIntoSubmenu.push(this.turnsIntoOne({
- menuId: "callout",
- icon: "iconCallout",
- label: window.siyuan.languages.callout,
- protyle,
- selectsElement,
- type: "Blocks2Callout"
- }));
- }
+ turnIntoSubmenu.push(this.turnsIntoGroups({
+ menuId: "list",
+ icon: "iconList",
+ label: window.siyuan.languages.list,
+ protyle,
+ accelerator: window.siyuan.config.keymap.editor.insert.list.custom,
+ selectsElementGroups,
+ type: "Blocks2ULs"
+ }));
+ turnIntoSubmenu.push(this.turnsIntoGroups({
+ menuId: "orderedList",
+ icon: "iconOrderedList",
+ label: window.siyuan.languages["ordered-list"],
+ accelerator: window.siyuan.config.keymap.editor.insert["ordered-list"].custom,
+ protyle,
+ selectsElementGroups,
+ type: "Blocks2OLs"
+ }));
+ turnIntoSubmenu.push(this.turnsIntoGroups({
+ menuId: "check",
+ icon: "iconCheck",
+ label: window.siyuan.languages.check,
+ accelerator: window.siyuan.config.keymap.editor.insert.check.custom,
+ protyle,
+ selectsElementGroups,
+ type: "Blocks2TLs"
+ }));
+ turnIntoSubmenu.push(this.turnsIntoGroups({
+ menuId: "quote",
+ icon: "iconQuote",
+ label: window.siyuan.languages.quote,
+ accelerator: window.siyuan.config.keymap.editor.insert.quote.custom,
+ protyle,
+ selectsElementGroups,
+ type: "Blocks2Blockquote"
+ }));
+ turnIntoSubmenu.push(this.turnsIntoGroups({
+ menuId: "callout",
+ icon: "iconCallout",
+ label: window.siyuan.languages.callout,
+ protyle,
+ selectsElementGroups,
+ type: "Blocks2Callout"
+ }));
turnIntoSubmenu.push(this.turnsInto({
menuId: "paragraph",
icon: "iconParagraph",
diff --git a/app/src/protyle/wysiwyg/transaction.ts b/app/src/protyle/wysiwyg/transaction.ts
index ad97ca2101..398782aeb1 100644
--- a/app/src/protyle/wysiwyg/transaction.ts
+++ b/app/src/protyle/wysiwyg/transaction.ts
@@ -1314,6 +1314,36 @@ export const turnsIntoOneTransaction = async (options: {
hideElements(["gutter"], options.protyle);
};
+export const turnsIntoGroupsTransaction = async (options: {
+ protyle: IProtyle,
+ selectsElementGroups: Element[][],
+ type: Exclude<TTurnIntoOne, "BlocksMergeSuperBlock">,
+}) => {
+ const firstElement = options.selectsElementGroups[0]?.[0];
+ if (!firstElement) {
+ return;
+ }
+ const firstID = firstElement.getAttribute("data-node-id");
+ const doOperations: IOperation[] = [];
+ const undoOperations: IOperation[] = [];
+ for (const selectsElement of options.selectsElementGroups) {
+ if (selectsElement.length === 0) {
+ continue;
+ }
+ const groupOperations = await turnsIntoOneTransaction({
+ protyle: options.protyle,
+ selectsElement,
+ type: options.type,
+ getOperations: true,
+ });
+ doOperations.push(...groupOperations.doOperations);
+ undoOperations.splice(0, 0, ...groupOperations.undoOperations);
+ }
+ transaction(options.protyle, doOperations, undoOperations);
+ focusBlock(options.protyle.wysiwyg.element.querySelector(`[data-node-id="${firstID}"]`));
+ hideElements(["gutter"], options.protyle);
+};
+
const removeUnfoldRepeatBlock = (html: string, protyle: IProtyle) => {
const temp = document.createElement("template");
temp.innerHTML = html;