fix: wrong color display of space characters

This commit is contained in:
--global 2024-08-24 23:28:28 +08:00
parent b13c331cba
commit 8243131b05
12 changed files with 37 additions and 17 deletions

View File

@ -25,7 +25,7 @@ class BBCodeAdapterClazz extends GradientProcessor {
textBuilder.withFormat(chunk.format?.strikethrough, this.format.strikethrough);
chunk.tags.forEach((tag) => {
textBuilder.appendCharacter(new CharacterBuilder(tag.character).withColor([`[color=${tag.color}]`, "[/color]"]));
textBuilder.appendCharacter(new CharacterBuilder(tag.character).withColor(tag.color, [`[color={color}]`, "[/color]"]));
});
return textBuilder.build();

View File

@ -14,7 +14,7 @@ class CMIAdapterClazz extends GradientProcessor {
textBuilder.withFormat(chunk.format?.strikethrough, this.format.strikethrough);
chunk.tags.forEach((tag) => {
textBuilder.appendCharacter(new CharacterBuilder(tag.character).withColor(`{${tag.color}}`));
textBuilder.appendCharacter(new CharacterBuilder(tag.character).withColor(tag.color, `{{color}}`));
});
return textBuilder.build();

View File

@ -26,7 +26,10 @@ class HTMLAdapterClazz extends GradientProcessor {
chunk.tags.forEach((tag) => {
textBuilder.appendCharacter(
new CharacterBuilder(tag.character.trim() === "" ? "&nbsp;" : tag.character).withColor([`<span style="color: ${tag.color};">`, "</span>"])
new CharacterBuilder(tag.character.trim() === "" ? "&nbsp;" : tag.character).withColor(tag.color, [
`<span style="color: {color};">`,
"</span>",
])
);
});

View File

@ -25,7 +25,7 @@ class MineDownAdapterClazz extends GradientProcessor {
textBuilder.withFormat(chunk.format?.strikethrough, this.format.strikethrough);
chunk.tags.forEach((tag) => {
textBuilder.appendCharacter(new CharacterBuilder(tag.character).withColor(`&${tag.color}&`));
textBuilder.appendCharacter(new CharacterBuilder(tag.character).withColor(tag.color, `&{color}&`));
});
return textBuilder.build();

View File

@ -37,7 +37,7 @@ class MiniMessageGradientAdapterClazz extends GradientProcessor {
const characterBuilder = new CharacterBuilder(texts.join(""));
if (colors.length === 1) {
textBuilder.appendCharacter(characterBuilder.withColor(`<${colors.join(":")}>`));
textBuilder.appendCharacter(characterBuilder.withColor(colors.join(":"), `<{color}>`));
return textBuilder.build();
}

View File

@ -25,7 +25,7 @@ class MiniMessageAdapterClazz extends GradientProcessor {
textBuilder.withFormat(chunk.format?.strikethrough, this.format.strikethrough);
chunk.tags.forEach((tag) => {
textBuilder.appendCharacter(new CharacterBuilder(tag.character).withColor(`<${tag.color}>`));
textBuilder.appendCharacter(new CharacterBuilder(tag.character).withColor(tag.color, `<{color}>`));
});
return textBuilder.build();

View File

@ -37,7 +37,7 @@ class MotdAdapterClazz extends GradientProcessor {
.reduce((acc, char) => `${acc}${this.charCode}${char}`, "") as string;
}
textBuilder.appendCharacter(new CharacterBuilder(tag.character).withColor(`${this.charCode}x${color}`));
textBuilder.appendCharacter(new CharacterBuilder(tag.character).withColor(color, `${this.charCode}x{color}`));
});
return textBuilder.build();

View File

@ -14,7 +14,7 @@ class TrChatAdapterClazz extends GradientProcessor {
textBuilder.withFormat(chunk.format?.strikethrough, this.format.strikethrough);
chunk.tags.forEach((tag) => {
textBuilder.appendCharacter(new CharacterBuilder(tag.character).withColor(`&{${tag.color}}`));
textBuilder.appendCharacter(new CharacterBuilder(tag.character).withColor(tag.color, `&{{color}}`));
});
return textBuilder.build();

View File

@ -23,7 +23,7 @@ class VanillaCompatibleAdapterClazz extends GradientProcessor {
.reduce((acc, char) => `${acc}${this.vanillaCharCode}${char}`, "") as string;
}
textBuilder.appendCharacter(new CharacterBuilder(tag.character).withColor(`${this.vanillaCharCode}x${color}`));
textBuilder.appendCharacter(new CharacterBuilder(tag.character).withColor(color, `${this.vanillaCharCode}x{color}`));
});
return textBuilder.build();

View File

@ -17,7 +17,7 @@ class VanillaAdapterClazz extends GradientProcessor {
const characterBuilder = new CharacterBuilder(tag.character);
if (tag.character !== "") {
characterBuilder.withColor(this.vanillaCharCode + tag.color);
characterBuilder.withColor(tag.color, `${this.vanillaCharCode}{color}`);
}
textBuilder.appendCharacter(characterBuilder);

View File

@ -32,24 +32,39 @@ export class TextBuilder {
export class CharacterBuilder {
public character: string;
public colorExpression: FormatExpression | null = null;
public colorExpression!: FormatExpression;
public colorValue!: string | HexColorString | null;
constructor(character: string) {
this.character = character;
}
public withColor(colorExpression: FormatExpression | null) {
public withColor(value: string | HexColorString | null, colorExpression: FormatExpression = "{color}") {
this.colorExpression = colorExpression;
this.colorValue = value;
return this;
}
public build(template: string) {
const char = template.replace("{text}", this.character);
if (Array.isArray(this.colorExpression)) {
return `${this.colorExpression[0]}${char}${this.colorExpression[1]}`;
} else if (!isEmpty(this.colorExpression)) {
return this.colorExpression + char;
if (this.colorValue !== null) {
const expression = this.colorConvert();
if (Array.isArray(expression)) {
return `${expression[0]}${char}${expression[1]}`;
} else {
return expression + char;
}
}
return char;
}
private colorConvert() {
if (Array.isArray(this.colorExpression)) {
return this.colorExpression.map((exp) => exp.replace("{color}", this.colorValue as string));
} else {
return this.colorExpression.replace("{color}", this.colorValue as string);
}
}
}

4
src/types/base.d.ts vendored
View File

@ -1,6 +1,8 @@
declare module "@editorjs/underline";
declare module "@sotaproject/strikethrough";
type ContainsString<T extends string> = `${string}${T}${string}`;
/**
* Representation of color in Hex format.
*/
@ -35,7 +37,7 @@ interface GradientProcessAdapterOptions {
type Formats = "bold" | "italic" | "underlined" | "strikethrough";
type RichFormats = "reset" | "obfuscated";
type FormatPresets = Record<Formats | RichFormats, FormatExpression>;
type FormatExpression = string | [string, string];
type FormatExpression = ContainsString<"{color}"> | string | [string, string];
interface GradientPresetsRecord extends BaseGradientPresets {
createTime: Date | null;