@xmcl/text-component
Version:
Parse Minecraft text component and render it to css.
224 lines (222 loc) • 8.28 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var __publicField = (obj, key, value) => {
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
return value;
};
// index.ts
var text_component_exports = {};
__export(text_component_exports, {
flat: () => flat,
fromFormattedString: () => fromFormattedString,
getStyleCode: () => getStyleCode,
getSuggestedStyle: () => getSuggestedStyle,
render: () => render,
toFormattedString: () => toFormattedString
});
module.exports = __toCommonJS(text_component_exports);
function getStyleCode(style) {
let code = "";
for (const l of TextFormat.list) {
if (l.matchStyle(style)) {
code += l;
}
}
return code;
}
function getSuggestedStyle(style) {
const styledObject = {};
for (const l of TextFormat.list) {
if (l.matchStyle(style)) {
Object.assign(styledObject, l.cssForeground);
}
}
return styledObject;
}
function render(src) {
const children = [];
for (const component of src.extra || []) {
children.push(render(component));
}
return { children, component: src, style: getSuggestedStyle(src) };
}
function flat(component) {
const arr = [component];
if (component.extra && component.extra.length !== 0) {
for (const s of component.extra) {
arr.push(...flat(s));
}
}
return arr;
}
function toFormattedString(comp) {
let v = "";
for (const component of flat(comp)) {
const text = component.text;
if (text.length !== 0) {
v += `${getStyleCode(component)}${text}${TextFormat.RESET}`;
}
}
return v;
}
function fromFormattedString(formatted) {
const firstCode = formatted.indexOf("\xA7");
if (firstCode === -1) {
return { text: formatted };
}
const textComponent = {
text: formatted.substring(0, firstCode)
};
let builder = "";
const style = {
bold: false,
obfuscated: false,
strikethrough: false,
underlined: false,
italic: false,
color: void 0
};
for (let i = firstCode; i < formatted.length; i++) {
const word = formatted.charCodeAt(i);
if (word === 167 && i + 1 < formatted.length) {
if (builder.length !== 0) {
if (!textComponent.extra) {
textComponent.extra = [];
}
textComponent.extra.push({ text: builder, ...style });
builder = "";
}
const format = TextFormat.fromCode(formatted.charAt(i + 1).toLowerCase());
if (format) {
format.applyToStyle(style);
}
++i;
} else {
builder += formatted[i];
}
}
if (builder.length !== 0) {
if (!textComponent.extra) {
textComponent.extra = [];
}
textComponent.extra.push({ text: builder, ...style });
}
return textComponent;
}
var _TextFormat = class {
constructor(key, value, name, code, cssForeground, cssBackground) {
this.key = key;
this.value = value;
this.name = name;
this.code = code;
this.cssForeground = cssForeground;
this.cssBackground = cssBackground;
}
applyToStyle(style) {
style[this.key] = this.value;
}
matchStyle(style) {
return style[this.key] === this.value;
}
/**
* Get the text format from text code
* @param code The text code character like 0, 1, 2
* @returns The TextFormat instance
*/
static fromCode(code) {
const seq = "0123456789abcdefklmnor";
const index = seq.indexOf(code);
if (!index) {
return void 0;
}
return this.list[index];
}
toString() {
return `${_TextFormat.CONTROL_STRING}${this.code}`;
}
};
var TextFormat = _TextFormat;
__publicField(TextFormat, "CONTROL_STRING", "\xA7");
__publicField(TextFormat, "BLACK", new _TextFormat("color", "black", "BLACK", "0", { color: "#000000" }, { color: "#000000" }));
__publicField(TextFormat, "DARK_BLUE", new _TextFormat("color", "dark_blue", "DARK_BLUE", "1", { color: "#0000AA" }, { color: "#00002A" }));
__publicField(TextFormat, "DARK_GREEN", new _TextFormat("color", "dark_green", "DARK_GREEN", "2", { color: "#00AA00" }, { color: "#002A00" }));
__publicField(TextFormat, "DARK_AQUA", new _TextFormat("color", "dark_aqua", "DARK_AQUA", "3", { color: "#00AAAA" }, { color: "#002A2A" }));
__publicField(TextFormat, "DARK_RED", new _TextFormat("color", "dark_red", "DARK_RED", "4", { color: "#AA0000" }, { color: "#2A0000" }));
__publicField(TextFormat, "DARK_PURPLE", new _TextFormat("color", "dark_purple", "DARK_PURPLE", "5", { color: "#AA00AA" }, { color: "#2A002A" }));
__publicField(TextFormat, "GOLD", new _TextFormat("color", "gold", "GOLD", "6", { color: "#FFAA00" }, { color: "#2A2A00" }));
__publicField(TextFormat, "GRAY", new _TextFormat("color", "gray", "GRAY", "7", { color: "#AAAAAA" }, { color: "#2A2A2A" }));
__publicField(TextFormat, "DARK_GRAY", new _TextFormat("color", "dark_gray", "DARK_GRAY", "8", { color: "#555555" }, { color: "#151515" }));
__publicField(TextFormat, "BLUE", new _TextFormat("color", "blue", "BLUE", "9", { color: "#5555FF" }, { color: "#15153F" }));
__publicField(TextFormat, "GREEN", new _TextFormat("color", "green", "GREEN", "a", { color: "#55FF55" }, { color: "#153F15" }));
__publicField(TextFormat, "AQUA", new _TextFormat("color", "aqua", "AQUA", "b", { color: "#55FFFF" }, { color: "#153F3F" }));
__publicField(TextFormat, "RED", new _TextFormat("color", "red", "RED", "c", { color: "#FF5555" }, { color: "#3F1515" }));
__publicField(TextFormat, "LIGHT_PURPLE", new _TextFormat("color", "light_purple", "LIGHT_PURPLE", "d", { color: "#FF55FF" }, { color: "#3F153F" }));
__publicField(TextFormat, "YELLOW", new _TextFormat("color", "yellow", "YELLOW", "e", { color: "#FFFF55" }, { color: "#3F3F15" }));
__publicField(TextFormat, "WHITE", new _TextFormat("color", "white", "WHITE", "f", { color: "#FFFFFF" }, { color: "#3F3F3F" }));
__publicField(TextFormat, "OBFUSCATED", new _TextFormat("obfuscated", true, "OBFUSCATED", "k", {}));
__publicField(TextFormat, "BOLD", new _TextFormat("bold", true, "BOLD", "l", { "font-weight": "bold" }));
__publicField(TextFormat, "STRIKETHROUGH", new _TextFormat("strikethrough", true, "STRIKETHROUGH", "m", { "text-decoration": "line-through" }));
__publicField(TextFormat, "UNDERLINE", new _TextFormat("underlined", true, "UNDERLINE", "n", { "text-decoration": "underline" }));
__publicField(TextFormat, "ITALIC", new _TextFormat("italic", true, "ITALIC", "o", { "font-style": "italic" }));
__publicField(TextFormat, "RESET", {
name: "RESET",
code: "r",
applyToStyle(style) {
style.bold = false;
style.strikethrough = false;
style.underlined = false;
style.italic = false;
style.obfuscated = false;
style.color = void 0;
},
matchStyle(style) {
return false;
},
cssBackground: {},
cssForeground: {},
toString() {
return `${_TextFormat.CONTROL_STRING}r`;
}
});
__publicField(TextFormat, "list", [
_TextFormat.BLACK,
_TextFormat.DARK_BLUE,
_TextFormat.DARK_GREEN,
_TextFormat.DARK_AQUA,
_TextFormat.DARK_RED,
_TextFormat.DARK_PURPLE,
_TextFormat.GOLD,
_TextFormat.GRAY,
_TextFormat.DARK_GRAY,
_TextFormat.BLUE,
_TextFormat.GREEN,
_TextFormat.AQUA,
_TextFormat.RED,
_TextFormat.LIGHT_PURPLE,
_TextFormat.YELLOW,
_TextFormat.WHITE,
_TextFormat.OBFUSCATED,
_TextFormat.BOLD,
_TextFormat.STRIKETHROUGH,
_TextFormat.UNDERLINE,
_TextFormat.ITALIC,
_TextFormat.RESET
]);
//# sourceMappingURL=index.js.map