├── .babelrc
├── .eslintrc
├── .gitignore
├── .npmignore
├── README.md
├── example
├── demo.gif
├── index.css
├── index.html
├── index.js
├── nodes
│ └── clear.js
└── state.json
├── lib
└── index.js
└── package.json
/.babelrc:
--------------------------------------------------------------------------------
1 | {
2 | "presets": [
3 | "es2015",
4 | "react",
5 | "stage-0"
6 | ]
7 | }
8 |
--------------------------------------------------------------------------------
/.eslintrc:
--------------------------------------------------------------------------------
1 | {
2 | "plugins": [
3 | "import",
4 | "react"
5 | ],
6 | "settings": {
7 | "import/extensions": [".js"]
8 | },
9 | "parser": "babel-eslint",
10 | "parserOptions": {
11 | "ecmaFeatures": {
12 | "jsx": true
13 | }
14 | },
15 | "env": {
16 | "browser": true,
17 | "mocha": true,
18 | "node": true
19 | },
20 | "rules": {
21 | "block-spacing": "error",
22 | "comma-dangle": ["error", "only-multiline"],
23 | "comma-spacing": ["error", { "before": false, "after": true }],
24 | "comma-style": ["error", "last"],
25 | "computed-property-spacing": ["error", "never"],
26 | "constructor-super": "error",
27 | "curly": ["error", "multi-line"],
28 | "dot-location": ["error", "property"],
29 | "dot-notation": ["error", { "allowKeywords": true }],
30 | "eol-last": "error",
31 | "func-style": ["error", "declaration"],
32 | "import/export": "error",
33 | "import/namespace": "error",
34 | "import/newline-after-import": "error",
35 | "import/no-deprecated": "error",
36 | "import/no-extraneous-dependencies": "error",
37 | "import/no-mutable-exports": "error",
38 | "import/no-named-as-default": "error",
39 | "import/no-named-as-default-member": "error",
40 | "import/no-unresolved": "error",
41 | "key-spacing": ["error", { "beforeColon": false, "afterColon": true }],
42 | "lines-around-comment": ["error", { "beforeBlockComment": true, "afterBlockComment": true }],
43 | "new-parens": "error",
44 | "no-array-constructor": "error",
45 | "no-class-assign": "error",
46 | "no-const-assign": "error",
47 | "no-debugger": "warn",
48 | "no-dupe-args": "error",
49 | "no-dupe-class-members": "error",
50 | "no-dupe-keys": "error",
51 | "no-duplicate-case": "error",
52 | "no-empty": "error",
53 | "no-empty-character-class": "error",
54 | "no-empty-pattern": "error",
55 | "no-ex-assign": "error",
56 | "no-extend-native": "error",
57 | "no-func-assign": "error",
58 | "no-invalid-regexp": "error",
59 | "no-mixed-spaces-and-tabs": ["error", false],
60 | "no-multi-spaces": "error",
61 | "no-multiple-empty-lines": ["error", { "max": 2, "maxEOF": 1 }],
62 | "no-native-reassign": "error",
63 | "no-negated-in-lhs": "error",
64 | "no-new-object": "error",
65 | "no-new-symbol": "error",
66 | "no-redeclare": "error",
67 | "no-regex-spaces": "error",
68 | "no-sequences": "error",
69 | "no-shadow": "error",
70 | "no-shadow-restricted-names": "error",
71 | "no-spaced-func": "error",
72 | "no-throw-literal": "error",
73 | "no-trailing-spaces": "error",
74 | "no-undef": "error",
75 | "no-unreachable": "error",
76 | "no-unsafe-finally": "error",
77 | "no-unused-expressions": "error",
78 | "no-useless-call": "error",
79 | "no-useless-computed-key": "error",
80 | "no-useless-constructor": "error",
81 | "no-useless-rename": "error",
82 | "no-var": "error",
83 | "no-void": "error",
84 | "no-whitespace-before-property": "error",
85 | "no-with": "error",
86 | "object-property-newline": ["error", { "allowMultiplePropertiesPerLine": true }],
87 | "object-shorthand": ["error", "always"],
88 | "padded-blocks": ["error", "never"],
89 | "prefer-arrow-callback": "error",
90 | "prefer-rest-params": "error",
91 | "prefer-spread": "error",
92 | "prefer-template": "error",
93 | "radix": "error",
94 | "react/jsx-boolean-value": ["error", "never"],
95 | "react/jsx-closing-bracket-location": "error",
96 | "react/jsx-curly-spacing": ["error", "never"],
97 | "react/jsx-equals-spacing": "error",
98 | "react/jsx-first-prop-new-line": ["error", "multiline"],
99 | "react/jsx-key": "error",
100 | "react/jsx-no-bind": "error",
101 | "react/jsx-no-duplicate-props": "error",
102 | "react/jsx-no-undef": "error",
103 | "react/jsx-space-before-closing": ["error", "always"],
104 | "react/no-deprecated": "error",
105 | "react/no-did-mount-set-state": "error",
106 | "react/no-did-update-set-state": "error",
107 | "react/no-string-refs": "error",
108 | "react/no-unknown-property": "error",
109 | "react/react-in-jsx-scope": "error",
110 | "react/self-closing-comp": "error",
111 | "react/sort-prop-types": "error",
112 | "react/wrap-multilines": "error",
113 | "rest-spread-spacing": ["error", "never"],
114 | "semi": ["error", "never"],
115 | "space-before-blocks": "error",
116 | "space-before-function-paren": ["error", { "anonymous": "always", "named": "never" }],
117 | "space-in-parens": "error",
118 | "space-infix-ops": "error",
119 | "space-unary-ops": ["error", { "words": true, "nonwords": false }],
120 | "spaced-comment": ["error", "always", { "exceptions": ["-"]}],
121 | "template-curly-spacing": "error",
122 | "unicode-bom": ["error", "never"],
123 | "use-isnan": "error",
124 | "valid-jsdoc": ["error", { "prefer": { "return": "returns" }, "requireReturn": false }],
125 | "valid-typeof": "error",
126 | "yield-star-spacing": ["error", "after"],
127 | "yoda": ["error", "never"]
128 | }
129 | }
130 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | dist
2 | example/build.js
3 | node_modules
4 |
--------------------------------------------------------------------------------
/.npmignore:
--------------------------------------------------------------------------------
1 | example
2 | lib
3 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
slate-wrapped-block-plugin
3 |
4 | A [**Slate**](https://github.com/ianstormtaylor/slate) plugin to automatically wrap a custom block component around the selected block by condition. Useful for implementing block floating button or other behaviors.
5 |
6 | 
7 |
8 | ---
9 |
10 | ### Install
11 |
12 | ```
13 | npm install --save slate-wrapped-block-plugin
14 | ```
15 |
16 | _You will need to have installed `slate` as a dependency already._
17 |
18 | ---
19 |
20 | ### Usage
21 |
22 | ```js
23 | import AutoWrapBlock from 'slate-wrapped-block-plugin'
24 | import { Editor } from 'slate'
25 |
26 | // Add the plugin to your set of plugins...
27 | const plugins = [
28 | AutoWrapBlock({
29 | type: 'wrap',
30 | condition: (node) =>
31 | node.type === 'paragraph' && !node.isEmpty
32 | component: (props) =>
33 | {props.children}
34 | })
35 | ]
36 |
37 | // And later pass it into the Slate editor...
38 |
42 | ```
43 |
44 | Option | Type | Description
45 | --- | --- | ---
46 | **`type`** | `String` | Type of block that is going to wrap around selected block.
47 | **`condition`** | `Function` | A function that use to check selected block is going to be wrap or not.
48 | **`component`** | `Function` | React component that is going to be wrap around selected block.
49 |
50 | ---
51 |
52 | ### Development
53 |
54 | Clone the repository and then run:
55 |
56 | ```
57 | npm install
58 | npm run watch
59 | ```
60 |
61 | And open the example page in your browser:
62 |
63 | ```
64 | http://localhost:8888/
65 | ```
66 |
67 | ---
68 |
69 | ### License
70 |
71 | Copyright © 2016, [Oozou](http://oozou.com)
72 |
73 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
74 |
75 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
76 |
77 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
78 |
--------------------------------------------------------------------------------
/example/demo.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/oozou/slate-wrapped-block-plugin/cf45ef0a842f2feb6ac28811170915823601feed/example/demo.gif
--------------------------------------------------------------------------------
/example/index.css:
--------------------------------------------------------------------------------
1 |
2 | html {
3 | font-family: 'Roboto', sans-serif;
4 | line-height: 1.4;
5 | background: #eee;
6 | padding: 50px;
7 | }
8 |
9 | body {
10 | margin: 0;
11 | }
12 |
13 | main {
14 | max-width: 42em;
15 | margin: 0 auto;
16 | padding: 20px;
17 | background: #fff;
18 | }
19 |
20 | blockquote {
21 | margin: 0;
22 | padding-left: 20px;
23 | border-left: 2px solid #aaa;
24 | }
25 |
26 | :not(ul) > li {
27 | background: red;
28 | }
29 |
--------------------------------------------------------------------------------
/example/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Example
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/example/index.js:
--------------------------------------------------------------------------------
1 |
2 | import AutoWrapBlock from '..'
3 | import React from 'react'
4 | import ReactDOM from 'react-dom'
5 | import initialState from './state.json'
6 | import ClearNode from './nodes/clear'
7 | import { Editor, Raw } from 'slate'
8 |
9 | class Example extends React.Component {
10 |
11 | plugins = [
12 | AutoWrapBlock({
13 | type: 'close',
14 | condition: (node) =>
15 | node.type === 'paragraph' && !node.isEmpty,
16 | component: ClearNode,
17 | })
18 | ];
19 |
20 | state = {
21 | state: Raw.deserialize(initialState, { terse: true })
22 | };
23 |
24 | onChange = (state) => {
25 | this.setState({ state })
26 | }
27 |
28 | render = () => {
29 | return (
30 |
35 | )
36 | }
37 | }
38 |
39 | const example =
40 | const root = document.body.querySelector('main')
41 | ReactDOM.render(example, root)
42 |
--------------------------------------------------------------------------------
/example/nodes/clear.js:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 |
3 | const clearNodeStyle = {
4 | backgroundColor: '#eee',
5 | position: 'relative'
6 | }
7 |
8 | const clearIconStyle = {
9 | position: 'absolute',
10 | top: 3,
11 | right: 2,
12 | width: '20px',
13 | height: '20px',
14 | textAlign: 'center',
15 | zIndex: 2000,
16 | cursor: 'pointer',
17 | color: '#999'
18 | }
19 |
20 | class ClearNode extends React.Component {
21 |
22 | clearContent = () => {
23 | const { editor, state, node } = this.props
24 |
25 | const textNode = node.nodes.first().nodes.first()
26 |
27 | const contentSelection = {
28 | anchorKey: textNode.key,
29 | anchorOffset: 0,
30 | focusKey: textNode.key,
31 | focusOffset: textNode.length
32 | }
33 |
34 | editor.props.onChange(
35 | state
36 | .transform()
37 | .moveTo(contentSelection)
38 | .delete()
39 | .apply()
40 | )
41 | }
42 |
43 | render() {
44 | return (
45 |
46 |
47 | {this.props.children}
48 |
49 | )
50 | }
51 | }
52 |
53 | export default ClearNode
54 |
--------------------------------------------------------------------------------
/example/state.json:
--------------------------------------------------------------------------------
1 | {
2 | "nodes": [
3 | {
4 | "kind": "block",
5 | "type": "paragraph",
6 | "nodes": [
7 | {
8 | "kind": "text",
9 | "text": "A long time ago, in a galaxy far, far away...It is a period of civil war. Rebel spaceships, striking from a hidden base, have won their first victory against the evil Galactic Empire."
10 | }
11 | ]
12 | },
13 | {
14 | "kind": "block",
15 | "type": "paragraph",
16 | "nodes": [
17 | {
18 | "kind": "text",
19 | "text": "I find your lack of faith disturbing. – Darth Vader"
20 | }
21 | ]
22 | },
23 | {
24 | "kind": "block",
25 | "type": "paragraph",
26 | "nodes": [
27 | {
28 | "kind": "text",
29 | "text": "In my experience there is no such thing as luck. – Obi-Wan Kenobi"
30 | }
31 | ]
32 | },
33 | {
34 | "kind": "block",
35 | "type": "paragraph",
36 | "nodes": [
37 | {
38 | "kind": "text",
39 | "text": "It’s a trap! – Admiral Ackbar"
40 | }
41 | ]
42 | },
43 | {
44 | "kind": "block",
45 | "type": "paragraph",
46 | "nodes": [
47 | {
48 | "kind": "text",
49 | "text": "Once you start down the dark path, forever will it dominate your destiny. - Yoda"
50 | }
51 | ]
52 | }
53 | ]
54 | }
55 |
--------------------------------------------------------------------------------
/lib/index.js:
--------------------------------------------------------------------------------
1 | /**
2 | * A Slate plugin that auto wrap selected block with some custom block.
3 | *
4 | * @param {Object} opts
5 | * return {Object}
6 | */
7 |
8 | function AutoWrapBlock(opts) {
9 | if (!opts.type) throw new Error('You must provide a `type` option.')
10 | if (!opts.condition) throw new Error('You must provide a `condition` option.')
11 | if (!opts.component) throw new Error('You must provide a `component` option.')
12 |
13 | /**
14 | * On select.
15 | *
16 | * @param {Event} e
17 | * @param {Object} data
18 | * @param {State} state
19 | * @param {Function} getState
20 | * @param {Object} props
21 | */
22 |
23 | function onSelect(e, data, state, { getState, props }) {
24 | setTimeout(() => autoWrap(getState, props.onChange), 1)
25 | }
26 |
27 | /**
28 | * On key down.
29 | *
30 | * @param {Event} e
31 | * @param {Object} data
32 | * @param {State} state
33 | * @param {Function} getState
34 | * @param {Object} props
35 | */
36 |
37 | function onKeyDown(e, data, state, { getState, props }) {
38 | setTimeout(() => autoWrap(getState, props.onChange), 1)
39 | }
40 |
41 | /**
42 | * Get schema
43 | *
44 | * @param {Object} node
45 | * @return {Function}
46 | */
47 |
48 | function getSchema(node) {
49 | const schema = {
50 | nodes: {}
51 | }
52 | schema.nodes[`${opts.type}`] = opts.component
53 | return schema
54 | }
55 |
56 | /**
57 | * Get wrapped block keys.
58 | *
59 | * @param {List} nodes
60 | * @return {List}
61 | */
62 |
63 | function getWrappedBlockKeys(nodes) {
64 | return nodes
65 | .filter((node) => node.type === opts.type)
66 | .map((node) => node.nodes.first().nodes.first().key)
67 | }
68 |
69 | /**
70 | * Remove all wrapped blocks.
71 | *
72 | * @param {State} wrappedBlockState
73 | * @return {State}
74 | */
75 |
76 | function removeAllWrappedBlock(wrappedBlockState) {
77 | const wrappedBlockKeys = getWrappedBlockKeys(wrappedBlockState.document.nodes)
78 |
79 | let state = wrappedBlockState
80 | if (wrappedBlockKeys.size > 0) {
81 | let transform = state.transform()
82 | for (const wrappedBlockKey of wrappedBlockKeys) {
83 | transform = transform
84 | .moveTo({ anchorKey: wrappedBlockKey, anchorOffset: 0 })
85 | .unwrapBlock(opts.type)
86 | }
87 | state = transform.apply()
88 | }
89 | return state
90 | }
91 |
92 | /**
93 | * Auto wrap.
94 | *
95 | * @param {Function} getState
96 | * @param {Function} onChange
97 | */
98 |
99 | function autoWrap(getState, onChange) {
100 | const state = getState()
101 | const { selection } = state
102 |
103 | const removedAllWrappedBlockState = removeAllWrappedBlock(state)
104 |
105 | let transform = removedAllWrappedBlockState
106 | .transform()
107 | .moveTo(selection)
108 |
109 | const currentNode = state.blocks.first()
110 | if (opts.condition(currentNode)) {
111 | transform = transform.wrapBlock(opts.type)
112 | }
113 |
114 | onChange(transform.apply())
115 | }
116 |
117 | /**
118 | * Return the plugin.
119 | *
120 | * @type {Object}
121 | */
122 |
123 | return {
124 | onSelect,
125 | onKeyDown,
126 | schema: getSchema()
127 | }
128 | }
129 |
130 | /**
131 | * Export.
132 | *
133 | * @type {Function}
134 | */
135 |
136 | export default AutoWrapBlock
137 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "slate-wrapped-block-plugin",
3 | "description": "A Slate plugin that auto wrap selected block with some custom block",
4 | "version": "0.2.0",
5 | "license": "MIT",
6 | "repository": "git://github.com/oozou/slate-wrapped-block-plugin.git",
7 | "main": "./dist/index.js",
8 | "scripts": {
9 | "clean": "rm -rf ./dist ./node_modules",
10 | "dist": "babel ./lib --out-dir ./dist",
11 | "example": "browserify ./example/index.js --debug --transform babelify > ./example/build.js",
12 | "gh-pages": "npm run dist && npm run example && gh-pages --dist ./example",
13 | "lint": "eslint 'lib/**/*.js' 'example/index.js'",
14 | "prepublish": "npm run dist",
15 | "start": "http-server ./example -p 8888",
16 | "test": "npm run lint",
17 | "watch": "npm-run-all --parallel --print-label watch:dist watch:example start",
18 | "watch:dist": "babel ./lib --out-dir ./dist --watch",
19 | "watch:example": "watchify ./example/index.js --debug --transform babelify --outfile ./example/build.js"
20 | },
21 | "peerDependencies": {
22 | "slate": "*"
23 | },
24 | "devDependencies": {
25 | "babel-cli": "^6.10.1",
26 | "babel-eslint": "^6.1.2",
27 | "babel-preset-es2015": "^6.9.0",
28 | "babel-preset-react": "^6.11.1",
29 | "babel-preset-stage-0": "^6.5.0",
30 | "babelify": "^7.3.0",
31 | "browserify": "^13.0.1",
32 | "eslint": "^3.0.1",
33 | "eslint-plugin-import": "^1.10.2",
34 | "eslint-plugin-react": "^5.2.2",
35 | "gh-pages": "^0.11.0",
36 | "http-server": "^0.9.0",
37 | "npm-run-all": "^2.3.0",
38 | "react": "^15.4.2",
39 | "react-dom": "^15.4.2",
40 | "slate": "^0.16.10",
41 | "watchify": "^3.7.0"
42 | },
43 | "keywords": [
44 | "slate"
45 | ]
46 | }
47 |
--------------------------------------------------------------------------------