之前一直报 data错误
selection = app.workspace.activeLeaf.view.data 改成这个之后可以过了,但是
卡在 app.workspace.activeLeaf.view.editor.cm.setValue(result)
查看发现cm下面没有setValue这个函数……
请问怎么搞
我是想把它改造成,查找“第x条”,然后统一给“第x条”进行编号
之前一直报 data错误
selection = app.workspace.activeLeaf.view.data 改成这个之后可以过了,但是
卡在 app.workspace.activeLeaf.view.editor.cm.setValue(result)
查看发现cm下面没有setValue这个函数……
请问怎么搞
我是想把它改造成,查找“第x条”,然后统一给“第x条”进行编号
我也是,是不是obsidian的接口变了导致函数有问题?
更新代码,亲测可用,使用AI,注意鉴别
module.exports = init;
let quickAddApi;
let list = ["📑 添加排序", "🆑 清除排序"];
let type = ["sort", "clear"];
let level;
let count = [0, 0, 0, 0, 0, 0];
let select = 1; // 判断是否划选
let ht = [
["一、", "二、", "三、", "四、", "五、", "六、", "七、", "八、", "九、", "十、"],
["(一)", "(二)", "(三)", "(四)", "(五)", "(六)", "(七)", "(八)", "(九)", "(十)"],
["1.", "2.", "3.", "4.", "5.", "6.", "7.", "8.", "9.", "10."],
["1)", "2)", "3)", "4)", "5)", "6)", "7)", "8)", "9)", "10)"],
["A.", "B.", "C.", "D.", "E.", "F.", "G.", "H.", "I.", "J."],
["a.", "b.", "c.", "d.", "e.", "f.", "g.", "h.", "i.", "j."]
];
async function init(params) {
({ quickAddApi } = params);
const q = await quickAddApi.suggester(list, type);
switch (q) {
case "sort":
return await sort();
case "clear":
return await clear();
}
}
async function sort() {
level = -1;
const activeLeaf = app.workspace.activeLeaf;
if (!activeLeaf || !activeLeaf.view.editor) {
console.error("未找到活动的编辑器。");
return "未找到活动的编辑器。";
}
let selection = activeLeaf.view.editor.getSelection();
if (selection === "") {
selection = activeLeaf.view.editor.getValue(); // 使用 getValue() 替代 view.data
select = 0;
}
const tArr = selection.split("\n");
let result = "";
for (let index = 0; index < tArr.length; index++) {
const line = tArr[index];
let h = line.split(" ")[0] + " ";
for (let hl = 0; hl < 6; hl++) {
if (h === "#".repeat(hl + 1) + " ") {
if (level > hl) count[level] = 0;
level = hl;
tArr[index] = tArr[index].replace(h, h + ht[level][count[level]++]);
}
}
result += tArr[index];
if (index < tArr.length - 1) result += "\n";
}
if (select) return result;
activeLeaf.view.editor.setValue(result); // 使用 setValue() 替代 cm.setValue()
}
async function clear() {
const activeLeaf = app.workspace.activeLeaf;
if (!activeLeaf || !activeLeaf.view.editor) {
console.error("未找到活动的编辑器。");
return "未找到活动的编辑器。";
}
let selection = activeLeaf.view.editor.getSelection();
if (selection === "") {
selection = activeLeaf.view.editor.getValue(); // 使用 getValue() 替代 view.data
select = 0;
}
for (let i = 0; i < ht.length; i++) {
for (let j = 0; j < ht[i].length; j++) {
const regex = new RegExp(`^#`.repeat(i + 1) + ` ${ht[i][j]}`, "gm");
selection = selection.replace(regex, "#".repeat(i + 1) + " ");
}
}
if (select) return selection;
activeLeaf.view.editor.setValue(selection); // 使用 setValue() 替代 cm.setValue()
}
String.prototype.repeat = function (n) {
return Array.prototype.join.call({ length: n + 1 }, this);
};