draft-convert icon indicating copy to clipboard operation
draft-convert copied to clipboard

Typescript typings

Open anamanaguchi opened this issue 7 years ago • 7 comments

Hello, do you have any typescript support for this plugin?

PS. this plugin saved my life! Thanks! ;)

anamanaguchi avatar Nov 16 '17 14:11 anamanaguchi

i don't currently - i've played around with adding flow annotations since draft-js uses flow. our internal stack doesn't include either currently but from what i understand we're most likely going to integrate typescript in the future, so i'd imagine that would propagate out to this library as well.

benbriggs avatar May 09 '18 20:05 benbriggs

Here's a TypeScript definition at least for convertToHTML, works for me:

Requires @types/draft-js from DefinitelyTyped project (see https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/draft-js for type defs)

Put in file draft-convert.d.ts:

declare module 'draft-convert' {
  import {
    ContentBlock,
    ContentState,
    DraftEntityMutability,
    DraftInlineStyleType,
    EntityInstance,
  } from 'draft-js';

  import { ReactElement, ReactNode } from 'react';

  interface ConvertToHTMLConfig {
    // Inline styles:
    styleToHTML?: <T extends DraftInlineStyleType>(style: T) => Tag | void;

    // Block styles:
    blockToHTML?: (block: ContentBlock) => Tag;

    // Entity styling:
    entityToHTML?: (
      entity: EntityInstance,
      originalText: string
    ) => Tag;
  }

  type ContentStateConverter = (contentState: ContentState) => string;

  type Tag =
    | ReactNode
    | { start: string; end: string; empty?: string }
    | { element: ReactNode; empty?: ReactNode };

  function convertToHTML(config: ConvertToHTMLConfig): ContentStateConverter;
}

sbusch avatar May 02 '19 07:05 sbusch

based on @sbusch comment I created the package on DefinitelyTyped

Now you can have typings installing npm install @types/draft-convert

avaleriani avatar Jun 13 '19 07:06 avaleriani

There are some wrong and confusing things about the examples and the types. According to the types htmlToStyle is (nodeName: string, node: Node) => DraftInlineStyle and the example above says: htmlToStyle: (nodeName, node, currentStyle) => { if (nodeName === 'span' && node.style.color === 'blue') { return currentStyle.add('BLUE'); } else { return currentStyle; } }. Which interface is correct?

kamenminkovcom avatar Jul 01 '19 15:07 kamenminkovcom

@kamenminkovcom seems @types/draft-convert has wrong typing. currentStyle argument is missing there.

seleckis avatar Aug 17 '20 13:08 seleckis

@seleckis what did you did about that problem

sayjeyhi avatar Feb 18 '21 18:02 sayjeyhi

@sayjeyhi like this:

// There is a mistake in draft-convert typing, which does not allow to define typing for currentStyle properly
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type HTMLToStyle = (nodeName: string, node: HTMLElement, currentStyle: any) => DraftInlineStyleType;
export const htmlToStyle: HTMLToStyle = (nodeName, node, currentStyle) => {
 // ... do something with style
  return currentStyle;
};

seleckis avatar Feb 18 '21 19:02 seleckis