From 7435c4019cd0417706d8561fa9dbe150623bb142 Mon Sep 17 00:00:00 2001 From: Ian Storm Taylor Date: Sat, 18 Jun 2016 23:54:08 -0700 Subject: [PATCH] added start of richtext example, refactor range grouping logic --- Makefile | 8 +++ examples/basic/index.css | 12 ++++ examples/basic/index.js | 78 ++++++++++----------- examples/richtext/index.css | 20 ++++++ examples/richtext/index.html | 11 +++ examples/richtext/index.js | 128 +++++++++++++++++++++++++++++++++++ lib/components/leaf.js | 24 +++---- lib/components/text.js | 26 ++++--- lib/serializers/raw.js | 27 ++------ lib/utils/group-by-marks.js | 48 +++++++++++++ 10 files changed, 298 insertions(+), 84 deletions(-) create mode 100644 examples/richtext/index.css create mode 100644 examples/richtext/index.html create mode 100644 examples/richtext/index.js create mode 100644 lib/utils/group-by-marks.js diff --git a/Makefile b/Makefile index b1ceb5bf3..93158a0e1 100644 --- a/Makefile +++ b/Makefile @@ -35,6 +35,10 @@ example-basic: ./node_modules example-plaintext: ./node_modules @ $(browserify) --debug --transform babelify --outfile ./examples/plaintext/build.js ./examples/plaintext/index.js +# Build the richtext example. +example-richtext: ./node_modules + @ $(browserify) --debug --transform babelify --outfile ./examples/richtext/build.js ./examples/richtext/index.js + # Lint the sources files with Standard JS. lint: ./node_modules @ $(standard) ./lib @@ -67,6 +71,10 @@ watch-example-basic: ./node_modules watch-example-plaintext: ./node_modules @ $(MAKE) example-plaintext browserify=$(watchify) +# Watch the richtext example. +watch-example-richtext: ./node_modules + @ $(MAKE) example-richtext browserify=$(watchify) + # Phony targets. .PHONY: examples .PHONY: test diff --git a/examples/basic/index.css b/examples/basic/index.css index 496f2a2ea..3a4e7ef3c 100644 --- a/examples/basic/index.css +++ b/examples/basic/index.css @@ -10,3 +10,15 @@ main { max-width: 40em; margin: 0 auto; } + +p { + margin: 0; +} + +pre { + margin: 0; +} + +main * + * { + margin-top: 1em; +} diff --git a/examples/basic/index.js b/examples/basic/index.js index bfc1cdfc4..fd6446c15 100644 --- a/examples/basic/index.js +++ b/examples/basic/index.js @@ -49,45 +49,6 @@ const state = { ] } -/** - * Renderers. - */ - -function renderNode(node) { - switch (node.type) { - case 'code': { - return (props) => { - return ( -
-            
-              {props.children}
-            
-          
- ) - } - } - case 'paragraph': { - return (props) => { - return ( -

- {props.children} -

- ) - } - } - } -} - -function renderMark(mark) { - switch (mark.type) { - case 'bold': { - return { - fontWeight: 'bold' - } - } - } -} - /** * App. */ @@ -101,8 +62,8 @@ class App extends React.Component { render() { return ( this.renderNode(node)} + renderMark={mark => this.renderMark(mark)} state={this.state.state} onChange={(state) => { console.log('State:', state.toJS()) @@ -113,6 +74,41 @@ class App extends React.Component { ) } + renderNode(node) { + switch (node.type) { + case 'code': { + return (props) => { + return ( +
+              
+                {props.children}
+              
+            
+ ) + } + } + case 'paragraph': { + return (props) => { + return ( +

+ {props.children} +

+ ) + } + } + } + } + + renderMark(mark) { + switch (mark.type) { + case 'bold': { + return { + fontWeight: 'bold' + } + } + } + } + } /** diff --git a/examples/richtext/index.css b/examples/richtext/index.css new file mode 100644 index 000000000..f19f1716a --- /dev/null +++ b/examples/richtext/index.css @@ -0,0 +1,20 @@ + +html { + background: #eee; + padding: 20px; +} + +main { + background: #fff; + padding: 10px; + max-width: 40em; + margin: 0 auto; +} + +p { + margin: 0; +} + +p + p { + margin-top: 1em; +} diff --git a/examples/richtext/index.html b/examples/richtext/index.html new file mode 100644 index 000000000..b84581191 --- /dev/null +++ b/examples/richtext/index.html @@ -0,0 +1,11 @@ + + + + Editor | Richtext Example + + + +
+ + + diff --git a/examples/richtext/index.js b/examples/richtext/index.js new file mode 100644 index 000000000..9bf451a08 --- /dev/null +++ b/examples/richtext/index.js @@ -0,0 +1,128 @@ + +import Editor from '../..' +import React from 'react' +import ReactDOM from 'react-dom' +import { Raw } from '../..' + +/** + * State. + */ + +const state = { + nodes: [ + { + type: 'paragraph', + nodes: [ + { + type: 'text', + ranges: [ + { + text: 'This is ' + }, + { + text: 'editable', + marks: [ + { + type: 'italic' + } + ] + }, + { + text: ' ' + }, + { + text: 'rich', + marks: [ + { + type: 'bold' + } + ] + }, + { + text: ' text, much better than a ' + }, + { + text: '