mirror of
https://ghfast.top/https://github.com/discourse/discourse-shared-edits.git
synced 2026-07-15 11:17:01 +08:00
Some checks failed
Discourse Plugin / ci (push) Has been cancelled
Competed with discourse-calendar and lost race
86 lines
2.6 KiB
JavaScript
86 lines
2.6 KiB
JavaScript
import { service } from "@ember/service";
|
|
import { withPluginApi } from "discourse/lib/plugin-api";
|
|
|
|
const SHARED_EDIT_ACTION = "sharedEdit";
|
|
|
|
export default {
|
|
name: "discourse-shared-edits-composer-service",
|
|
before: "inject-discourse-objects",
|
|
|
|
initialize: (container) => {
|
|
const siteSettings = container.lookup("service:site-settings");
|
|
if (!siteSettings.shared_edits_enabled) {
|
|
return;
|
|
}
|
|
|
|
withPluginApi((api) => {
|
|
api.modifyClass(
|
|
"service:composer",
|
|
(Superclass) =>
|
|
class extends Superclass {
|
|
@service sharedEditManager;
|
|
|
|
async open(opts) {
|
|
if (opts.action === SHARED_EDIT_ACTION && opts.post?.id) {
|
|
const subscription = await this.sharedEditManager.subscribe(
|
|
opts.post.id,
|
|
{ preOpen: true }
|
|
);
|
|
if (subscription?.reply !== undefined) {
|
|
opts.reply = subscription.reply;
|
|
}
|
|
}
|
|
|
|
await super.open(...arguments);
|
|
|
|
if (opts.action === SHARED_EDIT_ACTION && opts.post?.id) {
|
|
await this.sharedEditManager.finalizeSubscription();
|
|
}
|
|
}
|
|
|
|
collapse() {
|
|
if (this.model?.action === SHARED_EDIT_ACTION) {
|
|
return this.close();
|
|
}
|
|
return super.collapse(...arguments);
|
|
}
|
|
|
|
async close() {
|
|
const wasSharedEdit = this.model?.action === SHARED_EDIT_ACTION;
|
|
|
|
// Suppress composer observer during close to prevent Y.Text from being
|
|
// wiped when model.reply is cleared
|
|
if (wasSharedEdit) {
|
|
this.sharedEditManager.suppressComposerChange = true;
|
|
}
|
|
try {
|
|
const result = await super.close(...arguments);
|
|
if (wasSharedEdit) {
|
|
await this.sharedEditManager.commit();
|
|
}
|
|
return result;
|
|
} finally {
|
|
if (wasSharedEdit) {
|
|
this.sharedEditManager.suppressComposerChange = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
save() {
|
|
if (this.model?.action === SHARED_EDIT_ACTION) {
|
|
return this.close();
|
|
}
|
|
return super.save(...arguments);
|
|
}
|
|
|
|
_saveDraft() {
|
|
if (this.model?.action === SHARED_EDIT_ACTION) {
|
|
return;
|
|
}
|
|
return super._saveDraft(...arguments);
|
|
}
|
|
}
|
|
);
|
|
});
|
|
},
|
|
};
|