Skip to content

Commit

Permalink
Rewrite 'formatText', support for line pass formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
boybook committed Mar 28, 2024
1 parent 1788462 commit 1829cdb
Showing 1 changed file with 48 additions and 23 deletions.
71 changes: 48 additions & 23 deletions src/components/MCTextFormatter.vue
Original file line number Diff line number Diff line change
Expand Up @@ -259,31 +259,56 @@ export default {
formatted = formatted.replace(regex, replacement);
}
const boldRegex = /§l(.*?)(?=§r|\n|$)/g;
formatted = formatted.replace(boldRegex, `<strong>$1</strong>`);
const underlineRegex = /§n(.*?)(?=§r|\n|$)/g;
formatted = formatted.replace(underlineRegex, `<u>$1</u>`);
const strikethroughRegex = /§m(.*?)(?=§r|\n|$)/g;
formatted = formatted.replace(strikethroughRegex, `<s>$1</s>`);
const italicRegex = /§o(.*?)(?=§r|\n|$)/g;
formatted = formatted.replace(italicRegex, `<em>$1</em>`);
for (let code in colors.value) {
const regex = new RegExp(`${code}(.*?)(?=(§[0-9a-u]|\n|$))`, "g");
formatted = formatted.replace(regex, (match, content) => {
return `<span style="color: ${colors.value[code]}">${content}</span>`;
});
let state = {
color: "",
bold: false,
italic: false,
underline: false,
strikethrough: false,
};
let output = "";
let i = 0;
while (i < formatted.length) {
if (formatted[i] === '§') {
i++; // 跳过 '§'
let colorTag = '';
switch (formatted[i]) {
case 'r':
state = { color: "", bold: false, italic: false, underline: false, strikethrough: false };
break;
case 'l':
state.bold = true;
break;
case 'o':
state.italic = true;
break;
case 'n':
state.underline = true;
break;
case 'm':
state.strikethrough = true;
break;
default:
colorTag = `§${formatted[i]}`;
state.color = colors.value[colorTag] || state.color;
break;
}
} else if (formatted[i] === '\n') {
output += "<br>";
} else {
let spanStart = `<span style="color: ${state.color};`;
if (state.bold) spanStart += " font-weight: bold;";
if (state.italic) spanStart += " font-style: italic;";
if (state.underline) spanStart += " text-decoration: underline;";
if (state.strikethrough) spanStart += " text-decoration: line-through;";
spanStart += `">`;
output += spanStart + formatted[i] + "</span>";
}
i++;
}
const resetRegex = /§r/g;
formatted = formatted.replace(resetRegex, '');
const newlineRegex = /\n/g;
formatted = formatted.replace(newlineRegex, '<br>');
formattedText.value = formatted;
formattedText.value = output;
};
return {
Expand Down

0 comments on commit 1829cdb

Please sign in to comment.