diff --git a/src/component/comments/src/gitment.js b/src/component/comments/src/gitment.js index 62ce272..6bce9f1 100644 --- a/src/component/comments/src/gitment.js +++ b/src/component/comments/src/gitment.js @@ -1,10 +1,6 @@ import { autorun, observable } from 'mobx' -import { - LS_ACCESS_TOKEN_KEY, - LS_USER_KEY, - NOT_INITIALIZED_ERROR, -} from './constants' +import { LS_ACCESS_TOKEN_KEY, LS_USER_KEY } from './constants' import { getTargetContainer, http, Query } from './utils' import defaultTheme from './theme/default' @@ -139,15 +135,6 @@ export default class Gitment { } } - init() { - return this.createIssue() - .then(() => this.loadComments()) - .then(comments => { - this.state.error = null - return comments - }) - } - useTheme(theme = {}) { this.theme = theme @@ -173,20 +160,6 @@ export default class Gitment { }) } - createIssue() { - const { id, owner, repo, title, link, desc, labels } = this - - return http - .post(`/repos/${owner}/${repo}/issues`, { - title, - body: `${link}\n\n${desc}`, - }) - .then(meta => { - this.state.meta = meta - return meta - }) - } - getIssue() { if (this.state.meta.id) return Promise.resolve(this.state.meta) @@ -207,16 +180,26 @@ export default class Gitment { } loadMeta() { - const { id, owner, repo } = this - return http - .get(`/repos/${owner}/${repo}/issues`, { - creator: owner, - }) - .then(issues => { - if (!issues.length) return Promise.reject(NOT_INITIALIZED_ERROR) - this.state.meta = issues[0] - return issues[0] - }) + const { id, owner, repo, gistId, type = 'issues' } = this + if (type === 'gist') { + return http + .get(`/repos/${owner}/${repo}/${type}/${gistId}`, { + creator: owner, + }) + .then(issue => { + this.state.meta = issue + return issue + }) + } else { + return http + .get(`/repos/${owner}/${repo}/${type}/${id}`, { + creator: owner, + }) + .then(issue => { + this.state.meta = issue + return issue + }) + } } loadComments(page = this.state.currentPage) { diff --git a/src/component/comments/src/theme/default.js b/src/component/comments/src/theme/default.js index b4921db..c8c3d27 100644 --- a/src/component/comments/src/theme/default.js +++ b/src/component/comments/src/theme/default.js @@ -1,25 +1,28 @@ -import { github as githubIcon, heart as heartIcon, spinner as spinnerIcon } from '../icons' +import { + github as githubIcon, + heart as heartIcon, + spinner as spinnerIcon, +} from '../icons' import { NOT_INITIALIZED_ERROR } from '../constants' function renderHeader({ meta, user, reactions }, instance) { const container = document.createElement('div') - container.lang = "en-US" + container.lang = 'en-US' container.className = 'gitment-container gitment-header-container' const likeButton = document.createElement('span') - const likedReaction = reactions.find(reaction => ( - reaction.content === 'heart' && reaction.user.login === user.login - )) + const likedReaction = reactions.find( + reaction => + reaction.content === 'heart' && reaction.user.login === user.login + ) likeButton.className = 'gitment-header-like-btn' likeButton.innerHTML = ` ${heartIcon} - ${ likedReaction - ? 'Unlike' - : 'Like' - } - ${ meta.reactions && meta.reactions.heart - ? ` • ${meta.reactions.heart} Liked` - : '' + ${likedReaction ? 'Unlike' : 'Like'} + ${ + meta.reactions && meta.reactions.heart + ? ` • ${meta.reactions.heart} Liked` + : '' } ` @@ -34,10 +37,7 @@ function renderHeader({ meta, user, reactions }, instance) { const commentsCount = document.createElement('span') commentsCount.innerHTML = ` - ${ meta.comments - ? ` • ${meta.comments} Comments` - : '' - } + ${meta.comments ? ` • ${meta.comments} Comments` : ''} ` container.appendChild(commentsCount) @@ -51,35 +51,19 @@ function renderHeader({ meta, user, reactions }, instance) { return container } -function renderComments({ meta, comments, commentReactions, currentPage, user, error }, instance) { +function renderComments( + { meta, comments, commentReactions, currentPage, user, error }, + instance +) { const container = document.createElement('div') - container.lang = "en-US" + container.lang = 'en-US' container.className = 'gitment-container gitment-comments-container' if (error) { const errorBlock = document.createElement('div') errorBlock.className = 'gitment-comments-error' - if (error === NOT_INITIALIZED_ERROR - && user.login - && user.login.toLowerCase() === instance.owner.toLowerCase()) { - const initHint = document.createElement('div') - const initButton = document.createElement('button') - initButton.className = 'gitment-comments-init-btn' - initButton.onclick = () => { - initButton.setAttribute('disabled', true) - instance.init() - .catch(e => { - initButton.removeAttribute('disabled') - alert(e) - }) - } - initButton.innerText = 'Initialize Comments' - initHint.appendChild(initButton) - errorBlock.appendChild(initHint) - } else { - errorBlock.innerText = error - } + errorBlock.innerText = error container.appendChild(errorBlock) return container } else if (comments === undefined) { @@ -105,30 +89,42 @@ function renderComments({ meta, comments, commentReactions, currentPage, user, e const commentItem = document.createElement('li') commentItem.className = 'gitment-comment' commentItem.innerHTML = ` - - + +
- + ${comment.user.login} commented on ${createDate.toDateString()} - ${ createDate.toString() !== updateDate.toString() - ? ` • edited` - : '' + ${ + createDate.toString() !== updateDate.toString() + ? ` • edited` + : '' } -
${heartIcon} ${comment.reactions.heart || ''}
+
${heartIcon} ${comment.reactions + .heart || ''}
-
${comment.body_html}
+
${ + comment.body_html + }
` const likeButton = commentItem.querySelector('.gitment-comment-like-btn') - const likedReaction = commentReactions[comment.id] - && commentReactions[comment.id].find(reaction => ( - reaction.content === 'heart' && reaction.user.login === user.login - )) + const likedReaction = + commentReactions[comment.id] && + commentReactions[comment.id].find( + reaction => + reaction.content === 'heart' && reaction.user.login === user.login + ) if (likedReaction) { likeButton.classList.add('liked') likeButton.onclick = () => instance.unlikeAComment(comment.id) @@ -142,7 +138,8 @@ function renderComments({ meta, comments, commentReactions, currentPage, user, e const imgTrigger = document.createElement('img') const markdownBody = commentItem.querySelector('.gitment-comment-body') imgTrigger.className = 'gitment-hidden' - imgTrigger.src = "data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" + imgTrigger.src = + 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==' imgTrigger.onload = () => { if (markdownBody.clientHeight > instance.maxCommentHeight) { markdownBody.classList.add('gitment-comment-body-folded') @@ -203,19 +200,24 @@ function renderComments({ meta, comments, commentReactions, currentPage, user, e function renderEditor({ user, error }, instance) { const container = document.createElement('div') - container.lang = "en-US" + container.lang = 'en-US' container.className = 'gitment-container gitment-editor-container' const shouldDisable = user.login && !error ? '' : 'disabled' const disabledTip = user.login ? '' : 'Login to Comment' container.innerHTML = ` - ${ user.login - ? ` + ${ + user.login + ? ` ` - : user.isLoggingIn - ? `
${spinnerIcon}
` - : ` + : user.isLoggingIn + ? `
${spinnerIcon}
` + : `
${githubIcon} ` } @@ -227,11 +229,14 @@ function renderEditor({ user, error }, instance) {
- ${ user.login - ? 'Logout' - : user.isLoggingIn - ? 'Logging in...' - : `Login with GitHub` + ${ + user.login + ? 'Logout' + : user.isLoggingIn + ? 'Logging in...' + : `Login with GitHub` }
@@ -252,7 +257,8 @@ function renderEditor({ user, error }, instance) { ` if (user.login) { - container.querySelector('.gitment-editor-logout-link').onclick = () => instance.logout() + container.querySelector('.gitment-editor-logout-link').onclick = () => + instance.logout() } const writeField = container.querySelector('.gitment-editor-write-field') @@ -266,11 +272,13 @@ function renderEditor({ user, error }, instance) { const clientHeight = textarea.clientHeight const scrollHeight = textarea.scrollHeight if (clientHeight < scrollHeight) { - textarea.style.height = (height + scrollHeight - clientHeight) + 'px' + textarea.style.height = height + scrollHeight - clientHeight + 'px' } } - const [writeTab, previewTab] = container.querySelectorAll('.gitment-editor-tab') + const [writeTab, previewTab] = container.querySelectorAll( + '.gitment-editor-tab' + ) writeTab.onclick = () => { writeTab.classList.add('gitment-selected') previewTab.classList.remove('gitment-selected') @@ -293,15 +301,15 @@ function renderEditor({ user, error }, instance) { } preview.innerText = 'Loading preview...' - instance.markdown(content) - .then(html => preview.innerHTML = html) + instance.markdown(content).then(html => (preview.innerHTML = html)) } const submitButton = container.querySelector('.gitment-editor-submit') submitButton.onclick = () => { submitButton.innerText = 'Submitting...' submitButton.setAttribute('disabled', true) - instance.post(textarea.value.trim()) + instance + .post(textarea.value.trim()) .then(data => { textarea.value = '' textarea.style.height = 'auto' @@ -320,7 +328,7 @@ function renderEditor({ user, error }, instance) { function renderFooter() { const container = document.createElement('div') - container.lang = "en-US" + container.lang = 'en-US' container.className = 'gitment-container gitment-footer-container' container.innerHTML = ` Powered by @@ -333,7 +341,7 @@ function renderFooter() { function render(state, instance) { const container = document.createElement('div') - container.lang = "en-US" + container.lang = 'en-US' container.className = 'gitment-container gitment-root-container' container.appendChild(instance.renderHeader(state, instance)) container.appendChild(instance.renderComments(state, instance)) @@ -342,4 +350,10 @@ function render(state, instance) { return container } -export default { render, renderHeader, renderComments, renderEditor, renderFooter } +export default { + render, + renderHeader, + renderComments, + renderEditor, + renderFooter, +}