Add draft-convert type definitions. (#36109)

* -updated ts config.

* -Added draft-convert typings with test.

* -Better typing for entity found and applied.
This commit is contained in:
Agu Valeriani
2019-06-11 17:27:01 -07:00
committed by Ron Buckton
parent 4d7cd124b6
commit 6b4ddd0d5d
4 changed files with 189 additions and 0 deletions
@@ -0,0 +1,11 @@
import * as draftConvert from "draft-convert"
const html = '<div>this is a test</div>';
const configFromHtml = {};
const configToHtml = {};
// $ExpectType ContentState
const contentState = draftConvert.convertFromHTML(configFromHtml)(html);
// $ExpectType string
draftConvert.convertToHTML(configToHtml)(contentState);
+72
View File
@@ -0,0 +1,72 @@
// Type definitions for draft-convert v2.1.5
// Project: https://github.com/HubSpot/draft-convert
// Definitions by: Agustin Valeriani <https://github.com/avaleriani/>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.8
// Based on: https://github.com/HubSpot/draft-convert/issues/107#issuecomment-488581709 by <https://github.com/sbusch>
declare module 'draft-convert' {
import {
ContentBlock,
ContentState,
DraftBlockType,
DraftInlineStyleType,
Entity,
RawDraftEntity
} from 'draft-js';
import { ReactNode } from 'react';
interface IConvertToHTMLConfig {
// Inline styles:
styleToHTML?: <T extends DraftInlineStyleType>(style: T) => Tag | void;
// Block styles:
blockToHTML?: (block: ContentBlock) => Tag;
// Entity styling:
entityToHTML?: (entity: RawDraftEntity, originalText: string) => Tag;
}
type EntityKey = string;
interface IConvertFromHTMLConfig {
// Inline styles:
htmlToStyle?: (nodeName: string, node: HTMLElement) => DraftInlineStyleType;
// Block styles:
htmlToBlock?: (
nodeName: string,
node: HTMLElement
) => DraftBlockType | { type: DraftBlockType; data: object } | false;
// Html entities
htmlToEntity?: (
nodeName: string,
node: HTMLElement,
createEntity: (type: string, mutability: string, data: object) => EntityKey,
getEntity: (key: EntityKey) => Entity,
mergeEntityData: (key: string, data: object) => void,
replaceEntityData: (key: string, data: object) => void
) => void;
// Text entities
textToEntity?: (
text: string,
createEntity: (type: string, mutability: string, data: object) => EntityKey,
getEntity: (key: EntityKey) => Entity,
mergeEntityData: (key: string, data: object) => void,
replaceEntityData: (key: string, data: object) => void
) => Array<{ entity: EntityKey; offset: number; length: number; result?: string }>;
}
type ContentStateConverter = (contentState: ContentState) => string;
type htmlConverter = (html: string) => ContentState;
type Tag =
| ReactNode
| { start: string; end: string; empty?: string }
| { element: ReactNode; empty?: ReactNode };
export function convertToHTML(config: IConvertToHTMLConfig): ContentStateConverter;
export function convertFromHTML(config: IConvertFromHTMLConfig): htmlConverter;
}
+25
View File
@@ -0,0 +1,25 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6",
"dom"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"jsx": "react",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"draft-convert-tests.tsx"
]
}
+81
View File
@@ -0,0 +1,81 @@
{
"extends": "dtslint/dt.json",
"rules": {
"adjacent-overload-signatures": false,
"array-type": false,
"arrow-return-shorthand": false,
"ban-types": false,
"callable-types": false,
"comment-format": false,
"dt-header": false,
"npm-naming": false,
"eofline": false,
"export-just-namespace": false,
"import-spacing": false,
"interface-name": false,
"interface-over-type-literal": false,
"jsdoc-format": false,
"max-line-length": false,
"member-access": false,
"new-parens": false,
"no-any-union": false,
"no-boolean-literal-compare": false,
"no-conditional-assignment": false,
"no-consecutive-blank-lines": false,
"no-construct": false,
"no-declare-current-package": false,
"no-duplicate-imports": false,
"no-duplicate-variable": false,
"no-empty-interface": false,
"no-for-in-array": false,
"no-inferrable-types": false,
"no-internal-module": false,
"no-irregular-whitespace": false,
"no-mergeable-namespace": false,
"no-misused-new": false,
"no-namespace": false,
"no-object-literal-type-assertion": false,
"no-padding": false,
"no-redundant-jsdoc": false,
"no-redundant-jsdoc-2": false,
"no-redundant-undefined": false,
"no-reference-import": false,
"no-relative-import-in-test": false,
"no-self-import": false,
"no-single-declare-module": false,
"no-string-throw": false,
"no-unnecessary-callback-wrapper": false,
"no-unnecessary-class": false,
"no-unnecessary-generics": false,
"no-unnecessary-qualifier": false,
"no-unnecessary-type-assertion": false,
"no-useless-files": false,
"no-var-keyword": false,
"no-var-requires": false,
"no-void-expression": false,
"no-trailing-whitespace": false,
"object-literal-key-quotes": false,
"object-literal-shorthand": false,
"one-line": false,
"one-variable-per-declaration": false,
"only-arrow-functions": false,
"prefer-conditional-expression": false,
"prefer-const": false,
"prefer-declare-function": false,
"prefer-for-of": false,
"prefer-method-signature": false,
"prefer-template": false,
"radix": false,
"semicolon": false,
"space-before-function-paren": false,
"space-within-parens": false,
"strict-export-declare-modifiers": false,
"trim-file": false,
"triple-equals": false,
"typedef-whitespace": false,
"unified-signatures": false,
"use-default-type-parameter": false,
"void-return": false,
"whitespace": false
}
}