Giter Club home page Giter Club logo

slate-edit-code's Introduction

⚠️ 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

  • 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 EditCode from 'slate-edit-code'

const plugins = [
  EditCode()
]

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

slate-edit-code's People

Contributors

oyeanuj avatar samypesse avatar soreine avatar tommoor avatar zhouzi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

slate-edit-code's Issues

Multiple code blocks no longer work

Previously this functionality worked fine but with the upgrade to 0.13.2 and slate 0.30.X if there are multiple code blocks in a document only the first one is displayed, the text content from subsequent blocks disappears.

This bug disappears when commenting out the slate-edit-code plugin.

Weird behaviour on backspace

When pressing backspace, sometimes the change is not reflected directly in Slate (even when nothing is done by the plugin: default behaviour).

It may be a bug in Slate.

Edition doesn't mean editing

At first I thought it was weird wording as in "the Coding Edition of Slate", but I think it should be "editing"

And the description should use the singular of block, so "A Slate plugin to handle code block editing"

Accepting data in toggle & wrap functions

Hi folks, thank you as always for the plugin! I was wondering if the toggle and wrap functions should take an optional data to be able to pass in things like syntax (and thus allowing easier interoperability with slate-prism).

My personal usecase is Github style markdown of three ~~~/``` followed by the language name. Using regex, I can capture the syntax proposed but can't use the wrap or `toggle` functions to set the code block.

The other alternative would be to do an insertBlock but since the wrap & toggle seem to be exposed utilities, it felt 'safer' to use those.

Thoughts?

How to insert code blocks?

What exactly do you have to do to insert a slate-edit-code block once this is installed and added as a plugin?

I assume it's not all blocks by default. If it is, tab behavior is not working properly (tab loses focus)

Not compatible with slate 0.15

Just a note that the current version is not compatible with slate 0.15. The library expects Slate 0.14.

In case it was just a versioning issue, I tried changing the peer dependencies but I got this error:

Uncaught TypeError: rule.match is not a function(…)

I haven't investigated too much. Just wanted to give you a heads up.

Sunny

Publish example

An example should be published to GH Pages to showcase the plugin.

Prism highlighting does not update all lines when changing syntax

This probably has something to do with the lines refactor: #10. The more basic Slate code-highlighting example does not have this issue: http://slatejs.org/#/code-highlighting

Steps to Reproduce

  • open a document on gitbook.com
  • make a code block with
var line1;
var line2;
  • change syntax to javascript, then to css
  • only currently focused line is updated
  • if you focus the beginning of the incorrectly highlighted line, it triggers prism to update it
  • if you focus the end of the incorrectly highlighted line, it throws an error

Add line numbers

Hi,

Is it possible to add line numbers with this plugin?

Thanks.

Attempting to paste over multiple lines of code results in error

If you highlight a line and paste over it, it works perfectly.

If you highlight 2 or more lines and paste over them with text from anywhere besides the nodes you are pasting over, you'll get Uncaught Error: Could not find a node with key "###"

If you copy identical text or non-HTML (plaintext) over the nodes, it generally seems to work.

If you use Ctrl+A to select all contents of the codebox and paste, it seems to fail universally, even if you paste plaintext.

How to do syntax highlighting?

The docs don't make this clear, but if I used Prism.js markup to render the code_block block, will the indentation behavior work?

Allow custom indent

Hey, currently there is a default indentation defined as a const. Could you please maybe make it configurable via the options or export a function to set default indents?

All code ending up in a single line block

It seems like in the latest version 0.13.2 there are not multiple code line blocks created in the editor, all of the code is put on a single block (in this case my lineType option is set to code-line) with newline characters ignored:

{
  "kind": "block",
  "type": "code",
  "isVoid": false,
  "data": {},
  "nodes": [
    {
      "kind": "block",
      "type": "code-line",
      "isVoid": false,
      "data": {},
      "nodes": [
        {
          "kind": "text",
          "leaves": [
            {
              "kind": "leaf",
              "text": "type\ndescription\nname\n\nonDocumentCreate(document, settings, actor) {\n\n}\n\nonDocumentDelete() {\n\n}\n\nonCollectionCreate() {\n\n}",
              "marks": []
            }
          ]
        }
      ]
    }
  ]
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.