forked from ianstormtaylor/slate
-
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.
broke lots of stuff, but added tests
- Loading branch information
1 parent
b42fd18
commit 74cab69
Showing
241 changed files
with
4,149 additions
and
323 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
|
||
html { | ||
background: #eee; | ||
padding: 20px; | ||
} | ||
|
||
main { | ||
background: #fff; | ||
padding: 10px; | ||
max-width: 40em; | ||
margin: 0 auto; | ||
} | ||
|
||
p { | ||
margin: 0; | ||
} | ||
|
||
.editor > * > * + * { | ||
margin-top: 1em; | ||
} | ||
|
||
.menu { | ||
margin: 0 -10px; | ||
padding: 1px 0 9px 8px; | ||
border-bottom: 2px solid #eee; | ||
margin-bottom: 10px; | ||
} | ||
|
||
.menu > * { | ||
display: inline-block; | ||
} | ||
|
||
.menu > * + * { | ||
margin-left: 10px; | ||
} | ||
|
||
.button { | ||
color: #ccc; | ||
cursor: pointer; | ||
} | ||
|
||
.button[data-active="true"] { | ||
color: black; | ||
} | ||
|
||
.material-icons { | ||
font-size: 18px; | ||
} |
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,12 @@ | ||
<html> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title>Editor | Links Example</title> | ||
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons"> | ||
<link rel="stylesheet" href="index.css"> | ||
</head> | ||
<body> | ||
<main></main> | ||
<script src="build.js"></script> | ||
</body> | ||
</html> |
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,159 @@ | ||
|
||
import Editor, { Mark, Raw } from '../..' | ||
import React from 'react' | ||
import ReactDOM from 'react-dom' | ||
import state from './state.json' | ||
import { Map } from 'immutable' | ||
|
||
/** | ||
* App. | ||
*/ | ||
|
||
class App extends React.Component { | ||
|
||
state = { | ||
state: Raw.deserialize(state) | ||
}; | ||
|
||
/** | ||
* Check whether the current selection has a link in it. | ||
* | ||
* @return {Boolean} hasLinks | ||
*/ | ||
|
||
hasLinks() { | ||
let { state } = this.state | ||
const { currentInlineNodes } = state | ||
const hasLinks = currentInlineNodes.some(inline => inline.type == 'link') | ||
return hasLinks | ||
} | ||
|
||
/** | ||
* When clicking a link, if the selection has a link in it, remove the link. | ||
* Otherwise, add a new link with an href and text. | ||
* | ||
* @param {Event} e | ||
*/ | ||
|
||
onClickLink(e) { | ||
e.preventDefault() | ||
let { state } = this.state | ||
const hasLinks = this.hasLinks() | ||
|
||
if (hasLinks) { | ||
state = state | ||
.transform() | ||
.unwrapInline('link') | ||
.apply() | ||
} | ||
|
||
else if (state.isCurrentlyExpanded) { | ||
// const href = window.prompt('Enter the URL of the link:') | ||
state = state | ||
.transform() | ||
.wrapInline('link', new Map({ href: 'https://google.com' })) | ||
.apply() | ||
} | ||
|
||
else { | ||
const href = window.prompt('Enter the URL of the link:') | ||
const text = window.prompt('Enter the text for the link:') | ||
state = state | ||
.transform() | ||
.insertText(text) | ||
.extendBackward(text.length) | ||
.wrapInline('link', new Map({ href })) | ||
.apply() | ||
} | ||
|
||
this.setState({ state }) | ||
} | ||
|
||
/** | ||
* Render the app. | ||
* | ||
* @return {Component} component | ||
*/ | ||
|
||
render() { | ||
return ( | ||
<div> | ||
{this.renderToolbar()} | ||
{this.renderEditor()} | ||
</div> | ||
) | ||
} | ||
|
||
/** | ||
* Render the toolbar. | ||
* | ||
* @return {Component} component | ||
*/ | ||
|
||
renderToolbar() { | ||
const hasLinks = this.hasLinks() | ||
return ( | ||
<div className="menu"> | ||
<span className="button" onMouseDown={e => this.onClickLink(e)} data-active={hasLinks}> | ||
<span className="material-icons">link</span> | ||
</span> | ||
</div> | ||
) | ||
} | ||
|
||
/** | ||
* Render the editor. | ||
* | ||
* @return {Component} component | ||
*/ | ||
|
||
renderEditor() { | ||
return ( | ||
<div className="editor"> | ||
<Editor | ||
state={this.state.state} | ||
renderNode={node => this.renderNode(node)} | ||
onChange={(state) => { | ||
console.groupCollapsed('Change!') | ||
console.log('Document:', state.document.toJS()) | ||
console.log('Selection:', state.selection.toJS()) | ||
console.log('Content:', Raw.serialize(state)) | ||
console.groupEnd() | ||
this.setState({ state }) | ||
}} | ||
/> | ||
</div> | ||
) | ||
} | ||
|
||
/** | ||
* Render our custom `node`. | ||
* | ||
* @param {Node} node | ||
* @return {Component} component | ||
*/ | ||
|
||
renderNode(node) { | ||
switch (node.type) { | ||
case 'link': { | ||
return (props) => { | ||
const { data } = props.node | ||
const href = data.get('href') | ||
return <a href={href}>{props.children}</a> | ||
} | ||
} | ||
case 'paragraph': { | ||
return (props) => <p>{props.children}</p> | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Attach. | ||
*/ | ||
|
||
const app = <App /> | ||
const root = document.body.querySelector('main') | ||
ReactDOM.render(app, root) |
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,57 @@ | ||
{ | ||
"nodes": [ | ||
{ | ||
"kind": "block", | ||
"type": "paragraph", | ||
"nodes": [ | ||
{ | ||
"kind": "text", | ||
"ranges": [ | ||
{ | ||
"text": "In addition to block nodes, you can create inline nodes, like " | ||
}, | ||
] | ||
}, | ||
{ | ||
"kind": "inline", | ||
"type": "link", | ||
"data": { | ||
"href": "https://en.wikipedia.org/wiki/Hypertext" | ||
}, | ||
"nodes": [ | ||
{ | ||
"kind": "text", | ||
"ranges": [ | ||
{ | ||
"text": "hyperlinks" | ||
}, | ||
] | ||
}, | ||
] | ||
}, | ||
{ | ||
"kind": "text", | ||
"ranges": [ | ||
{ | ||
"text": "!" | ||
}, | ||
] | ||
} | ||
] | ||
}, | ||
{ | ||
"kind": "block", | ||
"type": "paragraph", | ||
"nodes": [ | ||
{ | ||
"kind": "text", | ||
"ranges": [ | ||
{ | ||
"text": "This example shows hyperlinks in action. It features two ways to add links. You can either add a link via the toolbar icon above, or if you want in on a little secret, copy a URL to your keyboard and paste it while a range of text is selected." | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] | ||
} |
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
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.