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";
|
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
|
|
|
)
|
2021-06-02 01:36:49 -04:00
|
|
|
matches(value, regexpString, words, isReplace, isTag, isLink) {
|
2020-09-22 10:28:28 -04:00
|
|
|
if (!value || !regexpString) {
|
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) => {
|
|
|
|
const regexp = new RegExp(word.regexp, "gi");
|
|
|
|
let match;
|
|
|
|
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) => {
|
|
|
|
const regexp = new RegExp(word.regexp, "gi");
|
|
|
|
let match;
|
|
|
|
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 {
|
|
|
|
return value.match(new RegExp(regexpString, "ig")) || [];
|
|
|
|
}
|
2019-08-02 05:53:03 -04:00
|
|
|
},
|
|
|
|
});
|