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 support for [email protected], update example using webpack #25

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 3 additions & 3 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"presets": [
"es2015",
"env",
"react",
"stage-0"
"stage-2"
]
}
}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
dist
example/build.js
example/bundle.js
node_modules
2 changes: 1 addition & 1 deletion example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
</head>
<body>
<main></main>
<script src="build.js"></script>
<script src="bundle.js"></script>
</body>
</html>
45 changes: 26 additions & 19 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import SuggestionsPlugin from '..'
import React from 'react'
import ReactDOM from 'react-dom'
import { Value } from 'slate'
import { Editor } from 'slate-react';

import SuggestionsPlugin from '../lib'
import initialState from './state.json'
import { Editor, Raw } from 'slate'

function getCurrentWord(text, index, initialIndex) {
if (index === initialIndex) {
Expand All @@ -25,6 +27,11 @@ const suggestions = [
value: '@Jon Snow',
suggestion: '@Jon Snow' // Can be either string or react component
},
{
key: 'John Evans',
value: '@John Evans',
suggestion: '@John Evans'
},
{
key: 'Daenerys Targaryen',
value: '@Daenerys Targaryen',
Expand All @@ -47,14 +54,12 @@ class Example extends React.Component {
constructor() {
super()

this.suggestionsPlugin = new SuggestionsPlugin({
this.suggestionsPlugin = SuggestionsPlugin({
trigger: '@',
capture: /@([\w]*)/,
suggestions,
onEnter: (suggestion) => {
const { state } = this.state

const { anchorText, anchorOffset } = state
onEnter: (suggestion, change) => {
const { anchorText, anchorOffset } = this.state.value

const text = anchorText.text

Expand All @@ -66,38 +71,38 @@ class Example extends React.Component {

const newText = `${text.substring(0, index.start)}${suggestion.value} `

return state
.transform()
change
.deleteBackward(anchorOffset)
.insertText(newText)
.apply()

return true;
}
})

this.plugins = [
this.suggestionsPlugin
]
}

state = {
state: Raw.deserialize(initialState, { terse: true })
};
this.state = {
value: Value.fromJSON(initialState)
};
}

onChange = (state) => {
this.setState({ state })
onChange = ({ value }) => {
this.setState({ value })
}

render = () => {
render() {
const { SuggestionPortal } = this.suggestionsPlugin
return (
<div>
<Editor
onChange={this.onChange}
plugins={this.plugins}
state={this.state.state}
value={this.state.value}
/>
<SuggestionPortal
state={this.state.state}
value={this.state.value}
/>
</div>
)
Expand All @@ -107,3 +112,5 @@ class Example extends React.Component {
const example = <Example />
const root = document.body.querySelector('main')
ReactDOM.render(example, root)

module.hot.accept()
71 changes: 19 additions & 52 deletions example/state.json
Original file line number Diff line number Diff line change
@@ -1,54 +1,21 @@
{
"nodes": [
{
"kind": "block",
"type": "paragraph",
"nodes": [
{
"kind": "text",
"text": ""
}
]
},
{
"kind": "block",
"type": "paragraph",
"nodes": [
{
"kind": "text",
"text": "Try typing '> ' at the start of this block."
}
]
},
{
"kind": "block",
"type": "paragraph",
"nodes": [
{
"kind": "text",
"text": "Try typing '### ' at the start of this block."
}
]
},
{
"kind": "block",
"type": "paragraph",
"nodes": [
{
"kind": "text",
"text": "Try typing '- ' at the start of this block."
}
]
},
{
"kind": "block",
"type": "paragraph",
"nodes": [
{
"kind": "text",
"text": "Try typing '---<enter>' in a new block."
}
]
}
]
"document": {
"nodes": [
{
"object": "block",
"type": "paragraph",
"nodes": [
{
"object": "text",
"leaves": [
{
"object": "leaf",
"text": "@Jon Snow "
}
]
}
]
}
]
}
}
20 changes: 13 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ function SuggestionsPlugin(opts) {
const capture = opts.capture
const callback = {}

function onKeyDown(e, data, state, editor) {
function onKeyDown(e, change, editor) {
const state = change.value
const keyCode = e.keyCode
callback.editor = editor

Expand All @@ -32,29 +33,34 @@ function SuggestionsPlugin(opts) {

// Close portal
if (callback.closePortal) {
callback.closePortal()
callback.closePortal(state, editor)
}

// Handle enter
if (callback.onEnter && callback.suggestion !== undefined) {
return callback.onEnter(callback.suggestion)
return callback.onEnter(callback.suggestion, change, editor)
}
} else {
if (callback.onKeyDown) {
callback.onKeyDown(keyCode)
return callback.onKeyDown(keyCode, change, editor)
}
}
}
}

return {
onKeyDown,
SuggestionPortal: (props) =>
function SuggestionPortal(props) {
return (
<Portal
{...props}
{...opts}
callback={callback}
/>
)
}

return {
onKeyDown,
SuggestionPortal
}
}

Expand Down
19 changes: 11 additions & 8 deletions lib/suggestion-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ class SuggestionItem extends React.Component {
onMouseEnter = () =>
this.props.setSelectedIndex(this.props.index)

render = () =>
<li
className={this.props.index === this.props.selectedIndex ? 'selected' : undefined}
onClick={this.onClick}
onMouseEnter={this.onMouseEnter}
>
{this.props.suggestion.suggestion}
</li>
render() {
return (
<li
className={this.props.index === this.props.selectedIndex ? 'selected' : undefined}
onClick={this.onClick}
onMouseEnter={this.onMouseEnter}
>
{this.props.suggestion.suggestion}
</li>
)
}
}

export default SuggestionItem
Loading