From 43caac1a4648f07ac70f1878de36d0c924f366c7 Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Mon, 27 May 2019 12:23:42 -0400 Subject: [PATCH] Fixes #734 - broken hover image with remote dev --- src/avatars.ts | 44 +++++++++++++++++++++++++++++++++++ src/container.ts | 2 +- src/git/formatters/commitFormatter.ts | 5 ++-- src/git/models/commit.ts | 2 +- src/git/models/contributor.ts | 2 +- src/gravatar.ts | 25 -------------------- 6 files changed, 49 insertions(+), 31 deletions(-) create mode 100644 src/avatars.ts delete mode 100644 src/gravatar.ts diff --git a/src/avatars.ts b/src/avatars.ts new file mode 100644 index 0000000..7265e1a --- /dev/null +++ b/src/avatars.ts @@ -0,0 +1,44 @@ +'use strict'; +import * as fs from 'fs'; +import { Uri } from 'vscode'; +import { GravatarDefaultStyle } from './config'; +import { Strings } from './system'; +import { ContactPresenceStatus } from './vsls/vsls'; +import { Container } from './container'; + +const gravatarCache = new Map(); +const missingGravatarHash = '00000000000000000000000000000000'; + +const presenceCache = new Map(); + +export function clearGravatarCache() { + gravatarCache.clear(); +} + +export function getGravatarUri(email: string | undefined, fallback: GravatarDefaultStyle, size: number = 16): Uri { + const hash = + email != null && email.length !== 0 ? Strings.md5(email.trim().toLowerCase(), 'hex') : missingGravatarHash; + + const key = `${hash}:${size}`; + let gravatar = gravatarCache.get(key); + if (gravatar !== undefined) return gravatar; + + gravatar = Uri.parse(`https://www.gravatar.com/avatar/${hash}.jpg?s=${size}&d=${fallback}`); + gravatarCache.set(key, gravatar); + + return gravatar; +} + +export function getPresenceDataUri(status: ContactPresenceStatus) { + let dataUri = presenceCache.get(status); + if (dataUri === undefined) { + const contents = fs + .readFileSync(Container.context.asAbsolutePath(`images/dark/icon-presence-${status}.svg`)) + .toString('base64'); + + dataUri = encodeURI(`data:image/svg+xml;base64,${contents}`); + presenceCache.set(status, dataUri); + } + + return dataUri; +} diff --git a/src/container.ts b/src/container.ts index 2f87572..d7bff6d 100644 --- a/src/container.ts +++ b/src/container.ts @@ -7,7 +7,7 @@ import { Commands, ToggleFileBlameCommandArgs } from './commands'; import { AnnotationsToggleMode, Config, configuration, ConfigurationWillChangeEvent } from './configuration'; import { GitFileSystemProvider } from './git/fsProvider'; import { GitService } from './git/gitService'; -import { clearGravatarCache } from './gravatar'; +import { clearGravatarCache } from './avatars'; import { LineHoverController } from './hovers/lineHoverController'; import { Keyboard } from './keyboard'; import { Logger, TraceLevel } from './logger'; diff --git a/src/git/formatters/commitFormatter.ts b/src/git/formatters/commitFormatter.ts index ae8c405..5e4af8d 100644 --- a/src/git/formatters/commitFormatter.ts +++ b/src/git/formatters/commitFormatter.ts @@ -15,6 +15,7 @@ import { Strings } from '../../system'; import { FormatOptions, Formatter } from './formatter'; import * as emojis from '../../emojis.json'; import { ContactPresence } from '../../vsls/vsls'; +import { getPresenceDataUri } from '../../avatars'; const emptyStr = ''; const emojiMap: { [key: string]: string } = emojis; @@ -134,9 +135,7 @@ export class CommitFormatter extends Formatter { presence.status === 'dnd' ? 'in ' : '' }${presence.statusText.toLocaleLowerCase()}`; - avatar += `![${title}](${encodeURI( - `file:///${Container.context.asAbsolutePath(`images/dark/icon-presence-${presence.status}.svg`)}` - )})`; + avatar += `![${title}](${getPresenceDataUri(presence.status)})`; avatar = `[${avatar}](# "${title}")`; } diff --git a/src/git/models/commit.ts b/src/git/models/commit.ts index ba646a8..4010e5c 100644 --- a/src/git/models/commit.ts +++ b/src/git/models/commit.ts @@ -6,7 +6,7 @@ import { Dates, memoize } from '../../system'; import { CommitFormatter } from '../formatters/formatters'; import { Git } from '../git'; import { GitUri } from '../gitUri'; -import { getGravatarUri } from '../../gravatar'; +import { getGravatarUri } from '../../avatars'; export interface GitAuthor { name: string; diff --git a/src/git/models/contributor.ts b/src/git/models/contributor.ts index 21c1377..a4710ad 100644 --- a/src/git/models/contributor.ts +++ b/src/git/models/contributor.ts @@ -1,7 +1,7 @@ 'use strict'; import { Uri } from 'vscode'; import { GravatarDefaultStyle } from '../../configuration'; -import { getGravatarUri } from '../../gravatar'; +import { getGravatarUri } from '../../avatars'; export class GitContributor { constructor( diff --git a/src/gravatar.ts b/src/gravatar.ts deleted file mode 100644 index 32dd147..0000000 --- a/src/gravatar.ts +++ /dev/null @@ -1,25 +0,0 @@ -'use strict'; -import { Uri } from 'vscode'; -import { GravatarDefaultStyle } from './config'; -import { Strings } from './system'; - -const gravatarCache: Map = new Map(); -const missingGravatarHash = '00000000000000000000000000000000'; - -export function clearGravatarCache() { - gravatarCache.clear(); -} - -export function getGravatarUri(email: string | undefined, fallback: GravatarDefaultStyle, size: number = 16): Uri { - const hash = - email != null && email.length !== 0 ? Strings.md5(email.trim().toLowerCase(), 'hex') : missingGravatarHash; - - const key = `${hash}:${size}`; - let gravatar = gravatarCache.get(key); - if (gravatar !== undefined) return gravatar; - - gravatar = Uri.parse(`https://www.gravatar.com/avatar/${hash}.jpg?s=${size}&d=${fallback}`); - gravatarCache.set(key, gravatar); - - return gravatar; -}