Skip to content

Commit

Permalink
upgrade#0.8.1 调整标题通过接口获取所有标题
Browse files Browse the repository at this point in the history
  • Loading branch information
hqweay committed Feb 10, 2025
1 parent d167274 commit 4b3d067
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "siyuan-hqweay-go",
"version": "0.7.5",
"version": "0.8.1",
"type": "module",
"description": "恐龙工具箱",
"repository": "",
Expand Down
2 changes: 1 addition & 1 deletion plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "siyuan-hqweay-go",
"author": "养恐龙",
"url": "https://github.com/hqweay/siyuan-hqweay-go",
"version": "0.7.5",
"version": "0.8.1",
"minAppVersion": "3.0.0",
"backends": [
"windows",
Expand Down
125 changes: 114 additions & 11 deletions src/siyuan-adjust-title-level/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { settings } from "@/settings";
import { plugin } from "@/utils";
import { request, sql } from "@/api";
export default class AdjustTitleLevel {
availableBlocks = ["NodeParagraph", "NodeHeading"];

Expand All @@ -8,7 +7,7 @@ export default class AdjustTitleLevel {
public editortitleiconEvent({ detail }) {
detail.menu.addItem({
iconHTML: "",
label: "调整标题",
label: "调整所有标题",
submenu: [
{
iconHTML: "",
Expand All @@ -24,8 +23,13 @@ export default class AdjustTitleLevel {
Array.from(
{ length: this.maxTitleLevel },
(v, i) => i + 1
).forEach((index) => {
this.adjustOriginTitleTo(detail, `h${index}`, `h${num}`);
).forEach(async (index) => {
await this.adjustOriginTitleTo(
detail,
`${index}`,
`${num}`,
false
);
});
},
};
Expand All @@ -48,8 +52,64 @@ export default class AdjustTitleLevel {
click: () => {
this.adjustOriginTitleTo(
detail,
`h${originNum}`,
`h${toNum}`
`${originNum}`,
`${toNum}`,
false
);
},
};
}),
};
}
),
],
});

detail.menu.addItem({
iconHTML: "",
label: "调整所有标题(带子标题)",
submenu: [
// {
// iconHTML: "",
// label: `调整所有标题为`,
// submenu: Array.from(
// { length: this.maxTitleLevel },
// (v, i) => i + 1
// ).map((num) => {
// return {
// iconHTML: "",
// label: `H${num}`,
// click: () => {
// Array.from(
// { length: this.maxTitleLevel },
// (v, i) => i + 1
// ).forEach(async (index) => {
// await this.adjustOriginTitleTo(detail, `${index}`, `${num}`);
// });
// },
// };
// }),
// },
...Array.from({ length: this.maxTitleLevel }, (v, i) => i + 1).map(
(originNum) => {
return {
iconHTML: "",
label: `调整 H${originNum} 为`,
submenu: Array.from(
{ length: this.maxTitleLevel },
(v, i) => i + 1
)
.filter((toNum) => toNum !== originNum)
.map((toNum) => {
return {
iconHTML: "",
label: `H${toNum}`,
click: () => {
this.adjustOriginTitleTo(
detail,
`${originNum}`,
`${toNum}`,
true
);
},
};
Expand Down Expand Up @@ -101,16 +161,59 @@ export default class AdjustTitleLevel {
detail.protyle.getInstance().transaction(doOperations);
}

private adjustOriginTitleTo(detail, originTitleLevel, toTitleLevel) {
private async adjustOriginTitleTo(
detail,
originTitleLevel,
toTitleLevel,
includeSub = true
) {
let res = await sql(
`select id from blocks where root_id = '${detail.data.rootID}' and subtype = 'h${originTitleLevel}'`
);

const doOperations: IOperation[] = [];
const undoOperations: IOperation[] = [];
for (const item of res) {
const response = await request("/api/block/getHeadingLevelTransaction", {
id: item.id,
level: Number(toTitleLevel),
});

response.doOperations.forEach((operation: IOperation, index: number) => {
detail.protyle.wysiwyg.element
.querySelectorAll(`[data-node-id="${operation.id}"]`)
.forEach((itemElement: HTMLElement) => {
itemElement.outerHTML = operation.data;
});
});
if (includeSub) {
doOperations.push(...response.doOperations);
undoOperations.push(...response.undoOperations);
} else {
doOperations.push(response.doOperations[0]);
undoOperations.push(response.undoOperations[0]);
}
}

doOperations.length > 0 &&
detail.protyle.getInstance().transaction(doOperations, undoOperations);

await request("/api/outline/getDocOutline", {
id: detail.data.rootID,
preview: false,
});
}

private adjustOriginTitleToByDom(detail, originTitleLevel, toTitleLevel) {
const doOperations: IOperation[] = [];

const editElements = detail.protyle.wysiwyg.element.querySelectorAll(
`[data-type="NodeHeading"][data-subtype="${originTitleLevel}"]`
`[data-type="NodeHeading"][data-subtype="h${originTitleLevel}"]`
);

editElements.forEach((item: HTMLElement) => {
item.setAttribute("data-subtype", toTitleLevel);
item.setAttribute("class", toTitleLevel);
item.setAttribute("data-subtype", `h${toTitleLevel}`);
item.setAttribute("class", `h${toTitleLevel}`);

doOperations.push({
id: item.dataset.nodeId,
Expand Down
17 changes: 16 additions & 1 deletion src/siyuan-typography-go/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ export default class TypographyGo extends AddIconThenClick {
}

async formatDoc(parentId) {
const doOperations: IOperation[] = [];

let childrenResult = await fetchSyncPost("/api/block/getChildBlocks", {
id: parentId,
});
Expand Down Expand Up @@ -219,8 +221,12 @@ export default class TypographyGo extends AddIconThenClick {
if (/^\{:.*\}$/.test(result.kramdown)) {
const deleteEmptyClock = true;
if (deleteEmptyClock) {
await fetchSyncPost("/api/block/deleteBlock", {
// await fetchSyncPost("/api/block/deleteBlock", {
// id: id,
// });
doOperations.push({
id: id,
action: "delete",
});
}
continue;
Expand Down Expand Up @@ -253,8 +259,17 @@ export default class TypographyGo extends AddIconThenClick {
data: formatResult,
id: id,
});
//doOperations 不支持 updateBlock dataType: "markdown" 呀
// doOperations.push({
// dataType: "markdown",
// data: formatResult,
// id: id,
// action: "update",
// });
}

this.customEvent.detail.protyle.getInstance().transaction(doOperations);

const netImg2LocalAssets = settings.getBySpace(
"typographyConfig",
"netImg2LocalAssets"
Expand Down

0 comments on commit 4b3d067

Please sign in to comment.