-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DEV: Add support for glimmer topic list #60
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
164 changes: 164 additions & 0 deletions
164
javascripts/discourse/components/topic-list-thumbnail.gjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
import Component from "@glimmer/component"; | ||
import { service } from "@ember/service"; | ||
import coldAgeClass from "discourse/helpers/cold-age-class"; | ||
import concatClass from "discourse/helpers/concat-class"; | ||
import formatDate from "discourse/helpers/format-date"; | ||
import dIcon from "discourse-common/helpers/d-icon"; | ||
|
||
export default class TopicListThumbnail extends Component { | ||
@service topicThumbnails; | ||
|
||
responsiveRatios = [1, 1.5, 2]; | ||
|
||
// Make sure to update about.json thumbnail sizes if you change these variables | ||
get displayWidth() { | ||
return this.topicThumbnails.displayList | ||
? settings.list_thumbnail_size | ||
: 400; | ||
} | ||
|
||
get topic() { | ||
return this.args.topic; | ||
} | ||
|
||
get hasThumbnail() { | ||
return !!this.topic.thumbnails; | ||
} | ||
|
||
get srcSet() { | ||
const srcSetArray = []; | ||
|
||
this.responsiveRatios.forEach((ratio) => { | ||
const target = ratio * this.displayWidth; | ||
const match = this.topic.thumbnails.find( | ||
(t) => t.url && t.max_width === target | ||
); | ||
if (match) { | ||
srcSetArray.push(`${match.url} ${ratio}x`); | ||
} | ||
}); | ||
|
||
if (srcSetArray.length === 0) { | ||
srcSetArray.push(`${this.original.url} 1x`); | ||
} | ||
|
||
return srcSetArray.join(","); | ||
} | ||
|
||
get original() { | ||
return this.topic.thumbnails[0]; | ||
} | ||
|
||
get width() { | ||
return this.original.width; | ||
} | ||
|
||
get isLandscape() { | ||
return this.original.width >= this.original.height; | ||
} | ||
|
||
get height() { | ||
return this.original.height; | ||
} | ||
|
||
get fallbackSrc() { | ||
const largeEnough = this.topic.thumbnails.filter((t) => { | ||
if (!t.url) { | ||
return false; | ||
} | ||
return t.max_width > this.displayWidth * this.responsiveRatios.lastObject; | ||
}); | ||
|
||
if (largeEnough.lastObject) { | ||
return largeEnough.lastObject.url; | ||
} | ||
|
||
return this.original.url; | ||
} | ||
|
||
get url() { | ||
return this.topic.get("linked_post_number") | ||
? this.topic.urlForPostNumber(this.topic.get("linked_post_number")) | ||
: this.topic.get("lastUnreadUrl"); | ||
} | ||
|
||
<template> | ||
<div | ||
class={{concatClass | ||
"topic-list-thumbnail" | ||
(if this.hasThumbnail "has-thumbnail" "no-thumbnail") | ||
}} | ||
> | ||
<a href={{this.url}}> | ||
{{#if this.hasThumbnail}} | ||
<img | ||
class="background-thumbnail" | ||
src={{this.fallbackSrc}} | ||
srcset={{this.srcSet}} | ||
width={{this.width}} | ||
height={{this.height}} | ||
loading="lazy" | ||
/> | ||
<img | ||
class="main-thumbnail" | ||
src={{this.fallbackSrc}} | ||
srcset={{this.srcSet}} | ||
width={{this.width}} | ||
height={{this.height}} | ||
loading="lazy" | ||
/> | ||
{{else}} | ||
<div class="thumbnail-placeholder"> | ||
{{dIcon settings.placeholder_icon}} | ||
</div> | ||
{{/if}} | ||
</a> | ||
</div> | ||
|
||
{{#if this.topicThumbnails.showLikes}} | ||
<div class="topic-thumbnail-likes"> | ||
{{dIcon "heart"}} | ||
<span class="number"> | ||
{{this.topic.like_count}} | ||
</span> | ||
</div> | ||
{{/if}} | ||
|
||
{{#if this.topicThumbnails.displayBlogStyle}} | ||
<div class="topic-thumbnail-blog-data"> | ||
<div class="topic-thumbnail-blog-data-views"> | ||
{{dIcon "eye"}} | ||
<span class="number"> | ||
{{this.topic.views}} | ||
</span> | ||
</div> | ||
<div class="topic-thumbnail-blog-data-likes"> | ||
{{dIcon "heart"}} | ||
<span class="number"> | ||
{{this.topic.like_count}} | ||
</span> | ||
</div> | ||
<div class="topic-thumbnail-blog-data-comments"> | ||
{{dIcon "comment"}} | ||
<span class="number"> | ||
{{this.topic.reply_count}} | ||
</span> | ||
</div> | ||
<div | ||
class={{concatClass | ||
"topic-thumbnail-blog-data-activity" | ||
"activity" | ||
(coldAgeClass | ||
this.topic.createdAt startDate=this.topic.bumpedAt class="" | ||
) | ||
}} | ||
title={{this.topic.bumpedAtTitle}} | ||
> | ||
<a class="post-activity" href={{this.topic.lastPostUrl}}> | ||
{{~formatDate this.topic.bumpedAt format="tiny" noTitle="true"~}} | ||
</a> | ||
</div> | ||
</div> | ||
{{/if}} | ||
</template> | ||
} |
61 changes: 61 additions & 0 deletions
61
javascripts/discourse/connectors/before-topic-list-body/topic-list-masonry-styles.gjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import Component from "@glimmer/component"; | ||
import { service } from "@ember/service"; | ||
import { modifier } from "ember-modifier"; | ||
import MasonryCalculator from "../../lib/masonry-calculator"; | ||
|
||
export default class TopicListMasonryStyles extends Component { | ||
@service topicThumbnails; | ||
|
||
attachResizeObserver = modifier((element) => { | ||
const topicList = element.closest(".topic-list"); | ||
|
||
if (!topicList) { | ||
// eslint-disable-next-line no-console | ||
console.error( | ||
"topic-list-thumbnails resize-observer must be inside a topic-list" | ||
); | ||
return; | ||
} | ||
|
||
this.topicThumbnails.masonryContainerWidth = | ||
topicList.getBoundingClientRect().width; | ||
|
||
const observer = new ResizeObserver(() => { | ||
this.topicThumbnails.masonryContainerWidth = | ||
topicList.getBoundingClientRect().width; | ||
}); | ||
observer.observe(topicList); | ||
|
||
return () => { | ||
observer.disconnect(); | ||
this.topicThumbnails.masonryContainerWidth = null; | ||
}; | ||
}); | ||
|
||
get masonryStyle() { | ||
if (!this.topicThumbnails.displayMasonry) { | ||
return; | ||
} | ||
|
||
if (!this.topicThumbnails.masonryContainerWidth) { | ||
return; | ||
} | ||
|
||
const calculator = new MasonryCalculator( | ||
this.topicThumbnails, | ||
this.args.outletArgs.topics, | ||
this.topicThumbnails.masonryContainerWidth | ||
); | ||
calculator.calculateMasonryLayout(); | ||
return calculator.masonryStyle; | ||
} | ||
|
||
<template> | ||
{{#if this.topicThumbnails.displayMasonry}} | ||
{{! template-lint-disable no-forbidden-elements }} | ||
<style {{this.attachResizeObserver}}> | ||
{{this.masonryStyle}} | ||
</style> | ||
{{/if}} | ||
</template> | ||
} |
1 change: 1 addition & 0 deletions
1
javascripts/discourse/connectors/topic-list-before-columns/topic-thumbnail.hbr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
{{!-- has-modern-replacement --}} | ||
{{~raw "topic-list-thumbnail" topic=context.topic location="before-columns"}} |
2 changes: 0 additions & 2 deletions
2
javascripts/discourse/connectors/topic-list-before-link/topic-thumbnail.hbr
This file was deleted.
Oops, something went wrong.
79 changes: 79 additions & 0 deletions
79
javascripts/discourse/initializers/topic-thumbnails-init-legacy.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { readOnly } from "@ember/object/computed"; | ||
import { service } from "@ember/service"; | ||
import { withPluginApi } from "discourse/lib/plugin-api"; | ||
import { withSilencedDeprecations } from "discourse-common/lib/deprecated"; | ||
import { | ||
getResolverOption, | ||
setResolverOption, | ||
} from "discourse-common/resolver"; | ||
import { observes } from "discourse-common/utils/decorators"; | ||
export default { | ||
name: "topic-thumbnails-init", | ||
initialize() { | ||
withSilencedDeprecations("discourse.hbr-topic-list-overrides", () => { | ||
withPluginApi("0.8.7", (api) => this.initWithApi(api)); | ||
}); | ||
}, | ||
|
||
initWithApi(api) { | ||
api.modifyClass("component:topic-list", { | ||
pluginId: "topic-thumbnails", | ||
topicThumbnailsService: service("topic-thumbnails"), | ||
classNameBindings: [ | ||
"isMinimalGrid:topic-thumbnails-minimal", | ||
"isThumbnailGrid:topic-thumbnails-grid", | ||
"isThumbnailList:topic-thumbnails-list", | ||
"isMasonryList:topic-thumbnails-masonry", | ||
"isBlogStyleGrid:topic-thumbnails-blog-style-grid", | ||
], | ||
isMinimalGrid: readOnly("topicThumbnailsService.displayMinimalGrid"), | ||
isThumbnailGrid: readOnly("topicThumbnailsService.displayGrid"), | ||
isThumbnailList: readOnly("topicThumbnailsService.displayList"), | ||
isMasonryList: readOnly("topicThumbnailsService.displayMasonry"), | ||
isBlogStyleGrid: readOnly("topicThumbnailsService.displayBlogStyle"), | ||
}); | ||
|
||
api.modifyClass("component:topic-list-item", { | ||
pluginId: "topic-thumbnails", | ||
topicThumbnailsService: service("topic-thumbnails"), | ||
|
||
// Hack to disable the mobile topic-list-item template | ||
// Our grid styling is responsive, and uses the desktop HTML structure | ||
@observes("topic.pinned") | ||
renderTopicListItem() { | ||
const wasMobileView = getResolverOption("mobileView"); | ||
if ( | ||
wasMobileView && | ||
(this.topicThumbnailsService.displayGrid || | ||
this.topicThumbnailsService.displayMasonry || | ||
this.topicThumbnailsService.displayMinimalGrid || | ||
this.topicThumbnailsService.displayBlogStyle) | ||
) { | ||
setResolverOption("mobileView", false); | ||
} | ||
|
||
this._super(); | ||
|
||
if (wasMobileView) { | ||
setResolverOption("mobileView", true); | ||
} | ||
}, | ||
}); | ||
|
||
api.modifyClass( | ||
"component:topic-list-item", | ||
(Superclass) => | ||
class extends Superclass { | ||
@service topicThumbnails; | ||
|
||
get classNames() { | ||
const result = super.classNames; | ||
if (this.topicThumbnails.displayMasonry) { | ||
return [...result, `masonry-${this.index}`]; | ||
} | ||
return result; | ||
} | ||
} | ||
); | ||
}, | ||
}; |
78 changes: 78 additions & 0 deletions
78
javascripts/discourse/initializers/topic-thumbnails-init.gjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { readOnly } from "@ember/object/computed"; | ||
import { service } from "@ember/service"; | ||
import { apiInitializer } from "discourse/lib/api"; | ||
import TopicListThumbnail from "../components/topic-list-thumbnail"; | ||
|
||
export default apiInitializer("0.8", (api) => { | ||
const ttService = api.container.lookup("service:topic-thumbnails"); | ||
|
||
api.registerValueTransformer("topic-list-class", ({ value }) => { | ||
if (ttService.displayMinimalGrid) { | ||
value.push("topic-thumbnails-minimal"); | ||
} else if (ttService.displayGrid) { | ||
value.push("topic-thumbnails-grid"); | ||
} else if (ttService.displayList) { | ||
value.push("topic-thumbnails-list"); | ||
} else if (ttService.displayMasonry) { | ||
value.push("topic-thumbnails-masonry"); | ||
} else if (ttService.displayBlogStyle) { | ||
value.push("topic-thumbnails-blog-style-grid"); | ||
} | ||
return value; | ||
}); | ||
|
||
api.registerValueTransformer("topic-list-columns", ({ value: columns }) => { | ||
if (ttService.enabledForRoute && !ttService.displayList) { | ||
columns.add( | ||
"thumbnail", | ||
{ item: TopicListThumbnail }, | ||
{ before: "topic" } | ||
); | ||
} | ||
return columns; | ||
}); | ||
|
||
api.renderInOutlet("topic-list-before-link", <template> | ||
{{#if ttService.displayList}} | ||
<TopicListThumbnail @topic={{@outletArgs.topic}} /> | ||
{{/if}} | ||
</template>); | ||
|
||
api.registerValueTransformer("topic-list-item-mobile-layout", ({ value }) => { | ||
if (ttService.enabledForRoute && !ttService.displayList) { | ||
// Force the desktop layout | ||
return false; | ||
} | ||
return value; | ||
}); | ||
|
||
api.registerValueTransformer( | ||
"topic-list-item-class", | ||
({ value, context: { index } }) => { | ||
if (ttService.displayMasonry) { | ||
value.push(`masonry-${index}`); | ||
} | ||
return value; | ||
} | ||
); | ||
|
||
const siteSettings = api.container.lookup("service:site-settings"); | ||
if (settings.docs_thumbnail_mode !== "none" && siteSettings.docs_enabled) { | ||
api.modifyClass("component:docs-topic-list", { | ||
pluginId: "topic-thumbnails", | ||
topicThumbnailsService: service("topic-thumbnails"), | ||
classNameBindings: [ | ||
"isMinimalGrid:topic-thumbnails-minimal", | ||
"isThumbnailGrid:topic-thumbnails-grid", | ||
"isThumbnailList:topic-thumbnails-list", | ||
"isMasonryList:topic-thumbnails-masonry", | ||
"isBlogStyleGrid:topic-thumbnails-blog-style-grid", | ||
], | ||
isMinimalGrid: readOnly("topicThumbnailsService.displayMinimalGrid"), | ||
isThumbnailGrid: readOnly("topicThumbnailsService.displayGrid"), | ||
isThumbnailList: readOnly("topicThumbnailsService.displayList"), | ||
isMasonryList: readOnly("topicThumbnailsService.displayMasonry"), | ||
isBlogStyleGrid: readOnly("topicThumbnailsService.displayBlogStyle"), | ||
}); | ||
} | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need to keep it as is or can we update it to the native class syntax?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't currently have any way to control classNameBindings via the native class syntax, so will leave it for now 😬