GithubHelp home page GithubHelp logo

jensneuse / slate-react-dnd-plugin Goto Github PK

View Code? Open in Web Editor NEW
36.0 5.0 10.0 732 KB

License: MIT License

HTML 1.83% CSS 0.80% JavaScript 83.06% TypeScript 14.30%
slate slatejs slate-react react-dnd dnd drag-and-drop

slate-react-dnd-plugin's Introduction

slate-react-dnd-plugin

Add react-dnd to slatejs.

TOS

Installation

npm install slate-react-dnd-plugin --save
yarn add slate-react-dnd-plugin
bower install slate-react-dnd-plugin --save

Usage

import * as React from 'react'
import {Editor} from 'slate-react'
import {Value} from 'slate'

import {inject} from "slate-react-dnd-plugin"
//import {inject} from "../../dist/index"

const initialValue = Value.fromJSON({
    document: {
        nodes: [
            {
                object: 'block',
                type: 'paragraph',
                nodes: [
                    {
                        object: 'text',
                        leaves: [
                            {
                                text: 'A line of text in a paragraph.'
                            }
                        ]
                    }
                ]
            }
        ]
    }
});

class ParagraphNode extends React.Component {
    render(){
        return (<p {...this.props.attributes} >{this.props.children}</p>)
    }
}

const blockStyle = {
    border: '1px dashed gray',
    padding: '0.5rem 1rem',
    marginBottom: '.5rem',
    backgroundColor: 'white',
    cursor: 'move'
};

const plugins = inject([
    {
        renderNode: (props) => {
            switch (props.node.type) {
                case 'paragraph':
                    return <ParagraphNode {...props} />;
                default:
                    return null;
            }
        }
    }
],{
    renderBlock: (isDragging,children) => {

        const opacity = isDragging? 0 : 1;

        return <div style={{
            ...blockStyle,
            opacity
        }}>{children}</div>
    }
});

class StoryEditor extends React.Component {

    state = {
        value: initialValue
    };

    onChange({value}){
        this.setState({value})
    }

    render(){
        return (
            <Editor
                value={this.state.value}
                plugins={plugins}
                onChange={this.onChange.bind(this)}/>
        )
    }
}

export default StoryEditor;
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';

import StoryEditor from "./StoryEditor"

import {DragPreviewBlock} from "slate-react-dnd-plugin"
import {DragDropContainer} from "slate-react-dnd-plugin"
import {DropBlock} from "slate-react-dnd-plugin"
import {EditorProvider} from "slate-react-dnd-plugin"

/*import DragPreviewBlock from "../../dist/drag-preview-block"
import DragDropContainer from "../../dist/container"
import DropBlock from "../../dist/drop-block"
import EdititorProvider from "../../dist/editor-provider"*/

const insertBlockFn = (hoverIndex, item, parent, change) => {

  const result = change.insertNodeByKey(parent.key, hoverIndex, {
    object: 'block',
    type: 'paragraph',
    nodes: [
      {
        object: 'text',
        leaves: [
          {
            text: 'A new Block via drag&drop'
          }
        ]
      }
    ]
  });

  item.key = result.value.document.nodes._tail.array[hoverIndex].key;

}

const canDrop = (props, monitor) => {
  return true;
}

const onDrop = (item, parent, change) => {
  console.log('onDrop', item, parent, change);
  change.removeNodeByKey(item.key);
};

const previewBlockStyle = {
	backgroundColor: 'white',
	cursor: 'move',
}

const renderPreviewBlock = (isDragging,children) => {
  const opacity = isDragging ? 0.4 : 1
  return <div style={{ ...previewBlockStyle, opacity }}>{children}</div>
}

class App extends Component {
  render() {
    return (
      <div className="App">
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <h1 className="App-title">Welcome to React</h1>
        </header>
        <DragDropContainer>
          <EditorProvider>
            <div className="wrapper">
              <div className="editor">
                <StoryEditor/>
              </div>
              <div className="rightMenu">
                <DragPreviewBlock className="rightMenu" onHover={insertBlockFn} renderBlock={renderPreviewBlock} >
                  <div className="dragItem">
                    <p>Insert paragraph from here.</p>
                  </div>
                </DragPreviewBlock>
                <DropBlock canDrop={canDrop} onDrop={onDrop}>
                  <div className="dustbin">
                    <p>Drop paragraph here to delete it.</p>
                  </div>
                </DropBlock>
              </div>
            </div>
          </EditorProvider>
        </DragDropContainer>
      </div>
    );
  }
}

export default App;

Contributors

This plugin was initially developed and maintained by one single person: Jens Neuse.

These users are actively maintaining and/or developing this plugin as of today:

slate-react-dnd-plugin's People

Contributors

dependabot[bot] avatar gorillamoe avatar jensneuse 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

Watchers

 avatar  avatar  avatar  avatar  avatar

slate-react-dnd-plugin's Issues

Using lib produces render error

Hey,
Repro steps scenario #1 (repo+example):
If you change the line in StoryEditor.js in example
import DragDropContainer from "../../dist/container"
to
import DragDropContainer from "slate-react-dnd-plugin"
the editor doesn't work.

Anytime I want to import the plugin to any of my project I have exact the same error. Somehow only the example works if you build the library one dir higher. But when it wants to use the actual npm package it is not working. Nor is the package working in any of my projects.

Am I doing something wrong or there is some configuration problem?

Repro steps scenario #2 (fresh react project):
Please try to create a simple react app anywhere, just use npm install slate-react-dnd-plugin --save and copy/paste App.js and StoryEditor.js. Change imports to 'slate-react-dnd-plugin' and you will see it doesn't work throwing render null errors. Here is the minified App.js that works in example folder (if lib is built one dir higher):

import React, { Component } from 'react';
import { Editor } from 'slate-react'
import { Value } from 'slate'

import DragDropContainer from "slate-react-dnd-plugin"
import { inject } from "slate-react-dnd-plugin"

const initialValue = Value.fromJSON({
    document: {
        nodes: [
            {
                object: 'block',
                type: 'paragraph',
                nodes: [
                    {
                        object: 'text',
                        leaves: [
                            {
                                text: 'A line of text in a paragraph.'
                            }
                        ]
                    }
                ]
            }
        ]
    }
});

class ParagraphNode extends React.Component {
    render() {
        return (<p {...this.props.attributes} >{this.props.children}</p>)
    }
}

ParagraphNode.PLUGIN_DEFAULT_TEMPLATE = {
}

const blockStyle = {
    backgroundColor: 'white',
    cursor: 'move',
    opacity: 1
};

const renderNode = (props) => {
    switch (props.node.type) {
        case 'paragraph':
            return <ParagraphNode {...props} />;
        default:
            return null;
    }
  }
  const renderBlock = (isDragging, children) => {
    return <div style={blockStyle}>{children}</div>
  }

class StoryEditor extends React.Component {

    state = {
        value: initialValue
    };

    onChange({ value }) {
        this.setState({ value })
    }
    plugins = inject([{renderNode}],{renderBlock});
    render() {
        return (
            <Editor
                value={this.state.value}
                plugins={this.plugins}
                onChange={this.onChange.bind(this)} />
        )
    }
}

class App extends Component {
  render() {
    return (
        <DragDropContainer>
                <StoryEditor/>
        </DragDropContainer>
    );
  }
}

export default App;

Here's the stack trace:

index.js:2178 Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

Check your code at App.js:81.
    in App (at index.js:7)
__stack_frame_overlay_proxy_console__ @ index.js:2178
printWarning @ warning.js:33
warning @ warning.js:57
createElementWithValidation @ react.development.js:1243
render @ App.js:80
finishClassComponent @ react-dom.development.js:7873
updateClassComponent @ react-dom.development.js:7850
beginWork @ react-dom.development.js:8225
performUnitOfWork @ react-dom.development.js:10224
workLoop @ react-dom.development.js:10288
callCallback @ react-dom.development.js:542
invokeGuardedCallbackDev @ react-dom.development.js:581
invokeGuardedCallback @ react-dom.development.js:438
renderRoot @ react-dom.development.js:10366
performWorkOnRoot @ react-dom.development.js:11014
performWork @ react-dom.development.js:10967
requestWork @ react-dom.development.js:10878
scheduleWorkImpl @ react-dom.development.js:10732
scheduleWork @ react-dom.development.js:10689
scheduleTopLevelUpdate @ react-dom.development.js:11193
updateContainer @ react-dom.development.js:11231
(anonymous) @ react-dom.development.js:15226
unbatchedUpdates @ react-dom.development.js:11102
renderSubtreeIntoContainer @ react-dom.development.js:15225
render @ react-dom.development.js:15290
./src/index.js @ index.js:7
__webpack_require__ @ bootstrap d13401ee605a32928799:678
fn @ bootstrap d13401ee605a32928799:88
0 @ registerServiceWorker.js:117
__webpack_require__ @ bootstrap d13401ee605a32928799:678
(anonymous) @ bootstrap d13401ee605a32928799:724
(anonymous) @ bootstrap d13401ee605a32928799:724
invariant.js:42 Uncaught Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

Check the render method of `App`.
    at invariant (http://localhost:3000/static/js/bundle.js:4611:15)
    at createFiberFromElement (http://localhost:3000/static/js/bundle.js:31284:5)
    at reconcileSingleElement (http://localhost:3000/static/js/bundle.js:33062:23)
    at reconcileChildFibers (http://localhost:3000/static/js/bundle.js:33166:35)
    at reconcileChildrenAtExpirationTime (http://localhost:3000/static/js/bundle.js:33287:30)
    at reconcileChildren (http://localhost:3000/static/js/bundle.js:33278:5)
    at finishClassComponent (http://localhost:3000/static/js/bundle.js:33412:5)
    at updateClassComponent (http://localhost:3000/static/js/bundle.js:33381:12)
    at beginWork (http://localhost:3000/static/js/bundle.js:33756:16)
    at performUnitOfWork (http://localhost:3000/static/js/bundle.js:35755:16)
    at workLoop (http://localhost:3000/static/js/bundle.js:35819:26)
    at HTMLUnknownElement.callCallback (http://localhost:3000/static/js/bundle.js:26073:14)
    at Object.invokeGuardedCallbackDev (http://localhost:3000/static/js/bundle.js:26112:16)
    at invokeGuardedCallback (http://localhost:3000/static/js/bundle.js:25969:27)
    at renderRoot (http://localhost:3000/static/js/bundle.js:35897:7)
    at performWorkOnRoot (http://localhost:3000/static/js/bundle.js:36545:24)
    at performWork (http://localhost:3000/static/js/bundle.js:36498:7)
    at requestWork (http://localhost:3000/static/js/bundle.js:36409:7)
    at scheduleWorkImpl (http://localhost:3000/static/js/bundle.js:36263:11)
    at scheduleWork (http://localhost:3000/static/js/bundle.js:36220:12)
    at scheduleTopLevelUpdate (http://localhost:3000/static/js/bundle.js:36724:5)
    at Object.updateContainer (http://localhost:3000/static/js/bundle.js:36762:7)
    at http://localhost:3000/static/js/bundle.js:40757:19
    at Object.unbatchedUpdates (http://localhost:3000/static/js/bundle.js:36633:12)
    at renderSubtreeIntoContainer (http://localhost:3000/static/js/bundle.js:40756:17)
    at Object.render (http://localhost:3000/static/js/bundle.js:40821:12)
    at Object../src/index.js (http://localhost:3000/static/js/bundle.js:77760:51)
    at __webpack_require__ (http://localhost:3000/static/js/bundle.js:679:30)
    at fn (http://localhost:3000/static/js/bundle.js:89:20)
    at Object.0 (http://localhost:3000/static/js/bundle.js:77906:18)
    at __webpack_require__ (http://localhost:3000/static/js/bundle.js:679:30)
    at http://localhost:3000/static/js/bundle.js:725:37
    at http://localhost:3000/static/js/bundle.js:728:10
invariant @ invariant.js:42
createFiberFromElement @ react-dom.development.js:5753
reconcileSingleElement @ react-dom.development.js:7531
reconcileChildFibers @ react-dom.development.js:7635
reconcileChildrenAtExpirationTime @ react-dom.development.js:7756
reconcileChildren @ react-dom.development.js:7747
finishClassComponent @ react-dom.development.js:7881
updateClassComponent @ react-dom.development.js:7850
beginWork @ react-dom.development.js:8225
performUnitOfWork @ react-dom.development.js:10224
workLoop @ react-dom.development.js:10288
callCallback @ react-dom.development.js:542
invokeGuardedCallbackDev @ react-dom.development.js:581
invokeGuardedCallback @ react-dom.development.js:438
renderRoot @ react-dom.development.js:10366
performWorkOnRoot @ react-dom.development.js:11014
performWork @ react-dom.development.js:10967
requestWork @ react-dom.development.js:10878
scheduleWorkImpl @ react-dom.development.js:10732
scheduleWork @ react-dom.development.js:10689
scheduleTopLevelUpdate @ react-dom.development.js:11193
updateContainer @ react-dom.development.js:11231
(anonymous) @ react-dom.development.js:15226
unbatchedUpdates @ react-dom.development.js:11102
renderSubtreeIntoContainer @ react-dom.development.js:15225
render @ react-dom.development.js:15290
./src/index.js @ index.js:7
__webpack_require__ @ bootstrap d13401ee605a32928799:678
fn @ bootstrap d13401ee605a32928799:88
0 @ registerServiceWorker.js:117
__webpack_require__ @ bootstrap d13401ee605a32928799:678
(anonymous) @ bootstrap d13401ee605a32928799:724
(anonymous) @ bootstrap d13401ee605a32928799:724
index.js:2178 The above error occurred in the <App> component:
    in App (at index.js:7)

Consider adding an error boundary to your tree to customize error handling behavior.
Visit https://fb.me/react-error-boundaries to learn more about error boundaries.
__stack_frame_overlay_proxy_console__ @ index.js:2178
logCapturedError @ react-dom.development.js:9747
captureError @ react-dom.development.js:10540
renderRoot @ react-dom.development.js:10391
performWorkOnRoot @ react-dom.development.js:11014
performWork @ react-dom.development.js:10967
requestWork @ react-dom.development.js:10878
scheduleWorkImpl @ react-dom.development.js:10732
scheduleWork @ react-dom.development.js:10689
scheduleTopLevelUpdate @ react-dom.development.js:11193
updateContainer @ react-dom.development.js:11231
(anonymous) @ react-dom.development.js:15226
unbatchedUpdates @ react-dom.development.js:11102
renderSubtreeIntoContainer @ react-dom.development.js:15225
render @ react-dom.development.js:15290
./src/index.js @ index.js:7
__webpack_require__ @ bootstrap d13401ee605a32928799:678
fn @ bootstrap d13401ee605a32928799:88
0 @ registerServiceWorker.js:117
__webpack_require__ @ bootstrap d13401ee605a32928799:678
(anonymous) @ bootstrap d13401ee605a32928799:724
(anonymous) @ bootstrap d13401ee605a32928799:724
invariant.js:42 Uncaught Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

Check the render method of `App`.
    at invariant (http://localhost:3000/static/js/bundle.js:4611:15)
    at createFiberFromElement (http://localhost:3000/static/js/bundle.js:31284:5)
    at reconcileSingleElement (http://localhost:3000/static/js/bundle.js:33062:23)
    at reconcileChildFibers (http://localhost:3000/static/js/bundle.js:33166:35)
    at reconcileChildrenAtExpirationTime (http://localhost:3000/static/js/bundle.js:33287:30)
    at reconcileChildren (http://localhost:3000/static/js/bundle.js:33278:5)
    at finishClassComponent (http://localhost:3000/static/js/bundle.js:33412:5)
    at updateClassComponent (http://localhost:3000/static/js/bundle.js:33381:12)
    at beginWork (http://localhost:3000/static/js/bundle.js:33756:16)
    at performUnitOfWork (http://localhost:3000/static/js/bundle.js:35755:16)
    at workLoop (http://localhost:3000/static/js/bundle.js:35819:26)
    at HTMLUnknownElement.callCallback (http://localhost:3000/static/js/bundle.js:26073:14)
    at Object.invokeGuardedCallbackDev (http://localhost:3000/static/js/bundle.js:26112:16)
    at invokeGuardedCallback (http://localhost:3000/static/js/bundle.js:25969:27)
    at renderRoot (http://localhost:3000/static/js/bundle.js:35897:7)
    at performWorkOnRoot (http://localhost:3000/static/js/bundle.js:36545:24)
    at performWork (http://localhost:3000/static/js/bundle.js:36498:7)
    at requestWork (http://localhost:3000/static/js/bundle.js:36409:7)
    at scheduleWorkImpl (http://localhost:3000/static/js/bundle.js:36263:11)
    at scheduleWork (http://localhost:3000/static/js/bundle.js:36220:12)
    at scheduleTopLevelUpdate (http://localhost:3000/static/js/bundle.js:36724:5)
    at Object.updateContainer (http://localhost:3000/static/js/bundle.js:36762:7)
    at http://localhost:3000/static/js/bundle.js:40757:19
    at Object.unbatchedUpdates (http://localhost:3000/static/js/bundle.js:36633:12)
    at renderSubtreeIntoContainer (http://localhost:3000/static/js/bundle.js:40756:17)
    at Object.render (http://localhost:3000/static/js/bundle.js:40821:12)
    at Object../src/index.js (http://localhost:3000/static/js/bundle.js:77760:51)
    at __webpack_require__ (http://localhost:3000/static/js/bundle.js:679:30)
    at fn (http://localhost:3000/static/js/bundle.js:89:20)
    at Object.0 (http://localhost:3000/static/js/bundle.js:77906:18)
    at __webpack_require__ (http://localhost:3000/static/js/bundle.js:679:30)
    at http://localhost:3000/static/js/bundle.js:725:37
    at http://localhost:3000/static/js/bundle.js:728:10

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.