How do I customise the styling of "solved topics" in the main list?

AI-generated summary

The discussion revolves around customizing the styling of โ€œsolved topicsโ€ in the main list. Jerome_Bouvard initially asked how to change the background color of a solved topic on the main page, but downey and sam mentioned that itโ€™s not possible without adding a custom class.

sam provided a CSS solution to style the accepted answer on the topic page, but not the main list. alxsal and ggurbet requested support for changing the background color of solved topics on the main page. sam stated that itโ€™s not currently supported, but it could be added in the future.

Johani provided a solution using the api.addPostClassesCallback method, which adds a topic-solution class to posts marked as the answer. This allows for styling of solved topics on the main list. loginerror also provided a workaround using JavaScript, but Johaniโ€™s solution is more straightforward and efficient.

The solution is a simple JavaScript code snippet that adds the topic-solution class to posts marked as the answer:

api.addPostClassesCallback((attrs) => {
  if (attrs.accepted_answer) return ["topic-solution"];
});

This allows for styling of solved topics on the main list using CSS.

Perfect plugin for me :smile:
How can I change the background color of a solved topic on the main page ?
Like some nice green to show everyone that this topic has a solution !
Best,
J.

You should be able to do it by targetting the post class via CSS.

Are you talking about the main list of all topics? If so, I donโ€™t think this is possible.

If itโ€™s on the topic page itself, AFAIK this is not possible either, because the only change to the โ€œquestionโ€ (first post) is the insertion of a paragraph in the post. You can style the post of the accepted solution, though, with CSS.

If someone knows a way, please share it because I also spent some time trying to do this. :wink:

I never added support for that, but on the actual topic page you can easily change it with

.topic-post.accepted-answer .topic-body {
  background-color: #E9FFE0;
}

Sorry for reviving this very old topic, but I had the exact same question :

Is changing the background color of solved topic on the main page now supported ?
We would like to make it more visible on topic lists / search results if a topic has an accepted answer or not.

No we do not yet decorate rows in the topic list with a .solved class. Not against adding this if we can do that with no performance impact.

์–ด๋А ์‹œ์ ์— .accepted-answer ํด๋ž˜์Šค๊ฐ€ ์ด div์—์„œ ์ œ๊ฑฐ๋œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์Šน์ธ๋œ ๋‹ต๋ณ€ ๊ฒŒ์‹œ๋ฌผ์„ ๋‹ค๋ฅด๊ฒŒ ์Šคํƒ€์ผ๋งํ•  ์ˆ˜ ์žˆ๋„๋ก ์ด ํด๋ž˜์Šค๋ฅผ ๋‹ค์‹œ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์„๊นŒ์š”?

๋‹ค๋ฅธ ํ† ํ”ฝ์—์„œ @awesomerobot์€ ์ด ํด๋ž˜์Šค๊ฐ€ ์ฒซ ๋ฒˆ์งธ ๊ฒŒ์‹œ๋ฌผ์˜ ์ธ์šฉ๋ฌธ์— ์ถ”๊ฐ€๋˜์—ˆ๋‹ค๊ณ  ๋งํ–ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ฒŒ์‹œ๋ฌผ ๋ณธ๋ฌธ(body)์—์„œ๋Š” ์ฐพ์„ ์ˆ˜ ์—†์—ˆ์Šต๋‹ˆ๋‹ค:

๊ทธ ํด๋ž˜์Šค๋Š” ๋ถ„๋ช…ํžˆ ๋ณด์ž…๋‹ˆ๋‹ค.

๋„ค, ์ฒซ ๋ฒˆ์งธ ๊ฒŒ์‹œ๋ฌผ์˜ ์ธ์šฉ๋ฌธ์— ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ €๋Š” ๋‹ต๋ณ€์ด ํฌํ•จ๋œ ๊ฒŒ์‹œ๋ฌผ์˜ ๋ณธ๋ฌธ์— ๋Œ€ํ•ด ๋ง์”€๋“œ๋ฆฌ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

ํ•ด๊ฒฐ๋œ ๊ฒŒ์‹œ๊ธ€์˜ div.topic-body์— ํด๋ž˜์Šค๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ์œ ์šฉํ•  ๊ฒƒ์ด๋ผ๋Š” ๋ฐ ๋™์˜ํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ž„์‹œ ํ•ด๊ฒฐ์ฑ…์„ ์ƒ๊ฐํ•ด ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

<script type="text/discourse-plugin" version="0.8.42">
api.onPageChange((url) => {
  $('button.accepted').parents('div.topic-body:first').toggleClass('solved-post-container', true);
});
</script>

๊ทธ๋ฆฌ๊ณ  ๋‹ค์Œ๊ณผ ๊ฐ™์€ CSS๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค:

.solved-post-container {
outline: 3px !important;
outline-color: var(--success) !important;
outline-style: solid !important;
}

ํ•˜์ง€๋งŒ ๊ฒŒ์‹œ๊ธ€์ด ํ•ด๊ฒฐ๋˜์ง€ ์•Š์€ ์ƒํƒœ๋กœ ๋˜๋Œ๋ ค์กŒ์„ ๋•Œ ํ…Œ๋‘๋ฆฌ๊ฐ€ ์‚ฌ๋ผ์ง€๋„๋ก ํ•ด๊ฒฐ ์ž‘์—…์— ๋Œ€ํ•œ ์ผ๋ถ€ ํ›…(hook)์ด ์—ฌ์ „ํžˆ ํ•„์š”ํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค(์—ฃ์ง€ ์ผ€์ด์Šค์ผ ์ˆ˜ ์žˆ์ง€๋งŒ, ์ด๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค).

ํ•ด๊ฒฐ๋œ ๊ฒŒ์‹œ๊ธ€ ๋ณธ๋ฌธ์— ํด๋ž˜์Šค๊ฐ€ ์žˆ๋‹ค๋ฉด ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค :slight_smile:

์š”์•ฝ

์ด ๋ฐฉ๋ฒ•์€ ๋‹ต๋ณ€์œผ๋กœ ํ‘œ์‹œ๋œ ๊ฒŒ์‹œ๊ธ€์— topic-solution ํด๋ž˜์Šค๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

api.addPostClassesCallback((attrs) => {
  if (attrs.accepted_answer) return ["topic-solution"];
});

๊ฒŒ์‹œ๊ธ€์ด ๊ฐ€์ง„ ํด๋ž˜์Šค๋Š” ์—ฌ๊ธฐ์„œ ๊ฒฐ์ •๋ฉ๋‹ˆ๋‹ค.

์›ํ•˜์‹œ๋Š” ํ›…์€ ์ด๋ฏธ ํ•ด๋‹น ํŒŒ์ผ์˜ ๋งจ ๋์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

ํ”Œ๋Ÿฌ๊ทธ์ธ API์—๋Š” ์กฐ๊ฑด์— ๋”ฐ๋ผ ๊ฒŒ์‹œ๊ธ€์— ํด๋ž˜์Šค๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

https://github.com/discourse/discourse/blob/main/app/assets/javascripts/discourse/app/lib/plugin-api.js#L819-L829

์ด ๋ฉ”์„œ๋“œ๋Š” ์ฃผ์–ด์ง„ ์ฝœ๋ฐฑ์„ ์ ์šฉํ•˜๊ณ , ์กฐ๊ฑด์ด ์ถฉ์กฑ๋˜๋ฉด ์ถ”๊ฐ€ํ•  ํด๋ž˜์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์ฝœ๋ฐฑ์ด ์‹คํ–‰๋˜๋ฉด Discourse๋Š” ๊ฒŒ์‹œ๊ธ€ ์†์„ฑ์„ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

api.addPostClassesCallback((attrs) => {
  console.log(attrs);
});

์ฝ˜์†”์„ ํ™•์ธํ•˜๋ฉด ๊ฐ ๊ฒŒ์‹œ๊ธ€์˜ ์†์„ฑ์ด ํ‘œ์‹œ๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๊ฒƒ๋“ค์€ ๋‹จ์ง€ ์˜ˆ์‹œ์ผ ๋ฟ์ด๋ฉฐ, ๋” ๋งŽ์€ ์†์„ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

๋‹คํ–‰ํžˆ๋„ ๊ฒŒ์‹œ๊ธ€์ด ํ•ด๊ฒฐ์ฑ…์œผ๋กœ ํ‘œ์‹œ๋˜๋ฉด Discourse๊ฐ€ ์ด๋ฅผ ์ถ”์ ํ•˜๊ณ  ๊ฒŒ์‹œ๊ธ€ ์†์„ฑ์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ์†์„ฑ์€ accepted_answer๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ํ•ด๋‹น ์†์„ฑ์ด true์ธ์ง€ ํ™•์ธํ•˜๊ณ , true์ธ ๊ฒฝ์šฐ ์‚ฌ์šฉ์ž ์ •์˜ ํด๋ž˜์Šค๋ฅผ ์ถ”๊ฐ€ํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด์š”.

api.addPostClassesCallback((attrs) => {
  if (attrs.accepted_answer) return ["topic-solution"];
});

์ด๊ฒƒ๋งŒ์œผ๋กœ ์›ํฌ์Šคํ„ฐ(OP)๊ฐ€ ๋งˆ์Œ์„ ๋ฐ”๊ฟ” ๋‹ค๋ฅธ ํ•ด๊ฒฐ์ฑ…์„ ์„ ํƒํ•˜๋”๋ผ๋„ ํด๋ž˜์Šค๊ฐ€ ์ „ํ™˜๋ฉ๋‹ˆ๋‹ค. ์ด์ „ ๋‹ต๋ณ€์—์„œ ํด๋ž˜์Šค๊ฐ€ ์ œ๊ฑฐ๋˜๊ณ  ์ƒˆ๋กœ์šด ๋‹ต๋ณ€์— ์ถ”๊ฐ€๋ฉ๋‹ˆ๋‹ค.

ํ›Œ๋ฅญํ•ด์š”, ์ •๋ง ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! :slight_smile: