diff --git a/src/libs/settings.ts b/src/libs/settings.ts new file mode 100644 index 0000000..7baf49a --- /dev/null +++ b/src/libs/settings.ts @@ -0,0 +1,29 @@ +import { SettingSchemaDesc } from '@logseq/libs/dist/LSPlugin' + +export const settingSchema: Array = [ + { + key: 'doneContent', + type: 'string', + default: '{content} - [[{date}]]', + title: 'Done内容', + description: + '插入的内容,支持几个变量,如`{content}`表示未填充信息之前的块内容(仅在content模式可用),`{date}`指当前日期,`{time}`指当前时间', + }, + { + key: 'displayMode', + type: 'enum', + default: 'content', + title: '展示模式', + description: + 'content模式直接修改内容,property模式增加属性,childBlock模式添加一个子块', + enumChoices: ['content', 'property', 'childBlock'], + enumPicker: 'radio', + }, + { + key: 'collapseMode', + type: 'boolean', + default: true, + title: '是否默认折叠子块', + description: 'true默认折叠,false默认不折叠(只在childBlock模式生效)', + }, +] diff --git a/src/main.tsx b/src/main.tsx index c9f05ec..8763883 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -7,6 +7,7 @@ import * as ReactDOM from 'react-dom/client' import './index.css' import App from './App' import { getDatePattern } from './libs/date' +import { settingSchema } from './libs/settings' import { logseq as pluginInfo } from '../package.json' // @ts-expect-error @@ -14,6 +15,13 @@ const css = (t, ...args) => String.raw(t, ...args) const pluginId = pluginInfo.id +interface Settings { + doneContent: string + displayMode: 'content' | 'property' | 'childBlock' + collapseMode: boolean + disabled: boolean +} + // main function async function main() { console.info(`#${pluginId}: MAIN`) @@ -61,19 +69,31 @@ async function main() { const { preferredDateFormat } = await logseq.App.getUserConfigs() + // 初始化设置(当安装插件之后第一次注入) + if (logseq.settings === undefined) { + logseq.updateSettings({ + doneContent: '{content} - [[{date}]]', + displayMode: 'content', + collapseMode: true, + }) + } + + const { doneContent, displayMode, collapseMode } = + logseq.settings as unknown as Settings + + console.log(doneContent, displayMode, collapseMode) + + logseq.useSettingsSchema(settingSchema) + // 监控数据变化 logseq.DB.onChanged(async (data) => { if (data.txMeta?.outlinerOp !== 'save-block') return if (data.txMeta?.undo || data.txMeta?.redo) return - console.log('data changed', data) - const block = await logseq.Editor.getBlock(data.blocks[0].uuid) - const currentBlockContent = block?.content - console.log('current block', block) const isDoneStatus = block?.marker === 'DONE' - if (!block || !currentBlockContent) return + if (!block || !block.content) return const date = new Date() const formattedUserDate = format(date, preferredDateFormat) @@ -87,13 +107,13 @@ async function main() { // 已经加入过内容,而且状态为DONE,则不操作 // 已经加入过内容,但是状态变为其他状态,删除添加的内容 // 没有加入过内容,而且状态为DONE则加入内容 - if (combinedPattern.test(currentBlockContent)) { + if (combinedPattern.test(block.content)) { if (isDoneStatus) return - const newContent = currentBlockContent.replace(combinedPattern, '').trim() + const newContent = block.content.replace(combinedPattern, '').trim() await logseq.Editor.updateBlock(block.uuid, newContent) return } else if (isDoneStatus) { - const newContent = ` ${currentBlockContent} ${addedContent}` + const newContent = ` ${block.content} ${addedContent}` await logseq.Editor.updateBlock(block.uuid, newContent) } })