@@ -251,6 +251,8 @@ export default {
251
251
// this.isDarkMode = !this.isDarkMode;
252
252
};
253
253
254
+ const colorCodeSymbol = ' §' ;
255
+
254
256
const formatText = () => {
255
257
let formatted = rawText .value ;
256
258
for (let braceCode in colorsBrace .value ) {
@@ -266,48 +268,63 @@ export default {
266
268
underline: false ,
267
269
strikethrough: false ,
268
270
};
271
+ let newState = { ... state };
269
272
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
+ };
271
288
272
- while (i < formatted . length ) {
273
- if (formatted [i] === ' §' ) {
289
+ for ( let i = 0 ; i < rawText . value . length ; i ++ ) {
290
+ if (rawText . value [i] === ' §' ) {
274
291
i++ ; // 跳过 '§'
275
- let colorTag = ' ' ;
276
- switch (formatted[i]) {
292
+ switch (rawText .value [i]) {
277
293
case ' r' :
278
- state = { color: " " , bold: false , italic: false , underline: false , strikethrough: false };
294
+ newState = { color: " " , bold: false , italic: false , underline: false , strikethrough: false };
279
295
break ;
280
296
case ' l' :
281
- state .bold = true ;
297
+ newState .bold = true ;
282
298
break ;
283
299
case ' o' :
284
- state .italic = true ;
300
+ newState .italic = true ;
285
301
break ;
286
302
case ' n' :
287
- state .underline = true ;
303
+ newState .underline = true ;
288
304
break ;
289
305
case ' m' :
290
- state .strikethrough = true ;
306
+ newState .strikethrough = true ;
291
307
break ;
292
308
default :
293
- colorTag = ` §${ formatted[i]} ` ;
294
- state .color = colors .value [colorTag] || state .color ;
309
+ newState .color = colors .value [` ${ colorCodeSymbol}${ rawText .value [i]} ` ] || newState .color ;
295
310
break ;
296
311
}
297
- } else if (formatted[i] === ' \n ' ) {
312
+ } else if (rawText .value [i] === ' \n ' ) {
313
+ applyStylesAndResetBuffer (); // 应用样式并清空缓冲
298
314
output += " <br>" ;
299
315
} 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 }; // 更新当前状态为新状态
307
323
}
308
- i++ ;
309
324
}
310
325
326
+ applyStylesAndResetBuffer (); // 确保最后的缓冲内容被应用
327
+
311
328
formattedText .value = output;
312
329
};
313
330
0 commit comments