what-text 0.12.3 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +131 -0
- package/package.json +5 -2
- package/src/text/TextCanvas.js +1 -1
- package/src/text-engine.js +8 -4
package/index.d.ts
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
// what-text — optional text engine for What Framework. TypeScript definitions.
|
|
2
|
+
//
|
|
3
|
+
// Declared by hand, like every other .d.ts here, and held to the runtime by
|
|
4
|
+
// `npm run hygiene:types`: every name below must be exported by src/index.js,
|
|
5
|
+
// and every name src/index.js exports must appear below.
|
|
6
|
+
|
|
7
|
+
// --- Configuration ---
|
|
8
|
+
|
|
9
|
+
export interface TextConfig {
|
|
10
|
+
/**
|
|
11
|
+
* Register the measurement hook with what-core's renderer. Off by default:
|
|
12
|
+
* measuring every inserted text node costs layout reads, so it is opt-in.
|
|
13
|
+
*/
|
|
14
|
+
measure: boolean;
|
|
15
|
+
/** Maximum number of prepared-text entries held in the measure cache. */
|
|
16
|
+
cacheSize: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Merge `overrides` into the active config and register or unregister the
|
|
21
|
+
* what-core text-insertion hook to match `measure`. Unknown keys are ignored
|
|
22
|
+
* rather than stored, so a typo cannot silently become config.
|
|
23
|
+
*/
|
|
24
|
+
export function configureText(overrides: Partial<TextConfig>): void;
|
|
25
|
+
|
|
26
|
+
/** A copy of the active config. Mutating the result does not change it. */
|
|
27
|
+
export function getTextConfig(): TextConfig;
|
|
28
|
+
|
|
29
|
+
// --- Pretext ---
|
|
30
|
+
//
|
|
31
|
+
// @chenglou/pretext is an OPTIONAL peer dependency, so these describe the
|
|
32
|
+
// surface what-text actually calls rather than re-exporting pretext's own
|
|
33
|
+
// types. Importing them here would make the optional peer mandatory for
|
|
34
|
+
// TypeScript users, which is the opposite of optional.
|
|
35
|
+
|
|
36
|
+
/** Opaque prepared-text handle produced by `prepareWithSegments`. */
|
|
37
|
+
export type PreparedText = unknown;
|
|
38
|
+
|
|
39
|
+
export interface TextLine {
|
|
40
|
+
text: string;
|
|
41
|
+
/** Present when pretext segments the line; used to place the baseline. */
|
|
42
|
+
start?: { segmentIndex: number };
|
|
43
|
+
[key: string]: unknown;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface TextLayout {
|
|
47
|
+
lines: TextLine[];
|
|
48
|
+
lineCount: number;
|
|
49
|
+
height: number;
|
|
50
|
+
[key: string]: unknown;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface PretextModule {
|
|
54
|
+
prepareWithSegments(text: string, font: string): PreparedText;
|
|
55
|
+
layoutWithLines(prepared: PreparedText, containerWidth: number, lineHeight: number): TextLayout;
|
|
56
|
+
[key: string]: unknown;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Load @chenglou/pretext once and cache it. Rejects with an install hint when
|
|
61
|
+
* the optional peer is absent; a failed load is not cached, so a later call
|
|
62
|
+
* after installing it succeeds.
|
|
63
|
+
*/
|
|
64
|
+
export function ensurePretext(): Promise<PretextModule>;
|
|
65
|
+
|
|
66
|
+
// --- Measurement ---
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Lay `text` out at `font` inside `containerWidth`. Waits for document fonts
|
|
70
|
+
* to settle first, because measuring against a fallback face and then against
|
|
71
|
+
* the real one produces two different answers for the same input.
|
|
72
|
+
*/
|
|
73
|
+
export function measureText(
|
|
74
|
+
text: string,
|
|
75
|
+
font: string,
|
|
76
|
+
containerWidth: number,
|
|
77
|
+
lineHeight: number,
|
|
78
|
+
): Promise<TextLayout>;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Drop every cached measurement. Called automatically when the document
|
|
82
|
+
* finishes loading fonts, since every prior measurement used a fallback face.
|
|
83
|
+
*/
|
|
84
|
+
export function clearMeasureCache(): void;
|
|
85
|
+
|
|
86
|
+
// --- Components ---
|
|
87
|
+
//
|
|
88
|
+
// These build and return DOM nodes directly rather than returning a What
|
|
89
|
+
// element, so they are usable both as JSX components and as plain calls.
|
|
90
|
+
|
|
91
|
+
/** A value, or a getter that What re-reads when its signals change. */
|
|
92
|
+
export type Reactive<T> = T | (() => T);
|
|
93
|
+
|
|
94
|
+
export interface TextFlowProps {
|
|
95
|
+
/** CSS column count. Defaults to 1. */
|
|
96
|
+
columns?: number;
|
|
97
|
+
/** Element the text should flow around. */
|
|
98
|
+
around?: Element | null;
|
|
99
|
+
/** CSS column-gap. Defaults to '1rem'. */
|
|
100
|
+
gap?: string;
|
|
101
|
+
children?: Reactive<unknown>;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Multi-column flowing text. Returns the container `<div>`. */
|
|
105
|
+
export function TextFlow(props: TextFlowProps): HTMLDivElement;
|
|
106
|
+
|
|
107
|
+
export interface TextCanvasProps {
|
|
108
|
+
/** Canvas width in px. Defaults to 300. */
|
|
109
|
+
width?: number;
|
|
110
|
+
/** Canvas height in px. Defaults to 150. */
|
|
111
|
+
height?: number;
|
|
112
|
+
/** CSS font shorthand. Defaults to '16px sans-serif'. */
|
|
113
|
+
font?: string;
|
|
114
|
+
children?: Reactive<unknown>;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Text rendered to a `<canvas>` via pretext layout. Returns the canvas. */
|
|
118
|
+
export function TextCanvas(props: TextCanvasProps): HTMLCanvasElement;
|
|
119
|
+
|
|
120
|
+
export interface TextSVGProps {
|
|
121
|
+
/** SVG width in px. Defaults to 300. */
|
|
122
|
+
width?: number;
|
|
123
|
+
/** SVG height in px. Defaults to 150. */
|
|
124
|
+
height?: number;
|
|
125
|
+
/** CSS font shorthand. Defaults to '16px sans-serif'. */
|
|
126
|
+
font?: string;
|
|
127
|
+
children?: Reactive<unknown>;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** Text rendered as `<text>`/`<tspan>` elements. Returns the `<svg>`. */
|
|
131
|
+
export function TextSVG(props: TextSVGProps): SVGSVGElement;
|
package/package.json
CHANGED
|
@@ -1,18 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "what-text",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"description": "Optional text engine for What Framework, powered by @chenglou/pretext",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.js",
|
|
7
|
+
"types": "index.d.ts",
|
|
7
8
|
"module": "src/index.js",
|
|
8
9
|
"exports": {
|
|
9
10
|
".": {
|
|
11
|
+
"types": "./index.d.ts",
|
|
10
12
|
"import": "./src/index.js",
|
|
11
13
|
"default": "./src/index.js"
|
|
12
14
|
}
|
|
13
15
|
},
|
|
14
16
|
"files": [
|
|
15
|
-
"src"
|
|
17
|
+
"src",
|
|
18
|
+
"index.d.ts"
|
|
16
19
|
],
|
|
17
20
|
"sideEffects": false,
|
|
18
21
|
"peerDependencies": {
|
package/src/text/TextCanvas.js
CHANGED
package/src/text-engine.js
CHANGED
|
@@ -43,10 +43,14 @@ export async function ensurePretext() {
|
|
|
43
43
|
return mod;
|
|
44
44
|
}).catch((err) => {
|
|
45
45
|
pretextLoadPromise = null;
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
46
|
+
// ERROR_CODES.PRETEXT_NOT_INSTALLED
|
|
47
|
+
throw Object.assign(
|
|
48
|
+
new Error(
|
|
49
|
+
`[what-text] Failed to load @chenglou/pretext. ` +
|
|
50
|
+
`Install it with: npm install @chenglou/pretext\n` +
|
|
51
|
+
`Original error: ${err.message}`
|
|
52
|
+
),
|
|
53
|
+
{ code: 'ERR_PRETEXT_NOT_INSTALLED' },
|
|
50
54
|
);
|
|
51
55
|
});
|
|
52
56
|
return pretextLoadPromise;
|