Canapin
(Coin-coin le Canapin)
September 17, 2020, 1:46pm
1
Hi!
I’d like to try to modify the buildQuote
function so it adds a new line before the opening [quote]
tag.
Basically, modifying this:
return `[quote="${params.join(", ")}"]\n${contents.trim()}\n[/quote]\n\n`;
to this:
return `\n[quote="${params.join(", ")}"]\n${contents.trim()}\n[/quote]\n\n`;
Is it possible to do such a thing with a theme component? How do I override this function?
Canapin
(Coin-coin le Canapin)
September 17, 2020, 4:03pm
2
I’ve managed to achieve it, but is there a prettier solution?
<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");
// If there is no current post, use the first post id from the stream
if (!postId && postStream) {
postId = postStream.get("stream.firstObject");
}
// If we're editing a post, fetch the reply when importing a quote
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 Likes
asc
February 27, 2023, 11:10pm
3
Sorry for bumping an old topic, but is this still working for you/have you had to update it at all? I’ve been trying to get this to work and I can’t get it to fire at all, but I’ve been having a terrible time with anything at all to do with modifyClass.
Canapin
(Coin-coin le Canapin)
February 28, 2023, 8:05am
4
I just tried it, and it is still working
I literally copy-pasted it in my theme, and it worked without any modification.
1 Like
merefield
(Robert)
February 28, 2023, 8:42am
5
2 Likes
asc
February 28, 2023, 2:10pm
6
Thanks @merefield and @Canapin . I’ve been trying to learn the Discourse plugin API fiddling around with what feels like they should be simple things like this but modifyClass is so inconsistent when it will/won’t work. I’ve got a little theme component (that one you were mentioned into @merefield ) where two modifyClass calls work but a third doesn’t, very frustrating trying to figure out if I’ve got something wrong or it’s just modifyClass.
Sorry for the OT on your topic and thanks for the quick reply @Canapin
Update:
1 Like
Canapin
(Coin-coin le Canapin)
February 28, 2023, 2:16pm
7
No worry, I don’t know what OT means but we’re here to help!
1 Like
Canapin
(Coin-coin le Canapin)
Closed
March 30, 2023, 2:16pm
8
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.