Skip to content

Commit fe7bc45

Browse files
committed
refactor: convert textkit package to TS
1 parent 8f9c2ad commit fe7bc45

File tree

268 files changed

+7312
-6341
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

268 files changed

+7312
-6341
lines changed

packages/fns/src/last.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
* @param value - The input value
55
* @returns The last character of the string
66
*/
7-
const last = (value: string | any[]): any => {
7+
function last(value: string): string;
8+
function last<T>(value: T[]): T;
9+
function last(value: string | any[]): any {
810
return value === '' ? '' : value[value.length - 1];
9-
};
11+
}
1012

1113
export default last;

packages/layout/tests/text/layoutText.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ describe('text layoutText', () => {
8787
const node = createTextNode(text, {}, { hyphenationCallback });
8888
const lines = layoutText(node, 50, 100, null);
8989

90-
expect(lines[0].string).toEqual('really');
91-
expect(lines[1].string).toEqual('long');
90+
expect(lines[0].string).toEqual('really-');
91+
expect(lines[1].string).toEqual('long-');
9292
expect(lines[2].string).toEqual('text');
9393
expect(hyphenationCallback).toHaveBeenCalledWith('reallylongtext');
9494
});

packages/textkit/babel.config.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

packages/textkit/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"description": "An advanced text layout framework",
66
"type": "module",
77
"main": "./lib/textkit.js",
8+
"types": "./lib/index.d.ts",
89
"repository": {
910
"type": "git",
1011
"url": "https://github.com/diegomura/react-pdf.git",
@@ -23,7 +24,6 @@
2324
"lib"
2425
],
2526
"dependencies": {
26-
"@babel/runtime": "^7.20.13",
2727
"@react-pdf/fns": "3.1.0",
2828
"bidi-js": "^1.0.2",
2929
"hyphen": "^1.6.4",

packages/textkit/rollup.config.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
1-
import babel from '@rollup/plugin-babel';
21
import localResolve from 'rollup-plugin-local-resolve';
2+
import typescript from '@rollup/plugin-typescript';
3+
import { dts } from 'rollup-plugin-dts';
4+
import del from 'rollup-plugin-delete';
35

46
import pkg from './package.json' with { type: 'json' };
57

6-
const babelConfig = () => ({
7-
babelrc: true,
8-
exclude: 'node_modules/**',
9-
babelHelpers: 'runtime',
10-
});
8+
const input = './src/index.ts';
119

12-
const input = './src/index.js';
10+
const getExternal = () => [...Object.keys(pkg.dependencies), /hyphen/];
1311

14-
const getExternal = () => [
15-
...Object.keys(pkg.dependencies),
16-
/@babel\/runtime/,
17-
/hyphen/,
18-
];
19-
20-
const getPlugins = () => [localResolve(), babel(babelConfig())];
12+
const getPlugins = () => [typescript(), localResolve()];
2113

2214
const config = {
2315
input,
@@ -26,4 +18,10 @@ const config = {
2618
plugins: getPlugins(),
2719
};
2820

29-
export default [config];
21+
const dtsConfig = {
22+
input: './lib/types/index.d.ts',
23+
output: [{ file: 'lib/index.d.ts', format: 'es' }],
24+
plugins: [dts(), del({ targets: 'lib/types', hook: 'buildEnd' })],
25+
};
26+
27+
export default [config, dtsConfig];

packages/textkit/src/attributedString/advanceWidth.js

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import runAdvanceWidth from '../run/advanceWidth';
2+
import { AttributedString, Run } from '../types';
3+
4+
/**
5+
* Returns attributed string advancewidth
6+
*
7+
* @param attributedString - Attributed string
8+
* @returns Advance width
9+
*/
10+
const advanceWidth = (attributedString: AttributedString) => {
11+
const reducer = (acc: number, run: Run) => acc + runAdvanceWidth(run);
12+
return attributedString.runs.reduce(reducer, 0);
13+
};
14+
15+
export default advanceWidth;

packages/textkit/src/attributedString/advanceWidthBetween.js renamed to packages/textkit/src/attributedString/advanceWidthBetween.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
import filterRuns from '../run/filter';
22
import runAdvanceWidthBetween from '../run/advanceWidthBetween';
3-
4-
/**
5-
* @typedef {import('../types.js').AttributedString} AttributedString
6-
*/
3+
import { AttributedString } from '../types';
74

85
/**
96
* Advance width between start and end
107
* Does not consider ligature splitting for the moment.
118
* Check performance impact on supporting this
129
*
13-
* @param {number} start offset
14-
* @param {number} end offset
15-
* @param {AttributedString} attributedString
16-
* @returns {number} advance width
10+
* @param start - Start offset
11+
* @param end - End offset
12+
* @param attributedString
13+
* @returns Advance width
1714
*/
18-
const advanceWidthBetween = (start, end, attributedString) => {
15+
const advanceWidthBetween = (
16+
start: number,
17+
end: number,
18+
attributedString: AttributedString,
19+
) => {
1920
const runs = filterRuns(start, end, attributedString.runs);
2021
return runs.reduce(
2122
(acc, run) => acc + runAdvanceWidthBetween(start, end, run),

packages/textkit/src/attributedString/append.js renamed to packages/textkit/src/attributedString/append.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,21 @@ import { last } from '@react-pdf/fns';
33
import emptyRun from '../run/empty';
44
import appendToRun from '../run/append';
55
import stringFromCodePoints from '../utils/stringFromCodePoints';
6-
7-
/**
8-
* @typedef {import('../types.js').AttributedString} AttributedString
9-
* @typedef {import('../types.js').Glyph} Glyph
10-
*/
6+
import { AttributedString, Glyph } from '../types';
117

128
/**
139
* Append glyph into last run of attributed string
1410
*
15-
* @param {Glyph} glyph glyph
16-
* @param {AttributedString} attributedString attributed string
17-
* @returns {AttributedString} attributed string with new glyph
11+
* @param glyph - Glyph or code point
12+
* @param attributedString - Attributed string
13+
* @returns Attributed string with new glyph
1814
*/
19-
const append = (glyph, attributedString) => {
20-
const codePoints = glyph?.codePoints || [];
21-
const codePointsString = stringFromCodePoints(codePoints);
15+
const append = (
16+
glyph: Glyph | number | null,
17+
attributedString: AttributedString,
18+
): AttributedString => {
19+
const codePoints = typeof glyph === 'number' ? [glyph] : glyph?.codePoints;
20+
const codePointsString = stringFromCodePoints(codePoints || []);
2221
const string = attributedString.string + codePointsString;
2322

2423
const firstRuns = attributedString.runs.slice(0, -1);

packages/textkit/src/attributedString/ascent.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)