From a428dd7d36eeb668a762778dc6d1c0020174601e Mon Sep 17 00:00:00 2001 From: Eelco Lempsink Date: Fri, 6 Jan 2017 16:43:16 +0100 Subject: [PATCH] [draft-js] Update definitions for version 0.9.0 (#13794) * Update for Draft JS 0.9.0 * Update tests * Fix customStyleFn parameters for 0.9.0 (block parameter will be added in 0.10.0) * Remove double author from the header, trips up Travis CI test * Add myself as author * Only support TypeScript 2.1 and up --- draft-js/draft-js-tests.tsx | 8 ++--- draft-js/index.d.ts | 69 ++++++++++++++++++++++--------------- 2 files changed, 46 insertions(+), 31 deletions(-) diff --git a/draft-js/draft-js-tests.tsx b/draft-js/draft-js-tests.tsx index 4409d820dc..1ac3c622a0 100644 --- a/draft-js/draft-js-tests.tsx +++ b/draft-js/draft-js-tests.tsx @@ -13,15 +13,15 @@ class RichEditorExample extends React.Component<{}, { editorState: EditorState } onChange: (editorState: EditorState) => void = (editorState: EditorState) => this.setState({ editorState }); - handleKeyCommand: (command: string) => boolean = (command: string) => { + handleKeyCommand = (command: string) => { const {editorState} = this.state; const newState = RichUtils.handleKeyCommand(editorState, command); if (newState) { this.onChange(newState); - return true; + return "handled"; } - return false; + return "not-handled"; } toggleBlockType: (blockType: string) => void = (blockType: string) => { @@ -175,4 +175,4 @@ const InlineStyleControls = (props: {editorState: EditorState, onToggle: (blockT ReactDOM.render( , document.getElementById('target') -); \ No newline at end of file +); diff --git a/draft-js/index.d.ts b/draft-js/index.d.ts index cbc3948d31..e02044a006 100644 --- a/draft-js/index.d.ts +++ b/draft-js/index.d.ts @@ -1,7 +1,8 @@ -// Type definitions for Draft.js v0.7.0 +// Type definitions for Draft.js v0.9.0 // Project: https://facebook.github.io/draft-js/ -// Definitions by: Dmitry Rogozhny +// Definitions by: Dmitry Rogozhny , Eelco Lempsink // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.1 import * as React from 'react'; import * as Immutable from 'immutable'; @@ -15,10 +16,12 @@ declare namespace Draft { import DraftEditorCommand = Draft.Model.Constants.DraftEditorCommand; import DraftBlockType = Draft.Model.Constants.DraftBlockType; import DraftDragType = Draft.Model.Constants.DraftDragType; + import DraftHandleValue = Draft.Model.Constants.DraftHandleValue; import EditorState = Draft.Model.ImmutableData.EditorState; import ContentBlock = Draft.Model.ImmutableData.ContentBlock; import SelectionState = Draft.Model.ImmutableData.SelectionState; + import DraftInlineStyle = Draft.Model.ImmutableData.DraftInlineStyle; import DraftBlockRenderConfig = Draft.Model.ImmutableData.DraftBlockRenderConfig; @@ -61,12 +64,16 @@ declare namespace Draft { // the default `TextEditorBlock` is used. blockRendererFn?(block: ContentBlock): any; - // Function that allows to define class names to apply to the given block when it is rendered. + // Function that allows to define class names to apply to the given block when it is rendered. blockStyleFn?(block: ContentBlock): string; // Provide a map of inline style names corresponding to CSS style objects // that will be rendered for matching ranges. - customStyleMap?: any, + customStyleMap?: any; + + // Provide a function that will construct CSS style objects given inline + // style names. + customStyleFn?: (style: DraftInlineStyle) => Object; // A function that accepts a synthetic key event and returns // the matching DraftEditorCommand constant, or null if no command should @@ -77,42 +84,42 @@ declare namespace Draft { // Set whether the `DraftEditor` component should be editable. Useful for // temporarily disabling edit behavior or allowing `DraftEditor` rendering // to be used for consumption purposes. - readOnly?: boolean, + readOnly?: boolean; // Note: spellcheck is always disabled for IE. If enabled in Safari, OSX // autocorrect is enabled as well. - spellCheck?: boolean, + spellCheck?: boolean; // Set whether to remove all style information from pasted content. If your // use case should not have any block or inline styles, it is recommended // that you set this to `true`. - stripPastedStyles?: boolean, + stripPastedStyles?: boolean; - tabIndex?: number, + tabIndex?: number; - ariaActiveDescendantID?: string, - ariaAutoComplete?: string, - ariaDescribedBy?: string, - ariaExpanded?: boolean, - ariaHasPopup?: boolean, - ariaLabel?: string, - ariaOwneeID?: string, + ariaActiveDescendantID?: string; + ariaAutoComplete?: string; + ariaDescribedBy?: string; + ariaExpanded?: boolean; + ariaHasPopup?: boolean; + ariaLabel?: string; + ariaOwneeID?: string; - webDriverTestID?: string, + webDriverTestID?: string; /** * Cancelable event handlers, handled from the top level down. A handler - * that returns true will be the last handler to execute for that event. + * that returns `handled` will be the last handler to execute for that event. */ // Useful for managing special behavior for pressing the `Return` key. E.g. // removing the style from an empty list item. - handleReturn?(e: SyntheticKeyboardEvent): boolean, + handleReturn?(e: SyntheticKeyboardEvent): DraftHandleValue, // Map a key command string provided by your key binding function to a - // specified behavior. - handleKeyCommand?(command: DraftEditorCommand): boolean, - handleKeyCommand?(command: string): boolean, + // specified behavior. + handleKeyCommand?(command: DraftEditorCommand): DraftHandleValue, + handleKeyCommand?(command: string): DraftHandleValue, // Handle intended text insertion before the insertion occurs. This may be @@ -120,17 +127,17 @@ declare namespace Draft { // to trigger some special behavior. E.g. immediately converting `:)` to an // emoji Unicode character, or replacing ASCII quote characters with smart // quotes. - handleBeforeInput?(chars: string): boolean, + handleBeforeInput?(chars: string): DraftHandleValue, - handlePastedText?(text: string, html?: string): boolean, + handlePastedText?(text: string, html?: string): DraftHandleValue, - handlePastedFiles?(files: Array): boolean, + handlePastedFiles?(files: Array): DraftHandleValue, // Handle dropped files - handleDroppedFiles?(selection: SelectionState, files: Array): boolean, + handleDroppedFiles?(selection: SelectionState, files: Array): DraftHandleValue, // Handle other drops to prevent default text movement/insertion behaviour - handleDrop?(selection: SelectionState, dataTransfer: Object, isInternal: DraftDragType): boolean, + handleDrop?(selection: SelectionState, dataTransfer: Object, isInternal: DraftDragType): DraftHandleValue, /** * Non-cancelable event triggers. @@ -298,6 +305,12 @@ declare namespace Draft { * around to indicate whether a deletion is forward or backward. */ type DraftRemovalDirection = "backward" | "forward"; + + /** + * A type that allows us to avoid returning boolean values + * to indicate whether an event was handled or not. + */ + type DraftHandleValue = "handled" | "not-handled"; } namespace Decorators { @@ -887,6 +900,7 @@ import CharacterMetadata = Draft.Model.ImmutableData.CharacterMetadata; import ContentBlock = Draft.Model.ImmutableData.ContentBlock; import ContentState = Draft.Model.ImmutableData.ContentState; import SelectionState = Draft.Model.ImmutableData.SelectionState; +import DraftInlineStyle = Draft.Model.ImmutableData.DraftInlineStyle; import AtomicBlockUtils = Draft.Model.Modifier.AtomicBlockUtils; import KeyBindingUtil = Draft.Component.Utils.KeyBindingUtil; @@ -919,6 +933,7 @@ export { ContentBlock, ContentState, SelectionState, + DraftInlineStyle, AtomicBlockUtils, KeyBindingUtil, @@ -932,7 +947,7 @@ export { convertFromRaw, convertToRaw, convertFromHTML, - + genKey, getDefaultKeyBinding, getVisibleSelectionRect