So I stumbled on the idea of allowing those with editing post permissions (TL4+ users) to be able to select text and convert that part to a codeblock.
After referencing some parts of the Discourse AI plugin code and looking at what data is passed into the plugin outlet I’m using, I came up with the code to get the text selected, and wrap code fencss around it, using the quote-share-buttons-before outlet.
Then now, I’m kind of stuck. The checklist plugin (somehow) does this with an argument in the checklistSyntax function called postDecorator, which they can then get the model (I think it’s the post model?) using .getModel(). How that is passed in I don’t know, but I don’t think that is available for me in my setup?
If it’s truly the post model being used in the checklist plugin, I seem to have trouble finding where the simple ‘save’ function used in
exists in models/post.js, unless I’m understanding it wrong.
I’m currently using this.args.outletArgs.data.editPost(this.post) (which IIRC is in models/topic.js, but please correct me if I’m wrong) which only opens up the composer to edit the post, not just edit it directly.
So my question is this - how can I edit the post like the checklist plugin, fuss-free? Is there a way to do that if I’m putting a button in an outlet instead of api.decorateCookedElement like what checklist does? Preferably without using the REST API?
I tried this.post.save() and it apparently “works” - it seems like the PUT request is going through (using ajax under the hood), but was met with some error with little to no information. But that was before this…
The this.post.save() works. The only problem is the one listed below:
My function is now like this:
@action
async addCodeFences() {
let selectedText = this.selectedText;
let newText = "```" + "\n" + selectedText + "\n" + "```";
let post = this.post;
console.log(this.post.cooked); // Works
let rawPost = post.raw; // Doesn't exist!!
console.log(rawPost); // undefined!
rawPost.replace(selectedText, "\n" + newText + "\n"); // Error, since rawPost is undefined!
await this.post.save({
raw: rawPost,
edit_reason: I18n.t(themePrefix("add_code_fence_edit_reason"))
});
}
For some reason, I cannot get the post’s raw contents, only the cooked. Besides doing an ajax request, is there a way to do it? I can’t seem to find raw as one of the attributes for the post… is there a reason why it isn’t included?
P.S. I’m trying not to turn to Ask Discourse here. I would really appreciate any help!
EDIT: I think each post is based of this API request? And the response doesn’t seem to have raw as one of the JSON keys .
Yeah that’s right - we don’t include raw in the default response. It’s not needed for normal rendering, so including it would be kinda wasteful in terms of data-transfer/speed.