directly update edited content

This commit is contained in:
Julien Calixte
2023-08-26 18:37:21 +02:00
parent ccb486a0b6
commit 80888696c9
4 changed files with 43 additions and 21 deletions

View File

@@ -16,6 +16,7 @@ import { useNoteOverlay } from '@/hooks/useNoteOverlay.hook'
import { useRouteQueryStackedNotes } from '@/hooks/useRouteQueryStackedNotes.hook' import { useRouteQueryStackedNotes } from '@/hooks/useRouteQueryStackedNotes.hook'
import { useTitleNotes } from '@/hooks/useTitleNotes.hook' import { useTitleNotes } from '@/hooks/useTitleNotes.hook'
import { useUserRepoStore } from '@/modules/repo/store/userRepo.store' import { useUserRepoStore } from '@/modules/repo/store/userRepo.store'
import { encodeUTF8ToBase64 } from '@/utils/decodeBase64ToUTF8'
import { filenameToNoteTitle } from '@/utils/noteTitle' import { filenameToNoteTitle } from '@/utils/noteTitle'
import { generateTweets } from '@/utils/twitter' import { generateTweets } from '@/utils/twitter'
@@ -39,10 +40,11 @@ const user = computed(() => props.user)
const repo = computed(() => props.repo) const repo = computed(() => props.repo)
const sha = computed(() => props.sha) const sha = computed(() => props.sha)
const index = computed(() => props.index) const index = computed(() => props.index)
const editedSha = ref(sha.value)
const { scrollToFocusedNote } = useRouteQueryStackedNotes() const { scrollToFocusedNote } = useRouteQueryStackedNotes()
const { path, content, rawContent, getRawContent } = useFile(sha) const { path, content, rawContent, getRawContent, saveCacheNote } = useFile(sha)
const initialRawContent = ref<string | null>(null) const initialRawContent = ref<string | null>(null)
const className = computed(() => `stacked-note-${props.index}`) const className = computed(() => `stacked-note-${props.index}`)
const { listenToClick } = useLinks(className.value, sha) const { listenToClick } = useLinks(className.value, sha)
@@ -57,8 +59,7 @@ const displayedTitle = computed(() => filenameToNoteTitle(props.title))
const { updateFile } = useGitHubUpdate({ const { updateFile } = useGitHubUpdate({
user: user.value, user: user.value,
repo: repo.value, repo: repo.value
sha: sha.value
}) })
const mode = ref<'read' | 'edit'>('read') const mode = ref<'read' | 'edit'>('read')
@@ -82,9 +83,21 @@ watch([content, mode], () => {
}) })
}) })
watch(mode, (newMode) => { watch(mode, async (newMode) => {
if (newMode === 'read' && rawContent.value !== initialRawContent.value) { if (newMode === 'read' && rawContent.value !== initialRawContent.value) {
updateFile({ content: rawContent.value, path: path.value }) const newSha = await updateFile({
content: rawContent.value,
path: path.value,
sha: editedSha.value
})
if (!newSha) {
return
}
editedSha.value = newSha
await saveCacheNote(encodeUTF8ToBase64(rawContent.value))
initialRawContent.value = rawContent.value
} }
}) })
</script> </script>

View File

@@ -13,14 +13,18 @@ export const useFile = (sha: Ref<string> | string, retrieveContent = true) => {
return file?.path ?? '' return file?.path ?? ''
}) })
const { render, getRawContent: getRawContentFromFile } = useMarkdown( const {
toValue(sha) render,
) renderFromUTF8,
getRawContent: getRawContentFromFile
} = useMarkdown(toValue(sha))
const { getCachedNote, saveCacheNote } = prepareNoteCache(toValue(sha)) const { getCachedNote, saveCacheNote } = prepareNoteCache(toValue(sha))
const fromCache = ref(false) const fromCache = ref(false)
const content = ref('')
const rawContent = ref('') const rawContent = ref('')
const content = computed(() =>
rawContent.value ? renderFromUTF8(rawContent.value) : ''
)
const getCachedFileContent = async (): Promise<string | null> => { const getCachedFileContent = async (): Promise<string | null> => {
const cachedNote = await getCachedNote() const cachedNote = await getCachedNote()
@@ -72,7 +76,6 @@ export const useFile = (sha: Ref<string> | string, retrieveContent = true) => {
} }
rawContent.value = getRawContentFromFile(fileContent) rawContent.value = getRawContentFromFile(fileContent)
content.value = render(fileContent)
}) })
} }
@@ -83,6 +86,7 @@ export const useFile = (sha: Ref<string> | string, retrieveContent = true) => {
getRawContent, getRawContent,
getContent, getContent,
getCachedFileContent, getCachedFileContent,
fromCache fromCache,
saveCacheNote
} }
} }

View File

@@ -4,19 +4,19 @@ import { confirmMessage, errorMessage } from '@/utils/notif'
export const useGitHubUpdate = ({ export const useGitHubUpdate = ({
user, user,
repo, repo
sha
}: { }: {
user: string user: string
repo: string repo: string
sha: string
}) => { }) => {
const updateFile = async ({ const updateFile = async ({
content, content,
path path,
sha
}: { }: {
content: string content: string
path: string path: string
sha: string
}) => { }) => {
try { try {
const octokit = await getOctokit() const octokit = await getOctokit()
@@ -35,7 +35,9 @@ export const useGitHubUpdate = ({
confirmMessage('file saved on GitHub') confirmMessage('file saved on GitHub')
return response?.data.commit.sha ?? null console.log(response)
return response?.data.content?.sha ?? null
} catch (error) { } catch (error) {
errorMessage('File could not be saved') errorMessage('File could not be saved')
} }

View File

@@ -41,15 +41,18 @@ const md = new MarkdownIt({
export const useMarkdown = (defaultPrefix?: Ref<string> | string) => { export const useMarkdown = (defaultPrefix?: Ref<string> | string) => {
const getRawContent = (content: string) => decodeBase64ToUTF8(content) const getRawContent = (content: string) => decodeBase64ToUTF8(content)
const renderFromUTF8 = (content: string, prefix?: string) =>
content
? md.render(content, {
docId: defaultPrefix ? toValue(defaultPrefix) : prefix ?? ''
})
: ''
return { return {
toHTML: (content: string) => (content ? md.render(content) : ''), toHTML: (content: string) => (content ? md.render(content) : ''),
render: (content: string, prefix?: string) => render: (content: string, prefix?: string) =>
content renderFromUTF8(decodeBase64ToUTF8(content), prefix),
? md.render(decodeBase64ToUTF8(content), { renderFromUTF8,
docId: defaultPrefix ? toValue(defaultPrefix) : prefix ?? ''
})
: '',
getRawContent getRawContent
} }
} }