DEV: Add composer-service-cannot-submit-post transformer (#30903)

- Add `composer-service-cannot-submit-post` transformer to allow for disabling submit based on custom conditions
- Add tests for transformer
- Add a couple helpful appEvents, that plugins can use add custom error popups to plugin-defined fields.
This commit is contained in:
Isaac Janzen
2025-02-07 10:27:26 -06:00
committed by GitHub
parent 42447770bf
commit 6595b11e25
3 changed files with 61 additions and 1 deletions

View File

@ -9,6 +9,7 @@ export const VALUE_TRANSFORMERS = Object.freeze([
// use only lowercase names // use only lowercase names
"category-description-text", "category-description-text",
"category-display-name", "category-display-name",
"composer-service-cannot-submit-post",
"header-notifications-avatar-size", "header-notifications-avatar-size",
"home-logo-href", "home-logo-href",
"home-logo-image-url", "home-logo-image-url",

View File

@ -29,6 +29,7 @@ import getURL from "discourse/lib/get-url";
import { disableImplicitInjections } from "discourse/lib/implicit-injections"; import { disableImplicitInjections } from "discourse/lib/implicit-injections";
import { wantsNewWindow } from "discourse/lib/intercept-click"; import { wantsNewWindow } from "discourse/lib/intercept-click";
import { emojiUnescape } from "discourse/lib/text"; import { emojiUnescape } from "discourse/lib/text";
import { applyValueTransformer } from "discourse/lib/transformer";
import { import {
authorizesOneOrMoreExtensions, authorizesOneOrMoreExtensions,
uploadIcon, uploadIcon,
@ -1008,13 +1009,21 @@ export default class ComposerService extends Service {
} }
const composer = this.model; const composer = this.model;
const cantSubmitPost = applyValueTransformer(
"composer-service-cannot-submit-post",
composer?.cantSubmitPost,
{ model: composer }
);
if (composer?.cantSubmitPost) { if (cantSubmitPost) {
if (composer?.viewFullscreen) { if (composer?.viewFullscreen) {
this.toggleFullscreen(); this.toggleFullscreen();
} }
this.set("lastValidatedAt", Date.now()); this.set("lastValidatedAt", Date.now());
this.appEvents.trigger("composer-service:last-validated-at-updated", {
model: composer,
});
return; return;
} }
@ -1752,6 +1761,7 @@ export default class ComposerService extends Service {
clearLastValidatedAt() { clearLastValidatedAt() {
this.set("lastValidatedAt", null); this.set("lastValidatedAt", null);
this.appEvents.trigger("composer-service:last-validated-at-cleared");
} }
} }

View File

@ -0,0 +1,49 @@
import { click, fillIn, visit } from "@ember/test-helpers";
import { test } from "qunit";
import { withPluginApi } from "discourse/lib/plugin-api";
import { acceptance } from "discourse/tests/helpers/qunit-helpers";
acceptance("composer-service-cannot-submit-post transformer", function (needs) {
needs.user();
needs.settings({
allow_uncategorized_topics: true,
});
test("applying a value transformation - disable submit", async function (assert) {
withPluginApi("1.34.0", (api) => {
api.registerValueTransformer(
"composer-service-cannot-submit-post",
() => {
// Return true -- explicitly block submission!
return true;
}
);
});
await visit("/new-topic?title=topic title that is pretty long");
await fillIn(".d-editor-input", "this is the *content* of a post");
await click(".submit-panel .create");
assert.dom(".d-editor-input").exists("composer is still open");
});
test("applying a value transformation - allow submission", async function (assert) {
withPluginApi("1.34.0", (api) => {
api.registerValueTransformer(
"composer-service-cannot-submit-post",
({ value }) => {
// Return value (which should be `false`, as we have a valid new topic to create)
return value;
}
);
});
await visit("/new-topic?title=topic title that is pretty long");
await fillIn(".d-editor-input", "this is the *content* of a post");
await click(".submit-panel .create");
assert
.dom(".d-editor-input")
.doesNotExist("closes the composer on successful creation");
});
});