Skip to content

whatever-company/slate-code-block

 
 

Repository files navigation

slate-code-block

A Slate plugin to handle code block editing.

This is a fork from rocketscience/slate-code-block which is a fork of tildepage/slate-code-block, which is a fork from linonetwo/slate-code-block, which is a fork from GitbookIO/slate-edit-code, (because) for compatibility with latest Slate.

Install

yarn add @whatever-company/slate-code-block

Features

  • Pressing Enter insert a new line starting with the right indentation
  • Pressing Tab insert the right indentation if selection is collapsed or indent all lines in selection
  • Pressing Delete remove the indentation before cursor if possible
  • Pressing Mod+Enter exits the code block
  • Pressing Mod+A selects all the text in the block

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 CodeBlock from '@whatever-company/slate-code-block'

const plugins = [CodeBlock()]

Options arguments

  • containerType = 'code_block' : string — The type of the code containers
  • lineType = 'code_line' : string — The type of the code lines
  • exitBlockType = 'paragraph' : null | stringMod+Enter will exit the code container, into the given block type. Backspace at start of an empty code container will convert it to the given block type. Pass null to disable this behavior.
  • onExit: (Change) => void | Change — Change to do when the user hits Mod+Enter. Defaults to exiting the code block, into a new exitBlockType block.
  • selectAll = true : boolean — True to select all code inside a code container on Mod+A
  • allowMarks = false : boolean — False disallow marks in code blocks by normalizing them away.
  • getIndent: (Value) => string — Returns the indent unit as a string. The current value is passed as context.

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 CodeBlock from '@whatever-company/slate-code-block'

const options = { ... };

const basePlugin = CodeBlock(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

Queries and Commands

editor.getCurrentIndent

editor.getCurrentIndent() => String

Returns the current indent detected.

editor.deserializeCode

editor.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.

editor.deserializeCode

editor.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.

editor.toggleCodeBlock

editor.toggleCodeBlock(type: String) => Editor

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

editor.wrapCodeBlockByKey

editor.wrapCodeBlockByKey(key: String) => Editor

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

editor.wrapCodeBlock

editor.wrapCodeBlock() => Editor

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

editor.unwrapCodeBlockByKey

editor.unwrapCodeBlockByKey(key: String, type: String) => Editor

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

editor.unwrapCodeBlock

editor.unwrapCodeBlock(type: String) => Editor

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

About

A Slate plugin for code block editing

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages

  • JavaScript 99.2%
  • Other 0.8%