Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add controller #2221

Merged
merged 38 commits into from
Oct 9, 2018
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
3b7f78f
fold Stack into Editor
ianstormtaylor Sep 28, 2018
cc76251
switch Change objects to be tied to editors, not values
ianstormtaylor Sep 28, 2018
e171605
introduce controller
ianstormtaylor Sep 28, 2018
9bf23b4
add the "commands" concept
ianstormtaylor Sep 28, 2018
4a8f37c
convert history into commands on `value.data`
ianstormtaylor Sep 28, 2018
fa42b41
add the ability to not normalize on editor creation/setting
ianstormtaylor Sep 29, 2018
0ab73e0
convert schema to a mutable constructor
ianstormtaylor Sep 29, 2018
4ca15bd
add editor.command method
ianstormtaylor Sep 29, 2018
ee9e248
convert plugin handlers to receive `next`
ianstormtaylor Oct 1, 2018
bba64a6
switch commands to use the onCommand middleware
ianstormtaylor Oct 1, 2018
fc3fe3e
add queries support, convert schema to queries
ianstormtaylor Oct 2, 2018
662bd71
split out browser plugin
ianstormtaylor Oct 2, 2018
4eaf140
remove noop util
ianstormtaylor Oct 2, 2018
6014a0c
fixes
ianstormtaylor Oct 2, 2018
2400ab1
merge master
ianstormtaylor Oct 2, 2018
3bbea2d
fixes
ianstormtaylor Oct 3, 2018
28a15c6
start fixing tests, refactor hyperscript to be more literal
ianstormtaylor Oct 3, 2018
d16080b
fix slate-html-serializer tests
ianstormtaylor Oct 3, 2018
1870acc
fix schema tests with hyperscript
ianstormtaylor Oct 4, 2018
90137b2
fix text model tests with hyperscript
ianstormtaylor Oct 4, 2018
4127cd9
fix more tests
ianstormtaylor Oct 4, 2018
f19b1bb
get all tests passing
ianstormtaylor Oct 4, 2018
2d43220
fix lint
ianstormtaylor Oct 4, 2018
d45b2ab
undo decorations example update
ianstormtaylor Oct 4, 2018
7e0e2af
update examples
ianstormtaylor Oct 5, 2018
28a7b72
small changes to the api to make it nicer
ianstormtaylor Oct 5, 2018
dbf5531
update docs
ianstormtaylor Oct 8, 2018
2c1762f
update commands/queries plugin logic
ianstormtaylor Oct 9, 2018
12a1fbd
change normalizeNode and validateNode to be middleware
ianstormtaylor Oct 9, 2018
5806fe8
fix decoration removal
ianstormtaylor Oct 9, 2018
ec0c73c
rename commands tests
ianstormtaylor Oct 9, 2018
fc9d145
add useful errors to existing APIs
ianstormtaylor Oct 9, 2018
11785e3
update changelogs
ianstormtaylor Oct 9, 2018
942978a
merge master
ianstormtaylor Oct 9, 2018
bb030be
cleanup
ianstormtaylor Oct 9, 2018
79daf81
fixes
ianstormtaylor Oct 9, 2018
d125387
update docs
ianstormtaylor Oct 9, 2018
72ebb39
add editor docs
ianstormtaylor Oct 9, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions examples/code-highlighting/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,9 @@ class CodeHighlighting extends React.Component {
* @return {Array}
*/

decorateNode = node => {
if (node.type != 'code') return
decorateNode = (node, next) => {
const others = next()
if (node.type != 'code') return others

const language = node.data.get('language')
const texts = node.getTexts().toArray()
Expand Down Expand Up @@ -245,7 +246,7 @@ class CodeHighlighting extends React.Component {
start = end
}

return decorations
return [...others, ...decorations]
}
}

Expand Down
33 changes: 21 additions & 12 deletions examples/emojis/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ class Emojis extends React.Component {
},
}

/**
* Store a reference to the `editor`.
*
* @param {Editor} editor
*/

ref = editor => {
this.editor = editor
}

/**
* Render the app.
*
Expand All @@ -99,6 +109,7 @@ class Emojis extends React.Component {
</Toolbar>
<Editor
placeholder="Write some 😍👋🎉..."
ref={this.ref}
value={this.state.value}
schema={this.schema}
onChange={this.onChange}
Expand Down Expand Up @@ -156,18 +167,16 @@ class Emojis extends React.Component {

onClickEmoji = (e, code) => {
e.preventDefault()
const { value } = this.state
const change = value.change()

change
.insertInline({
type: 'emoji',
data: { code },
})
.moveToStartOfNextText()
.focus()

this.onChange(change)

this.editor.change(change => {
change
.insertInline({
type: 'emoji',
data: { code },
})
.moveToStartOfNextText()
.focus()
})
}
}

Expand Down
27 changes: 18 additions & 9 deletions examples/history/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ class History extends React.Component {
value: Value.fromJSON(initialValue),
}

/**
* Store a reference to the `editor`.
*
* @param {Editor} editor
*/

ref = editor => {
this.editor = editor
}

/**
* Render the editor.
*
Expand All @@ -30,7 +40,9 @@ class History extends React.Component {

render() {
const { value } = this.state
const { history } = value
const { data } = value
const undos = data.get('undos')
const redos = data.get('redos')
return (
<div>
<Toolbar>
Expand All @@ -40,11 +52,12 @@ class History extends React.Component {
<Button onMouseDown={this.onClickRedo}>
<Icon>redo</Icon>
</Button>
<span>Undos: {history.undos.size}</span>
<span>Redos: {history.redos.size}</span>
<span>Undos: {undos ? undos.size : 0}</span>
<span>Redos: {redos ? redos.size : 0}</span>
</Toolbar>
<Editor
placeholder="Enter some text..."
ref={this.ref}
value={this.state.value}
onChange={this.onChange}
/>
Expand All @@ -69,9 +82,7 @@ class History extends React.Component {

onClickRedo = event => {
event.preventDefault()
const { value } = this.state
const change = value.change().redo()
this.onChange(change)
this.editor.change(change => change.redo())
}

/**
Expand All @@ -81,9 +92,7 @@ class History extends React.Component {

onClickUndo = event => {
event.preventDefault()
const { value } = this.state
const change = value.change().undo()
this.onChange(change)
this.editor.change(change => change.undo())
}
}

Expand Down
31 changes: 22 additions & 9 deletions examples/hovering-menu/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ class HoverMenu extends React.Component {
*/

renderMarkButton(type, icon) {
const { value } = this.props
const { editor } = this.props
const { value } = editor
const isActive = value.activeMarks.some(mark => mark.type == type)
return (
<Button
Expand All @@ -84,10 +85,9 @@ class HoverMenu extends React.Component {
*/

onClickMark(event, type) {
const { value, onChange } = this.props
const { editor } = this.props
event.preventDefault()
const change = value.change().toggleMark(type)
onChange(change)
editor.change(change => change.toggleMark(type))
}
}

Expand Down Expand Up @@ -157,21 +157,34 @@ class HoveringMenu extends React.Component {
render() {
return (
<div>
<HoverMenu
innerRef={menu => (this.menu = menu)}
value={this.state.value}
onChange={this.onChange}
/>
<Editor
placeholder="Enter some text..."
value={this.state.value}
onChange={this.onChange}
renderEditor={this.renderEditor}
renderMark={this.renderMark}
/>
</div>
)
}

/**
* Render the editor.
*
* @param {Object} props
* @param {Editor} editor
* @return {Element}
*/

renderEditor = (props, editor) => {
return (
<React.Fragment>
{props.children}
<HoverMenu innerRef={menu => (this.menu = menu)} editor={editor} />
</React.Fragment>
)
}

/**
* Render a Slate mark.
*
Expand Down
20 changes: 14 additions & 6 deletions examples/images/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ class Images extends React.Component {
value: Value.fromJSON(initialValue),
}

/**
* Store a reference to the `editor`.
*
* @param {Editor} editor
*/

ref = editor => {
this.editor = editor
}

/**
* Render the app.
*
Expand All @@ -109,6 +119,7 @@ class Images extends React.Component {
</Toolbar>
<Editor
placeholder="Enter some text..."
ref={this.ref}
value={this.state.value}
schema={schema}
onChange={this.onChange}
Expand Down Expand Up @@ -158,21 +169,18 @@ class Images extends React.Component {
event.preventDefault()
const src = window.prompt('Enter the URL of the image:')
if (!src) return

const change = this.state.value.change().call(insertImage, src)

this.onChange(change)
this.editor.change(insertImage, src)
}

/**
* On drop, insert the image wherever it is dropped.
*
* @param {Event} event
* @param {Change} change
* @param {Editor} editor
*/

onDropOrPaste = (event, change, editor) => {
onDropOrPaste = (event, change) => {
const { editor } = change
const target = getEventRange(event, change.value)
if (!target && event.type == 'drop') return

Expand Down
19 changes: 13 additions & 6 deletions examples/input-tester/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,17 @@ class InputTester extends React.Component {
window.document.addEventListener('selectionchange', this.onEvent)
}

ref = editor => {
this.editor = editor
}

render() {
return (
<Wrapper innerRef={this.onRef}>
<Editor
spellCheck
placeholder="Enter some text..."
ref={this.ref}
value={this.state.value}
onChange={this.onChange}
renderNode={({ attributes, children, node }) => {
Expand Down Expand Up @@ -268,19 +273,20 @@ class InputTester extends React.Component {
}

recordEvent = event => {
const { value } = this.state
const { editor } = this
const { value } = editor
let targetRange

if (event.getTargetRanges) {
const [nativeTargetRange] = event.getTargetRanges()
targetRange = nativeTargetRange && findRange(nativeTargetRange, value)
targetRange = nativeTargetRange && findRange(nativeTargetRange, editor)
}

const nativeSelection = window.getSelection()
const nativeRange = nativeSelection.rangeCount
? nativeSelection.getRangeAt(0)
: undefined
const selection = nativeRange && findRange(nativeRange, value)
const selection = nativeRange && findRange(nativeRange, editor)

EventsValue.push({
event,
Expand All @@ -291,12 +297,13 @@ class InputTester extends React.Component {
}

logEvent = event => {
const { value } = this.state
const { editor } = this
const { value } = editor
const nativeSelection = window.getSelection()
const nativeRange = nativeSelection.rangeCount
? nativeSelection.getRangeAt(0)
: undefined
const selection = nativeRange && findRange(nativeRange, value)
const selection = nativeRange && findRange(nativeRange, editor)

const {
type,
Expand All @@ -323,7 +330,7 @@ class InputTester extends React.Component {
style += '; background-color: lightskyblue'
const [nativeTargetRange] = event.getTargetRanges()
const targetRange =
nativeTargetRange && findRange(nativeTargetRange, value)
nativeTargetRange && findRange(nativeTargetRange, editor)

details = {
inputType,
Expand Down
Loading