save editedSha in cached note to remember even after navigations

This commit is contained in:
Julien Calixte
2023-09-06 22:49:44 +02:00
parent 746082c688
commit 2f6e8e7ca8
6 changed files with 29 additions and 9 deletions

View File

@@ -41,11 +41,17 @@ 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, saveCacheNote } = useFile(sha) const {
path,
content,
rawContent,
getRawContent,
saveCacheNote,
getEditedSha
} = 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)
@@ -83,18 +89,19 @@ watch([content, mode], () => {
watch(mode, async (newMode) => { watch(mode, async (newMode) => {
if (newMode === 'read' && rawContent.value !== initialRawContent.value) { if (newMode === 'read' && rawContent.value !== initialRawContent.value) {
const editedSha = (await getEditedSha()) ?? sha.value
const newSha = await updateFile({ const newSha = await updateFile({
content: rawContent.value, content: rawContent.value,
path: path.value, path: path.value,
sha: editedSha.value sha: editedSha
}) })
if (!newSha) { if (!newSha) {
return return
} }
editedSha.value = newSha await saveCacheNote(encodeUTF8ToBase64(rawContent.value), newSha)
await saveCacheNote(encodeUTF8ToBase64(rawContent.value))
initialRawContent.value = rawContent.value initialRawContent.value = rawContent.value
} }
}) })

View File

@@ -17,7 +17,7 @@ const isMarkdown = (filename?: string) => filename?.endsWith('.md') ?? false
export const useComputeBacklinks = () => { export const useComputeBacklinks = () => {
const store = useUserRepoStore() const store = useUserRepoStore()
watch(store, async () => { watch(store,async () => {
if (!store.userSettings?.backlink) { if (!store.userSettings?.backlink) {
return return
} }

View File

@@ -26,6 +26,16 @@ export const useFile = (sha: Ref<string> | string, retrieveContent = true) => {
rawContent.value ? renderFromUTF8(rawContent.value) : '' rawContent.value ? renderFromUTF8(rawContent.value) : ''
) )
const getEditedSha = async () => {
const note = await getCachedNote()
if (!note) {
return null
}
return note.editedSha ?? null
}
const getCachedFileContent = async (): Promise<string | null> => { const getCachedFileContent = async (): Promise<string | null> => {
const cachedNote = await getCachedNote() const cachedNote = await getCachedNote()
@@ -86,6 +96,7 @@ export const useFile = (sha: Ref<string> | string, retrieveContent = true) => {
getRawContent, getRawContent,
getContent, getContent,
getCachedFileContent, getCachedFileContent,
getEditedSha,
fromCache, fromCache,
saveCacheNote saveCacheNote
} }

View File

@@ -37,7 +37,7 @@ export const useGitHubContent = ({
return response?.data.content?.sha ?? null return response?.data.content?.sha ?? null
} catch (error) { } catch (error) {
errorMessage('File could not be saved') errorMessage('Note could not be saved')
} }
return null return null

View File

@@ -6,11 +6,12 @@ export const prepareNoteCache = (sha: string) => {
const noteId = data.generateId(DataType.Note, sha) const noteId = data.generateId(DataType.Note, sha)
const getCachedNote = async () => data.get<DataType.Note, Note>(noteId) const getCachedNote = async () => data.get<DataType.Note, Note>(noteId)
const saveCacheNote = async (content: string) => { const saveCacheNote = async (content: string, editedSha?: string) => {
const newNote: Note = { const newNote: Note = {
_id: noteId, _id: noteId,
$type: DataType.Note, $type: DataType.Note,
content content,
editedSha
} }
await data.update(newNote) await data.update(newNote)

View File

@@ -3,4 +3,5 @@ import { Model } from '@/data/models/Model'
export interface Note extends Model<DataType.Note> { export interface Note extends Model<DataType.Note> {
content: string content: string
editedSha?: string
} }