Skip to content

Commit

Permalink
fix: lose child block after move block
Browse files Browse the repository at this point in the history
  • Loading branch information
haydenull committed Apr 23, 2022
1 parent 0d75bdd commit 96180ed
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
6 changes: 1 addition & 5 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ const App: React.FC<{ env: string }> = ({ env }) => {
console.log('[faiz:] === move journal schedule')
const { preferredDateFormat } = await logseq.App.getUserConfigs()
const journalName = format(dayjs(changes.start).valueOf(), preferredDateFormat)
const newBlock = await moveBlockToNewPage(schedule.raw?.id, journalName, _content)
const newBlock = await moveBlockToNewPage(schedule.raw?.id, journalName)
console.log('[faiz:] === newBlock', newBlock, schedule, schedule?.id)
if (newBlock) {
calendarRef.current?.deleteSchedule(String(schedule.id), schedule.calendarId)
Expand All @@ -362,10 +362,6 @@ const App: React.FC<{ env: string }> = ({ env }) => {
blockData: newBlock,
calendarConfig: calendarList?.find(calendar => calendar.id === 'journal'),
})])
// calendarRef.current?.updateSchedule(schedule.id, schedule.calendarId, { raw: {
// ...newBlock,
// page: await logseq.Editor.getPage(newBlock.page?.id),
// } })
}
} else {
await updateBlock(schedule.raw?.id, _content)
Expand Down
9 changes: 8 additions & 1 deletion src/components/ModifySchedule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,14 @@ const ModifySchedule: React.FC<{
})])
} else if (newCalendarId !== oldCalendarId && initialValues?.id) {
// move schedule: move block to new page
const newBlock = await moveBlockToNewPage(Number(initialValues.id), newCalendarId, newTitle, newBlockPropeties)
const newBlock = await moveBlockToNewPage(Number(initialValues.id), newCalendarId)
// journal 移动到 agenda 日历后,需要设置时间
await updateBlock(newBlock.uuid, newTitle, newBlockPropeties)
// agenda 日历移动到 journal 需要去除 start end 属性
if (Object.keys(newBlockPropeties).length === 0) {
await logseq.Editor.removeBlockProperty(newBlock.uuid, 'start')
await logseq.Editor.removeBlockProperty(newBlock.uuid, 'end')
}
if (!newBlock || !initialValues.calendarId) return
const _newBlock = await logseq.Editor.getBlock(newBlock.uuid)
// updateSchedule can't update id, so we need to create new schedule after delete old one
Expand Down
12 changes: 3 additions & 9 deletions src/util/logseq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,13 @@ export const updateBlock = async (blockId: number | string, content: string | fa
return Promise.allSettled(upsertBlockPropertyPromises)
}

export const moveBlockToNewPage = async (blockId: number, pageName: string, content?: string | false, properties?: Record<string, any>) => {
export const moveBlockToNewPage = async (blockId: number, pageName: string) => {
const block = await getBlockData({ id: blockId })
if (!block) return logseq.App.showMsg('moveBlockToNewPage: Block not found', 'error')
const page = await logseq.Editor.createPage(pageName)
if (!page) return logseq.App.showMsg('Create page failed', 'error')
await logseq.Editor.removeBlock(block.uuid)
const newBlock = await logseq.Editor.insertBlock(pageName, content || block?.content, {
isPageBlock: true,
sibling: true,
properties: properties || block?.properties,
})
if (newBlock) return await getBlockData({ uuid: newBlock.uuid })
return logseq.App.showMsg('Failed to move block to new page')
await logseq.Editor.moveBlock(block.uuid, page.uuid)
return await getBlockData({ uuid: block.uuid })
}

// https://logseq.github.io/plugins/interfaces/IEditorProxy.html#getBlock
Expand Down

0 comments on commit 96180ed

Please sign in to comment.