diff --git a/src/components/SignInGithub.vue b/src/components/SignInGithub.vue index 232775a..0f8c24b 100644 --- a/src/components/SignInGithub.vue +++ b/src/components/SignInGithub.vue @@ -2,7 +2,7 @@ const GITHUB_URL = 'https://github.com/login/oauth/authorize' const CLIENT_ID = 'Iv1.12dc43d013ce3623' -const SCOPE = 'repo' +const SCOPE = 'repo%20workflow' const REDIRECT_URI = window.location.origin const url = new URL(GITHUB_URL) diff --git a/src/components/StackedNote.vue b/src/components/StackedNote.vue index 0b67efc..8834e42 100644 --- a/src/components/StackedNote.vue +++ b/src/components/StackedNote.vue @@ -1,7 +1,15 @@ @@ -96,7 +122,7 @@ watch([content, mode], () => { - + diff --git a/src/hooks/useFile.hook.ts b/src/hooks/useFile.hook.ts index 9fa8aad..f4bba38 100644 --- a/src/hooks/useFile.hook.ts +++ b/src/hooks/useFile.hook.ts @@ -1,4 +1,4 @@ -import { Ref, ref, toValue } from 'vue' +import { computed, Ref, ref, toValue } from 'vue' import { useMarkdown } from '@/hooks/useMarkdown.hook' import { prepareNoteCache } from '@/modules/note/cache/prepareNoteCache' @@ -6,10 +6,16 @@ import { getFileContent } from '@/modules/repo/services/repo' import { useUserRepoStore } from '@/modules/repo/store/userRepo.store' export const useFile = (sha: Ref | string, retrieveContent = true) => { + const store = useUserRepoStore() + + const path = computed(() => { + const file = store.files.find((file) => file.sha === toValue(sha)) + return file?.path ?? '' + }) + const { render, getRawContent: getRawContentFromFile } = useMarkdown( toValue(sha) ) - const store = useUserRepoStore() const { getCachedNote, saveCacheNote } = prepareNoteCache(toValue(sha)) const fromCache = ref(false) @@ -41,28 +47,37 @@ export const useFile = (sha: Ref | string, retrieveContent = true) => { const getRawContent = async () => { const fileContent = await getCachedFileContent() + if (!fileContent) { - return + return null } - rawContent.value = getRawContentFromFile(fileContent) - return rawContent.value + + return getRawContentFromFile(fileContent) } const getContent = async () => { const fileContent = await getCachedFileContent() + if (!fileContent) { - return + return null } - content.value = render(fileContent) - return content.value + + return render(fileContent) } if (retrieveContent) { - getContent() - getRawContent() + getCachedFileContent().then((fileContent) => { + if (!fileContent) { + return + } + + rawContent.value = getRawContentFromFile(fileContent) + content.value = render(fileContent) + }) } return { + path, content, rawContent, getRawContent, diff --git a/src/hooks/useGitHubUpdate.hook.ts b/src/hooks/useGitHubUpdate.hook.ts new file mode 100644 index 0000000..d664791 --- /dev/null +++ b/src/hooks/useGitHubUpdate.hook.ts @@ -0,0 +1,49 @@ +import { getOctokit } from '@/modules/repo/services/octo' +import { encodeUTF8ToBase64 } from '@/utils/decodeBase64ToUTF8' +import { confirmMessage, errorMessage } from '@/utils/notif' + +export const useGitHubUpdate = ({ + user, + repo, + sha +}: { + user: string + repo: string + sha: string +}) => { + const updateFile = async ({ + content, + path + }: { + content: string + path: string + }) => { + try { + const octokit = await getOctokit() + + const response = await octokit.request( + `PUT /repos/{owner}/{repo}/contents/{path}`, + { + owner: user, + repo, + path, + message: `Updating ${path} from Lite Note`, + content: encodeUTF8ToBase64(content), + sha + } + ) + + confirmMessage('file saved on GitHub') + + return response?.data.commit.sha ?? null + } catch (error) { + errorMessage('File could not be saved') + } + + return null + } + + return { + updateFile + } +} diff --git a/src/modules/note/components/EditNote.vue b/src/modules/note/components/EditNote.vue index 811818e..08c69fe 100644 --- a/src/modules/note/components/EditNote.vue +++ b/src/modules/note/components/EditNote.vue @@ -1,24 +1,24 @@ handleInput((e.target as any)?.innerText ?? '')" - >{{ initialContent }}{{ modelValue }} diff --git a/src/utils/decodeBase64ToUTF8.ts b/src/utils/decodeBase64ToUTF8.ts index 65b37d5..7103ef3 100644 --- a/src/utils/decodeBase64ToUTF8.ts +++ b/src/utils/decodeBase64ToUTF8.ts @@ -6,3 +6,7 @@ export const decodeBase64ToUTF8 = (content: string): string => { .join('') ) } +export const encodeUTF8ToBase64 = (content: string): string => { + const utf8Bytes: Uint8Array = new TextEncoder().encode(content) + return btoa(String.fromCharCode(...utf8Bytes)) +}
handleInput((e.target as any)?.innerText ?? '')" - >{{ initialContent }}