Skip to content

Commit

Permalink
TypeScript support (#378)
Browse files Browse the repository at this point in the history
* Add TS Support:
* Created declaration files
* Finished declarations for Config.js

* Completed the TS types for the DocumentView component

* Updates to the typings of DocumentView.js

* Use literals in config, add options file

* Add DocumentView methods

* Add DocumentView methods

* Updated typings for Config.js

* Add DocumentView methods

* Completed the typings for RNPdftron

* Refactor options

* Updated:
* Typings of the DocumentView props
* API docs of onDocumentLoaded and onDocumentError

* Use literals in method params

* PDFViewCtrl TS support

* Fixed typings for PDFViewCtrl

* Changes to index.d.ts and document_view.d.ts
* Added export for PDFViewCtrl in index.d.ts
* Fixed config.options.d.ts exports in index.d.ts
* Added exports for the remainig interfaces within document_view.d.ts
* Fixed error in document_view.d.ts

* Refactor exports and fix a type

* specify object interface for onBehaviorActivated

* Fixed onDocumentLoaded and onDocumentError typing

* move types to own folder

* Edit filepaths

* Edit mode prop types

* Edit onToolChanged types

* Updated package.json files

* Added missing import in AnnotOptions.d.ts

* edit reflowOrientation type

* edit defaultEraserType type

* Use promises for method return types

* make initialLoad optional

* Updated interfaces
* Rect to include width and height optional properties
* Annotation to include pageRect and screenRect optional properties

* make CropBox from Rect

* Updated definition of CropBox

* Add types from latest master updates

* Use Record utility

* Remove redundant null

* Add save copy option buttons

* Redefined CustomToolbarKey in ConfigOptions.d.ts

* Added pageRect and screenRect to stickyNoteData

* fix typo

* Use parameter destructuring

* migrate js files to ts, tsx

* move d.ts files

move d.ts files

* Revert "move d.ts files"

This reverts commit 1390bb3.

* generate new d.ts, .js files

* add tsconfig to get correct output structure

* Added a build script and updated the exports

* Fixed build script

* Restructed repo

* fix typo

* Updated PropTypes in DocumentView.tsx

* Rename ConfigOptions to Config

* Created custom PropTypes

* Refactor event function params

* Script for Build Process

* Updating Build Process Script

* document funcProp

* Created functions to make propTypes easier to read

* funcProp to func

* arrayOfProp to arrayOf

* oneOfProp to oneOf

* use Validator<T>, add JSDocs for functions

* Edit comments, rename param val to obj

* edit comments

* Refactor and add JSDoc

* Add JSDoc for propTypes for dev use
* Use arrayOf for custom annotationToolbars items
* Use template literal for validator Error string

* make oneOf, arrayOf accept multiple objects

* make oneOf, arrayOf accept multiple values

* simplify oneOf, arrayOf

* refactor, edit comments

* Simplify arrayOf

* Add pull_request to build event triggers

* Revert "Add pull_request to build event triggers"

This reverts commit 210bc33.

* Add JSDoc examples

* Edit JSDocs

* Return null at end of validator function

* also specify onChange event type

* remove onChange from propTypes & clean comments

* Refactor methods to use one Promise

* enable allowSyntheticDefaultImports

* minor edit to ts-ignore description

* use strict mode

* Make type-related dependencies any-ver devDependencies

* Updated Field interfaces due to changes to master

* Replaced an object type with its primitive version

* Create npmignore file excl. lib folder

* Add extra ts-ignore to onChange

* Updated README and API docs in prep for TS release

* Edit TypeScript docs

* update Installation and Usage with TypeScript

* move TS section of readme

* Update TS support version in README, edit CONTRIBUTING

* Remove extraneous args from PDFViewCtrl requireNativeComponent

* Remove unused object

* Updated PDFViewCtrl to match DocumentView

* Made document a required prop in PDFViewCtrl

* (test) Add a prestart script (#372)

* Add prestart script to generate JS files

* Change branch where we want builds to be

* Added/edited minor details/wording

* Add preandroid and preios scripts to generate lib

* Removed rect from the Annotation interface

* Removed PointWithPage interface

* Edit build process comments

* add periods to end of comments
* clarify example branches
* remove extra rect from StickyNoteData interface

* avoid emitting JS files if there is type error

* Added some comments to Config and PDFViewCtrl

* Rename Pdftron interface to RNPdftron

* Show 2 variants for constructor in Usage example

* Explicitly mention PDFTron React Native dependency in README

* Avoid "our", "we"

* replace "Migrating to TypeScript" with small sentence in "All Users"

* Updated API section on TypeScript

* Add return type info for getSavedSignatureJpgFolder

* (iOS) Remove old podspec nightly mentions (#379)

* Remove podspec nightly mentions, no longer necessary

* bump ver

* iOS enable user bookmarks list editing by default (#380)

* Set userBookmarksListEditingEnabled to true by default

* bump version

* change wording

Co-authored-by: JamesTron <[email protected]>

* change wording

Co-authored-by: JamesTron <[email protected]>

* change wording of TS version recommendation

Co-authored-by: JamesTron <[email protected]>

* change wording of PR step

Co-authored-by: JamesTron <[email protected]>

* link to wiki

* Updated version to 2.0.3-beta.184 (#381)

Updated Android version to snapshot 9.1.1-beta02
Updated version to 2.0.3-beta.184

* (iOS) defaultEraserType (#382)

* (iOS) defaultEraserType prop

* Remove duplicate docs and constants, update original docs

* bump ver

* Update to Android 9.1.1beta3 (#383)

* (Support) (iOS) Quick Navigation Button (#384)

* (iOS) showQuickNavigationButton

* Updated documentation

* Fixed mistake in documentation

* Bump version number

* Updated default value of prop to true

* Updated Android version to 9.1.1-beta05

* Remove package-lock.json and edit build process to install deps first

* Edit build process to apply changes from dev to master

- Also update to major version

Co-authored-by: Dominic Cupidon <[email protected]>
Co-authored-by: Jamie Dassoulas <[email protected]>
Co-authored-by: JamesTron <[email protected]>
Co-authored-by: eamon-mallon <[email protected]>
Co-authored-by: sgong-pdftron <[email protected]>
Co-authored-by: Dominic Cupidon <[email protected]>
Co-authored-by: brandenfung2 <[email protected]>
  • Loading branch information
8 people authored Sep 23, 2021
1 parent 28715fb commit a06bfeb
Show file tree
Hide file tree
Showing 23 changed files with 748 additions and 338 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/jsBuilder.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Transpile TS

on:
push:
branches: [dev] # <-- Change this to the primary branch (for example, "dev" if you want all changes to `dev` to be watched).

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
with:
persist-credentials: false # otherwise, the token used is the GITHUB_TOKEN, instead of your personal token.
fetch-depth: 0 # otherwise, you will failed to push refs to dest repo.

- name: Install dependencies
run: |
npm i
- name: Node setup
uses: actions/setup-node@v2
with:
node-version: '16'
cache: 'npm'

- name: Build JavaScript files
run: | # Change last line to your build script command #.
npm run start
- name: Force add JS files to override .gitignore
run: git add --force ./lib # <-- Change this to your build path.

- name: Commit files
run: | # Change last line to your preferred commit message (I like `chore: build js files`).
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git commit -m "Updating JS files" -a
- name: Push changes
uses: ad-m/[email protected]
with:
force: true
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: master # <-- Change this to the branch where you want builds to be (for example, "master" if you want changes to the primary branch to be applied to `master`).
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,6 @@ buck-out/

example/android/app/src/main/assets/index.android.bundle
example/android/app/src/main/assets/index.android.bundle.meta

# Build
/lib
49 changes: 49 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

# OSX
#
.DS_Store

# node.js
#
node_modules/
npm-debug.log
yarn-error.log


# Xcode
#
build/
Pods/
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata
*.xccheckout
*.moved-aside
DerivedData
*.hmap
*.ipa
*.xcuserstate
project.xcworkspace


# Android/IntelliJ
#
build/
.idea
.gradle
local.properties
*.iml

# BUCK
buck-out/
\.buckd/
*.keystore

example/android/app/src/main/assets/index.android.bundle
example/android/app/src/main/assets/index.android.bundle.meta
48 changes: 26 additions & 22 deletions API.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# PDFTron React Native API

## TypeScript

PDFTron React Native supports TypeScript. Since not all customers use the language, the typings used in this document will be described using normal JavaScript types. For TypeScript users, type information is automatically provided while coding, and exact type aliases and constants used in our custom typings can be found in [AnnotOptions](src/AnnotOptions) and [Config](src/Config) source folders.

## RNPdftron

RNPdftron contains static methods for global library initialization, configuration, and utility methods.
Expand Down Expand Up @@ -225,10 +229,15 @@ Defines whether the viewer is read-only. If true, the UI will not allow the user
/>
```
#### defaultEraserType
one of [`Config.EraserType`](./src/Config/Config.js) constants, optional
one of the [`Config.EraserType`](./src/Config/Config.js) constants, optional

Sets the default eraser tool type. Value only applied after a clean install. Android only.
Example:
Sets the default eraser tool type. Value only applied after a clean install.

Eraser Type | Description
--- | ---
`annotationEraser` | Erases everything as an object; if you touch ink, the entire object is erased.
`hybrideEraser` | Erases ink by pixel, but erases other annotation types as objects.
`inkEraser` | Erases ink by pixel only. Android only.

```js
<DocumentView
Expand Down Expand Up @@ -289,6 +298,12 @@ function, optional

This function is called when the document finishes loading.

Parameters:

Name | Type | Description
--- | --- | ---
path | string | File path that the document has been saved to

```js
<DocumentView
onDocumentLoaded = {(path) => {
Expand All @@ -302,6 +317,12 @@ function, optional

This function is called when document opening encounters an error.

Parameters:

Name | Type | Description
--- | --- | ---
error | string | Error message produced

```js
<DocumentView
onDocumentError = {(error) => {
Expand Down Expand Up @@ -1302,22 +1323,7 @@ Defines annotation types that cannot be edited after creation.
#### excludedAnnotationListTypes
array of [`Config.Tools`](./src/Config/Config.js) constants, optional, defaults to none

Defines types to be excluded from the annotation list. This feature will be soon be added to the official iOS release; to access it in the meantime, you can use the following podspec in the Podfile:
```
pod 'PDFNet', podspec: 'https://nightly-pdftron.s3-us-west-2.amazonaws.com/stable/2021-08-04/9.0/cocoapods/xcframeworks/pdfnet/2021-08-04_stable_rev77892.podspec'
```

and uncomment the following line in `ios/RNTPTDocumentView.m`:
```objc
- (void)excludeAnnotationListTypes:(NSArray<NSString*> *)excludedAnnotationListTypes documentViewController:(PTDocumentBaseViewController *)documentViewController
{
...
if (annotTypes.count > 0) {
//documentViewController.navigationListsViewController.annotationViewController.excludedAnnotationTypes = annotTypes;
}
}
```

Defines types to be excluded from the annotation list.
Example use:

```js
Expand Down Expand Up @@ -1536,7 +1542,7 @@ Defines whether the page stack navigation buttons will appear in the viewer.
```

#### showQuickNavigationButton
bool, optional, defaults to true, Android only
bool, optional, defaults to true

Defines whether the quick navigation buttons will appear in the viewer.

Expand Down Expand Up @@ -3211,8 +3217,6 @@ this._viewer.openOutlineList();
#### openLayersList
On Android it displays the layers dialog while on iOS it displays the layers tab of the existing list container. If this tab has been disabled or there are no layers in the document, the method does nothing.

**Note** For proper functionality the PDFNet podspec with: https://nightly-pdftron.s3-us-west-2.amazonaws.com/stable/2021-07-16/9.0/cocoapods/pdfnet/2021-07-16_stable_rev77863.podspec

Returns a Promise.

```js
Expand Down
9 changes: 5 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

## Pull requests
1. Fork the repository.
2. Create a branch from `master`.
3. Update the source code.
2. Create a branch from `dev`.
3. Update the source code (in `*.ts` files).
4. Commit and push the changes with descriptive messages.
5. Create a pull request to `master`.
5. Create a pull request to `dev`.

\* Please note that all pull requests should be tied to an issue, and all but the most trivial pull requests should be discussed beforehand.
\* Please note that all pull requests should be tied to an issue, and all but the most trivial pull requests should be discussed beforehand.
\*\* Changes made to `dev` will automatically be applied to `master`. For more detailed information on developing post-TypeScript migration, see the [wiki](https://github.com/PDFTron/pdftron-react-native/wiki).
39 changes: 34 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
- [Preview](#preview)
- [Installation](#installation)
- [Usage](#usage)
- [Components](#components)
- [TypeScript](#typescript)
- [Contributing](#contributing)
- [License](#license)

## API
Expand All @@ -16,6 +17,7 @@ APIs are available on the [API page](API.md).
- npm or yarn
- PDFTron SDK >= 9.0.0
- react-native >= 0.60.0
- TypeScript >= 3.4.1 (optional; see [TypeScript](#typescript))

## Preview

Expand All @@ -33,7 +35,7 @@ The release can be found here: https://github.com/PDFTron/pdftron-react-native/r

0. If using yarn, do: `yarn global add react-native-cli`

1. First, follow the official getting started guide on [setting up the React Native environment](https://reactnative.dev/docs/environment-setup), [setting up the iOS and Android environment](https://reactnative.dev/docs/environment-setup), and [creating a React Native project](https://reactnative.dev/docs/environment-setup), the following steps will assume your app is created through `react-native init MyApp`.
1. First, follow the official getting started guide on [setting up the React Native environment](https://reactnative.dev/docs/environment-setup), [setting up the iOS and Android environment](https://reactnative.dev/docs/environment-setup), and [creating a React Native project](https://reactnative.dev/docs/environment-setup). The following steps will assume your app is created through `react-native init MyApp`. This guide also applies if you are using the [TypeScript template](https://reactnative.dev/docs/environment-setup#optional-using-a-specific-version-or-template).

2. In `MyApp` folder, install `react-native-pdftron` by calling:
```shell
Expand Down Expand Up @@ -150,7 +152,7 @@ The release can be found here: https://github.com/PDFTron/pdftron-react-native/r
+ public class MainApplication extends MultiDexApplication implements ReactApplication {
```
6. Replace `App.js` with what is shown [here](#usage)
6. Replace `App.js` (or `App.tsx`) with what is shown [here](#usage).
7. Finally in the root project directory, run `react-native run-android`.
### iOS
Expand All @@ -166,13 +168,13 @@ The release can be found here: https://github.com/PDFTron/pdftron-react-native/r
```
2. In the `ios` folder, run `pod install`.
3. Replace `App.js` with what is shown [here](#usage).
3. Replace `App.js` (or `App.tsx`) with what is shown [here](#usage).
4. Finally in the root project directory, run `react-native run-ios`.
5. (Optional) If you need a close button icon, you will need to add the PNG resources to `MyApp` as well, i.e. `ic_close_black_24px`.
## Usage
Replace `App.js` with the code below.
Replace `App.js` (or `App.tsx` if you are [using TypeScript](#typescript)) with the code below.
If you set your path variable to point to a local storage file,
then the `PermissionsAndroid` component is required to ensure that storage permission is properly granted.
Expand Down Expand Up @@ -202,6 +204,8 @@ import { DocumentView, RNPdftron } from 'react-native-pdftron';
type Props = {};
export default class App extends Component<Props> {
// If you are using TypeScript, use `constructor(props: Props) {`
// Otherwise, use:
constructor(props) {
super(props);
Expand Down Expand Up @@ -321,6 +325,31 @@ const styles = StyleSheet.create({
document="content://..."
```
## TypeScript
PDFTron React Native introduced support for TypeScript in version 3.0.0. This update mainly benefits those who already use TypeScript in their applications. It also provides certain benefits to all customers, including those who use JavaScript without TypeScript.
To get access to TypeScript support, simply update your PDFTron React Native dependency to version 3.0.0 or higher.
### All Users
For non-TypeScript users, updating your PDFTron React Native dependency to version 3.0.0 or higher will not automatically install TypeScript itself, and you can continue to use the library as before (without TypeScript support). If you currently do not use TypeScript itself in your project and would like to, see [Adding TypeScript to an Existing Project](https://reactnative.dev/docs/typescript#adding-typescript-to-an-existing-project).
Regardless of whether you use TypeScript, the following benefits are available:
- Proper API typings which can be used in tools such as [IntelliSense](https://code.visualstudio.com/docs/editor/intellisense). This offers insight into the data being passed without referring to the [API documentation](API.md).
- Greater type safety and reliability due to the migration of source files from JavaScript to TypeScript.
- Updated Add an API guides for [Android](https://www.pdftron.com/documentation/android/get-started/react-native/add-an-api/) and [iOS](https://www.pdftron.com/documentation/ios/get-started/react-native/add-an-api) to offer step-by-step, TypeScript-supported examples on accessing properties, methods, and events. Contributors should be aware that PRs must now be made to the `dev` branch (see [Contributing](./CONTRIBUTING.md)).
If you have questions, head to the FAQ's Integration section for [Android](https://www.pdftron.com/documentation/android/faq) and [iOS](https://www.pdftron.com/documentation/ios/faq/).
### TypeScript Users
If you are an existing TypeScript user, then the custom typings will be available to you simply by updating your PDFTron React Native dependency to version 3.0.0 or higher.
Note:
- TypeScript version 3.4.1+ is recommended. Although compilation still works with lower versions, typings may degrade to `any`.
- Due to the introduction of proper typings to PDFTron's APIs, your compiler may now give warnings or errors about your usage of these APIs. In most cases these will not prevent your app from running but it is advised that you address them to take full advantage of TypeScript. It is particularly important to address them if you use TypeScript to emit files and have enabled `noEmitOnError` in your `tsconfig.json`.
## Contributing
See [Contributing](./CONTRIBUTING.md)
Expand Down
8 changes: 4 additions & 4 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ rootProject.allprojects {
google()
jcenter()
maven {
url "https://pdftron-maven.s3.amazonaws.com/release"
url "https://pdftron-maven.s3.amazonaws.com/snapshot"
}
}
}
Expand Down Expand Up @@ -55,7 +55,7 @@ repositories {
dependencies {
implementation "com.facebook.react:react-native:+"

implementation "com.pdftron:pdftron:9.1.0"
implementation "com.pdftron:tools:9.1.0"
implementation "com.pdftron:collab:9.1.0"
implementation "com.pdftron:pdftron:9.1.1-beta05"
implementation "com.pdftron:tools:9.1.1-beta05"
implementation "com.pdftron:collab:9.1.1-beta05"
}
3 changes: 3 additions & 0 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
"version": "0.0.1",
"private": true,
"scripts": {
"preandroid": "cd node_modules/react-native-pdftron && npm start && cd ../../",
"android": "react-native run-android",
"preios": "cd node_modules/react-native-pdftron && npm start && cd ../../",
"ios": "react-native run-ios",
"prestart": "cd node_modules/react-native-pdftron && npm start && cd ../../",
"start": "react-native start",
"test": "jest",
"lint": "eslint ."
Expand Down
16 changes: 0 additions & 16 deletions index.js

This file was deleted.

26 changes: 26 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { NativeModules } from 'react-native';
import { PDFViewCtrl } from './src/PDFViewCtrl/PDFViewCtrl';
import { DocumentView } from './src/DocumentView/DocumentView';
import { Config } from './src/Config/Config';
import * as AnnotOptions from './src/AnnotOptions/AnnotOptions';

interface RNPdftron {
initialize(licenseKey: string) : void;
enableJavaScript(enabled: boolean) : void;
getVersion() : Promise<string>;
getPlatformVersion() : Promise<string>;
getSystemFontList() : Promise<string>;
clearRubberStampCache() : Promise<void>;
encryptDocument(filePath: string, password: string, currentPassword: string) : Promise<void>;
pdfFromOfficeTemplate(docxPath: string, json: object) : Promise<string>;
}

const RNPdftron : RNPdftron = NativeModules.RNPdftron;

export {
RNPdftron,
PDFViewCtrl,
DocumentView,
Config,
AnnotOptions,
};
Loading

0 comments on commit a06bfeb

Please sign in to comment.