forked from facebookarchive/draft-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
editorKey
prop for SSR (facebookarchive#796)
* add editorKey functionnality * add simple tests for editorKey functionality * fix flow error * minimal working error case for universal rendering adds a small express-backed universal react app that only accepts `/` requests but that exhibits a client/server mismatch issue * fix failing test when length of generated hash is small * Update universal example since this PR was created some changes were made and this updates the example to fit with our other examples. - moves into the version specific subdirectories - updates package.json - removes some ES6 destucturing temporarily because polyfill seems to be not loading correctly
- Loading branch information
Showing
17 changed files
with
371 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{ "presets": ["react"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
static | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* Copyright (c) 2013-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @providesModule Draft | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var React = require('react'); | ||
var ReactDom = require('react-dom'); | ||
|
||
var SimpleEditor = require('./editor.js').SimpleEditor; | ||
|
||
ReactDom.render(<SimpleEditor />, document.getElementById('react-content')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* Copyright (c) 2013-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @providesModule Draft | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const Draft = require('draft-js'); | ||
const React = require('react'); | ||
|
||
class SimpleEditor extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.state = {editorState: Draft.EditorState.createWithContent(emptyContentState)}; | ||
this.onChange = (editorState) => this.setState({editorState}); | ||
} | ||
render() { | ||
const Editor = Draft.Editor; | ||
const editorState = this.state.editorState; | ||
return <Editor placeholder="heyyyyy" editorKey="foobaz" editorState={editorState} onChange={this.onChange} />; | ||
} | ||
} | ||
module.exports = { | ||
SimpleEditor: SimpleEditor, | ||
}; | ||
|
||
const emptyContentState = Draft.convertFromRaw({ | ||
entityMap: {}, | ||
blocks: [ | ||
{ | ||
text: '', | ||
key: 'foo', | ||
type: 'unstyled', | ||
entityRanges: [], | ||
}, | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* Copyright (c) 2013-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @providesModule Draft | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var React = require('react'); | ||
var ReactDOMServer = require('react-dom/server'); | ||
|
||
var SimpleEditor = require('./editor.js').SimpleEditor; | ||
|
||
var express = require('express'); | ||
|
||
var app = express(); | ||
|
||
app.use('/static', express.static('static')); | ||
|
||
app.get('/', (req, res) => { | ||
const rendered = ReactDOMServer.renderToString(<SimpleEditor />); | ||
const page = `<!doctype html> | ||
<html> | ||
<body> | ||
<div id="react-content">${ rendered }</div> | ||
<script src="/static/bundle.js"></script> | ||
</body> | ||
</html> | ||
`; | ||
res.send(page); | ||
}); | ||
|
||
app.listen(3003); | ||
console.log('app now listening at http://localhost:3003'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "universal", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"license": "MIT", | ||
"dependencies": { | ||
"draft-js": "file:../../../", | ||
"express": "^4.14.0", | ||
"immutable": "^3.8.1", | ||
"react": "^15.4.1", | ||
"react-dom": "^15.4.1" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.18.0", | ||
"babel-preset-react": "^6.16.0", | ||
"babelify": "^7.3.0", | ||
"browserify": "^13.1.1" | ||
}, | ||
"scripts": { | ||
"build": "mkdir -p static; browserify client.js -t babelify -o static/bundle.js", | ||
"start": "babel-node index.js", | ||
"demo": "npm run build && npm run start" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Universal rendering | ||
|
||
Draft is well suited for universal (isomorphic) rendering contexts: | ||
|
||
Here, we have three files: | ||
|
||
* editor.js | ||
A simple draftjs editor exported as `<SimpleEditor />` | ||
* client.js | ||
A simple clientside entrypoint that clientside renders the index page route's logic into a `#react-content` div. | ||
* index.js | ||
A simple express server that prerenders a <SimpleEditor /> in the `#react-content` div | ||
|
||
you can run this by first building draft-js and then installing this demo's dependencies | ||
|
||
```bash | ||
# in draft-js folder | ||
yarn | ||
pushd examples/universal | ||
yarn | ||
``` | ||
|
||
then, run | ||
|
||
`npm run demo` | ||
|
||
which will open a server listening on [http://localhost:3003](http://localhost:3003) | ||
|
||
visiting the main route will return a simple unstyled draft editor but the client, on re-rendering the `<SimpleEditor />` will complain that there is a mismatch between the server rendered content and the client-rendered content. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{ "presets": ["react"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
static | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* Copyright (c) 2013-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @providesModule Draft | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var React = require('react'); | ||
var ReactDom = require('react-dom'); | ||
|
||
var SimpleEditor = require('./editor.js').SimpleEditor; | ||
|
||
ReactDom.render(<SimpleEditor />, document.getElementById('react-content')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* Copyright (c) 2013-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @providesModule Draft | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const Draft = require('draft-js'); | ||
const React = require('react'); | ||
|
||
class SimpleEditor extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.state = {editorState: Draft.EditorState.createWithContent(emptyContentState)}; | ||
this.onChange = (editorState) => this.setState({editorState}); | ||
} | ||
render() { | ||
const Editor = Draft.Editor; | ||
const editorState = this.state.editorState; | ||
return <Editor placeholder="heyyyyy" editorKey="foobaz" editorState={editorState} onChange={this.onChange} />; | ||
} | ||
} | ||
module.exports = { | ||
SimpleEditor: SimpleEditor, | ||
}; | ||
|
||
const emptyContentState = Draft.convertFromRaw({ | ||
entityMap: {}, | ||
blocks: [ | ||
{ | ||
text: '', | ||
key: 'foo', | ||
type: 'unstyled', | ||
entityRanges: [], | ||
}, | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* Copyright (c) 2013-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @providesModule Draft | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var React = require('react'); | ||
var ReactDOMServer = require('react-dom/server'); | ||
|
||
var SimpleEditor = require('./editor.js').SimpleEditor; | ||
|
||
var express = require('express'); | ||
|
||
var app = express(); | ||
|
||
app.use('/static', express.static('static')); | ||
|
||
app.get('/', (req, res) => { | ||
const rendered = ReactDOMServer.renderToString(<SimpleEditor />); | ||
const page = `<!doctype html> | ||
<html> | ||
<body> | ||
<div id="react-content">${ rendered }</div> | ||
<script src="/static/bundle.js"></script> | ||
</body> | ||
</html> | ||
`; | ||
res.send(page); | ||
}); | ||
|
||
app.listen(3003); | ||
console.log('app now listening at http://localhost:3003'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "universal", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"license": "MIT", | ||
"dependencies": { | ||
"draft-js": "file:../../../", | ||
"express": "^4.14.0", | ||
"immutable": "^3.8.1", | ||
"react": "^15.4.1", | ||
"react-dom": "^15.4.1" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.18.0", | ||
"babel-preset-react": "^6.16.0", | ||
"babelify": "^7.3.0", | ||
"browserify": "^13.1.1" | ||
}, | ||
"scripts": { | ||
"build": "mkdir -p static; browserify client.js -t babelify -o static/bundle.js", | ||
"start": "babel-node index.js", | ||
"demo": "npm run build && npm run start" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Universal rendering | ||
|
||
Draft is well suited for universal (isomorphic) rendering contexts: | ||
|
||
Here, we have three files: | ||
|
||
* editor.js | ||
A simple draftjs editor exported as `<SimpleEditor />` | ||
* client.js | ||
A simple clientside entrypoint that clientside renders the index page route's logic into a `#react-content` div. | ||
* index.js | ||
A simple express server that prerenders a <SimpleEditor /> in the `#react-content` div | ||
|
||
you can run this by first building draft-js and then installing this demo's dependencies | ||
|
||
```bash | ||
# in draft-js folder | ||
yarn | ||
pushd examples/universal | ||
yarn | ||
``` | ||
|
||
then, run | ||
|
||
`npm run demo` | ||
|
||
which will open a server listening on [http://localhost:3003](http://localhost:3003) | ||
|
||
visiting the main route will return a simple unstyled draft editor but the client, on re-rendering the `<SimpleEditor />` will complain that there is a mismatch between the server rendered content and the client-rendered content. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.