Skip to content

Commit

Permalink
feat: add settings schema
Browse files Browse the repository at this point in the history
  • Loading branch information
mlhiter committed Aug 1, 2024
1 parent d8b7c04 commit d893f78
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
29 changes: 29 additions & 0 deletions src/libs/settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { SettingSchemaDesc } from '@logseq/libs/dist/LSPlugin'

export const settingSchema: Array<SettingSchemaDesc> = [
{
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模式生效)',
},
]
36 changes: 28 additions & 8 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,21 @@ 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
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`)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
})
Expand Down

0 comments on commit d893f78

Please sign in to comment.