diff --git a/types/draft-convert/draft-convert-tests.tsx b/types/draft-convert/draft-convert-tests.tsx new file mode 100644 index 0000000000..42e1c0883f --- /dev/null +++ b/types/draft-convert/draft-convert-tests.tsx @@ -0,0 +1,11 @@ +import * as draftConvert from "draft-convert" + +const html = '
this is a test
'; +const configFromHtml = {}; +const configToHtml = {}; + +// $ExpectType ContentState +const contentState = draftConvert.convertFromHTML(configFromHtml)(html); + +// $ExpectType string +draftConvert.convertToHTML(configToHtml)(contentState); diff --git a/types/draft-convert/index.d.ts b/types/draft-convert/index.d.ts new file mode 100644 index 0000000000..e27c8d84a8 --- /dev/null +++ b/types/draft-convert/index.d.ts @@ -0,0 +1,72 @@ +// Type definitions for draft-convert v2.1.5 +// Project: https://github.com/HubSpot/draft-convert +// Definitions by: Agustin Valeriani +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.8 + +// Based on: https://github.com/HubSpot/draft-convert/issues/107#issuecomment-488581709 by +declare module 'draft-convert' { + import { + ContentBlock, + ContentState, + DraftBlockType, + DraftInlineStyleType, + Entity, + RawDraftEntity + } from 'draft-js'; + import { ReactNode } from 'react'; + + interface IConvertToHTMLConfig { + // Inline styles: + styleToHTML?: (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; +} diff --git a/types/draft-convert/tsconfig.json b/types/draft-convert/tsconfig.json new file mode 100644 index 0000000000..099eabf8e7 --- /dev/null +++ b/types/draft-convert/tsconfig.json @@ -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" + ] +} diff --git a/types/draft-convert/tslint.json b/types/draft-convert/tslint.json new file mode 100644 index 0000000000..89955128d5 --- /dev/null +++ b/types/draft-convert/tslint.json @@ -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 + } +}