Skip to content

Commit

Permalink
✨支持设置脚注内容放到当前块后 #5
Browse files Browse the repository at this point in the history
  • Loading branch information
Achuan-2 committed Nov 23, 2024
1 parent 63bcf6d commit 0ffd7b1
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 26 deletions.
3 changes: 2 additions & 1 deletion public/i18n/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"description": "Current document, Specified document or Child Document",
"current": "Current Document",
"specified": "Specified Document",
"childDoc": "Child Document"
"childDoc": "Child Document",
"afterParent": "After Parent Block"
},
"docId": {
"title": "Specified Document ID",
Expand Down
3 changes: 2 additions & 1 deletion public/i18n/zh_CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"description": "将脚注存放于当前文档、指定文档或子文档",
"current": "当前文档",
"specified": "指定文档",
"childDoc": "子文档"
"childDoc": "子文档",
"afterParent": "父块后"
},
"docId": {
"title": "指定文档的文档ID",
Expand Down
123 changes: 99 additions & 24 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
import "@/index.scss";
import { IMenuItem } from "siyuan/types";

import { appendBlock, deleteBlock, setBlockAttrs, pushErrMsg, sql, getChildBlocks, insertBlock, renameDocByID, prependBlock, updateBlock, createDocWithMd } from "./api";
import { appendBlock, deleteBlock, setBlockAttrs, pushErrMsg, sql, getChildBlocks, insertBlock, renameDocByID, prependBlock, updateBlock, createDocWithMd, getBlockKramdown } from "./api";
import { SettingUtils } from "./libs/setting-utils";

const STORAGE_NAME = "config";
Expand Down Expand Up @@ -94,7 +94,8 @@ export default class PluginFootnote extends Plugin {
options: {
1: this.i18n.settings.saveLocation.current,
2: this.i18n.settings.saveLocation.specified,
3: this.i18n.settings.saveLocation.childDoc
3: this.i18n.settings.saveLocation.childDoc,
4: this.i18n.settings.saveLocation.afterParent
}
});

Expand Down Expand Up @@ -272,6 +273,8 @@ export default class PluginFootnote extends Plugin {
await this.settingUtils.load(); //导入配置
// 获取当前光标所在块的 ID
const currentBlockId = protyle.toolbar.range.startContainer.parentElement.closest('[data-node-id]')?.getAttribute('data-node-id');
const currentParentBlockId = protyle.toolbar.range.startContainer.parentElement.closest('.protyle-wysiwyg > [data-node-id]')?.getAttribute('data-node-id');
console.log(currentBlockId, currentParentBlockId);
// 先复制选中内容
const getSelectedHtml = (range: Range): string => {
// 创建临时容器
Expand Down Expand Up @@ -433,6 +436,11 @@ export default class PluginFootnote extends Plugin {
}
footnoteContainerID = docID;
break;

case '4': // 父块后
docID = protyle.block.id;
footnoteContainerID = currentParentBlockId;
break;
}


Expand All @@ -456,37 +464,104 @@ export default class PluginFootnote extends Plugin {

// 插入脚注
let back;
switch (this.settingUtils.get("order")) {
case '2':
// 倒序
if (this.settingUtils.get("saveLocation") != 3) {
back = await appendBlock("markdown", templates, footnoteContainerID);
} else {
back = await prependBlock("markdown", templates, footnoteContainerID);
}
break;
default:
if (this.settingUtils.get("saveLocation") != 3) {
let children = await getChildBlocks(footnoteContainerID);
// 默认顺序插入
if (children.length > 0) {
// 在最后一个子块后面添加(使用 insertBlock 并指定 previousID)
// 如果this.settingUtils.get("saveLocation")不等于4,则按照设置来插入,否则直接在父块后插入
if (this.settingUtils.get("saveLocation") == '4') {
switch (this.settingUtils.get("order")) {
case '2':
// 逆序
back = await insertBlock(
"markdown",
templates,
undefined, // nextID
footnoteContainerID, // previousID
undefined // parentID
);
break;
case '1':
default:
// getBlockKramdown

function findNextSiblingIdWithoutFootnote(id) {
// 首先找到目标块
const targetBlock = document.querySelector(`[data-node-id="${id}"]`);

if (!targetBlock) {
return null;
}

// 使用nextElementSibling获取下一个兄弟元素
let nextSibling = targetBlock.nextElementSibling;

// 遍历所有后续兄弟元素,直到找到符合条件的元素
while (nextSibling) {
// 检查是否没有custom-plugin-footnote-content="true"属性
if (!nextSibling.hasAttribute('custom-plugin-footnote-content')) {
// 返回找到的元素的data-node-id属性值
return nextSibling.getAttribute('data-node-id');
}
nextSibling = nextSibling.nextElementSibling;
}

return null; // 如果没找到符合条件的元素,返回null
}


let nextID = findNextSiblingIdWithoutFootnote(footnoteContainerID);
if (nextID == null) {
back = await insertBlock(
"markdown",
templates,
undefined, // nextID
children[children.length - 1].id, // previousID - 放在最后一个子块后面
footnoteContainerID, // previousID
undefined // parentID
);
}
else {
back = await insertBlock(
"markdown",
templates,
nextID, // nextID
undefined, // previousID
undefined // parentID
);
}
break;
}
}
else {
switch (this.settingUtils.get("order")) {
case '2':
// 倒序
if (this.settingUtils.get("saveLocation") != 3) {
back = await appendBlock("markdown", templates, footnoteContainerID);
} else {
// 如果没有子块,直接在标题下添加
back = await prependBlock("markdown", templates, footnoteContainerID);
}
break;
case '1':
default:
if (this.settingUtils.get("saveLocation") != 3) {
let children = await getChildBlocks(footnoteContainerID);
// 默认顺序插入
if (children.length > 0) {
// 在最后一个子块后面添加(使用 insertBlock 并指定 previousID)
back = await insertBlock(
"markdown",
templates,
undefined, // nextID
children[children.length - 1].id, // previousID - 放在最后一个子块后面
undefined // parentID
);
} else {
// 如果没有子块,直接在标题下添加
back = await appendBlock("markdown", templates, footnoteContainerID);
}
}
else {
back = await appendBlock("markdown", templates, footnoteContainerID);
}
}
else {
back = await appendBlock("markdown", templates, footnoteContainerID);
}
break;
break;
}
}

let newBlockId = back[0].doOperations[0].id
Expand Down

0 comments on commit 0ffd7b1

Please sign in to comment.