2023-03-15 17:39:33 +00:00
|
|
|
import { equal } from "@ember/object/computed";
|
2019-10-23 13:06:54 -04:00
|
|
|
import Controller from "@ember/controller";
|
2019-08-02 10:53:03 +01:00
|
|
|
import ModalFunctionality from "discourse/mixins/modal-functionality";
|
2020-01-16 18:56:53 +01:00
|
|
|
import discourseComputed from "discourse-common/utils/decorators";
|
2022-08-02 08:06:03 +00:00
|
|
|
import {
|
|
|
|
createWatchedWordRegExp,
|
|
|
|
toWatchedWord,
|
|
|
|
} from "discourse-common/utils/watched-words";
|
2019-08-02 10:53:03 +01:00
|
|
|
|
2023-03-15 17:39:33 +00:00
|
|
|
export default class AdminWatchedWordTestController extends Controller.extend(
|
|
|
|
ModalFunctionality
|
|
|
|
) {
|
|
|
|
@equal("model.nameKey", "replace") isReplace;
|
|
|
|
|
|
|
|
@equal("model.nameKey", "tag") isTag;
|
|
|
|
@equal("model.nameKey", "link") isLink;
|
2021-05-21 17:50:24 +03:00
|
|
|
|
|
|
|
@discourseComputed(
|
|
|
|
"value",
|
|
|
|
"model.compiledRegularExpression",
|
|
|
|
"model.words",
|
|
|
|
"isReplace",
|
2021-06-02 08:36:49 +03:00
|
|
|
"isTag",
|
|
|
|
"isLink"
|
2021-05-21 17:50:24 +03:00
|
|
|
)
|
2022-08-02 08:06:03 +00:00
|
|
|
matches(value, regexpList, words, isReplace, isTag, isLink) {
|
|
|
|
if (!value || regexpList.length === 0) {
|
2021-06-25 12:08:52 +03:00
|
|
|
return [];
|
2020-09-22 16:28:28 +02:00
|
|
|
}
|
2021-05-21 17:50:24 +03:00
|
|
|
|
2021-06-02 08:36:49 +03:00
|
|
|
if (isReplace || isLink) {
|
2021-06-25 12:08:52 +03:00
|
|
|
const matches = [];
|
|
|
|
words.forEach((word) => {
|
2022-08-02 08:06:03 +00:00
|
|
|
const regexp = createWatchedWordRegExp(word);
|
2021-06-25 12:08:52 +03:00
|
|
|
let match;
|
2022-08-02 08:06:03 +00:00
|
|
|
|
2021-06-25 12:08:52 +03:00
|
|
|
while ((match = regexp.exec(value)) !== null) {
|
|
|
|
matches.push({
|
|
|
|
match: match[1],
|
|
|
|
replacement: word.replacement,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return matches;
|
2021-05-21 17:50:24 +03:00
|
|
|
} else if (isTag) {
|
2021-06-25 12:08:52 +03:00
|
|
|
const matches = {};
|
|
|
|
words.forEach((word) => {
|
2022-08-02 08:06:03 +00:00
|
|
|
const regexp = createWatchedWordRegExp(word);
|
2021-06-25 12:08:52 +03:00
|
|
|
let match;
|
2022-08-02 08:06:03 +00:00
|
|
|
|
2021-06-25 12:08:52 +03:00
|
|
|
while ((match = regexp.exec(value)) !== null) {
|
|
|
|
if (!matches[match[1]]) {
|
|
|
|
matches[match[1]] = new Set();
|
2021-05-21 17:50:24 +03:00
|
|
|
}
|
|
|
|
|
2021-06-25 12:08:52 +03:00
|
|
|
let tags = matches[match[1]];
|
|
|
|
word.replacement.split(",").forEach((tag) => {
|
|
|
|
tags.add(tag);
|
|
|
|
});
|
|
|
|
}
|
2021-05-21 17:50:24 +03:00
|
|
|
});
|
|
|
|
|
2021-06-25 12:08:52 +03:00
|
|
|
return Object.entries(matches).map((entry) => ({
|
|
|
|
match: entry[0],
|
|
|
|
tags: Array.from(entry[1]),
|
|
|
|
}));
|
|
|
|
} else {
|
2022-08-02 08:06:03 +00:00
|
|
|
let matches = [];
|
|
|
|
regexpList.forEach((regexp) => {
|
|
|
|
const wordRegexp = createWatchedWordRegExp(toWatchedWord(regexp));
|
|
|
|
|
|
|
|
matches.push(...(value.match(wordRegexp) || []));
|
|
|
|
});
|
|
|
|
|
|
|
|
return matches;
|
2021-06-25 12:08:52 +03:00
|
|
|
}
|
2023-03-15 17:39:33 +00:00
|
|
|
}
|
|
|
|
}
|