diff --git a/app/assets/javascripts/admin/addon/components/modal/watched-word-test.hbs b/app/assets/javascripts/admin/addon/components/modal/watched-word-test.hbs
new file mode 100644
index 00000000000..4c9f9d073d2
--- /dev/null
+++ b/app/assets/javascripts/admin/addon/components/modal/watched-word-test.hbs
@@ -0,0 +1,45 @@
+ {{i18n "admin.watched_words.test.description"}}
+ {{i18n "admin.watched_words.test.found_matches"}}
+
+ {{#if (or this.isReplace this.isLink)}}
+ {{#each this.matches as |match|}}
+
+
{{i18n "admin.watched_words.test.no_matches"}}
+ {{/if}} + + \ No newline at end of file diff --git a/app/assets/javascripts/admin/addon/components/modal/watched-word-test.js b/app/assets/javascripts/admin/addon/components/modal/watched-word-test.js new file mode 100644 index 00000000000..bbf92e29cff --- /dev/null +++ b/app/assets/javascripts/admin/addon/components/modal/watched-word-test.js @@ -0,0 +1,79 @@ +import Component from "@glimmer/component"; +import { tracked } from "@glimmer/tracking"; +import { + createWatchedWordRegExp, + toWatchedWord, +} from "discourse-common/utils/watched-words"; + +export default class WatchedWordTest extends Component { + @tracked value; + + get isReplace() { + return this.args.model.watchedWord.nameKey === "replace"; + } + + get isTag() { + return this.args.model.watchedWord.nameKey === "tag"; + } + + get isLink() { + return this.args.model.watchedWord.nameKey === "link"; + } + + get matches() { + if ( + !this.value || + this.args.model.watchedWord.compiledRegularExpression.length === 0 + ) { + return []; + } + + if (this.isReplace || this.isLink) { + const matches = []; + this.args.model.watchedWord.words.forEach((word) => { + const regexp = createWatchedWordRegExp(word); + let match; + + while ((match = regexp.exec(this.value)) !== null) { + matches.push({ + match: match[1], + replacement: word.replacement, + }); + } + }); + return matches; + } else if (this.isTag) { + const matches = {}; + this.args.model.watchedWord.words.forEach((word) => { + const regexp = createWatchedWordRegExp(word); + let match; + + while ((match = regexp.exec(this.value)) !== null) { + if (!matches[match[1]]) { + matches[match[1]] = new Set(); + } + + let tags = matches[match[1]]; + word.replacement.split(",").forEach((tag) => { + tags.add(tag); + }); + } + }); + + return Object.entries(matches).map((entry) => ({ + match: entry[0], + tags: Array.from(entry[1]), + })); + } else { + let matches = []; + this.args.model.watchedWord.compiledRegularExpression.forEach( + (regexp) => { + const wordRegexp = createWatchedWordRegExp(toWatchedWord(regexp)); + matches.push(...(this.value.match(wordRegexp) || [])); + } + ); + + return matches; + } + } +} diff --git a/app/assets/javascripts/admin/addon/controllers/admin-watched-words-action.js b/app/assets/javascripts/admin/addon/controllers/admin-watched-words-action.js index 9529d6bb7f3..ca097c2ac6c 100644 --- a/app/assets/javascripts/admin/addon/controllers/admin-watched-words-action.js +++ b/app/assets/javascripts/admin/addon/controllers/admin-watched-words-action.js @@ -8,10 +8,11 @@ import { ajax } from "discourse/lib/ajax"; import discourseComputed from "discourse-common/utils/decorators"; import { fmt } from "discourse/lib/computed"; import { schedule } from "@ember/runloop"; -import showModal from "discourse/lib/show-modal"; +import WatchedWordTestModal from "admin/components/modal/watched-word-test"; export default class AdminWatchedWordsActionController extends Controller { @service dialog; + @service modal; @controller adminWatchedWords; actionNameKey = null; @@ -91,9 +92,8 @@ export default class AdminWatchedWordsActionController extends Controller { test() { WatchedWord.findAll().then((data) => { this.adminWatchedWords.set("model", data); - showModal("admin-watched-word-test", { - admin: true, - model: this.currentAction, + this.modal.show(WatchedWordTestModal, { + model: { watchedWord: this.currentAction }, }); }); } diff --git a/app/assets/javascripts/admin/addon/controllers/modals/admin-watched-word-test.js b/app/assets/javascripts/admin/addon/controllers/modals/admin-watched-word-test.js deleted file mode 100644 index 5b8f9f3b132..00000000000 --- a/app/assets/javascripts/admin/addon/controllers/modals/admin-watched-word-test.js +++ /dev/null @@ -1,78 +0,0 @@ -import { equal } from "@ember/object/computed"; -import Controller from "@ember/controller"; -import ModalFunctionality from "discourse/mixins/modal-functionality"; -import discourseComputed from "discourse-common/utils/decorators"; -import { - createWatchedWordRegExp, - toWatchedWord, -} from "discourse-common/utils/watched-words"; - -export default class AdminWatchedWordTestController extends Controller.extend( - ModalFunctionality -) { - @equal("model.nameKey", "replace") isReplace; - - @equal("model.nameKey", "tag") isTag; - @equal("model.nameKey", "link") isLink; - - @discourseComputed( - "value", - "model.compiledRegularExpression", - "model.words", - "isReplace", - "isTag", - "isLink" - ) - matches(value, regexpList, words, isReplace, isTag, isLink) { - if (!value || regexpList.length === 0) { - return []; - } - - if (isReplace || isLink) { - const matches = []; - words.forEach((word) => { - const regexp = createWatchedWordRegExp(word); - let match; - - while ((match = regexp.exec(value)) !== null) { - matches.push({ - match: match[1], - replacement: word.replacement, - }); - } - }); - return matches; - } else if (isTag) { - const matches = {}; - words.forEach((word) => { - const regexp = createWatchedWordRegExp(word); - let match; - - while ((match = regexp.exec(value)) !== null) { - if (!matches[match[1]]) { - matches[match[1]] = new Set(); - } - - let tags = matches[match[1]]; - word.replacement.split(",").forEach((tag) => { - tags.add(tag); - }); - } - }); - - return Object.entries(matches).map((entry) => ({ - match: entry[0], - tags: Array.from(entry[1]), - })); - } else { - let matches = []; - regexpList.forEach((regexp) => { - const wordRegexp = createWatchedWordRegExp(toWatchedWord(regexp)); - - matches.push(...(value.match(wordRegexp) || [])); - }); - - return matches; - } - } -} diff --git a/app/assets/javascripts/admin/addon/templates/modal/admin-watched-word-test.hbs b/app/assets/javascripts/admin/addon/templates/modal/admin-watched-word-test.hbs deleted file mode 100644 index 01d04391627..00000000000 --- a/app/assets/javascripts/admin/addon/templates/modal/admin-watched-word-test.hbs +++ /dev/null @@ -1,42 +0,0 @@ -{{i18n "admin.watched_words.test.description"}}
- - {{#if this.matches}} -- {{i18n "admin.watched_words.test.found_matches"}} -
{{i18n "admin.watched_words.test.no_matches"}}
- {{/if}} -