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