@@ -58,6 +126,12 @@ class HoveringMenu extends React.Component {
)
}
+ /**
+ * Render the hovering menu.
+ *
+ * @return {Element}
+ */
+
renderMenu = () => {
const { state } = this.state
const isOpen = state.isExpanded && state.isFocused
@@ -73,6 +147,14 @@ class HoveringMenu extends React.Component {
)
}
+ /**
+ * Render a mark-toggling toolbar button.
+ *
+ * @param {String} type
+ * @param {String} icon
+ * @return {Element}
+ */
+
renderMarkButton = (type, icon) => {
const isActive = this.hasMark(type)
const onMouseDown = e => this.onClickMark(e, type)
@@ -84,6 +166,12 @@ class HoveringMenu extends React.Component {
)
}
+ /**
+ * Render the Slate editor.
+ *
+ * @return {Element}
+ */
+
renderEditor = () => {
return (
@@ -96,10 +184,21 @@ class HoveringMenu extends React.Component {
)
}
+ /**
+ * Return a mark renderer for a Slate `mark`.
+ *
+ * @param {Mark} mark
+ * @return {Object or Void}
+ */
+
renderMark = (mark) => {
return MARKS[mark.type]
}
+ /**
+ * Update the menu's absolute position.
+ */
+
updateMenu = () => {
const { menu, state } = this.state
if (!menu) return
@@ -115,32 +214,6 @@ class HoveringMenu extends React.Component {
menu.style.left = `${rect.left + window.scrollX - menu.offsetWidth / 2 + rect.width / 2}px`
}
- hasMark = (type) => {
- const { state } = this.state
- return state.marks.some(mark => mark.type == type)
- }
-
- onChange = (state) => {
- this.setState({ state })
- }
-
- onClickMark = (e, type) => {
- e.preventDefault()
- const isActive = this.hasMark(type)
- let { state } = this.state
-
- state = state
- .transform()
- [isActive ? 'unmark' : 'mark'](type)
- .apply()
-
- this.setState({ state })
- }
-
- onOpen = (el) => {
- this.setState({ menu: el.firstChild })
- }
-
}
/**
diff --git a/examples/images/index.js b/examples/images/index.js
index f3834bdf2..a62ef5019 100644
--- a/examples/images/index.js
+++ b/examples/images/index.js
@@ -6,7 +6,7 @@ import initialState from './state.json'
import { Map } from 'immutable'
/**
- * Node renderers.
+ * Define a set of node renderers.
*
* @type {Object}
*/
@@ -29,6 +29,12 @@ const NODES = {
class Images extends React.Component {
+ /**
+ * Deserialize the raw initial state.
+ *
+ * @type {Object}
+ */
+
state = {
state: Raw.deserialize(initialState)
};
diff --git a/examples/links/index.js b/examples/links/index.js
index 13e335793..ae15aae07 100644
--- a/examples/links/index.js
+++ b/examples/links/index.js
@@ -6,7 +6,7 @@ import initialState from './state.json'
import { Map } from 'immutable'
/**
- * Node renderers.
+ * Define a set of node renderers.
*
* @type {Object}
*/
@@ -28,6 +28,12 @@ const NODES = {
class Links extends React.Component {
+ /**
+ * Deserialize the raw initial state.
+ *
+ * @type {Object}
+ */
+
state = {
state: Raw.deserialize(initialState)
};
diff --git a/examples/paste-html/index.js b/examples/paste-html/index.js
index e487f7fff..1fd20fca5 100644
--- a/examples/paste-html/index.js
+++ b/examples/paste-html/index.js
@@ -4,7 +4,7 @@ import React from 'react'
import initialState from './state.json'
/**
- * Node renderers.
+ * Define a set of node renderers.
*
* @type {Object}
*/
@@ -29,7 +29,7 @@ const NODES = {
}
/**
- * Mark renderers.
+ * Define a set of mark renderers.
*
* @type {Object}
*/
@@ -157,17 +157,58 @@ const RULES = [
const serializer = new Html(RULES)
/**
- * The rich text example.
+ * The pasting html example.
*
* @type {Component}
*/
class PasteHtml extends React.Component {
+ /**
+ * Deserialize the raw initial state.
+ *
+ * @type {Object}
+ */
+
state = {
state: Raw.deserialize(initialState)
};
+ /**
+ * On change, save the new state.
+ *
+ * @param {State} state
+ */
+
+ onChange = (state) => {
+ this.setState({ state })
+ }
+
+ /**
+ * On paste, deserialize the HTML and then insert the fragment.
+ *
+ * @param {Event} e
+ * @param {Object} paste
+ * @param {State} state
+ */
+
+ onPaste = (e, paste, state) => {
+ if (paste.type != 'html') return
+ const { html } = paste
+ const { document } = serializer.deserialize(html)
+
+ return state
+ .transform()
+ .insertFragment(document)
+ .apply()
+ }
+
+ /**
+ * Render.
+ *
+ * @return {Component}
+ */
+
render = () => {
return (
@@ -182,29 +223,28 @@ class PasteHtml extends React.Component {
)
}
+ /**
+ * Return a node renderer for a Slate `node`.
+ *
+ * @param {Node} node
+ * @return {Component or Void}
+ */
+
renderNode = (node) => {
return NODES[node.type]
}
+ /**
+ * Return a mark renderer for a Slate `mark`.
+ *
+ * @param {Mark} mark
+ * @return {Object or Void}
+ */
+
renderMark = (mark) => {
return MARKS[mark.type]
}
- onChange = (state) => {
- this.setState({ state })
- }
-
- onPaste = (e, paste, state, editor) => {
- if (paste.type != 'html') return
- const { html } = paste
- const { document } = serializer.deserialize(html)
-
- return state
- .transform()
- .insertFragment(document)
- .apply()
- }
-
}
/**
diff --git a/examples/rich-text/index.js b/examples/rich-text/index.js
index 74128a8c4..cf4f4a165 100644
--- a/examples/rich-text/index.js
+++ b/examples/rich-text/index.js
@@ -5,7 +5,7 @@ import initialState from './state.json'
import keycode from 'keycode'
/**
- * Defin a set of node renderers.
+ * Define a set of node renderers.
*
* @type {Object}
*/
diff --git a/examples/tables/index.js b/examples/tables/index.js
index 6e0056e47..2c8a27b41 100644
--- a/examples/tables/index.js
+++ b/examples/tables/index.js
@@ -5,7 +5,7 @@ import initialState from './state.json'
import keycode from 'keycode'
/**
- * Node renderers.
+ * Define a set of node renderers.
*
* @type {Object}
*/
@@ -17,7 +17,7 @@ const NODES = {
}
/**
- * Mark renderers.
+ * Define a set of mark renderers.
*
* @type {Object}
*/
@@ -46,75 +46,6 @@ class Tables extends React.Component {
state: Raw.deserialize(initialState)
};
- /**
- * Render the example.
- *
- * @return {Component} component
- */
-
- render = () => {
- return (
-
-
-
- )
- }
-
- /**
- * Render a `node`.
- *
- * @param {Node} node
- * @return {Element}
- */
-
- renderNode = (node) => {
- return NODES[node.type]
- }
-
- /**
- * Render a `mark`.
- *
- * @param {Mark} mark
- * @return {Element}
- */
-
- renderMark = (mark) => {
- return MARKS[mark.type]
- }
-
- /**
- * On change.
- *
- * @param {State} state
- */
-
- onChange = (state) => {
- this.setState({ state })
- }
-
- /**
- * On key down, check for our specific key shortcuts.
- *
- * @param {Event} e
- * @param {State} state
- * @return {State or Null} state
- */
-
- onKeyDown = (e, state) => {
- if (state.startBlock.type != 'table-cell') return
- switch (keycode(e.which)) {
- case 'backspace': return this.onBackspace(e, state)
- case 'delete': return this.onDelete(e, state)
- case 'enter': return this.onEnter(e, state)
- }
- }
-
/**
* On backspace, do nothing if at the start of a table cell.
*
@@ -129,6 +60,16 @@ class Tables extends React.Component {
return state
}
+ /**
+ * On change.
+ *
+ * @param {State} state
+ */
+
+ onChange = (state) => {
+ this.setState({ state })
+ }
+
/**
* On delete, do nothing if at the end of a table cell.
*
@@ -156,6 +97,65 @@ class Tables extends React.Component {
return state
}
+ /**
+ * On key down, check for our specific key shortcuts.
+ *
+ * @param {Event} e
+ * @param {State} state
+ * @return {State or Null} state
+ */
+
+ onKeyDown = (e, state) => {
+ if (state.startBlock.type != 'table-cell') return
+ switch (keycode(e.which)) {
+ case 'backspace': return this.onBackspace(e, state)
+ case 'delete': return this.onDelete(e, state)
+ case 'enter': return this.onEnter(e, state)
+ }
+ }
+
+ /**
+ * Render the example.
+ *
+ * @return {Component} component
+ */
+
+ render = () => {
+ return (
+
+
+
+ )
+ }
+
+ /**
+ * Return a node renderer for a Slate `node`.
+ *
+ * @param {Node} node
+ * @return {Component or Void}
+ */
+
+ renderNode = (node) => {
+ return NODES[node.type]
+ }
+
+ /**
+ * Return a mark renderer for a Slate `mark`.
+ *
+ * @param {Mark} mark
+ * @return {Object or Void}
+ */
+
+ renderMark = (mark) => {
+ return MARKS[mark.type]
+ }
+
}
/**