Skip to content

Commit

Permalink
added ability to open directory on successful export
Browse files Browse the repository at this point in the history
  • Loading branch information
iskaktoltay committed Oct 17, 2024
1 parent 218d6a4 commit 0114b6f
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 14 deletions.
5 changes: 4 additions & 1 deletion frontend/apps/desktop/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ ipcMain.on(
const titleCounter: {[key: string]: number} = {}
let success: {success: boolean; message: string} = {
success: true,
message: `Successfully exported documents to: ${exportDir}.`,
message: exportDir,
}

for (const {title, markdown} of documents) {
Expand Down Expand Up @@ -310,6 +310,9 @@ ipcMain.on(
},
)

ipcMain.on('open-directory', (_event, directory: string) => {
shell.openPath(directory)
})
ipcMain.on('open-external-link', (_event, linkUrl) => {
shell.openExternal(linkUrl)
})
Expand Down
3 changes: 3 additions & 0 deletions frontend/apps/desktop/src/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@ function MainApp({
platform={appInfo.platform()}
queryClient={queryClient}
ipc={ipc}
openDirectory={async (directory: string) => {
ipc.send?.('open-directory', directory)
}}
externalOpen={async (url: string) => {
ipc.send?.('open-external-link', url)
}}
Expand Down
2 changes: 1 addition & 1 deletion frontend/apps/desktop/src/save-markdown-file.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export async function saveMarkdownFile(
let updatedMarkdownContent = markdownContent
let success: {success: boolean; message: string} = {
success: true,
message: `Successfully exported ${title} to: ${filePath}.`,
message: filePath,
}

const uploadMediaFile = ({
Expand Down
4 changes: 4 additions & 0 deletions frontend/packages/app/app-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type AppContext = {
queryClient: AppQueryClient
ipc: AppIPC
externalOpen: (url: string) => Promise<void>
openDirectory: (directory: string) => Promise<void>
windowUtils: WindowUtils
saveCidAsFile: (cid: string, name: string) => Promise<void>
exportDocument: (
Expand Down Expand Up @@ -44,6 +45,7 @@ export function AppContextProvider({
queryClient,
ipc,
externalOpen,
openDirectory,
windowUtils,
saveCidAsFile,
exportDocument,
Expand All @@ -56,6 +58,7 @@ export function AppContextProvider({
queryClient: AppQueryClient
ipc: AppIPC
externalOpen: (url: string) => Promise<void>
openDirectory: (directory: string) => Promise<void>
windowUtils: WindowUtils
saveCidAsFile: (cid: string, name: string) => Promise<void>
exportDocument: (
Expand All @@ -82,6 +85,7 @@ export function AppContextProvider({
queryClient,
ipc,
externalOpen,
openDirectory,
windowUtils,
saveCidAsFile,
exportDocument,
Expand Down
28 changes: 26 additions & 2 deletions frontend/packages/app/components/export-doc-button.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {HMBlockNode, toHMBlock} from '@mintter/shared'
import {Button, Tooltip, toast} from '@mintter/ui'
import {Download} from '@tamagui/lucide-icons'
import {SizableText, YStack} from 'tamagui'
import {useAppContext} from '../app-context'
import {usePublication} from '../models/documents'
import {convertBlocksToMarkdown} from '../utils/blocks-to-markdown'
Expand All @@ -14,7 +15,7 @@ export const ExportDocButton = ({
}) => {
const pub = usePublication({id: docId, version: version})
const title = pub.data?.document?.title || 'document'
const {exportDocument} = useAppContext()
const {exportDocument, openDirectory} = useAppContext()
return (
<>
<Tooltip content={'Export Document to Markdown'}>
Expand All @@ -34,7 +35,30 @@ export const ExportDocButton = ({
const markdownWithTitle = `# ${title}\n\n${markdownContent}`
exportDocument(title, markdownWithTitle, mediaFiles)
.then((res) => {
toast.success(res)
const success = (
<>
<YStack gap="$1.5" maxWidth={700}>
<SizableText
wordWrap="break-word"
textOverflow="break-word"
>
Successfully exported document "{title}" to:{' '}
<b>{`${res}`}</b>.
</SizableText>
<SizableText
textDecorationLine="underline"
color="$blue9"
tag={'a'}
onPress={() => {
openDirectory(res)
}}
>
Show directory
</SizableText>
</YStack>
</>
)
toast.success('', {customContent: success})
})
.catch((err) => {
toast.error(err)
Expand Down
23 changes: 21 additions & 2 deletions frontend/packages/app/pages/export-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default function ExportPage() {
const [documents, setDocuments] = useState<HMPublication[]>([])
const [allSelected, setAllSelected] = useState(false)
const [loading, setLoading] = useState(false)
const {exportDocuments} = useAppContext()
const {exportDocuments, openDirectory} = useAppContext()

const route = useNavRoute()
if (route.key !== 'export') throw new Error('invalid route')
Expand Down Expand Up @@ -173,7 +173,26 @@ export default function ExportPage() {

exportDocuments(documentsToExport)
.then((res) => {
toast.success(res)
const success = (
<>
<YStack gap="$1.5" maxWidth={700}>
<SizableText wordWrap="break-word" textOverflow="break-word">
Successfully exported documents to: <b>{`${res}`}</b>.
</SizableText>
<SizableText
textDecorationLine="underline"
color="$blue9"
tag={'a'}
onPress={() => {
openDirectory(res)
}}
>
Show directory
</SizableText>
</YStack>
</>
)
toast.success('', {customContent: success})
setLoading(false)
})
.catch((err) => {
Expand Down
15 changes: 7 additions & 8 deletions frontend/packages/app/utils/blocks-to-markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function applyStyles(text, styles) {

function convertContentItemToHtml(
contentItem,
docMap: Map<string, {name: string; path: string}>,
docMap?: Map<string, {name: string; path: string}>,
) {
let text = contentItem.text || ''
const {styles = {}} = contentItem
Expand All @@ -27,10 +27,9 @@ function convertContentItemToHtml(
contentItem.content[0].text,
contentItem.content[0].styles || {},
)
const docPath = docMap.get(contentItem.href)
return `<a href="${
docPath ? docPath.path : contentItem.href
}">${linkText}</a>`
let docPath = contentItem.href
if (docMap) docPath = docMap.get(contentItem.href)
return `<a href="${docPath}">${linkText}</a>`
} else {
return text
}
Expand All @@ -39,7 +38,7 @@ function convertContentItemToHtml(
function convertBlockToHtml(
block,
isListItem = false,
docMap: Map<string, {name: string; path: string}>,
docMap?: Map<string, {name: string; path: string}>,
) {
let childrenHtml = ''
if (block.children) {
Expand Down Expand Up @@ -106,7 +105,7 @@ function convertBlockToHtml(

function convertBlocksToHtml(
blocks: HMBlock[],
docMap: Map<string, {name: string; path: string}>,
docMap?: Map<string, {name: string; path: string}>,
) {
const htmlContent: string = blocks
.map((block) => convertBlockToHtml(block, undefined, docMap))
Expand Down Expand Up @@ -156,7 +155,7 @@ async function extractMediaFiles(blocks: HMBlock[]) {

export async function convertBlocksToMarkdown(
blocks: HMBlock[],
docMap: Map<string, {name: string; path: string}>,
docMap?: Map<string, {name: string; path: string}>,
) {
const mediaFiles = await extractMediaFiles(blocks)
const markdownFile = await unified()
Expand Down

0 comments on commit 0114b6f

Please sign in to comment.