diff --git a/examples/focus-blur/Readme.md b/examples/focus-blur/Readme.md new file mode 100644 index 000000000..04b913be4 --- /dev/null +++ b/examples/focus-blur/Readme.md @@ -0,0 +1,8 @@ + +# Links Example + +![](../../docs/images/links-example.png) + +This example shows you how you can wrap text in "inline" nodes to associate metadata, like an `href`, with a piece of text. This is how you'd add links to Slate, but it's also how you might add hashtags, at-mentions, and many more inline features! + +Check out the [Examples readme](..) to see how to run it! diff --git a/examples/focus-blur/index.js b/examples/focus-blur/index.js new file mode 100644 index 000000000..311be90d6 --- /dev/null +++ b/examples/focus-blur/index.js @@ -0,0 +1,145 @@ + +import { Editor, Mark, Raw } from '../..' +import React from 'react' +import ReactDOM from 'react-dom' +import initialState from './state.json' +import isUrl from 'is-url' +import { Map } from 'immutable' + +/** + * Define a schema. + * + * @type {Object} + */ + +const schema = { + nodes: { + paragraph: props =>

{props.children}

+ } +} + +/** + * The focus and blur example. + * + * @type {Component} + */ + +class FocusBlur extends React.Component { + + /** + * Deserialize the raw initial state. + * + * @type {Object} + */ + + state = { + state: Raw.deserialize(initialState, { terse: true }) + }; + + /** + * On change. + * + * @param {State} state + */ + + onChange = (state) => { + this.setState({ state }) + } + + /** + * Apply a focus or blur transform by `name` after a `timeout`. + * + * @param {String} name + * @param {Number} timeout + */ + + onClick = (e, name, timeout = 0) => { + e.preventDefault() + + setTimeout(() => { + const state = this.state.state + .transform() + [name]() + .apply() + + this.setState({ state }) + }, timeout) + } + + /** + * Generate focus and blur button handlers. + * + * @param {Event} e + */ + + onClickFocus = e => this.onClick(e, 'focus') + onClickFocusDelay = e => this.onClick(e, 'focus', 3000) + onClickBlur = e => this.onClick(e, 'blur') + onClickBlurDelay = e => this.onClick(e, 'blur', 3000) + + /** + * Render the app. + * + * @return {Element} element + */ + + render = () => { + return ( +
+ {this.renderToolbar()} + {this.renderEditor()} +
+ ) + } + + /** + * Render the toolbar. + * + * @return {Element} element + */ + + renderToolbar = () => { + return ( +
+ + done Focus + + + timer Focus + + + done Blur + + + timer Blur + +
+ ) + } + + /** + * Render the editor. + * + * @return {Element} element + */ + + renderEditor = () => { + return ( +
+ +
+ ) + } + +} + +/** + * Export. + */ + +export default FocusBlur diff --git a/examples/focus-blur/state.json b/examples/focus-blur/state.json new file mode 100644 index 000000000..7338c109b --- /dev/null +++ b/examples/focus-blur/state.json @@ -0,0 +1,24 @@ +{ + "nodes": [ + { + "kind": "block", + "type": "paragraph", + "nodes": [ + { + "kind": "text", + "text": "This is a testing ground for focusing and blurring in Slate." + } + ] + }, + { + "kind": "block", + "type": "paragraph", + "nodes": [ + { + "kind": "text", + "text": "You can use the toolbar buttons above to focus and blur immediately, or after a short delay." + } + ] + } + ] +} diff --git a/examples/index.css b/examples/index.css index ce2ab9406..94ddda65f 100644 --- a/examples/index.css +++ b/examples/index.css @@ -79,6 +79,7 @@ input:focus { .material-icons { font-size: 18px; + vertical-align: text-bottom; } /** @@ -126,7 +127,7 @@ input:focus { } .menu > * + * { - margin-left: 10px; + margin-left: 15px; } .button { diff --git a/examples/index.js b/examples/index.js index cb938132b..5b88ab660 100644 --- a/examples/index.js +++ b/examples/index.js @@ -11,6 +11,7 @@ import AutoMarkdown from './auto-markdown' import CodeHighlighting from './code-highlighting' import Embeds from './embeds' import Emojis from './emojis' +import FocusBlur from './focus-blur' import HoveringMenu from './hovering-menu' import Iframes from './iframes' import Images from './images' @@ -81,6 +82,7 @@ class App extends React.Component { {this.renderTab('RTL', 'rtl')} {this.renderTab('Plugins', 'plugins')} {this.renderTab('Iframes', 'iframes')} + {this.renderTab('Focus & Blur', 'focus-blur')} ) } @@ -129,6 +131,7 @@ const router = ( +