Canapin
(Coin-coin le Canapin)
1
嗨!
我想尝试修改 buildQuote 函数,使其在 [quote] 开始标签前添加一个新行。
基本上,将这段代码:
return `[quote="${params.join(", ")}"]\n${contents.trim()}\n[/quote]\n\n`;
修改为:
return `\n[quote="${params.join(", ")}"]\n${contents.trim()}\n[/quote]\n\n`;
使用主题组件可以实现这样的操作吗?我该如何覆盖这个函数?
Canapin
(Coin-coin le Canapin)
2
我已经实现了这个功能,但有没有更优雅的解决方案?
<script type="text/discourse-plugin" version="0.8.23">
function buildQuote(post, contents, opts = {}) {
if (!post || !contents) {
return "";
}
const params = [
opts.username || post.username,
`post:${opts.post || post.post_number}`,
`topic:${opts.topic || post.topic_id}`
];
if (opts.full) params.push("full:true");
return `\n[quote="${params.join(", ")}"]\n${contents.trim()}\n[/quote]\n\n`;
}
api.modifyClass('controller:composer', {
actions: {
importQuote(toolbarEvent) {
const postStream = this.get("topic.postStream");
let postId = this.get("model.post.id");
// 如果没有当前帖子,则使用流中的第一个帖子 ID
if (!postId && postStream) {
postId = postStream.get("stream.firstObject");
}
// 如果正在编辑帖子,导入引用时获取回复
if (this.get("model.editingPost")) {
const replyToPostNumber = this.get("model.post.reply_to_post_number");
if (replyToPostNumber) {
const replyPost = postStream.posts.findBy(
"post_number",
replyToPostNumber
);
if (replyPost) {
postId = replyPost.id;
}
}
}
if (postId) {
this.set("model.loading", true);
return this.store.find("post", postId).then(post => {
const quote = buildQuote(post, post.raw, {
full: true
});
toolbarEvent.addText(quote);
this.set("model.loading", false);
});
}
}
}
});
</script>
2 个赞
asc
3
抱歉打扰了旧话题,但这对你来说仍然有效吗/你是否需要更新它?我一直在尝试让它生效,但它根本无法触发,而且我在处理任何与 modifyClass 相关的事情时都遇到了麻烦。
Canapin
(Coin-coin le Canapin)
4
我刚试了一下,它仍然有效 
我把它直接复制粘贴到我的主题中,它就能正常工作,无需任何修改。
1 个赞
asc
6
感谢 @merefield 和 @Canapin。我一直在尝试学习 Discourse 插件 API,摆弄一些感觉上应该很简单的事情,比如这个,但 modifyClass 在何时/何时不起作用方面非常不一致。我有一个小主题组件(就是你提到的那个 @merefield),其中两个 modifyClass 调用有效,但第三个无效,弄清楚是我做错了什么还是仅仅是 modifyClass 的问题,非常令人沮丧。
抱歉在你的话题上跑题了,感谢 @Canapin 的快速回复。
更新:
1 个赞
Canapin
(Coin-coin le Canapin)
7
没关系,我不知道 OT 是什么意思,但我们在这里提供帮助!
1 个赞
Canapin
(Coin-coin le Canapin)
关闭
8
此主题在上次回复后 30 天自动关闭。不再允许回复。