Coder Social home page Coder Social logo

york-ie-labs / slate-lists Goto Github PK

View Code? Open in Web Editor NEW
11.0 3.0 1.0 1.18 MB

Lists (ol, ul) plugin for the latest version of slate.

Home Page: https://york-ie-labs.github.io/slate-lists/

HTML 9.02% JavaScript 82.43% CSS 8.55%
slate slate-plugins contenteditable javascript wysiwyg

slate-lists's Introduction

slate-lists

Lists (ol, ul) plugin for the latest version of slate.

NPM JavaScript Style Guide

Install

npm install --save @york-ie-labs/slate-lists

Usage

import React, { useState, useMemo } from "react";
import { createEditor } from "slate";
import { Slate, Editable, withReact } from "slate-react";
import { withHistory } from "slate-history";
import isHotkey from "is-hotkey";

import { withLists, indentItem, undentItem } from "@york-ie-labs/slate-lists";

const Element = (props) => {
  const { attributes, children, element } = props;

  // standard components
  switch (element.type) {
    case "bulleted-list":
      return <ul {...attributes}>{children}</ul>;
    case "numbered-list":
      return <ol {...attributes}>{children}</ol>;
    case "list-item":
      return <li {...attributes}>{children}</li>;
    default:
      return <p {...attributes}>{children}</p>;
  }
};

const Editor = ({ value, setValue }) => {
  const editor = useMemo(
    () => withLists(withHistory(withReact(createEditor()))),
    []
  );

  const onKeyDown = (event, change) => {
    if (isHotkey("shift+tab", event)) {
      // attempt to un-indent on shift+tab within list
      event.preventDefault();
      undentItem(editor);
    } else if (isHotkey("tab", event)) {
      // attempt to indent on tab within list
      event.preventDefault();
      indentItem(editor);
    }
  };

  return (
    <Slate editor={editor} value={value} onChange={(value) => setValue(value)}>
      <Editable
        autoFocus={true}
        onKeyDown={onKeyDown}
        renderElement={(props) => <Element {...props} />}
      />
    </Slate>
  );
};

const App = () => {
  // Add the initial value when setting up our state.
  const [value, setValue] = useState([{ children: [{ text: "" }] }]);

  return <Editor value={value} setValue={setValue} />;
};

export default App;

Or view the example code for reference.

License

MIT © York IE Labs

slate-lists's People

Contributors

anothrnick avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

markharper

slate-lists's Issues

Copying and pastring lists bypasses list's max depth

As the title says. I run into the same problem in every plugin that makes lists for slate. I wonder if it is possible to delete list data from copied and pasted data so it prevents the nesting functionality.

image

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.