Skip to content

Text-component Module

npm versionDownloadsInstall sizenpmBuild Status

Provide functions to parse Minecraft text component.

Usage

TextComponent

Create TextComponent from string OR Minecraft's formatted string, like '§cThis is red':

ts
import { TextComponent, fromFormattedString } from "@xmcl/text-component";
const formattedString: string;
const fromFormatted: TextComponent = fromFormattedString(formattedString);

Render the TextComponent to css:

ts
import { TextComponent, render, RenderNode } from "@xmcl/text-component";
const yourComponent: TextComponent;
const node: RenderNode = render(yourComponent);

node.text; // the text of the node
node.style; // style of the node
node.children; // children

// you can render in dom like this:

function renderToDom(node: RenderNode) {
    const span = document.createElement('span');
    span.style = node.style;
    span.textContent = node.text;
    for (const child of node.children) {
        span.appendChild(renderToDom(child));
    }
}

Iterate the TextComponent and its children:

ts
import { TextComponent, flat } from "@xmcl/text-component";
const yourComponent: TextComponent;
const selfAndAllChildren: Array<TextComponent> = flat(yourComponent);

🤝 Interfaces

🏭 Functions

flat

ts
flat(component: TextComponent): TextComponent[]

Flat all components (this component and its children) in this component by DFS into a list.

Parameters

  • component: TextComponent The root component

Return Type

  • TextComponent[]

Defined in: packages/text-component/index.ts:195

fromFormattedString

ts
fromFormattedString(formatted: string): TextComponent

Convert a formatted string to text component json

Parameters

  • formatted: string The formatted string

Return Type

  • TextComponent

Defined in: packages/text-component/index.ts:223

getStyleCode

ts
getStyleCode(style: TextComponent): string

Get Minecraft style code for the style

Parameters

  • style: TextComponent

Return Type

  • string

Defined in: packages/text-component/index.ts:138

getSuggestedStyle

ts
getSuggestedStyle(style: TextComponent | Style): object

Get suggest css style object for input style

Parameters

  • style: TextComponent | Style

Return Type

  • object

Defined in: packages/text-component/index.ts:169

render

ts
render(src: TextComponent): RenderNode

Render a text component into html style object

Parameters

  • src: TextComponent

Return Type

  • RenderNode

Defined in: packages/text-component/index.ts:183

toFormattedString

ts
toFormattedString(comp: TextComponent): string

Convert a text component to Minecraft specific formatted string like §1colored§r

Parameters

  • comp: TextComponent

Return Type

  • string

Defined in: packages/text-component/index.ts:208

⏩ Type Aliases

ClickEventAction

ts
ClickEventAction: "open_file" | "open_url" | "run_command" | "suggest_command"

Defined in: packages/text-component/index.ts:120

HoverEventAction

ts
HoverEventAction: "show_text" | "show_item" | "show_entity"

Defined in: packages/text-component/index.ts:121

RenderNode

ts
RenderNode: Object

The renderable node

Defined in: packages/text-component/index.ts:151