MCPcopy Index your code
hub / github.com/SamyPesse/draft-js-code

github.com/SamyPesse/draft-js-code @v0.3.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.3.0 ↗ · + Follow
34 symbols 66 edges 20 files 12 documented · 35%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

draft-js-code

NPM version Coverage Status

draft-js-code is a collection of low-level utilities to make code block editing in DraftJS editors nicer.

It works well with draft-js-prism or draft-js-prism-plugin.

Demo: samypesse.github.io/draft-js-code/

Features

  • [x] Indent with TAB
  • [x] Insert new line with correct indentation with ENTER
  • [x] Remove indentation with DELETE
  • [ ] Remove indentation with SHIFT+TAB (#6)
  • [ ] Handle input of pair characters like (), [], {}, "", etc. (#3)

Installation

$ npm install draft-js-code --save

API

CodeUtils.hasSelectionInBlock(editorState)

Returns true if user is editing a code block. You should call this method to encapsulate all other methods when limiting code edition behaviour to code-block.

CodeUtils.handleKeyCommand(editorState, command)

Handle key command for code blocks, returns a new EditorState or null.

CodeUtils.onTab(e, editorState)

Handle user pressing tab, to insert indentation, it returns a new EditorState.

CodeUtils.handleReturn(e, editorState)

Handle user pressing return, to insert a new line inside the code block, it returns a new EditorState.

Usage

import React from 'react';
import Draft from 'draft-js';
import CodeUtils from 'draft-js-code';

class Editor extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      editorState: Draft.EditorState.createEmpty()
    };
  }

  onChange = (editorState) => {
    this.setState({
      editorState
    })
  }

  handleKeyCommand = (command) => {
    const { editorState } = this.state;
    let newState;

    if (CodeUtils.hasSelectionInBlock(editorState)) {
      newState = CodeUtils.handleKeyCommand(editorState, command);
    }

    if (!newState) {
      newState = RichUtils.handleKeyCommand(editorState, command);
    }

    if (newState) {
      this.onChange(newState);
      return 'handled';
    }
    return 'not-handled';
  }

  keyBindingFn = (evt) => {
    const { editorState } = this.state;
    if (!CodeUtils.hasSelectionInBlock(editorState)) return Draft.getDefaultKeyBinding(evt);

    const command = CodeUtils.getKeyBinding(evt);

    return command || Draft.getDefaultKeyBinding(evt);
  }

  handleReturn = (evt) => {
    const { editorState } = this.state;
    if (!CodeUtils.hasSelectionInBlock(editorState)) return 'not-handled';

    this.onChange(CodeUtils.handleReturn(evt, editorState));
    return 'handled';
  }

  onTab = (evt) => {
    const { editorState } = this.state;
    if (!CodeUtils.hasSelectionInBlock(editorState)) return 'not-handled';

    this.onChange(CodeUtils.onTab(evt, editorState));
    return 'handled';
  }

  render() {
    return (
      <Draft.Editor
        editorState={this.state.editorState}
        onChange={this.onChange}
        keyBindingFn={this.keyBindingFn}
        handleKeyCommand={this.handleKeyCommand}
        handleReturn={this.handleReturn}
        onTab={this.onTab}
      />
    );
  }
}

Core symbols most depended-on inside this repo

handleKeyCommand
called by 6
lib/handleKeyCommand.js
getLineAnchorForOffset
called by 6
lib/utils/getLineAnchorForOffset.js
hasSelectionInBlock
called by 4
lib/hasSelectionInBlock.js
onTab
called by 4
lib/onTab.js
getNewLine
called by 4
lib/utils/getNewLine.js
handleReturn
called by 3
lib/handleReturn.js
getIndentation
called by 2
lib/utils/getIndentation.js
getLines
called by 2
lib/utils/getLines.js

Shape

Function 20
Method 10
Class 4

Languages

TypeScript100%

Modules by API surface

demo/main.js15 symbols
lib/__tests__/onTab.test.js3 symbols
lib/__tests__/handleReturn.test.js2 symbols
lib/__tests__/handleKeyCommand.js2 symbols
lib/utils/removeIndent.js1 symbols
lib/utils/insertNewLine.js1 symbols
lib/utils/getNewLine.js1 symbols
lib/utils/getLines.js1 symbols
lib/utils/getLineAnchorForOffset.js1 symbols
lib/utils/getIndentation.js1 symbols
lib/utils/getIndentForLine.js1 symbols
lib/onTab.js1 symbols

For agents

$ claude mcp add draft-js-code \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact