一个常见的词正是我不想要的。我不想让我的搜索结果包含所有工单,而是想找到一种搜索特定工单的方法。这就是为什么主题 ID 是理想的。它们是自动生成且唯一的。
不,我认为我正在使用的代码最初也是 awesomerobot 的,但我似乎找不到它的来源了。
我更喜欢我当前使用的代码在文本下方的放置方式,而不是那个代码块。
好吧,我做不到。也许你能做到。
“我有一套特定的技能。编码不属于其中。”
一个常见的词正是我不想要的。我不想让我的搜索结果包含所有工单,而是想找到一种搜索特定工单的方法。这就是为什么主题 ID 是理想的。它们是自动生成且唯一的。
不,我认为我正在使用的代码最初也是 awesomerobot 的,但我似乎找不到它的来源了。
我更喜欢我当前使用的代码在文本下方的放置方式,而不是那个代码块。
好吧,我做不到。也许你能做到。
“我有一套特定的技能。编码不属于其中。”
那么也许你应该把这个话题移到 Marketplace 或 #feature。
你误会我的意思了。我的意思是,在搜索词中包含 topic:id 参数,这样就只会显示该主题中的结果。常用词可以省去更改“hi”和“hello”这类词的麻烦。
@tknospdr @pfaffman 我拼凑了一个快速组件,允许您输入主题 ID 并跳转到它。
创建一个新组件,并在“编辑 CSS/HTML”按钮下的 JS 选项卡中添加此内容[1]:
import { apiInitializer } from "discourse/lib/api";
import Component from '@glimmer/component';
import { action } from "@ember/object";
import Form from "discourse/components/form";
import DiscourseURL from "discourse/lib/url";
export default apiInitializer((api) => {
api.renderBeforeWrapperOutlet("full-page-search-filters",
class GoToTopic extends Component {
@action
handleSubmit(data) {
DiscourseURL.routeTo(`/t/${data.id}`);
}
<template>
<div class="topic-id-go-to" style="margin-top: 1em;">
<Form @onSubmit={{this.handleSubmit}} as |form|>
<form.Field @name="id" @title="Topic id" as |field|>
<field.Input @type="number" @validation="required" />
</form.Field>
<form.Submit />
</Form>
</div>
</template>
}
);
});
这会在搜索页面添加一个输入框:
希望这对您有帮助!
感谢 @NateDhaliwal,我非常感谢您的努力。
我创建了该组件,将代码粘贴到了 JS 选项卡下。确保它在我的主题上处于激活状态,但没有效果。
这是我的搜索页面的样子:
浏览器控制台中是否有任何错误?
我禁用了“高级搜索横幅”组件,然后重新启用了它,现在它起作用了。
![]()
好的,这是目前情况的总结:
@awesomerobot
您的代码效果很好,但看起来除了文本之外的任何 CSS 装饰都会在第一个帖子之后的每个帖子中显示为空白,并按照 CSS 的方式进行格式化。
关闭内联块,您将获得一个全宽的彩色条。
更改背景颜色,药丸会改变颜色
等等…
根据您的描述,您似乎告诉它只在第一个帖子中显示,但这只会影响文本。我们还能做些什么来在所有附加帖子中删除该元素吗?
好的,正如我所说,我不是程序员,但我很擅长向 Grok 提问。这是处理这两个问题的代码。
也许地球上没有人关心,但对于我们这些使用 Discourse 作为票务系统的人来说,这似乎是跟踪主题的好方法。
import { apiInitializer } from "discourse/lib/api";
import Component from "@glimmer/component";
class TopicIdentifier extends Component {
get topicId() {
return this.args.post?.topic?.id;
}
get shouldShow() {
const firstPost = this.args.post?.post_number === 1; // 主题中的第一个帖子
const desiredCategories = [9, 23]; // 您希望其显示的类别 ID 的逗号分隔列表
const isInCategory = desiredCategories.includes(
this.args.post?.topic.category.id
);
return firstPost && isInCategory;
}
<template>
{{#if this.shouldShow}}
<!-- 您可以编辑下面的内容,{{this.topicId}} 将填充主题 ID -->
Issue Tracking # is
{{this.topicId}}
<!-- 您可以编辑上面的内容 -->
{{/if}}
</template>
}
export default apiInitializer("0.8.40", (api) => {
api.decorateCookedElement((element, helper) => {
```gjs
import { apiInitializer } from "discourse/lib/api";
import Component from "@glimmer/component";
class TopicIdentifier extends Component {
get topicId() {
return this.args.post?.topic?.id;
}
get shouldShow() {
const firstPost = this.args.post?.post_number === 1; // 主题中的第一个帖子
const desiredCategories = [9, 23]; // 您希望其显示的类别 ID 的逗号分隔列表
const isInCategory = desiredCategories.includes(
this.args.post?.topic.category.id
);
return firstPost && isInCategory;
}
<template>
{{#if this.shouldShow}}
<!-- 您可以编辑下面的内容,{{this.topicId}} 将填充主题 ID -->
Issue Tracking # is
{{this.topicId}}
<!-- 您可以编辑上面的内容 -->
{{/if}}
</template>
}
export default apiInitializer("0.8.40", (api) => {
api.decorateCookedElement((element, helper) => {
// 仅当这是第一个帖子且不在解决方案引用中时继续
// 仅当这是第一个帖子,不在引用中,并且在所需的类别中时继续
const post = helper?.model;
const desiredCategories = [9, 23]; // 匹配 TopicIdentifier 中的类别
if (
if (
helper?.model?.post_number !== 1 ||
!post ||
post.post_number !== 1 ||
!desiredCategories.includes(post.topic?.category?.id) || // 检查类别
element.classList.contains("post__contents-cooked-quote") || // 检查元素本身是否是引用内容
element.closest("aside.accepted-answer") || // 检查解决方案/引用包装器
element.closest(".quote") // 对通用引用的附加检查
) {
return;
}
const wrapper = document.createElement("div");
wrapper.className = "tracking-id";
helper?.renderGlimmer(
wrapper,
<template><TopicIdentifier @post={{helper.model}} /></template>
);
element.appendChild(wrapper);
});
});
我没能早点处理这件事,但我很高兴你解决了!这看起来是一个合理的解决方案。
我知道我可以轻松地测试它,但您是否能立即告诉我,如果我将此 JS 和 CSS 放入其自己的组件中,它是否会起作用,还是必须放在主题本身中?
我发现了另一个小异常。我看到主题的第一个帖子中出现了空的类气泡,而不是我想要的两个,所以我不得不稍微更新一下代码。这是最终产品。
import { apiInitializer } from "discourse/lib/api";
import Component from "@glimmer/component";
class TopicIdentifier extends Component {
get topicId() {
return this.args.post?.topic?.id;
}
get shouldShow() {
const firstPost = this.args.post?.post_number === 1; // 主题的第一个帖子
const desiredCategories = [9, 23]; // 你希望它出现的分类 ID 的逗号分隔列表
const isInCategory = desiredCategories.includes(
this.args.post?.topic.category.id
);
return firstPost && isInCategory;
}
<template>
{{#if this.shouldShow}}
<!-- 你可以编辑下面的内容,{{this.topicId}} 将会填充主题 ID -->
Issue Tracking # is
{{this.topicId}}
<!-- 你可以编辑上面的内容 -->
{{/if}}
</template>
}
export default apiInitializer("0.8.40", (api) => {
api.decorateCookedElement((element, helper) => {
// Only proceed if this is the first post and not inside a solution quote
// Only proceed if this is the first post, not in a quote, and in the desired category
const post = helper?.model;
const desiredCategories = [9, 23]; // Match the categories from TopicIdentifier
if (
if (
helper?.model?.post_number !== 1 ||
!post ||
post.post_number !== 1 ||
!desiredCategories.includes(post.topic?.category?.id) || // Check category
element.classList.contains("post__contents-cooked-quote") || // Check if the element itself is the quote content
element.closest("aside.accepted-answer") || // Check for solution/quote wrapper
element.closest(".quote") // Additional check for generic quotes
) {
return;
}
const wrapper = document.createElement("div");
wrapper.className = "tracking-id";
helper?.renderGlimmer(
wrapper,
<template><TopicIdentifier @post={{helper.model}} /></template>
);
element.appendChild(wrapper);
});
});
element.classList.contains("post__contents-cooked-quote") || // Check if the element itself is the quote content
element.closest("aside.accepted-answer") || // Check for solution/quote wrapper
element.closest(".quote") // Additional check for generic quotes
) {
return;
}
const wrapper = document.createElement("div");
wrapper.className = "tracking-id";
helper?.renderGlimmer(
wrapper,
<template><TopicIdentifier @post={{helper.model}} /></template>
);
element.appendChild(wrapper);
});
});
为了回答我自己的问题,我将所有代码移到了一个新的主题组件中,它仍然可以正常工作。