Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- img "OpenProject Admin"
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- img "John"
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- img "OpenProject Org"
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- img "Alice Johnson"
- img "Bob Smith"
- img "Charlie Brown"
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 29 additions & 6 deletions app/components/primer/beta/avatar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,33 @@ def initialize(src: nil, alt: nil, size: DEFAULT_SIZE, shape: DEFAULT_SHAPE, hre
@system_arguments[:classes] = class_names(
system_arguments[:classes],
"avatar",
"avatar-small" => size < SMALL_THRESHOLD,
"circle" => shape == DEFAULT_SHAPE,
"avatar-small" => @size < SMALL_THRESHOLD,
"circle" => @shape == DEFAULT_SHAPE,
"lh-0" => href # Addresses an overflow issue with linked avatars
)
end

def call
if @href
render(Primer::Beta::Link.new(href: @href, classes: @system_arguments[:classes])) do
render(Primer::BaseComponent.new(**@system_arguments.except(:classes))) { content }
avatar_element =
if @href
render(Primer::Beta::Link.new(href: @href, classes: @system_arguments[:classes])) do
render(Primer::BaseComponent.new(**@system_arguments.except(:classes))) { content }
end
else
render(Primer::BaseComponent.new(**@system_arguments)) { content }
end

# Wrap in Catalyst controller for client-side color correction (only for fallback SVGs)
if @system_arguments[:src]&.start_with?("data:image/svg+xml")
render(Primer::BaseComponent.new(
tag: :"avatar-fallback",
data: {
unique_id: @unique_id,
alt_text: @alt
}
)) { avatar_element }
else
render(Primer::BaseComponent.new(**@system_arguments)) { content }
avatar_element
end
end

Expand Down Expand Up @@ -129,6 +143,15 @@ def generate_avatar_color
"hsl(#{value_hash(text)}, 50%, 30%)"
end

# Mimics OP Core's string hash function to ensure consistent color generation
#
# Note: Due to differences in integer overflow handling between JavaScript and Ruby,
# the generated hash values differ, hence the color correction in the TS component.
#
# @see https://github.com/opf/openproject/blob/1b6eb3f9e45c3bdb05ce49d2cbe92995b87b4df5/frontend/src/app/shared/components/colors/colors.service.ts#L19-L26
#
# @param value [String] The input string
# @return [Integer] A hash value between 0 and 359
def value_hash(value)
return 0 if value.blank?

Expand Down
59 changes: 59 additions & 0 deletions app/components/primer/beta/avatar_fallback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { attr, controller } from '@github/catalyst'

@controller
export class AvatarFallbackElement extends HTMLElement {
@attr uniqueId = ''
@attr altText = ''

connectedCallback() {
// Only update color if we have the necessary data and an SVG fallback
if (!this.uniqueId || !this.altText) return

const img = this.querySelector('img[src^="data:image/svg+xml"]')
if (!img) return

// Calculate correct color using OP Core hash algorithm
const text = `${this.uniqueId}${this.altText}`
const hue = this.valueHash(text)
const color = `hsl(${hue}, 50%, 30%)`

// Update SVG with correct color
this.updateSvgColor(img as HTMLImageElement, color)
}

/*
* Mimics OP Core's string hash function to ensure consistent color generation
* @see https://github.com/opf/openproject/blob/1b6eb3f9e45c3bdb05ce49d2cbe92995b87b4df5/frontend/src/app/shared/components/colors/colors.service.ts#L19-L26
*/
private valueHash(value: string): number {
let hash = 0
for (let i = 0; i < value.length; i++) {
hash = value.charCodeAt(i) + ((hash << 5) - hash)
}
return hash % 360
}

private updateSvgColor(img: HTMLImageElement, color: string) {
// Decode current SVG
const dataUri = img.src
const base64 = dataUri.replace('data:image/svg+xml;base64,', '')
const svg = atob(base64)

// Replace fill color in rect element
const updatedSvg = svg.replace(/fill="hsl\([^"]+\)"/, `fill="${color}"`)

// Encode and update
img.src = `data:image/svg+xml;base64,${btoa(updatedSvg)}`
}
}

declare global {
interface Window {
AvatarFallbackElement: typeof AvatarFallbackElement
}
}

if (!window.customElements.get('avatar-fallback')) {
window.AvatarFallbackElement = AvatarFallbackElement
window.customElements.define('avatar-fallback', AvatarFallbackElement)
}
1 change: 1 addition & 0 deletions app/components/primer/primer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import './alpha/toggle_switch'
import './alpha/tool_tip'
import './alpha/x_banner'
import './beta/auto_complete/auto_complete'
import './beta/avatar_fallback'
import './beta/clipboard_copy'
import './beta/relative_time'
import './alpha/tab_container'
Expand Down
2 changes: 1 addition & 1 deletion previews/primer/beta/avatar_preview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def shape_square
# @label Fallback default
# @snapshot
def fallback_default
render(Primer::Beta::Avatar.new(alt: "OpenProject Admin", unique_id: 1))
render(Primer::Beta::Avatar.new(alt: "OpenProject Admin", unique_id: 4))
end

# @label Fallback single name
Expand Down
17 changes: 17 additions & 0 deletions test/components/beta/avatar_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,23 @@ def test_raises_when_both_src_and_alt_are_blank
assert_includes(error.message, "`src` or `alt` is required")
end

def test_fallback_wrapped_in_catalyst_controller
render_inline(Primer::Beta::Avatar.new(src: nil, alt: "Test User", unique_id: 123))

# Should be wrapped in avatar-fallback element with data attributes
assert_selector("avatar-fallback[data-unique-id='123'][data-alt-text='Test User']") do
assert_selector("img.avatar[src^='data:image/svg+xml']")
end
end

def test_real_image_not_wrapped_in_catalyst_controller
render_inline(Primer::Beta::Avatar.new(src: "https://example.com/avatar.png", alt: "Test"))

# Real images should NOT be wrapped
refute_selector("avatar-fallback")
assert_selector("img.avatar[src='https://example.com/avatar.png']")
end

def test_status
assert_component_state(Primer::Beta::Avatar, :beta)
end
Expand Down