Skip to content

Commit

Permalink
chore: type settigns
Browse files Browse the repository at this point in the history
  • Loading branch information
benjypng committed Jun 30, 2024
1 parent c23b7ab commit 3e62dff
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 45 deletions.
26 changes: 26 additions & 0 deletions global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export interface PluginSettings {
preferredDateFormat: string
semiAuto: boolean
insertDateProperty: boolean
lang: 'en' | 'ja' | 'fr' | 'nl' | 'ru' | 'de' | 'pt'
dateChar: string
scheduledChar: string
deadlineChar: string
removeTime: boolean
gotoShortcut: string
completeTaskShortcut: string
}

declare global {
interface LSPluginBaseInfo {
id: string
mode: 'shadow' | 'iframe'
settings: {
disabled: boolean
[key: string]: any
} & PluginSettings
[key: string]: any
}

const logseq: LSPluginBaseInfo
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"dev": "npx vite",
"build": "npx tsc && npx vite build",
"preview": "npx vite preview",
"build:css": "npx tailwindcss -i ./src/tailwind.css -o ./src/output.css"
"build:css": "npx tailwindcss -i ./src/tailwind.css -o ./src/output.css",
"lint": "npx eslint . --fix"
},
"release": {
"branches": [
Expand Down
3 changes: 2 additions & 1 deletion src/features/complete-task/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ export const completeTask = (): void => {
key: 'logseq-datenlp-plugin-completetask',
label: '@Complete task',
keybinding: {
binding: 'mod+shift+d',
binding: logseq.settings!.completeTaskShortcut,
},
},

async () => {
const currBlk = await logseq.Editor.getCurrentBlock()
if (!currBlk) return
Expand Down
2 changes: 1 addition & 1 deletion src/features/go-to-date/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const goToDate = (): void => {
{
key: 'logseq-datenlp-plugin-gotodate',
label: '@Goto date using NLP',
keybinding: { binding: 'mod+g' },
keybinding: { binding: logseq.settings!.gotoShortcut },
},
() => {
createRoot(document.getElementById('app')!).render(<GotoDate />)
Expand Down
13 changes: 6 additions & 7 deletions src/features/parse/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ export const checkIfChronoObjHasTime = (startObj): string => {
}
}

const handleMultipleParsedText = (
const handleMultipleParsedText = async (
chronoBlock: ParsedResult[],
content: string,
options?: { flag: string },
): string => {
): Promise<string> => {
let parsedStr = ''
for (let i = 0; i < chronoBlock.length; i++) {
const parsedText = chronoBlock[i]!.text
const parsedStart = chronoBlock[i]!.start.date()
const parsedEnd = chronoBlock[i]!.end?.date()
if (i === 0) {
if (!options?.flag) {
const str = semiAutoParse(
const str = await semiAutoParse(
content,
chronoBlock,
parsedText,
Expand All @@ -48,7 +48,7 @@ const handleMultipleParsedText = (
)
if (str !== '') parsedStr = str
} else {
const str = manualParse(
const str = await manualParse(
options.flag,
content,
chronoBlock,
Expand All @@ -61,15 +61,15 @@ const handleMultipleParsedText = (
if (i > 0) {
if (logseq.settings!.insertDateProperty) break
if (!options?.flag) {
parsedStr = semiAutoParse(
parsedStr = await semiAutoParse(
parsedStr,
chronoBlock,
parsedText,
parsedStart,
parsedEnd,
)
} else {
parsedStr = manualParse(
parsedStr = await manualParse(
options.flag,
parsedStr,
chronoBlock,
Expand All @@ -89,7 +89,6 @@ export const inlineParsing = async (
const { content } = currBlock
const { lang } = logseq.settings!

// @ts-expect-error Type doesn't match
const chronoBlock: ParsedResult[] = chrono[lang].parse(content, new Date())
if (!chronoBlock || !chronoBlock[0]) return ''

Expand Down
45 changes: 27 additions & 18 deletions src/features/parse/manual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,33 @@ ${getDeadlineDateDay(parsedStart)}${checkTime}`
}

export const manualParsing = () => {
logseq.Editor.registerSlashCommand('Parse dates', async (e) => {
const blk = await logseq.Editor.getBlock(e.uuid)
if (!blk) return
const content = await inlineParsing(blk, { flag: 'manual-date' })
if (content) await logseq.Editor.updateBlock(e.uuid, content)
})
logseq.Editor.registerSlashCommand(
'Parse dates',
async (e: { uuid: string }) => {
const blk = await logseq.Editor.getBlock(e.uuid)
if (!blk) return
const content = await inlineParsing(blk, { flag: 'manual-date' })
if (content) await logseq.Editor.updateBlock(e.uuid, content)
},
)

logseq.Editor.registerSlashCommand('Parse scheduled', async (e) => {
const blk = await logseq.Editor.getBlock(e.uuid)
if (!blk) return
const content = await inlineParsing(blk, { flag: 'manual-scheduled' })
if (content) await logseq.Editor.updateBlock(e.uuid, content)
})
logseq.Editor.registerSlashCommand(
'Parse scheduled',
async (e: { uuid: string }) => {
const blk = await logseq.Editor.getBlock(e.uuid)
if (!blk) return
const content = await inlineParsing(blk, { flag: 'manual-scheduled' })
if (content) await logseq.Editor.updateBlock(e.uuid, content)
},
)

logseq.Editor.registerSlashCommand('Parse deadline', async (e) => {
const blk = await logseq.Editor.getBlock(e.uuid)
if (!blk) return
const content = await inlineParsing(blk, { flag: 'manual-deadline' })
if (content) await logseq.Editor.updateBlock(e.uuid, content)
})
logseq.Editor.registerSlashCommand(
'Parse deadline',
async (e: { uuid: string }) => {
const blk = await logseq.Editor.getBlock(e.uuid)
if (!blk) return
const content = await inlineParsing(blk, { flag: 'manual-deadline' })
if (content) await logseq.Editor.updateBlock(e.uuid, content)
},
)
}
4 changes: 1 addition & 3 deletions src/features/parse/semi-auto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
} from 'logseq-dateutils'

import * as parse from '~/features/parse/index'
import { PluginSettings } from '~/settings/types'
import { getPreferredDateFormat } from '~/utils'

export const semiAutoParse = async (
Expand All @@ -16,8 +15,7 @@ export const semiAutoParse = async (
parsedStart: Date,
parsedEnd: Date | undefined,
): Promise<string> => {
const { dateChar, scheduledChar, deadlineChar } =
logseq.settings! as Partial<PluginSettings>
const { dateChar, scheduledChar, deadlineChar } = logseq.settings!
if (!dateChar || !scheduledChar || !deadlineChar) throw new Error()

// handle special characters in code
Expand Down
18 changes: 17 additions & 1 deletion src/settings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const settings: SettingSchemaDesc[] = [
{
key: 'insertDateProperty',
type: 'boolean',
default: true,
default: false,
title: 'Insert Date Property',
description:
'If set to true, when parsing dates, a date property wil be inserted instead of inline.',
Expand All @@ -34,6 +34,22 @@ export const settings: SettingSchemaDesc[] = [
title: 'Remove Time',
description: 'Remove time from scheduled and deadline parsing.',
},
{
key: 'gotoShortcut',
type: 'string',
default: 'mod+g',
title: 'Set shortcut of Go to Date',
description:
'(Requires restarting Logseq) Modify the shortcut to trigger the pop-up for Go to Date.',
},
{
key: 'completeTaskShortcut',
type: 'string',
default: 'mod+shift+d',
title: 'Set shortcut to Complete Task',
description:
'(Requires restarting Logseq) Modify the shortcut to mark a task complete.',
},
{
key: 'specialCharHeading',
type: 'heading',
Expand Down
10 changes: 0 additions & 10 deletions src/settings/types.ts

This file was deleted.

18 changes: 15 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
"compilerOptions": {
"jsx": "react-jsx",
"target": "es2017",
"lib": ["dom", "dom.iterable", "esnext"],
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"checkJs": true,
"skipLibCheck": true,
Expand All @@ -18,10 +22,18 @@
"noUncheckedIndexedAccess": true,
"baseUrl": "./",
"paths": {
"~/*": ["src/*"]
"~/*": [
"src/*"
]
}
},
"include": [".eslintrc.cjs", "**/*.ts", "**/*.tsx", "**/*.cjs", "**/*.mjs"],
"include": [
"**/*.ts",
"**/*.tsx",
"**/*.cjs",
"**/*.mjs",
"global.d.ts"
],
"exclude": [
"node_modules",
"./dist/**/*",
Expand Down

0 comments on commit 3e62dff

Please sign in to comment.