Skip to content

Commit d1ac475

Browse files
committed
Optimize rendering performance.
1 parent 1829cdb commit d1ac475

File tree

1 file changed

+38
-21
lines changed

1 file changed

+38
-21
lines changed

src/components/MCTextFormatter.vue

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,8 @@ export default {
251251
// this.isDarkMode = !this.isDarkMode;
252252
};
253253
254+
const colorCodeSymbol = '§';
255+
254256
const formatText = () => {
255257
let formatted = rawText.value;
256258
for (let braceCode in colorsBrace.value) {
@@ -266,48 +268,63 @@ export default {
266268
underline: false,
267269
strikethrough: false,
268270
};
271+
let newState = { ...state };
269272
let output = "";
270-
let i = 0;
273+
let contentBuffer = "";
274+
275+
// 应用样式到缓冲内容,并重置缓冲
276+
const applyStylesAndResetBuffer = () => {
277+
if (contentBuffer) {
278+
let spanStart = `<span style="color: ${state.color};`;
279+
if (state.bold) spanStart += " font-weight: bold;";
280+
if (state.italic) spanStart += " font-style: italic;";
281+
if (state.underline) spanStart += " text-decoration: underline;";
282+
if (state.strikethrough) spanStart += " text-decoration: line-through;";
283+
spanStart += `">`;
284+
output += spanStart + contentBuffer + "</span>";
285+
contentBuffer = ""; // 重置内容缓冲
286+
}
287+
};
271288
272-
while (i < formatted.length) {
273-
if (formatted[i] === '§') {
289+
for (let i = 0; i < rawText.value.length; i++) {
290+
if (rawText.value[i] === '§') {
274291
i++; // 跳过 '§'
275-
let colorTag = '';
276-
switch (formatted[i]) {
292+
switch (rawText.value[i]) {
277293
case 'r':
278-
state = { color: "", bold: false, italic: false, underline: false, strikethrough: false };
294+
newState = { color: "", bold: false, italic: false, underline: false, strikethrough: false };
279295
break;
280296
case 'l':
281-
state.bold = true;
297+
newState.bold = true;
282298
break;
283299
case 'o':
284-
state.italic = true;
300+
newState.italic = true;
285301
break;
286302
case 'n':
287-
state.underline = true;
303+
newState.underline = true;
288304
break;
289305
case 'm':
290-
state.strikethrough = true;
306+
newState.strikethrough = true;
291307
break;
292308
default:
293-
colorTag = `§${formatted[i]}`;
294-
state.color = colors.value[colorTag] || state.color;
309+
newState.color = colors.value[`${colorCodeSymbol}${rawText.value[i]}`] || newState.color;
295310
break;
296311
}
297-
} else if (formatted[i] === '\n') {
312+
} else if (rawText.value[i] === '\n') {
313+
applyStylesAndResetBuffer(); // 应用样式并清空缓冲
298314
output += "<br>";
299315
} else {
300-
let spanStart = `<span style="color: ${state.color};`;
301-
if (state.bold) spanStart += " font-weight: bold;";
302-
if (state.italic) spanStart += " font-style: italic;";
303-
if (state.underline) spanStart += " text-decoration: underline;";
304-
if (state.strikethrough) spanStart += " text-decoration: line-through;";
305-
spanStart += `">`;
306-
output += spanStart + formatted[i] + "</span>";
316+
contentBuffer += rawText.value[i]; // 加入当前字符到缓冲
317+
}
318+
319+
// 检查状态是否改变
320+
if (JSON.stringify(state) !== JSON.stringify(newState)) {
321+
applyStylesAndResetBuffer(); // 应用当前样式并重置缓冲
322+
state = { ...newState }; // 更新当前状态为新状态
307323
}
308-
i++;
309324
}
310325
326+
applyStylesAndResetBuffer(); // 确保最后的缓冲内容被应用
327+
311328
formattedText.value = output;
312329
};
313330

0 commit comments

Comments
 (0)