Skip to content

Commit 2328b3e

Browse files
committed
Merge branch 'master' of github.com:RobinNagpal/react-image-annotation-ts
2 parents e2043a6 + aa02e2f commit 2328b3e

File tree

3 files changed

+177
-106
lines changed

3 files changed

+177
-106
lines changed

README.md

+172-105
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,182 @@
1-
# TSDX React w/ Storybook User Guide
1+
React Image Annotation(TypeScript)
2+
=========================
23

3-
Congrats! You just saved yourself hours of work by bootstrapping this project with TSDX. Let’s get you oriented with what’s here and how to use it.
4+
This is a fork of https://github.com/Secretmapper/react-image-annotation
45

5-
> This TSDX setup is meant for developing React component libraries (not apps!) that can be published to NPM. If you’re looking to build a React-based app, you should use `create-react-app`, `razzle`, `nextjs`, `gatsby`, or `react-static`.
6+
Since the original project was inactive, only option was to fork and update the code.
67

7-
> If you’re new to TypeScript and React, checkout [this handy cheatsheet](https://github.com/sw-yx/react-typescript-cheatsheet/)
8+
![Annotation demo](https://raw.githubusercontent.com/RobinNagpal/react-image-annotation-ts/HEAD/demo.gif)
89

9-
## Commands
10+
## Installation
11+
12+
```
13+
npm install --save react-image-annotation-ts
14+
# or
15+
yarn add react-image-annotation-ts
16+
```
17+
18+
## Usage
19+
20+
```js
21+
export default class Simple extends Component {
22+
state = {
23+
annotations: [],
24+
annotation: {}
25+
}
26+
27+
onChange = (annotation) => {
28+
this.setState({ annotation })
29+
}
30+
31+
onSubmit = (annotation) => {
32+
const { geometry, data } = annotation
33+
34+
this.setState({
35+
annotation: {},
36+
annotations: this.state.annotations.concat({
37+
geometry,
38+
data: {
39+
...data,
40+
id: Math.random()
41+
}
42+
})
43+
})
44+
}
45+
46+
render () {
47+
return (
48+
<Root>
49+
<Annotation
50+
src={img}
51+
alt='Two pebbles anthropomorphized holding hands'
52+
53+
annotations={this.state.annotations}
54+
55+
type={this.state.type}
56+
value={this.state.annotation}
57+
onChange={this.onChange}
58+
onSubmit={this.onSubmit}
59+
/>
60+
</Root>
61+
)
62+
}
63+
}
64+
```
65+
66+
67+
### Props
68+
69+
Prop | Description | Default
70+
---- | ----------- | -------
71+
`src` | Image src attribute |
72+
`alt` | Image alt attribute |
73+
`annotations` | Array of annotations |
74+
`value` | Annotation object currently being created. See [annotation object](#annotation-object) |
75+
`onChange` | `onChange` handler for annotation object |
76+
`onSubmit` | `onSubmit` handler for annotation object |
77+
`type` | Selector type. See [custom shapes](#using-custom-shapes) | `RECTANGLE`
78+
`allowTouch` | Set to `true` to allow the target to handle touch events. This disables one-finger scrolling | `false`
79+
`selectors` | An array of selectors. See [adding custom selector logic](#adding-custom-selector-logic) | `[RectangleSelector, PointSelector, OvalSelector]`
80+
`activeAnnotations` | Array of annotations that will be passed as 'active' (active highlight and shows content) |
81+
`activeAnnotationComparator` | Method to compare annotation and `activeAnnotation` item (from `props.activeAnnotations`). Return `true` if it's the annotations are equal | `(a, b) => a === b`
82+
`disableAnnotation` | Set to `true` to disable creating of annotations (note that no callback methods will be called if this is `true`) | `false`
83+
`disableSelector` | Set to `true` to not render `Selector` | `false`
84+
`disableEditor` | Set to `true` to not render `Editor` | `false`
85+
`disableOverlay` | Set to `true` to not render `Overlay` | `false`
86+
`renderSelector` | Function that renders `Selector` Component | See [custom components](#using-custom-components)
87+
`renderEditor` | Function that renders `Editor` Component | See [custom components](#using-custom-components)
88+
`renderHighlight` | Function that renders `Highlight` Component | See [custom components](#using-custom-components)
89+
`renderContent` | Function that renders `Content` | See [custom components](#using-custom-components)
90+
`renderOverlay` | Function that renders `Overlay` | See [custom components](#using-custom-components)
91+
`onMouseUp` | `onMouseUp` handler on annotation target |
92+
`onMouseDown` | `onMouseDown` handler on annotation target |
93+
`onMouseMove` | `onMouseMove` handler on annotation target |
94+
`onClick` | `onClick` handler on annotation target |
95+
96+
#### Annotation object
97+
98+
An Annotation object is an object that conforms to the object shape
99+
100+
```js
101+
({
102+
selection: T.object, // temporary object for selector logic
103+
geometry: T.shape({ // geometry data for annotation
104+
type: T.string.isRequired // type is used to resolve Highlighter/Selector renderer
105+
}),
106+
// auxiliary data object for application.
107+
// Content data can be stored here (text, image, primary key, etc.)
108+
data: T.object
109+
})
110+
```
111+
112+
## Using custom components
113+
114+
`Annotation` supports `renderProp`s for almost every internal component.
115+
116+
This allows you to customize everything about the the look of the annotation interface, and you can even use canvas elements for performance or more complex interaction models.
117+
118+
- `renderSelector` - used for selecting annotation area (during annotation creation)
119+
- `renderEditor` - appears after annotation area has been selected (during annotation creation)
120+
- `renderHighlight` - used to render current annotations in the annotation interface. It is passed an object that contains the property `active`, which is true if the mouse is hovering over the higlight
121+
- `renderComponent` - auxiliary component that appears when mouse is hovering over the highlight. It is passed an object that contains the annotation being hovered over. `{ annotation }`
122+
- `renderOverlay` - Component overlay for Annotation (i.e. 'Click and Drag to Annotate')
123+
124+
You can view the default renderProps [here](src/components/defaultProps.js)
125+
126+
**Note**: You cannot use `:hover` selectors in css for components returned by `renderSelector` and `renderHighlight`. This is due to the fact that `Annotation` places DOM layers on top of these components, preventing triggering of `:hover`
127+
128+
## Using custom shapes
129+
130+
`Annotation` supports three shapes by default, `RECTANGLE`, `POINT` and `OVAL`.
131+
132+
You can switch the shape selector by passing the appropriate `type` as a property. Default shape `TYPE`s are accessible on their appropriate selectors:
133+
134+
```js
135+
import {
136+
PointSelector,
137+
RectangleSelector,
138+
OvalSelector
139+
} from 'react-image-annotation/lib/selectors'
140+
141+
<Annotation
142+
type={PointSelector.TYPE}
143+
/>
144+
```
145+
146+
### Adding custom selector logic
147+
148+
#### This is an Advanced Topic
149+
150+
The Annotation API allows support for custom shapes that use custom logic such as polygon or freehand selection. This is done by defining your own selection logic and passing it as a selector in the `selectors` property.
151+
152+
Selectors are objects that must have the following properties:
153+
154+
- `TYPE` - string that uniquely identifies this selector (i.e. `RECTANGLE`)
155+
- `intersects` - method that returns true if the mouse point intersects with the annotation geometry
156+
- `area` - method that calculates and returns the area of the annotation geometry
157+
- `methods` - object that can contain various listener handlers (`onMouseUp`, `onMouseDown`, `onMouseMove`, `onClick`). These listener handlers are called when triggered in the annotation area. These handlers must be reducer-like methods - returning a new annotation object depending on the change of the method
158+
159+
You can view a defined `RectangleSelector` [here](src/hocs/RectangleSelector.js)
160+
161+
### Connecting selector logic to Redux/MobX
162+
163+
First see [Selectors](#adding-custom-selector-logic)
164+
165+
You can use `Selector` methods to connect these method logic to your stores. This is due to the fact that selector methods function as reducers, returning new state depending on the event.
166+
167+
***Note that it is not necessary to connect the selector logic with redux/mobx. Connecting the annotation and annotations state is more than enough for most use cases.***
168+
169+
## License
170+
171+
MIT
10172

11-
TSDX scaffolds your new library inside `/src`, and also sets up a [Parcel-based](https://parceljs.org) playground for it inside `/example`.
12173

13-
The recommended workflow is to run TSDX in one terminal:
174+
# Contributing
175+
We use TSDX to build this project.
14176

177+
178+
## Commands
179+
Build the project
15180
```bash
16181
npm start # or yarn start
17182
```
@@ -42,8 +207,6 @@ npm i # or yarn to install dependencies
42207
npm start # or yarn start
43208
```
44209

45-
The default example imports and live reloads whatever is in `/dist`, so if you are seeing an out of date component, make sure TSDX is running in watch mode like we recommend above. **No symlinking required**, we use [Parcel's aliasing](https://parceljs.org/module_resolution.html#aliases).
46-
47210
To do a one-off build, use `npm run build` or `yarn build`.
48211

49212
To run tests, use `npm test` or `yarn test`.
@@ -60,73 +223,6 @@ Jest tests are set up to run with `npm test` or `yarn test`.
60223

61224
Calculates the real cost of your library using [size-limit](https://github.com/ai/size-limit) with `npm run size` and visulize it with `npm run analyze`.
62225

63-
#### Setup Files
64-
65-
This is the folder structure we set up for you:
66-
67-
```txt
68-
/example
69-
index.html
70-
index.tsx # test your component here in a demo app
71-
package.json
72-
tsconfig.json
73-
/src
74-
index.tsx # EDIT THIS
75-
/test
76-
blah.test.tsx # EDIT THIS
77-
/stories
78-
Thing.stories.tsx # EDIT THIS
79-
/.storybook
80-
main.js
81-
preview.js
82-
.gitignore
83-
package.json
84-
README.md # EDIT THIS
85-
tsconfig.json
86-
```
87-
88-
#### React Testing Library
89-
90-
We do not set up `react-testing-library` for you yet, we welcome contributions and documentation on this.
91-
92-
### Rollup
93-
94-
TSDX uses [Rollup](https://rollupjs.org) as a bundler and generates multiple rollup configs for various module formats and build settings. See [Optimizations](#optimizations) for details.
95-
96-
### TypeScript
97-
98-
`tsconfig.json` is set up to interpret `dom` and `esnext` types, as well as `react` for `jsx`. Adjust according to your needs.
99-
100-
## Continuous Integration
101-
102-
### GitHub Actions
103-
104-
Two actions are added by default:
105-
106-
- `main` which installs deps w/ cache, lints, tests, and builds on all pushes against a Node and OS matrix
107-
- `size` which comments cost comparison of your library on every pull request using [size-limit](https://github.com/ai/size-limit)
108-
109-
## Optimizations
110-
111-
Please see the main `tsdx` [optimizations docs](https://github.com/palmerhq/tsdx#optimizations). In particular, know that you can take advantage of development-only optimizations:
112-
113-
```js
114-
// ./types/index.d.ts
115-
declare var __DEV__: boolean;
116-
117-
// inside your code...
118-
if (__DEV__) {
119-
console.log('foo');
120-
}
121-
```
122-
123-
You can also choose to install and use [invariant](https://github.com/palmerhq/tsdx#invariant) and [warning](https://github.com/palmerhq/tsdx#warning) functions.
124-
125-
## Module Formats
126-
127-
CJS, ESModules, and UMD module formats are supported.
128-
129-
The appropriate paths are configured in `package.json` and `dist/index.js` accordingly. Please report if any issues are found.
130226

131227
## Deploying the Example Playground
132228

@@ -145,37 +241,8 @@ netlify init
145241
# build command: yarn build && cd example && yarn && yarn build
146242
# directory to deploy: example/dist
147243
# pick yes for netlify.toml
148-
```
149-
150-
## Named Exports
151-
152-
Per Palmer Group guidelines, [always use named exports.](https://github.com/palmerhq/typescript#exports) Code split inside your React app instead of your React library.
153-
154-
## Including Styles
155-
156-
There are many ways to ship styles, including with CSS-in-JS. TSDX has no opinion on this, configure how you like.
157-
158-
For vanilla CSS, you can include it at the root directory and add it to the `files` section in your `package.json`, so that it can be imported separately by your users and run through their bundler's loader.
159244

160245
## Publishing to NPM
161246

162247
We recommend using [np](https://github.com/sindresorhus/np).
163248

164-
## Usage with Lerna
165-
166-
When creating a new package with TSDX within a project set up with Lerna, you might encounter a `Cannot resolve dependency` error when trying to run the `example` project. To fix that you will need to make changes to the `package.json` file _inside the `example` directory_.
167-
168-
The problem is that due to the nature of how dependencies are installed in Lerna projects, the aliases in the example project's `package.json` might not point to the right place, as those dependencies might have been installed in the root of your Lerna project.
169-
170-
Change the `alias` to point to where those packages are actually installed. This depends on the directory structure of your Lerna project, so the actual path might be different from the diff below.
171-
172-
```diff
173-
"alias": {
174-
- "react": "../node_modules/react",
175-
- "react-dom": "../node_modules/react-dom"
176-
+ "react": "../../../node_modules/react",
177-
+ "react-dom": "../../../node_modules/react-dom"
178-
},
179-
```
180-
181-
An alternative to fixing this problem would be to remove aliases altogether and define the dependencies referenced as aliases as dev dependencies instead. [However, that might cause other problems.](https://github.com/palmerhq/tsdx/issues/64)

demo.gif

5.28 MB
Loading

package.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "0.1.0",
2+
"version": "0.1.3",
33
"license": "MIT",
44
"main": "dist/index.js",
55
"typings": "dist/index.d.ts",
@@ -72,5 +72,9 @@
7272
"dependencies": {
7373
"styled-components": "^5.3.3",
7474
"types": "^0.1.1"
75+
},
76+
"repository": {
77+
"type": "git",
78+
"url": "https://github.com/RobinNagpal/react-image-annotation-ts"
7579
}
7680
}

0 commit comments

Comments
 (0)