-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: build textkit with rollup & define public api (#1906)
- Loading branch information
Showing
15 changed files
with
193 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'@react-pdf/textkit': major | ||
'@react-pdf/layout': patch | ||
'@react-pdf/render': patch | ||
--- | ||
|
||
feat: build textkit with rollup & define public api |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* Create attributed string from text fragments | ||
* | ||
* @param {Array} fragments | ||
* @return {Object} attributed string | ||
*/ | ||
const fromFragments = fragments => { | ||
let offset = 0; | ||
let string = ''; | ||
const runs = []; | ||
|
||
fragments.forEach(fragment => { | ||
string += fragment.string; | ||
|
||
runs.push({ | ||
start: offset, | ||
end: offset + fragment.string.length, | ||
attributes: fragment.attributes || {}, | ||
}); | ||
|
||
offset += fragment.string.length; | ||
}); | ||
|
||
return { string, runs }; | ||
}; | ||
|
||
export default fromFragments; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import fromFragments from '../../src/text/fromFragments'; | ||
|
||
describe('attributeString fromFragments operator', () => { | ||
test('should return empty attributed string for no fragments', () => { | ||
const attributedString = fromFragments([]); | ||
|
||
expect(attributedString.string).toBe(''); | ||
expect(attributedString.runs).toHaveLength(0); | ||
}); | ||
|
||
test('should be constructed by one fragment', () => { | ||
const attributedString = fromFragments([{ string: 'Hey' }]); | ||
|
||
expect(attributedString.string).toBe('Hey'); | ||
expect(attributedString.runs[0]).toHaveProperty('start', 0); | ||
expect(attributedString.runs[0]).toHaveProperty('end', 3); | ||
}); | ||
|
||
test('should be constructed by fragments', () => { | ||
const attributedString = fromFragments([ | ||
{ string: 'Hey' }, | ||
{ string: ' ho' }, | ||
]); | ||
|
||
expect(attributedString.string).toBe('Hey ho'); | ||
expect(attributedString.runs[0]).toHaveProperty('start', 0); | ||
expect(attributedString.runs[0]).toHaveProperty('end', 3); | ||
expect(attributedString.runs[1]).toHaveProperty('start', 3); | ||
expect(attributedString.runs[1]).toHaveProperty('end', 6); | ||
}); | ||
|
||
test('should preserve fragment attributes', () => { | ||
const attributedString = fromFragments([ | ||
{ string: 'Hey', attributes: { attr: 1 } }, | ||
{ string: ' ho', attributes: { attr: 2 } }, | ||
]); | ||
|
||
expect(attributedString.runs[0]).toHaveProperty('attributes', { attr: 1 }); | ||
expect(attributedString.runs[1]).toHaveProperty('attributes', { attr: 2 }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
module.exports = { extends: '../../babel.config.js' }; | ||
module.exports = { | ||
extends: '../../babel.config.js', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import babel from '@rollup/plugin-babel'; | ||
import localResolve from 'rollup-plugin-local-resolve'; | ||
import pkg from './package.json'; | ||
|
||
const cjs = { | ||
exports: 'named', | ||
format: 'cjs', | ||
}; | ||
|
||
const esm = { | ||
format: 'es', | ||
}; | ||
|
||
const getCJS = override => Object.assign({}, cjs, override); | ||
const getESM = override => Object.assign({}, esm, override); | ||
|
||
const configBase = { | ||
input: './src/index.js', | ||
external: Object.keys(pkg.dependencies), | ||
plugins: [ | ||
localResolve(), | ||
babel({ | ||
babelrc: true, | ||
babelHelpers: 'runtime', | ||
exclude: 'node_modules/**', | ||
}), | ||
], | ||
}; | ||
|
||
const config = Object.assign({}, configBase, { | ||
output: [ | ||
getESM({ file: 'lib/textkit.es.js' }), | ||
getCJS({ file: 'lib/textkit.cjs.js' }), | ||
], | ||
}); | ||
|
||
export default config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.