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

How to insert spaces/tabs with onTab (Keydown keyCode=8 to action)

Open buffaly opened this issue 10 years ago • 8 comments

How to insert spaces/tabs with onTab (Keydown keyCode=8 to action)

buffaly avatar Feb 29 '16 19:02 buffaly

Use the onTab prop.

const MyComponent extends React.Component {
  constructor(props) {
    // etc.
    this.onTab = this.onTab.bind(this);
  }

  onTab(e) {
    // Use `Modifier` API to insert spaces
    // e.preventDefault() if successful
    this.setState({editorState: stateWithSpacesInserted});
  }

  render() {
    return <Editor onTab={this.onTab} ... />;
  }
}

hellendag avatar Feb 29 '16 19:02 hellendag

Here's a more fleshed out example with the Modifier API, adapted from draft-js-code:

  const tabCharacter = "    ";
  // ...
  handleTab(e) {
    e.preventDefault();

    let currentState = this.state.editorState;
    let newContentState = Modifier.replaceText(
      currentState.getCurrentContent(),
      currentState.getSelection(),
      tabCharacter
    );

    this.setState({ 
      editorState: EditorState.push(currentState, newContentState, 'insert-characters')
    });
  }

avk avatar Aug 08 '16 22:08 avk

Just a note. This won't work with auto indenting lists as tab is supposed too. It will actually break the functionality.

ollyde avatar Dec 05 '18 16:12 ollyde

Just wanted to drop a fragment of code that addresses @voidstarfire 's concern about losing list functionality. The point is you check for the list BlockStyles before deciding what to do next.


onTab = (e) => {
        e.preventDefault();
        let currentState = this.state.editorState

        const selection = currentState.getSelection();
        const blockType = currentState
            .getCurrentContent()
            .getBlockForKey(selection.getStartKey())
            .getType();

        if(blockType === "unordered-list-item" || blockType === "ordered-list-item"){
            this.handleChange(RichUtils.onTab(e, currentState, 3));
        }else{
            let newContentState = Modifier.replaceText(
                currentState.getCurrentContent(),
                currentState.getSelection(),
                '    '
            );
            
            this.handleChange(EditorState.push(currentState, newContentState, 'insert-characters'))
        }
    }

MichaelCPell avatar Jan 06 '19 18:01 MichaelCPell

@MichaelCPell Could you explain what it does with lists? The code can't seem to work. But it does capture the tab on both list block types.

CristianoYL avatar Apr 18 '19 22:04 CristianoYL

It defaults to the onTab behavior if your cursor is in a list.

MichaelCPell avatar Apr 23 '19 15:04 MichaelCPell

In latest version "onTab" is deprectaed and it is said that we should use the stuff with the key / command dispatching - but I'm not sure how to put it together properly, bcs RichUtils wants the event in the onTab call which isn't around when you're handling commands...

flq avatar Dec 03 '19 15:12 flq

In latest version "onTab" is deprectaed and it is said that we should use the stuff with the key / command dispatching - but I'm not sure how to put it together properly, bcs RichUtils wants the event in the onTab call which isn't around when you're handling commands...

The event passed to keyBindingFn seems to work just fine for RichUtils.onTab, although it seems the "correct" thing to do in the keyBindingFn is to just return a command string, to be executed in handleKeyCommand (which does not seem to have any event param, as @flq observes). Any reason why I can't just handle the tab in my keyBindingFn and return null?

    function custKeyBindingFn(event) {
        const selection = editorState.getSelection();
        const startKey = selection.getStartKey();
        const blockType = editorState.getCurrentContent().getBlockForKey(startKey).getType();
        
        if(event.keyCode === 9) {
            if(blockType === "unordered-list-item" || blockType === "ordered-list-item") {    
                setEditorState(RichUtils.onTab(event, editorState, MAX_TABDEPTH));
                
                event.preventDefault(); // For good measure. (?)
                return null;
            }
        }
        // ...
        return getDefaultKeyBinding(event);
    }

aaronncfca avatar Nov 06 '21 21:11 aaronncfca