diff --git a/src/components/StackedNote.vue b/src/components/StackedNote.vue index 18d6323..face61e 100644 --- a/src/components/StackedNote.vue +++ b/src/components/StackedNote.vue @@ -41,11 +41,17 @@ const user = computed(() => props.user) const repo = computed(() => props.repo) const sha = computed(() => props.sha) const index = computed(() => props.index) -const editedSha = ref(sha.value) const { scrollToFocusedNote } = useRouteQueryStackedNotes() -const { path, content, rawContent, getRawContent, saveCacheNote } = useFile(sha) +const { + path, + content, + rawContent, + getRawContent, + saveCacheNote, + getEditedSha +} = useFile(sha) const initialRawContent = ref(null) const className = computed(() => `stacked-note-${props.index}`) const { listenToClick } = useLinks(className.value, sha) @@ -83,18 +89,19 @@ watch([content, mode], () => { watch(mode, async (newMode) => { if (newMode === 'read' && rawContent.value !== initialRawContent.value) { + const editedSha = (await getEditedSha()) ?? sha.value + const newSha = await updateFile({ content: rawContent.value, path: path.value, - sha: editedSha.value + sha: editedSha }) if (!newSha) { return } - editedSha.value = newSha - await saveCacheNote(encodeUTF8ToBase64(rawContent.value)) + await saveCacheNote(encodeUTF8ToBase64(rawContent.value), newSha) initialRawContent.value = rawContent.value } }) diff --git a/src/hooks/useComputeBacklinks.hook.ts b/src/hooks/useComputeBacklinks.hook.ts index f622c19..ac64362 100644 --- a/src/hooks/useComputeBacklinks.hook.ts +++ b/src/hooks/useComputeBacklinks.hook.ts @@ -17,7 +17,7 @@ const isMarkdown = (filename?: string) => filename?.endsWith('.md') ?? false export const useComputeBacklinks = () => { const store = useUserRepoStore() - watch(store, async () => { + watch(store,async () => { if (!store.userSettings?.backlink) { return } diff --git a/src/hooks/useFile.hook.ts b/src/hooks/useFile.hook.ts index 8dfde3c..a0fbe85 100644 --- a/src/hooks/useFile.hook.ts +++ b/src/hooks/useFile.hook.ts @@ -26,6 +26,16 @@ export const useFile = (sha: Ref | string, retrieveContent = true) => { rawContent.value ? renderFromUTF8(rawContent.value) : '' ) + const getEditedSha = async () => { + const note = await getCachedNote() + + if (!note) { + return null + } + + return note.editedSha ?? null + } + const getCachedFileContent = async (): Promise => { const cachedNote = await getCachedNote() @@ -86,6 +96,7 @@ export const useFile = (sha: Ref | string, retrieveContent = true) => { getRawContent, getContent, getCachedFileContent, + getEditedSha, fromCache, saveCacheNote } diff --git a/src/hooks/useGitHubContent.hook.ts b/src/hooks/useGitHubContent.hook.ts index e9e5b6a..a0bd38f 100644 --- a/src/hooks/useGitHubContent.hook.ts +++ b/src/hooks/useGitHubContent.hook.ts @@ -37,7 +37,7 @@ export const useGitHubContent = ({ return response?.data.content?.sha ?? null } catch (error) { - errorMessage('File could not be saved') + errorMessage('Note could not be saved') } return null diff --git a/src/modules/note/cache/prepareNoteCache.ts b/src/modules/note/cache/prepareNoteCache.ts index 1cac15b..7165cb5 100644 --- a/src/modules/note/cache/prepareNoteCache.ts +++ b/src/modules/note/cache/prepareNoteCache.ts @@ -6,11 +6,12 @@ export const prepareNoteCache = (sha: string) => { const noteId = data.generateId(DataType.Note, sha) const getCachedNote = async () => data.get(noteId) - const saveCacheNote = async (content: string) => { + const saveCacheNote = async (content: string, editedSha?: string) => { const newNote: Note = { _id: noteId, $type: DataType.Note, - content + content, + editedSha } await data.update(newNote) diff --git a/src/modules/note/models/Note.ts b/src/modules/note/models/Note.ts index f88d920..fa38c99 100644 --- a/src/modules/note/models/Note.ts +++ b/src/modules/note/models/Note.ts @@ -3,4 +3,5 @@ import { Model } from '@/data/models/Model' export interface Note extends Model { content: string + editedSha?: string }