GitbookIO / slate-edit-code

A Slate plugin for code block editing
https://gitbookio.github.io/slate-edit-code/
Apache License 2.0
43 stars 34 forks source link
react rich-text-editor slate wysiwyg

⚠️ This repository is archived and has moved to GitBook's fork of ianstormtaylor/slate. Previous versions are still available on NPM All the versions using GitBook's fork of slate are now published under the @gitbook NPM scope. To learn more about why we forked Slate, read our manifest

slate-edit-code

NPM version Linux Build Status

A Slate plugin to handle code block editing.

Install

npm install slate-edit-code

Features

Mod means Ctrl on Windows/Linux and Command on Mac.

Structure

This plugin uses the following structure for code blocks:

<code_block>
  <code_line>A code block is made of</code_line>
  <code_line>several code lines</code_line>
</code_block>

Texts inside code_blocks that contain newlines \n are automatically split into the appropriate number of code_lines.

Simple Usage

import EditCode from 'slate-edit-code'

const plugins = [
  EditCode()
]

Options arguments

Suppressing onKeyDown behavior

Some behavior implemented by this plugins have no corresponding option. While there is an option selectAll to disable the behavior on Mod+A, If you would like to fine tune these behavior, you can always redefine the exported onKeyDown function.

The following example disable all indent behavior

import EditCode from 'slate-edit-code'

const options = { ... };

const basePlugin = EditCode(options);

const customPlugin = {
  ...basePlugin,
  onKeyDown(event, change, editor) {
    if (event.key === 'Tab') {
      // Bypass the original plugin behavior on `Tab`
      return;
    } else {
      return basePlugin.onKeyDown(event, change, editor);
    }
  }
}

// Use customPlugin later on

Utilities and Changes

slate-edit-code exports utilities, accessible like so:

const plugin = EditCode()

// Access exported utilities there
plugin.utils

utils.deserializeCode

plugin.utils.deserializeCode(text: String) => Block

Split a text string into lines, and deserialize them to a code_container Block, with one children code_line Block per line.

changes.toggleCodeBlock

plugin.changes.toggleCodeBlock(change: Change, type: String) => Change

Toggle a block into a code block or a normal block (defined by type).

changes.wrapCodeBlockByKey

plugin.changes.wrapCodeBlockByKey(change: Change, key: String) => Change

Convert a block (paragraph, etc) into a code block.

changes.wrapCodeBlock

plugin.changes.wrapCodeBlock(change: Change) => Change

Convert current block (paragraph, etc) into a code block.

changes.unwrapCodeBlockByKey

plugin.changes.unwrapCodeBlockByKey(change: Change, key: String, type: String) => Change

Convert a code block into a normal block (paragraph, etc).

changes.unwrapCodeBlock

plugin.changes.unwrapCodeBlock(change: Change, type: String) => Change

Convert current code block into a normal block (paragraph, etc).