Last active
July 28, 2025 09:15
-
-
Save Moyf/06c34f5057e7355fd26a2abb8d7cacfa to your computer and use it in GitHub Desktop.
CCR 多重命令执行者 - 用于整合和管理多个 Obsidian 插件的工具和模板
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @file CCR 多重命令执行者 - 用于整合和管理多个 Obsidian 插件的工具和模板 | |
| * (All in One / Consolidate Commands Runner) | |
| * | |
| * @description | |
| * 这个脚本用于集中管理和访问以下 Obsidian 插件的资源: | |
| * - Components:基础组件和脚本 | |
| * - Form Flow:表单文件 | |
| * - Templater:模板文件 | |
| * - QuickAdd:快速添加模板 | |
| * - Note Toolbar:工具栏命令 | |
| * - Dataview:数据视图脚本 | |
| * | |
| * 主要功能包括: | |
| * - 安全获取插件设置 | |
| * - 提取各类插件的工具和模板 | |
| * - 统一管理文件加载和过滤 | |
| */ | |
| /* | |
| * 缓存光标位置 | |
| */ | |
| const editor = app.workspace.activeEditor?.editor; | |
| const currentCursor = editor.getCursor(); | |
| /* | |
| * 尝试获取设置 | |
| */ | |
| const getSafeSetting = (plugin, path) => { | |
| if (!plugin) return undefined;const parts = path.split('.'); | |
| let current = plugin; | |
| for (const part of parts) { | |
| if (current === undefined || current === null) return undefined; | |
| current = current[part]; | |
| } | |
| return current; | |
| }; | |
| /* | |
| * 提取出所有 NTB 的工具 | |
| */ | |
| const getNtbTools = (ntbPlugin, onlyCommand = true) => { | |
| const toolbars = ntbPlugin.toolbars; | |
| const tools = toolbars.reduce((prev, curr) => [...prev, ...curr.items], []); | |
| if (onlyCommand) { | |
| return tools.filter(tool => tool.hasCommand); | |
| } | |
| return tools; | |
| } | |
| /* | |
| * 提取出所有 TP 的模板 | |
| */ | |
| const getTpFiles = (tpPlugin) => { | |
| const templatesFolder = getSafeSetting(tpPlugin, 'templates_folder'); | |
| const templates = app.vault.getFiles().filter(file => file.path.startsWith(templatesFolder)); | |
| return templates; | |
| } | |
| /* | |
| * 提取出所有 FF 的表单 | |
| */ | |
| const getFfForms = (ffPlugin) => { | |
| const formsFolder = getSafeSetting(ffPlugin, 'formFolder'); | |
| const forms = app.vault.getFiles().filter(file => file.path.startsWith(formsFolder)); | |
| return forms; | |
| } | |
| // 各个插件 | |
| const ntbPlugin = app.plugins.plugins["note-toolbar"]; | |
| const ffPlugin = app.plugins.plugins["form-flow"]; | |
| const tpPlugin = app.plugins.plugins["templater-obsidian"]; | |
| const qaPlugin = app.plugins.plugins["quickadd"]; | |
| /** | |
| * 主入口函数 管理各种插件设置 | |
| * @async | |
| * @function entry | |
| * @description 初始化 Components、FormFlow、Templater、QuickAdd 和 NoteToolbar 等插件的设置 | |
| * 创建不同插件的脚本文件夹映射 | |
| * 包含一个用于过滤和排序文件库文件的嵌套 LoadFiles 函数 | |
| * @returns {Promise<void>} | |
| */ | |
| async function entry() { | |
| console.log("ENTRY 开始时光标位置:" + JSON.stringify(editor.getCursor())) | |
| // const { app, form, requestUrl, obsidian, Notice, $selection } = this.$context; | |
| // const compPlugin = app.plugins.plugins["components"]?.settings; | |
| // Form Flow 的表单 | |
| const ffSettings = ffPlugin?.settings; | |
| // Templater 的模板 | |
| const tpSettings = tpPlugin?.settings; | |
| // QuickAdd 的命令 | |
| const qaSettings = qaPlugin?.settings; | |
| // NoteToolbar 的工具 | |
| const ntbSettings = ntbPlugin?.settings; | |
| // Zotlit——我不用所以先不管 | |
| // const ztPlugin = app.plugins.plugins["zotlit"].settings; | |
| const scriptFolders = { | |
| // "Components Basic": getSafeSetting(compPlugin, 'folder'), | |
| // "Components Script": getSafeSetting(compPlugin, 'scriptFolder'), | |
| "Form Flow": getSafeSetting(ffSettings, 'formFolder'), | |
| "QuickAdd Script": getSafeSetting(qaSettings, 'templateFolderPath'), | |
| "Dataview Script": "Resources/Attachments/plug-in/_Dataview", | |
| "Templater": getTpFiles(tpSettings), | |
| "NoteToolbar": getNtbTools(ntbSettings), | |
| } | |
| async function LoadFiles(fileFolder, extension = '') { | |
| const allFiles = app.vault.getFiles(); | |
| const filterFiles = allFiles.filter(file => { | |
| const isInFolder = file.path.startsWith(fileFolder); | |
| const hasCorrectExtension = !extension || file.name.endsWith(extension); | |
| return isInFolder && hasCorrectExtension; | |
| }); | |
| filterFiles.sort((a, b) => a.name.localeCompare(b.name)); | |
| const filesArray = filterFiles.map(file => ({ | |
| value: file.path, | |
| label: file.name | |
| })); | |
| console.debug(filesArray) | |
| return filesArray; | |
| } | |
| // Templater 模板 | |
| const tpFiles = getTpFiles(tpSettings).map(file => ({ | |
| label: file.name, | |
| value: file, // 整个文件传入进去 | |
| source: "TP" | |
| })); | |
| // NoteToolbar 工具 | |
| const ntbTools = getNtbTools(ntbSettings).map(tool => ({ | |
| label: tool.label, | |
| value: tool, // NTB 是整个传过去 | |
| source: "NTB" | |
| })); | |
| // Form Flow 表单 | |
| const ffForms = getFfForms(ffSettings).map(file => ({ | |
| label: file.basename, | |
| value: file.path, | |
| source: "FF" | |
| })); | |
| // 汇总所有选项 | |
| const allOptions = [ | |
| ...ntbTools, | |
| ...ffForms, | |
| ...tpFiles | |
| ]; | |
| // 使用 NoteToolbar 插件的 suggester API 展示 | |
| if (ntbPlugin && typeof ntb.suggester === "function") { | |
| const chosen = await ntb.suggester( | |
| allOptions.map(item => `**[${item.source}]** ${item.label}`), | |
| allOptions, | |
| { placeholder: "请选择一个命令" } | |
| ); | |
| if (chosen) { | |
| // 先聚焦回编辑器的正确位置 | |
| if (!editor) { | |
| new Notice("No active editor found."); | |
| return; | |
| } else { | |
| editor.focus(); | |
| editor.setCursor(currentCursor); | |
| } | |
| await runBySource(chosen); | |
| } | |
| } else { | |
| new Notice("未找到 NoteToolbar 的 suggester,请确认安装了 NoteToolbar 插件"); | |
| } | |
| } | |
| /** | |
| * 根据 source 调用对应插件的执行函数 | |
| * @param {object} chosen - 选中的项,包含 source 和 value | |
| */ | |
| async function runBySource(chosen) { | |
| const { source, value } = chosen; | |
| const execMap = { | |
| "NTB": async (val) => { | |
| // NoteToolbar 执行命令 | |
| if (ntbPlugin && typeof ntbPlugin.handleItemLink === "function") { | |
| new Notice("调用 NTB 运行工具:" + val.label); | |
| await ntbPlugin.handleItemLink(val); | |
| } else { | |
| new Notice("NTB: 未找到 handleItemScript 方法"); | |
| } | |
| }, | |
| "TP": async (val) => { | |
| // Templater 执行模板 | |
| if (tpPlugin && tpPlugin.templater?.append_template_to_active_file && typeof tpPlugin.templater.append_template_to_active_file === "function") { | |
| // 然后执行脚本 | |
| await tpPlugin.templater.append_template_to_active_file(val); | |
| } else { | |
| new Notice("TP: 未找到 run_template 方法"); | |
| } | |
| }, | |
| "FF": async (val) => { | |
| // Form Flow 执行表单 | |
| if (ffPlugin && typeof ffPlugin.api.submitFormFile === "function") { | |
| await ffPlugin.api.submitFormFile(val); | |
| } else { | |
| new Notice("FF: 未找到 submitFormFile 方法"); | |
| } | |
| } | |
| }; | |
| if (execMap[source]) { | |
| await execMap[source](value); | |
| } else { | |
| new Notice(`未知 source: ${source}`); | |
| } | |
| } | |
| // new Notice("正在启动统一命令执行者") | |
| entry(); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ntb的 API 来做弹出选框,必须安装 Notetoolbar 插件