Skip to content

Commit

Permalink
feat: supports edit schedule by drag
Browse files Browse the repository at this point in the history
  • Loading branch information
haydenull committed Mar 19, 2022
1 parent 67500b8 commit 51562c0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
47 changes: 30 additions & 17 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import Calendar, { ISchedule } from 'tui-calendar'
import { Button, Modal, Select, Tooltip } from 'antd'
import { LeftOutlined, RightOutlined, SettingOutlined, ReloadOutlined, FullscreenOutlined, FullscreenExitOutlined } from '@ant-design/icons'
import { format, formatISO, isSameDay, parse } from 'date-fns'
import { getSchedules, ISettingsForm, managePluginTheme } from './util/util'
import { getSchedules, ISettingsForm, managePluginTheme, updateBlock } from './util/util'
import Settings from './components/Settings'
import Weekly from './components/Weekly'
import 'tui-calendar/dist/tui-calendar.css'
import './App.css'
import { CALENDAR_THEME, SHOW_DATE_FORMAT, CALENDAR_VIEWS } from './util/constants'
import ModifySchedule from './components/ModifySchedule'
import type { IScheduleValue } from './components/ModifySchedule'
import dayjs from 'dayjs'
import dayjs, { Dayjs } from 'dayjs'

const getDefaultOptions = () => {
let defaultView = logseq.settings?.defaultView || 'month'
Expand Down Expand Up @@ -266,22 +266,35 @@ const App: React.FC<{ env: string }> = ({ env }) => {
})
calendarRef.current?.render()
})
calendarRef.current.on('beforeUpdateSchedule', function(event) {
calendarRef.current.on('beforeUpdateSchedule', async function(event) {
console.log('[faiz:] === beforeUpdateSchedule', event)
const { schedule } = event
setModifyScheduleModal({
visible: true,
type: 'update',
values: {
id: schedule.id,
start: dayjs(schedule.start),
end: dayjs(schedule.end),
isAllDay: schedule.isAllDay,
calendarId: schedule.calendarId,
title: schedule.raw?.content?.split('\n')[0],
raw: schedule.raw,
},
})
const { schedule, changes, triggerEventName } = event
if (triggerEventName === 'click') {
setModifyScheduleModal({
visible: true,
type: 'update',
values: {
id: schedule.id,
start: dayjs(schedule.start),
end: dayjs(schedule.end),
isAllDay: schedule.isAllDay,
calendarId: schedule.calendarId,
title: schedule.raw?.content?.split('\n')[0],
raw: schedule.raw,
},
})
} else if (changes) {
let properties = {}
Object.keys(changes).forEach(key => {
if (schedule.isAllDay) {
properties[key] = dayjs(changes[key]).format('YYYY-MM-DD')
} else {
properties[key] = dayjs(changes[key]).format('YYYY-MM-DD HH:mm')
}
})
await updateBlock(schedule.id, false, properties)
setSchedules()
}
})
calendarRef.current.on('beforeDeleteSchedule', function(event) {
console.log('[faiz:] === beforeDeleteSchedule', event)
Expand Down
8 changes: 5 additions & 3 deletions src/util/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,14 +564,16 @@ export const copyToClipboard = (text: string) => {
document.body.removeChild(textArea)
}

export const updateBlock = async (blockId: number, content: string, properties?: Record<string, any>) => {
export const updateBlock = async (blockId: number, content: string | false, properties?: Record<string, any>) => {
const block = await logseq.Editor.getBlock(blockId)
if (!block) {
logseq.App.showMsg('Block not found', 'error')
return Promise.reject(new Error('Block not found'))
}
// propteties param not working
await logseq.Editor.updateBlock(block.uuid, content)
if (content) {
// propteties param not working
await logseq.Editor.updateBlock(block.uuid, content)
}
const upsertBlockPropertyPromises = Object.keys(properties || {}).map(key => logseq.Editor.upsertBlockProperty(block.uuid, key, properties?.[key]))
return Promise.allSettled(upsertBlockPropertyPromises)
}

0 comments on commit 51562c0

Please sign in to comment.