Revert "Revert "DEV: Migrate custom header links to new objects setting type (#53)""

This reverts commit a178fdd2f7.
This commit is contained in:
OsamaSayegh 2024-04-25 02:03:58 +03:00
parent a178fdd2f7
commit 803787ba74
No known key found for this signature in database
GPG Key ID: 060E5AC82223685F
8 changed files with 204 additions and 32 deletions

View File

@ -1,3 +1,4 @@
< 3.3.0.beta2-dev: 0a837d7c044798417b1a7389cc37a67859c25dbe
< 3.3.0.beta1-dev: 50061254831a658eba821238a8a1ae0b7029ab09 < 3.3.0.beta1-dev: 50061254831a658eba821238a8a1ae0b7029ab09
< 3.2.0.beta2: 061adfe5ae20abbb82be711d373894c30987ec75 < 3.2.0.beta2: 061adfe5ae20abbb82be711d373894c30987ec75
< 3.2.0.beta1-dev: c344d0f519bbb3660e306c50c0d1aa1a776c5e13 < 3.2.0.beta1-dev: c344d0f519bbb3660e306c50c0d1aa1a776c5e13

View File

@ -12,12 +12,13 @@
> >
{{#each this.links as |link|}} {{#each this.links as |link|}}
<li <li
class="headerLink class={{concat-class
{{link.device}} "headerLink"
{{link.keepOnScrollClass}} link.device
{{link.locale}} link.locale
{{link.linkClass}} link.linkClass
{{link.keepOnScroll}}" link.hideOnScroll
}}
> >
<a <a
title={{link.anchorAttributes.title}} title={{link.anchorAttributes.title}}

View File

@ -7,16 +7,14 @@ export default class CustomHeaderLinks extends Component {
} }
get links() { get links() {
return settings.custom_header_links.split("|").reduce((result, item) => { return settings.custom_header_links.reduce((result, link) => {
let [ const linkText = link.text;
linkText, const linkTitle = link.title;
linkTitle, const linkHref = link.url;
linkHref, const target = link.target;
device, const hideOnScroll = link.hide_on_scroll;
target = "", const locale = link.locale;
keepOnScroll, const device = link.view;
locale,
] = item.split(",").map((s) => s.trim());
if (!linkText || (locale && document.documentElement.lang !== locale)) { if (!linkText || (locale && document.documentElement.lang !== locale)) {
return result; return result;
@ -32,8 +30,8 @@ export default class CustomHeaderLinks extends Component {
result.push({ result.push({
device: `headerLink--${device}`, device: `headerLink--${device}`,
keepOnScroll: `headerLink--${keepOnScroll}`, hideOnScroll: `headerLink--${hideOnScroll}`,
locale: `headerLink--${locale}`, locale: locale ? `headerLink--${locale}` : null,
linkClass, linkClass,
anchorAttributes, anchorAttributes,
linkText, linkText,

View File

@ -1,5 +1,36 @@
en: en:
theme_metadata: theme_metadata:
settings: settings:
custom_header_links: "Comma delimited in this order: link text, link title, URL, view, target, hide on scroll<br><b>Link text:</b> The text for the link<br><b>Link title:</b> the text that shows when the link is hovered<br><b>URL:</b> The path for the link (can be relative)<br><b>View:</b> vdm = desktop and mobile, vdo = desktop only, vmo = mobile only<br><b>Target:</b> blank = opens in a new tab, self = opens in the same tab<br><b>Hide on scroll:</b> remove = hides the link when the title is expanded on topic pages keep = keeps the link visible even when the title is visible on topic pages<br><b>Language:</b> blank = no locale assoaciated to the link, else insert a locale code (en, fr, de, ...)"
links_position: "Note that when links are displayed on the left, they're automatically hidden while scrolling within topics to make room for the title" links_position: "Note that when links are displayed on the left, they're automatically hidden while scrolling within topics to make room for the title"
custom_header_links:
description: Custom links to be displayed in the header
schema:
properties:
text:
label: Text
description: The text for the link
title:
label: Title
description: The title attribute for the link
url:
label: URL
description: The URL for the link
view:
label: View
description: |
vdm = desktop and mobile
vdo = desktop only
vmo = mobile only
target:
label: Target
description: |
blank = opens in a new tab
self = opens in the same tab
hide_on_scroll:
label: Hide on scroll
description: |
remove = hides the link when the title is expanded on topic pages
keep = keeps the link visible even when the title is visible on topic pages
locale:
label: Locale
description: The locale in which the link should be displayed on. The link will be displayed on all locales if left blank.

View File

@ -0,0 +1,33 @@
export default function migrate(settings) {
const oldSetting = settings.get("custom_header_links");
if (oldSetting) {
const newSetting = oldSetting.split("|").map((link) => {
const [text, title, url, view, target, hide_on_scroll, locale] = link
.split(",")
.map((s) => s.trim());
const newLink = {
text,
title,
url,
view,
target,
hide_on_scroll,
locale,
};
Object.keys(newLink).forEach((key) => {
if (newLink[key] === undefined) {
delete newLink[key];
}
});
return newLink;
});
settings.set("custom_header_links", newSetting);
}
return settings;
}

View File

@ -1,7 +1,65 @@
custom_header_links: custom_header_links:
type: list type: objects
list_type: simple default:
default: "External link, this link will open in a new tab, https://meta.discourse.org, vdo, blank, remove|Most Liked, Posts with the most amount of likes, /latest/?order=op_likes, vdo, self, keep|Privacy, Our Privacy Policy, /privacy, vdm, self, keep" - text: "External link"
title: "This link will open in a new tab"
url: "https://meta.discourse.org"
view: "vdo"
target: "blank"
hide_on_scroll: "remove"
- text: "Most Liked"
title: "Posts with the most amount of likes"
url: "/latest/?order=op_likes"
view: "vdo"
target: "self"
hide_on_scroll: "keep"
- text: "Privacy"
title: "Our Privacy Policy"
url: "/privacy"
view: "vdm"
target: "self"
hide_on_scroll: "keep"
schema:
name: "link"
properties:
text:
type: string
required: true
validations:
min_length: 1
max_length: 100
title:
type: string
required: true
validations:
min_length: 1
max_length: 1000
url:
type: string
required: true
validations:
min_length: 1
max_length: 2048
url: true
view:
type: enum
choices:
- vdm
- vdo
- vmo
target:
type: enum
choices:
- blank
- self
hide_on_scroll:
type: enum
choices:
- remove
- keep
default: keep
locale:
type: string
links_position: links_position:
default: right default: right

View File

@ -9,8 +9,7 @@ RSpec.describe "Viewing Custom Header Links", system: true do
context "when glimmer headers are enabled" do context "when glimmer headers are enabled" do
before do before do
if SiteSetting.respond_to?(:experimental_glimmer_header_groups) if SiteSetting.respond_to?(:experimental_glimmer_header_groups)
SiteSetting.experimental_glimmer_header_groups = SiteSetting.experimental_glimmer_header_groups = Group::AUTO_GROUPS[:everyone]
Group::AUTO_GROUPS[:everyone]
else else
SiteSetting.glimmer_header_mode = "enabled" SiteSetting.glimmer_header_mode = "enabled"
end end
@ -24,19 +23,19 @@ RSpec.describe "Viewing Custom Header Links", system: true do
expect(custom_header_link).to have_custom_header_link( expect(custom_header_link).to have_custom_header_link(
"External link", "External link",
href: "https://meta.discourse.org", href: "https://meta.discourse.org",
title: "this link will open in a new tab" title: "This link will open in a new tab",
) )
expect(custom_header_link).to have_custom_header_link( expect(custom_header_link).to have_custom_header_link(
"Most Liked", "Most Liked",
href: "/latest/?order=op_likes", href: "/latest/?order=op_likes",
title: "Posts with the most amount of likes" title: "Posts with the most amount of likes",
) )
expect(custom_header_link).to have_custom_header_link( expect(custom_header_link).to have_custom_header_link(
"Privacy", "Privacy",
href: "/privacy", href: "/privacy",
title: "Our Privacy Policy" title: "Our Privacy Policy",
) )
end end
end end
@ -44,8 +43,7 @@ RSpec.describe "Viewing Custom Header Links", system: true do
context "when glimmer headers are disabled" do context "when glimmer headers are disabled" do
before do before do
if SiteSetting.respond_to?(:experimental_glimmer_header_groups) if SiteSetting.respond_to?(:experimental_glimmer_header_groups)
SiteSetting.experimental_glimmer_header_groups = SiteSetting.experimental_glimmer_header_groups = nil
nil
else else
SiteSetting.glimmer_header_mode = "disabled" SiteSetting.glimmer_header_mode = "disabled"
end end
@ -59,19 +57,19 @@ RSpec.describe "Viewing Custom Header Links", system: true do
expect(custom_header_link).to have_custom_header_link( expect(custom_header_link).to have_custom_header_link(
"External link", "External link",
href: "https://meta.discourse.org", href: "https://meta.discourse.org",
title: "this link will open in a new tab" title: "This link will open in a new tab",
) )
expect(custom_header_link).to have_custom_header_link( expect(custom_header_link).to have_custom_header_link(
"Most Liked", "Most Liked",
href: "/latest/?order=op_likes", href: "/latest/?order=op_likes",
title: "Posts with the most amount of likes" title: "Posts with the most amount of likes",
) )
expect(custom_header_link).to have_custom_header_link( expect(custom_header_link).to have_custom_header_link(
"Privacy", "Privacy",
href: "/privacy", href: "/privacy",
title: "Our Privacy Policy" title: "Our Privacy Policy",
) )
end end
end end

View File

@ -0,0 +1,52 @@
import { module, test } from "qunit";
import migrate from "../../../../migrations/settings/0002-migrate-custom-header-links";
module(
"Unit | Migrations | Settings | 0002-migrate-custom-header-links",
function () {
test("migrate", function (assert) {
const settings = new Map(
Object.entries({
custom_header_links:
"External link, this link will open in a new tab, https://meta.discourse.org, vdo, blank, remove|Most Liked, Posts with the most amount of likes, /latest/?order=op_likes, vdo, self, keep|Privacy, Our Privacy Policy, /privacy, vdm, self, keep, en",
})
);
const result = migrate(settings);
const expectedResult = new Map(
Object.entries({
custom_header_links: [
{
text: "External link",
title: "this link will open in a new tab",
url: "https://meta.discourse.org",
view: "vdo",
target: "blank",
hide_on_scroll: "remove",
},
{
text: "Most Liked",
title: "Posts with the most amount of likes",
url: "/latest/?order=op_likes",
view: "vdo",
target: "self",
hide_on_scroll: "keep",
},
{
text: "Privacy",
title: "Our Privacy Policy",
url: "/privacy",
view: "vdm",
target: "self",
hide_on_scroll: "keep",
locale: "en",
},
],
})
);
assert.deepEqual(Array.from(result), Array.from(expectedResult));
});
}
);