Skip to content

Commit

Permalink
refactor: convert textkit package to TS (#3077)
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomura authored Feb 16, 2025
1 parent 8f9c2ad commit 442ce35
Show file tree
Hide file tree
Showing 269 changed files with 7,319 additions and 6,341 deletions.
7 changes: 7 additions & 0 deletions .changeset/stupid-dryers-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@react-pdf/fns": patch
"@react-pdf/layout": patch
"@react-pdf/textkit": patch
---

refactor: convert textkit package to TS
6 changes: 4 additions & 2 deletions packages/fns/src/last.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
* @param value - The input value
* @returns The last character of the string
*/
const last = (value: string | any[]): any => {
function last(value: string): string;
function last<T>(value: T[]): T;
function last(value: string | any[]): any {
return value === '' ? '' : value[value.length - 1];
};
}

export default last;
4 changes: 2 additions & 2 deletions packages/layout/tests/text/layoutText.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ describe('text layoutText', () => {
const node = createTextNode(text, {}, { hyphenationCallback });
const lines = layoutText(node, 50, 100, null);

expect(lines[0].string).toEqual('really');
expect(lines[1].string).toEqual('long');
expect(lines[0].string).toEqual('really-');
expect(lines[1].string).toEqual('long-');
expect(lines[2].string).toEqual('text');
expect(hyphenationCallback).toHaveBeenCalledWith('reallylongtext');
});
Expand Down
3 changes: 0 additions & 3 deletions packages/textkit/babel.config.js

This file was deleted.

2 changes: 1 addition & 1 deletion packages/textkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"description": "An advanced text layout framework",
"type": "module",
"main": "./lib/textkit.js",
"types": "./lib/index.d.ts",
"repository": {
"type": "git",
"url": "https://github.com/diegomura/react-pdf.git",
Expand All @@ -23,7 +24,6 @@
"lib"
],
"dependencies": {
"@babel/runtime": "^7.20.13",
"@react-pdf/fns": "3.1.0",
"bidi-js": "^1.0.2",
"hyphen": "^1.6.4",
Expand Down
28 changes: 13 additions & 15 deletions packages/textkit/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
import babel from '@rollup/plugin-babel';
import localResolve from 'rollup-plugin-local-resolve';
import typescript from '@rollup/plugin-typescript';
import { dts } from 'rollup-plugin-dts';
import del from 'rollup-plugin-delete';

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

const babelConfig = () => ({
babelrc: true,
exclude: 'node_modules/**',
babelHelpers: 'runtime',
});
const input = './src/index.ts';

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

const getExternal = () => [
...Object.keys(pkg.dependencies),
/@babel\/runtime/,
/hyphen/,
];

const getPlugins = () => [localResolve(), babel(babelConfig())];
const getPlugins = () => [typescript(), localResolve()];

const config = {
input,
Expand All @@ -26,4 +18,10 @@ const config = {
plugins: getPlugins(),
};

export default [config];
const dtsConfig = {
input: './lib/types/index.d.ts',
output: [{ file: 'lib/index.d.ts', format: 'es' }],
plugins: [dts(), del({ targets: 'lib/types', hook: 'buildEnd' })],
};

export default [config, dtsConfig];
18 changes: 0 additions & 18 deletions packages/textkit/src/attributedString/advanceWidth.js

This file was deleted.

15 changes: 15 additions & 0 deletions packages/textkit/src/attributedString/advanceWidth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import runAdvanceWidth from '../run/advanceWidth';
import { AttributedString, Run } from '../types';

/**
* Returns attributed string advancewidth
*
* @param attributedString - Attributed string
* @returns Advance width
*/
const advanceWidth = (attributedString: AttributedString) => {
const reducer = (acc: number, run: Run) => acc + runAdvanceWidth(run);
return attributedString.runs.reduce(reducer, 0);
};

export default advanceWidth;
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import filterRuns from '../run/filter';
import runAdvanceWidthBetween from '../run/advanceWidthBetween';

/**
* @typedef {import('../types.js').AttributedString} AttributedString
*/
import { AttributedString } from '../types';

/**
* Advance width between start and end
* Does not consider ligature splitting for the moment.
* Check performance impact on supporting this
*
* @param {number} start offset
* @param {number} end offset
* @param {AttributedString} attributedString
* @returns {number} advance width
* @param start - Start offset
* @param end - End offset
* @param attributedString
* @returns Advance width
*/
const advanceWidthBetween = (start, end, attributedString) => {
const advanceWidthBetween = (
start: number,
end: number,
attributedString: AttributedString,
) => {
const runs = filterRuns(start, end, attributedString.runs);
return runs.reduce(
(acc, run) => acc + runAdvanceWidthBetween(start, end, run),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@ import { last } from '@react-pdf/fns';
import emptyRun from '../run/empty';
import appendToRun from '../run/append';
import stringFromCodePoints from '../utils/stringFromCodePoints';

/**
* @typedef {import('../types.js').AttributedString} AttributedString
* @typedef {import('../types.js').Glyph} Glyph
*/
import { AttributedString, Glyph } from '../types';

/**
* Append glyph into last run of attributed string
*
* @param {Glyph} glyph glyph
* @param {AttributedString} attributedString attributed string
* @returns {AttributedString} attributed string with new glyph
* @param glyph - Glyph or code point
* @param attributedString - Attributed string
* @returns Attributed string with new glyph
*/
const append = (glyph, attributedString) => {
const codePoints = glyph?.codePoints || [];
const codePointsString = stringFromCodePoints(codePoints);
const append = (
glyph: Glyph | number | null,
attributedString: AttributedString,
): AttributedString => {
const codePoints = typeof glyph === 'number' ? [glyph] : glyph?.codePoints;
const codePointsString = stringFromCodePoints(codePoints || []);
const string = attributedString.string + codePointsString;

const firstRuns = attributedString.runs.slice(0, -1);
Expand Down
18 changes: 0 additions & 18 deletions packages/textkit/src/attributedString/ascent.js

This file was deleted.

15 changes: 15 additions & 0 deletions packages/textkit/src/attributedString/ascent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import runAscent from '../run/ascent';
import { AttributedString, Run } from '../types';

/**
* Returns attributed string ascent
*
* @param attributedString - Attributed string
* @returns Ascent
*/
const ascent = (attributedString: AttributedString) => {
const reducer = (acc, run: Run) => Math.max(acc, runAscent(run));
return attributedString.runs.reduce(reducer, 0);
};

export default ascent;
18 changes: 0 additions & 18 deletions packages/textkit/src/attributedString/descent.js

This file was deleted.

15 changes: 15 additions & 0 deletions packages/textkit/src/attributedString/descent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import runDescent from '../run/descent';
import { AttributedString, Run } from '../types';

/**
* Returns attributed string descent
*
* @param attributedString - Attributed string
* @returns Descent
*/
const descent = (attributedString: AttributedString) => {
const reducer = (acc: number, run: Run) => Math.min(acc, runDescent(run));
return attributedString.runs.reduce(reducer, 0);
};

export default descent;
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { adjust, dropLast as arrayDropLast } from '@react-pdf/fns';

import runDropLast from '../run/dropLast';

/**
* @typedef {import('../types.js').AttributedString} AttributedString
*/
import { AttributedString } from '../types';

/**
* Drop last glyph
*
* @param {AttributedString} attributedString attributed string
* @returns {AttributedString} attributed string with new glyph
* @param attributedString - Attributed string
* @returns Attributed string with new glyph
*/
const dropLast = (attributedString) => {
const dropLast = (attributedString: AttributedString) => {
const string = arrayDropLast(attributedString.string);
const runs = adjust(-1, runDropLast, attributedString.runs);

Expand Down
8 changes: 0 additions & 8 deletions packages/textkit/src/attributedString/empty.js

This file was deleted.

10 changes: 10 additions & 0 deletions packages/textkit/src/attributedString/empty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { AttributedString } from '../types';

/**
* Returns empty attributed string
*
* @returns Empty attributed string
*/
const empty = (): AttributedString => ({ string: '', runs: [] });

export default empty;
18 changes: 0 additions & 18 deletions packages/textkit/src/attributedString/end.js

This file was deleted.

15 changes: 15 additions & 0 deletions packages/textkit/src/attributedString/end.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { last } from '@react-pdf/fns';
import { AttributedString } from '../types';

/**
* Get attributed string end value
*
* @param attributedString - Attributed string
* @returns End
*/
const end = (attributedString: AttributedString) => {
const { runs } = attributedString;
return runs.length === 0 ? 0 : last(runs).end;
};

export default end;
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
/**
* @typedef {import('../types.js').AttributedString} AttributedString
* @typedef {import('../types.js').Fragment} Fragment
*/
import { AttributedString, Fragment } from '../types';

/**
* Create attributed string from text fragments
*
* @param {Fragment[]} fragments fragments
* @returns {AttributedString} attributed string
* @param fragments - Fragments
* @returns Attributed string
*/
const fromFragments = (fragments) => {
const fromFragments = (fragments: Fragment[]): AttributedString => {
let offset = 0;
let string = '';
const runs = [];
Expand Down
21 changes: 0 additions & 21 deletions packages/textkit/src/attributedString/glyphWidthAt.js

This file was deleted.

Loading

0 comments on commit 442ce35

Please sign in to comment.