Skip to content

Commit

Permalink
fix(api): use more precise Flow types for public APIs. Fix #30
Browse files Browse the repository at this point in the history
- Replaced Object type annotations with the required shape (not exact, can have extra attributes for convenience). This might raise type issues, but only if there is indeed a problem in the library usage. See [Flow’s `unclear-type`](https://flow.org/en/docs/linting/rule-reference/#toc-unclear-type) rule for further reference.
- Use [$ReadOnlyArray<T>](https://flow.org/en/docs/types/arrays/#toc-readonlyarray) in place of Array<T>. This is a supertype of Array (so no change required in implementation), which guarantees draftjs-filters does not mutate arrays.
  • Loading branch information
thibaudcolas committed Jan 25, 2019
1 parent 507e279 commit f274aaa
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/lib/filters/atomic.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export const resetAtomicBlocks = (content: ContentState) => {
* Removes atomic blocks for which the entity isn't whitelisted.
*/
export const removeInvalidAtomicBlocks = (
whitelist: Array<Object>,
whitelist: $ReadOnlyArray<{ type: string }>,
content: ContentState,
) => {
const blockMap = content.getBlockMap()
Expand Down
4 changes: 2 additions & 2 deletions src/lib/filters/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const removeInvalidDepthBlocks = (content: ContentState) => {
* ends up in the text. Other use cases may not be well covered.
*/
export const preserveBlockByText = (
rules: Array<{
rules: $ReadOnlyArray<{
test: string,
type: string,
depth: number,
Expand Down Expand Up @@ -125,7 +125,7 @@ export const limitBlockDepth = (max: number, content: ContentState) => {
* Also sets depth to 0 (for potentially nested list items).
*/
export const filterBlockTypes = (
whitelist: Array<string>,
whitelist: $ReadOnlyArray<string>,
content: ContentState,
) => {
const blockMap = content.getBlockMap()
Expand Down
13 changes: 7 additions & 6 deletions src/lib/filters/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,23 @@ import {
import { replaceTextBySpaces } from "./text"
import { applyContentWithSelection } from "./selection"

import { ContentState } from "draft-js"
import type { EditorState as EditorStateType } from "draft-js"

type FilterOptions = {
// Whitelist of allowed block types. unstyled and atomic are always included.
blocks: Array<string>,
blocks: $ReadOnlyArray<string>,
// Whitelist of allowed inline styles.
styles: Array<string>,
styles: $ReadOnlyArray<string>,
// Whitelist of allowed entities.
entities: Array<{
entities: $ReadOnlyArray<{
// Entity type, eg. "LINK"
type: string,
// Allowed attributes. Other attributes will be removed.
attributes: Array<string>,
attributes: $ReadOnlyArray<string>,
// Refine which entities are kept by whitelisting acceptable values with regular expression patterns.
whitelist: {
[attribute: string]: string,
[attribute: string]: string | boolean,
},
}>,
// Maximum amount of depth for lists (0 = no nesting).
Expand Down Expand Up @@ -137,7 +138,7 @@ export const filterEditorState = (

const content = editorState.getCurrentContent()
const nextContent = filters.reduce(
(c, filter: Function) => filter(c),
(c, filter: (ContentState) => ContentState) => filter(c),
content,
)

Expand Down
16 changes: 12 additions & 4 deletions src/lib/filters/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export const filterEntityRanges = (
* Keeps all entity types (images, links, documents, embeds) that are enabled.
*/
export const shouldKeepEntityType = (
whitelist: Array<Object>,
whitelist: $ReadOnlyArray<{ type: string }>,
type: string,
) => {
return whitelist.some((e) => e.type === type)
Expand All @@ -143,9 +143,14 @@ export const shouldRemoveImageEntity = (
* Filters entities based on the data they contain.
*/
export const shouldKeepEntityByAttribute = (
entityTypes: Array<Object>,
entityTypes: $ReadOnlyArray<{
type: string,
whitelist: {
[attribute: string]: string | boolean,
},
}>,
entityType: string,
data: Object,
data: {},
) => {
const config = entityTypes.find((t) => t.type === entityType)
// If no whitelist is defined, the filter keeps the entity.
Expand All @@ -172,7 +177,10 @@ export const shouldKeepEntityByAttribute = (
* of unneeded attributes (width, height, etc).
*/
export const filterEntityData = (
entityTypes: Array<Object>,
entityTypes: $ReadOnlyArray<{
type: string,
attributes: $ReadOnlyArray<string>,
}>,
content: ContentState,
) => {
let newContent = content
Expand Down
2 changes: 1 addition & 1 deletion src/lib/filters/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ContentState, CharacterMetadata } from "draft-js"
* Removes all styles not present in the whitelist.
*/
export const filterInlineStyles = (
whitelist: Array<string>,
whitelist: $ReadOnlyArray<string>,
content: ContentState,
) => {
const blockMap = content.getBlockMap()
Expand Down
2 changes: 1 addition & 1 deletion src/lib/filters/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ContentState } from "draft-js"
* Replaces the given characters by their equivalent length of spaces, in all blocks.
*/
export const replaceTextBySpaces = (
characters: Array<string>,
characters: $ReadOnlyArray<string>,
content: ContentState,
) => {
const blockMap = content.getBlockMap()
Expand Down

0 comments on commit f274aaa

Please sign in to comment.