mirror of
https://github.com/discourse/discourse.git
synced 2025-05-25 00:32:52 +08:00
DEV: Make global search context suggestion first (#20581)
Currently, the global search context suggestion("in all posts and topics") which also doubles as the default context on pressing Enter is displayed as the second item in the initial search options suggested. This changes makes it the first item in the suggested options.
This commit is contained in:
@ -486,18 +486,6 @@ createWidget("search-menu-assistant", {
|
|||||||
this.router.currentRouteName.startsWith("topic.")
|
this.router.currentRouteName.startsWith("topic.")
|
||||||
) {
|
) {
|
||||||
const user = attrs.results[0];
|
const user = attrs.results[0];
|
||||||
content.push(
|
|
||||||
this.attach("search-menu-assistant-item", {
|
|
||||||
prefix,
|
|
||||||
user,
|
|
||||||
setTopicContext: true,
|
|
||||||
slug: `${prefix}@${user.username}`,
|
|
||||||
suffix: h(
|
|
||||||
"span.label-suffix",
|
|
||||||
` ${I18n.t("search.in_this_topic")}`
|
|
||||||
),
|
|
||||||
})
|
|
||||||
);
|
|
||||||
content.push(
|
content.push(
|
||||||
this.attach("search-menu-assistant-item", {
|
this.attach("search-menu-assistant-item", {
|
||||||
extraHint: I18n.t("search.enter_hint"),
|
extraHint: I18n.t("search.enter_hint"),
|
||||||
@ -510,6 +498,18 @@ createWidget("search-menu-assistant", {
|
|||||||
),
|
),
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
content.push(
|
||||||
|
this.attach("search-menu-assistant-item", {
|
||||||
|
prefix,
|
||||||
|
user,
|
||||||
|
setTopicContext: true,
|
||||||
|
slug: `${prefix}@${user.username}`,
|
||||||
|
suffix: h(
|
||||||
|
"span.label-suffix",
|
||||||
|
` ${I18n.t("search.in_this_topic")}`
|
||||||
|
),
|
||||||
|
})
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
attrs.results.forEach((user) => {
|
attrs.results.forEach((user) => {
|
||||||
content.push(
|
content.push(
|
||||||
@ -552,6 +552,10 @@ createWidget("search-menu-initial-options", {
|
|||||||
const content = [];
|
const content = [];
|
||||||
|
|
||||||
if (attrs.term || ctx) {
|
if (attrs.term || ctx) {
|
||||||
|
if (attrs.term) {
|
||||||
|
content.push(this.defaultRow(attrs.term, { withLabel: true }));
|
||||||
|
}
|
||||||
|
|
||||||
if (ctx) {
|
if (ctx) {
|
||||||
const term = attrs.term || "";
|
const term = attrs.term || "";
|
||||||
switch (ctx.type) {
|
switch (ctx.type) {
|
||||||
@ -650,9 +654,6 @@ createWidget("search-menu-initial-options", {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (attrs.term) {
|
|
||||||
content.push(this.defaultRow(attrs.term, { withLabel: true }));
|
|
||||||
}
|
|
||||||
return content;
|
return content;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ import {
|
|||||||
count,
|
count,
|
||||||
exists,
|
exists,
|
||||||
query,
|
query,
|
||||||
|
queryAll,
|
||||||
} from "discourse/tests/helpers/qunit-helpers";
|
} from "discourse/tests/helpers/qunit-helpers";
|
||||||
import {
|
import {
|
||||||
click,
|
click,
|
||||||
@ -131,14 +132,13 @@ acceptance("Search - Anonymous", function (needs) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test("search scope", async function (assert) {
|
test("search scope", async function (assert) {
|
||||||
const firstResult =
|
const contextSelector = ".search-menu .results .search-menu-assistant-item";
|
||||||
".search-menu .results .search-menu-assistant-item:first-child";
|
|
||||||
|
|
||||||
await visit("/tag/important");
|
await visit("/tag/important");
|
||||||
await click("#search-button");
|
await click("#search-button");
|
||||||
|
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
query(firstResult).textContent.trim(),
|
queryAll(contextSelector)[0].firstChild.textContent.trim(),
|
||||||
`${I18n.t("search.in")} test`,
|
`${I18n.t("search.in")} test`,
|
||||||
"contextual tag search is first available option with no term"
|
"contextual tag search is first available option with no term"
|
||||||
);
|
);
|
||||||
@ -146,22 +146,22 @@ acceptance("Search - Anonymous", function (needs) {
|
|||||||
await fillIn("#search-term", "smth");
|
await fillIn("#search-term", "smth");
|
||||||
|
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
query(firstResult).textContent.trim(),
|
queryAll(contextSelector)[1].firstChild.textContent.trim(),
|
||||||
`smth ${I18n.t("search.in")} test`,
|
`smth ${I18n.t("search.in")} test`,
|
||||||
"tag-scoped search is first available option"
|
"tag-scoped search is second available option"
|
||||||
);
|
);
|
||||||
|
|
||||||
await visit("/c/bug");
|
await visit("/c/bug");
|
||||||
await click("#search-button");
|
await click("#search-button");
|
||||||
|
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
query(firstResult).textContent.trim(),
|
queryAll(contextSelector)[1].firstChild.textContent.trim(),
|
||||||
`smth ${I18n.t("search.in")} bug`,
|
`smth ${I18n.t("search.in")} bug`,
|
||||||
"category-scoped search is first available option"
|
"category-scoped search is first available option with no search term"
|
||||||
);
|
);
|
||||||
|
|
||||||
assert.ok(
|
assert.ok(
|
||||||
exists(`${firstResult} span.badge-wrapper`),
|
exists(`${contextSelector} span.badge-wrapper`),
|
||||||
"category badge is a span (i.e. not a link)"
|
"category badge is a span (i.e. not a link)"
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -169,20 +169,20 @@ acceptance("Search - Anonymous", function (needs) {
|
|||||||
await click("#search-button");
|
await click("#search-button");
|
||||||
|
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
query(firstResult).textContent.trim(),
|
queryAll(contextSelector)[1].firstChild.textContent.trim(),
|
||||||
`smth ${I18n.t("search.in_this_topic")}`,
|
`smth ${I18n.t("search.in_this_topic")}`,
|
||||||
"topic-scoped search is first available option"
|
"topic-scoped search is first available option with no search term"
|
||||||
);
|
);
|
||||||
|
|
||||||
await visit("/u/eviltrout");
|
await visit("/u/eviltrout");
|
||||||
await click("#search-button");
|
await click("#search-button");
|
||||||
|
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
query(firstResult).textContent.trim(),
|
queryAll(contextSelector)[1].firstChild.textContent.trim(),
|
||||||
`smth ${I18n.t("search.in_posts_by", {
|
`smth ${I18n.t("search.in_posts_by", {
|
||||||
username: "eviltrout",
|
username: "eviltrout",
|
||||||
})}`,
|
})}`,
|
||||||
"user-scoped search is first available option"
|
"user-scoped search is first available option with no search term"
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -197,17 +197,18 @@ acceptance("Search - Anonymous", function (needs) {
|
|||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
query(firstResult).textContent.trim(),
|
query(firstResult).textContent.trim(),
|
||||||
I18n.t("search.in_this_topic"),
|
I18n.t("search.in_this_topic"),
|
||||||
"contextual topic search is first available option"
|
"contextual topic search is first available option with no search term"
|
||||||
);
|
);
|
||||||
|
|
||||||
await fillIn("#search-term", "a proper");
|
await fillIn("#search-term", "a proper");
|
||||||
await query("input#search-term").focus();
|
await query("input#search-term").focus();
|
||||||
await triggerKeyEvent(".search-menu", "keydown", "ArrowDown");
|
await triggerKeyEvent(".search-menu", "keydown", "ArrowDown");
|
||||||
|
await triggerKeyEvent(".search-menu", "keydown", "ArrowDown");
|
||||||
|
|
||||||
await click(document.activeElement);
|
await click(document.activeElement);
|
||||||
assert.ok(
|
assert.ok(
|
||||||
exists(".search-menu .search-result-post ul li"),
|
exists(".search-menu .search-result-post ul li"),
|
||||||
"clicking first option formats results as posts"
|
"clicking second option scopes search to current topic"
|
||||||
);
|
);
|
||||||
|
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
@ -233,6 +234,7 @@ acceptance("Search - Anonymous", function (needs) {
|
|||||||
await fillIn("#search-term", "dev");
|
await fillIn("#search-term", "dev");
|
||||||
await query("input#search-term").focus();
|
await query("input#search-term").focus();
|
||||||
await triggerKeyEvent(".search-menu", "keydown", "ArrowDown");
|
await triggerKeyEvent(".search-menu", "keydown", "ArrowDown");
|
||||||
|
await triggerKeyEvent(".search-menu", "keydown", "ArrowDown");
|
||||||
await click(document.activeElement);
|
await click(document.activeElement);
|
||||||
|
|
||||||
assert.ok(
|
assert.ok(
|
||||||
@ -255,18 +257,18 @@ acceptance("Search - Anonymous", function (needs) {
|
|||||||
|
|
||||||
await click("#search-button");
|
await click("#search-button");
|
||||||
|
|
||||||
const firstResult =
|
const contextSelector = ".search-menu .results .search-menu-assistant-item";
|
||||||
".search-menu .results .search-menu-assistant-item:first-child";
|
|
||||||
|
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
query(firstResult).textContent.trim(),
|
queryAll(contextSelector)[0].firstChild.textContent.trim(),
|
||||||
I18n.t("search.in_this_topic"),
|
I18n.t("search.in_this_topic"),
|
||||||
"contextual topic search is first available option"
|
"contextual topic search is first available option with no search term"
|
||||||
);
|
);
|
||||||
|
|
||||||
await fillIn("#search-term", "proper");
|
await fillIn("#search-term", "proper");
|
||||||
await query("input#search-term").focus();
|
await query("input#search-term").focus();
|
||||||
await triggerKeyEvent(".search-menu", "keydown", "ArrowDown");
|
await triggerKeyEvent(".search-menu", "keydown", "ArrowDown");
|
||||||
|
await triggerKeyEvent(".search-menu", "keydown", "ArrowDown");
|
||||||
await click(document.activeElement);
|
await click(document.activeElement);
|
||||||
|
|
||||||
assert.ok(
|
assert.ok(
|
||||||
@ -300,16 +302,16 @@ acceptance("Search - Anonymous", function (needs) {
|
|||||||
query(
|
query(
|
||||||
".search-menu-assistant-item:first-child .search-item-user .label-suffix"
|
".search-menu-assistant-item:first-child .search-item-user .label-suffix"
|
||||||
).textContent.trim(),
|
).textContent.trim(),
|
||||||
I18n.t("search.in_this_topic"),
|
I18n.t("search.in_topics_posts"),
|
||||||
"first result hints in this topic search"
|
"first result hints at global search"
|
||||||
);
|
);
|
||||||
|
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
query(
|
query(
|
||||||
".search-menu-assistant-item:nth-child(2) .search-item-user .label-suffix"
|
".search-menu-assistant-item:nth-child(2) .search-item-user .label-suffix"
|
||||||
).textContent.trim(),
|
).textContent.trim(),
|
||||||
I18n.t("search.in_topics_posts"),
|
I18n.t("search.in_this_topic"),
|
||||||
"second result hints global search"
|
"second result hints at search within current topic"
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user