2023-08-01 17:08:56 -04:00
|
|
|
import Component from "@glimmer/component";
|
|
|
|
import { tracked } from "@glimmer/tracking";
|
|
|
|
|
|
|
|
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) => {
|
2024-06-10 08:44:31 -04:00
|
|
|
const regexp = new RegExp(
|
|
|
|
word.regexp,
|
|
|
|
word.case_sensitive ? "gu" : "gui"
|
|
|
|
);
|
2023-08-01 17:08:56 -04:00
|
|
|
let match;
|
|
|
|
|
|
|
|
while ((match = regexp.exec(this.value)) !== null) {
|
|
|
|
matches.push({
|
|
|
|
match: match[1],
|
|
|
|
replacement: word.replacement,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return matches;
|
2024-06-10 08:44:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.isTag) {
|
|
|
|
const matches = new Map();
|
2023-08-01 17:08:56 -04:00
|
|
|
this.args.model.watchedWord.words.forEach((word) => {
|
2024-06-10 08:44:31 -04:00
|
|
|
const regexp = new RegExp(
|
|
|
|
word.regexp,
|
|
|
|
word.case_sensitive ? "gu" : "gui"
|
|
|
|
);
|
2023-08-01 17:08:56 -04:00
|
|
|
let match;
|
|
|
|
|
|
|
|
while ((match = regexp.exec(this.value)) !== null) {
|
2024-06-10 08:44:31 -04:00
|
|
|
if (!matches.has(match[1])) {
|
|
|
|
matches.set(match[1], new Set());
|
2023-08-01 17:08:56 -04:00
|
|
|
}
|
|
|
|
|
2024-06-10 08:44:31 -04:00
|
|
|
const tags = matches.get(match[1]);
|
|
|
|
word.replacement.split(",").forEach((tag) => tags.add(tag));
|
2023-08-01 17:08:56 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-06-10 08:44:31 -04:00
|
|
|
return Array.from(matches, ([match, tagsSet]) => ({
|
|
|
|
match,
|
|
|
|
tags: Array.from(tagsSet),
|
2023-08-01 17:08:56 -04:00
|
|
|
}));
|
2024-06-10 08:44:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
let matches = [];
|
|
|
|
this.args.model.watchedWord.compiledRegularExpression.forEach((entry) => {
|
|
|
|
const [regexp, options] = Object.entries(entry)[0];
|
|
|
|
const wordRegexp = new RegExp(
|
|
|
|
regexp,
|
|
|
|
options.case_sensitive ? "gu" : "gui"
|
2023-08-01 17:08:56 -04:00
|
|
|
);
|
|
|
|
|
2024-06-10 08:44:31 -04:00
|
|
|
matches.push(...(this.value.match(wordRegexp) || []));
|
|
|
|
});
|
|
|
|
|
|
|
|
return matches;
|
2023-08-01 17:08:56 -04:00
|
|
|
}
|
|
|
|
}
|