@djsp/autocomplete
Advanced tools
Comparing version 0.0.3 to 0.0.5
import React, { Component } from 'react'; | ||
import { constants, withEditorContext } from '@djsp/editor'; | ||
import { constants, withPluginContext } from '@djsp/core'; | ||
var styles = { "ul": "styles_ul__37R59", "li": "styles_li__2kGXu", "suggestion": "styles_suggestion__1bOge", "suggestionFocused": "styles_suggestionFocused__2ZMxu" }; | ||
var classCallCheck = function (instance, Constructor) { | ||
function _classCallCheck(instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
}; | ||
} | ||
var createClass = function () { | ||
function defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
function _defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
return function (Constructor, protoProps, staticProps) { | ||
if (protoProps) defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
}; | ||
}(); | ||
function _createClass(Constructor, protoProps, staticProps) { | ||
if (protoProps) _defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) _defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
} | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
var _extends = Object.assign || function (target) { | ||
function _objectSpread(target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
var source = arguments[i] != null ? arguments[i] : {}; | ||
var ownKeys = Object.keys(source); | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key]; | ||
} | ||
if (typeof Object.getOwnPropertySymbols === 'function') { | ||
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { | ||
return Object.getOwnPropertyDescriptor(source, sym).enumerable; | ||
})); | ||
} | ||
ownKeys.forEach(function (key) { | ||
_defineProperty(target, key, source[key]); | ||
}); | ||
} | ||
return target; | ||
}; | ||
} | ||
var inherits = function (subClass, superClass) { | ||
function _inherits(subClass, superClass) { | ||
if (typeof superClass !== "function" && superClass !== null) { | ||
throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); | ||
throw new TypeError("Super expression must either be null or a function"); | ||
} | ||
@@ -60,3 +68,2 @@ | ||
value: subClass, | ||
enumerable: false, | ||
writable: true, | ||
@@ -66,47 +73,77 @@ configurable: true | ||
}); | ||
if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; | ||
}; | ||
if (superClass) _setPrototypeOf(subClass, superClass); | ||
} | ||
function _getPrototypeOf(o) { | ||
_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { | ||
return o.__proto__ || Object.getPrototypeOf(o); | ||
}; | ||
return _getPrototypeOf(o); | ||
} | ||
function _setPrototypeOf(o, p) { | ||
_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { | ||
o.__proto__ = p; | ||
return o; | ||
}; | ||
return _setPrototypeOf(o, p); | ||
} | ||
function _assertThisInitialized(self) { | ||
if (self === void 0) { | ||
throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); | ||
} | ||
return self; | ||
} | ||
function _possibleConstructorReturn(self, call) { | ||
if (call && (typeof call === "object" || typeof call === "function")) { | ||
return call; | ||
} | ||
return _assertThisInitialized(self); | ||
} | ||
var styles = {"ul":"styles_ul__37R59","li":"styles_li__2kGXu","suggestion":"styles_suggestion__1bOge","suggestionFocused":"styles_suggestionFocused__2ZMxu"}; | ||
var Suggestions = | ||
/*#__PURE__*/ | ||
function (_Component) { | ||
_inherits(Suggestions, _Component); | ||
function Suggestions() { | ||
var _getPrototypeOf2; | ||
var possibleConstructorReturn = function (self, call) { | ||
if (!self) { | ||
throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); | ||
} | ||
var _this; | ||
return call && (typeof call === "object" || typeof call === "function") ? call : self; | ||
}; | ||
_classCallCheck(this, Suggestions); | ||
// import Draft from 'draft-js' | ||
// const { EditorState, Modifier } = Draft | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
var Suggestions = function (_Component) { | ||
inherits(Suggestions, _Component); | ||
_this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(Suggestions)).call.apply(_getPrototypeOf2, [this].concat(args))); | ||
function Suggestions(props) { | ||
classCallCheck(this, Suggestions); | ||
var _this = possibleConstructorReturn(this, (Suggestions.__proto__ || Object.getPrototypeOf(Suggestions)).call(this, props)); | ||
_this.state = { | ||
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "state", { | ||
isOpen: false, | ||
isSearching: false, | ||
suggestions: [], | ||
selectedItem: 0, | ||
isSearching: false | ||
}; | ||
searchText: null | ||
}); | ||
_this.onBlur = function () { | ||
return _this.setState({ isOpen: false }); | ||
}; | ||
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "_unregister", void 0); | ||
_this.handleReturn = function () { | ||
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "onBlur", function () { | ||
return _this.setState({ | ||
isOpen: false | ||
}); | ||
}); | ||
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleReturn", function () { | ||
if (_this.state.isOpen && _this.props.suggestions.length > 0) { | ||
_this.onSelect(_this.props.suggestions[_this.state.selectedItem]); | ||
return constants.HANDLED; | ||
@@ -116,40 +153,45 @@ } | ||
return constants.NOT_HANDLED; | ||
}; | ||
}); | ||
_this.onDownArrow = function (e) { | ||
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "onDownArrow", function (e) { | ||
if (_this.state.isOpen && _this.props.suggestions.length > 0) { | ||
e.preventDefault(); | ||
var _selectedItem = _this.state.selectedItem >= _this.props.suggestions.length - 1 ? 0 : _this.state.selectedItem + 1; | ||
var selectedItem = _this.state.selectedItem >= _this.props.suggestions.length - 1 ? 0 : _this.state.selectedItem + 1; | ||
_this.setState({ selectedItem: _selectedItem }); | ||
_this.setState({ | ||
selectedItem: selectedItem | ||
}); | ||
return constants.HANDLED; | ||
} | ||
return constants.NOT_HANDLED; | ||
}; | ||
}); | ||
_this.onUpArrow = function (e) { | ||
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "onUpArrow", function (e) { | ||
if (_this.state.isOpen && _this.props.suggestions.length > 0) { | ||
e.preventDefault(); | ||
var _selectedItem2 = _this.state.selectedItem === 0 ? _this.props.suggestions.length - 1 : _this.state.selectedItem - 1; | ||
var selectedItem = _this.state.selectedItem === 0 ? _this.props.suggestions.length - 1 : _this.state.selectedItem - 1; | ||
_this.setState({ selectedItem: _selectedItem2 }); | ||
_this.setState({ | ||
selectedItem: selectedItem | ||
}); | ||
return constants.HANDLED; | ||
} | ||
return constants.NOT_HANDLED; | ||
}; | ||
}); | ||
_this.onSelect = function (item) { | ||
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "onSelect", function (item) { | ||
_this.props.onSelect(item, _this.state.searchText); | ||
}; | ||
}); | ||
_this.list = React.createRef(); | ||
return _this; | ||
} | ||
createClass(Suggestions, [{ | ||
key: 'componentDidMount', | ||
_createClass(Suggestions, [{ | ||
key: "componentDidMount", | ||
value: function componentDidMount() { | ||
var registerPlugin = this.props.pluginProps.registerPlugin; | ||
this._unregister = registerPlugin({ | ||
@@ -163,3 +205,3 @@ onBlur: this.onBlur, | ||
}, { | ||
key: 'componentWillUnmount', | ||
key: "componentWillUnmount", | ||
value: function componentWillUnmount() { | ||
@@ -169,3 +211,3 @@ this._unregister(); | ||
}, { | ||
key: 'componentDidUpdate', | ||
key: "componentDidUpdate", | ||
value: function componentDidUpdate(prevProps, prevState) { | ||
@@ -177,34 +219,28 @@ if (this.state.searchText != null && prevState.searchText !== this.state.searchText) { | ||
}, { | ||
key: 'render', | ||
key: "render", | ||
value: function render() { | ||
var _this2 = this; | ||
var _props = this.props, | ||
suggestions = _props.suggestions, | ||
renderSuggestion = _props.renderSuggestion; | ||
var _state = this.state, | ||
isOpen = _state.isOpen, | ||
selectedItem = _state.selectedItem; | ||
var _this$props = this.props, | ||
suggestions = _this$props.suggestions, | ||
renderSuggestion = _this$props.renderSuggestion; | ||
var _this$state = this.state, | ||
isOpen = _this$state.isOpen, | ||
selectedItem = _this$state.selectedItem; | ||
if (isOpen === true && suggestions.length > 0) { | ||
return React.createElement( | ||
'ul', | ||
{ ref: this.list, className: styles.ul }, | ||
suggestions.map(function (suggestion, index) { | ||
return React.createElement( | ||
'li', | ||
{ | ||
className: styles.li, | ||
key: 'autocomplete-option-' + index, | ||
onMouseDown: function onMouseDown() { | ||
return _this2.onSelect(suggestion); | ||
} }, | ||
renderSuggestion({ | ||
isFocused: selectedItem === index, | ||
suggestion: suggestion | ||
}) | ||
); | ||
}) | ||
); | ||
return React.createElement("ul", { | ||
className: styles.ul | ||
}, suggestions.map(function (suggestion, index) { | ||
return React.createElement("li", { | ||
className: styles.li, | ||
key: "autocomplete-option-".concat(index), | ||
onMouseDown: function onMouseDown() { | ||
return _this2.onSelect(suggestion); | ||
} | ||
}, renderSuggestion({ | ||
isFocused: selectedItem === index, | ||
suggestion: suggestion | ||
})); | ||
})); | ||
} | ||
@@ -215,3 +251,3 @@ | ||
}], [{ | ||
key: 'getDerivedStateFromProps', | ||
key: "getDerivedStateFromProps", | ||
value: function getDerivedStateFromProps(props, state) { | ||
@@ -221,4 +257,2 @@ var trigger = props.trigger, | ||
editorState = props.editorProps.editorState; | ||
var selection = editorState.getSelection(); | ||
@@ -231,3 +265,2 @@ | ||
var textUntilCursor = editorState.getCurrentContent().getBlockForKey(selection.getStartKey()).getText().slice(0, selection.getStartOffset()); | ||
var searchText = null; | ||
@@ -237,2 +270,3 @@ | ||
var lastWord = textUntilCursor.split(/\s/).slice(-1)[0]; | ||
if (lastWord[0] === trigger) { | ||
@@ -245,3 +279,3 @@ searchText = lastWord; | ||
return _extends({}, state, { | ||
return _objectSpread({}, state, { | ||
selectedItem: state.selectedItem > suggestions.length - 1 ? 0 : state.selectedItem, | ||
@@ -253,23 +287,20 @@ searchText: searchText, | ||
}]); | ||
return Suggestions; | ||
}(Component); | ||
Suggestions.defaultProps = { | ||
_defineProperty(Suggestions, "defaultProps", { | ||
renderSuggestion: function renderSuggestion(_ref) { | ||
var suggestion = _ref.suggestion, | ||
isFocused = _ref.isFocused; | ||
var classNames = [styles.suggestion]; | ||
if (isFocused) classNames.push(styles.suggestionFocused); | ||
return React.createElement( | ||
'span', | ||
{ className: '' + classNames.join(' ') }, | ||
suggestion.label | ||
); | ||
return React.createElement("span", { | ||
className: "".concat(classNames.join(' ')) | ||
}, suggestion.label); | ||
} | ||
}; | ||
}); | ||
var index = withPluginContext(Suggestions); | ||
var index = withEditorContext(Suggestions); | ||
export default index; |
@@ -7,55 +7,63 @@ 'use strict'; | ||
var React__default = _interopDefault(React); | ||
var editor = require('@djsp/editor'); | ||
var core = require('@djsp/core'); | ||
var styles = { "ul": "styles_ul__37R59", "li": "styles_li__2kGXu", "suggestion": "styles_suggestion__1bOge", "suggestionFocused": "styles_suggestionFocused__2ZMxu" }; | ||
var classCallCheck = function (instance, Constructor) { | ||
function _classCallCheck(instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
}; | ||
} | ||
var createClass = function () { | ||
function defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
function _defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
return function (Constructor, protoProps, staticProps) { | ||
if (protoProps) defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
}; | ||
}(); | ||
function _createClass(Constructor, protoProps, staticProps) { | ||
if (protoProps) _defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) _defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
} | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
var _extends = Object.assign || function (target) { | ||
function _objectSpread(target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
var source = arguments[i] != null ? arguments[i] : {}; | ||
var ownKeys = Object.keys(source); | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key]; | ||
} | ||
if (typeof Object.getOwnPropertySymbols === 'function') { | ||
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { | ||
return Object.getOwnPropertyDescriptor(source, sym).enumerable; | ||
})); | ||
} | ||
ownKeys.forEach(function (key) { | ||
_defineProperty(target, key, source[key]); | ||
}); | ||
} | ||
return target; | ||
}; | ||
} | ||
var inherits = function (subClass, superClass) { | ||
function _inherits(subClass, superClass) { | ||
if (typeof superClass !== "function" && superClass !== null) { | ||
throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); | ||
throw new TypeError("Super expression must either be null or a function"); | ||
} | ||
@@ -66,3 +74,2 @@ | ||
value: subClass, | ||
enumerable: false, | ||
writable: true, | ||
@@ -72,89 +79,124 @@ configurable: true | ||
}); | ||
if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; | ||
}; | ||
if (superClass) _setPrototypeOf(subClass, superClass); | ||
} | ||
function _getPrototypeOf(o) { | ||
_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { | ||
return o.__proto__ || Object.getPrototypeOf(o); | ||
}; | ||
return _getPrototypeOf(o); | ||
} | ||
function _setPrototypeOf(o, p) { | ||
_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { | ||
o.__proto__ = p; | ||
return o; | ||
}; | ||
return _setPrototypeOf(o, p); | ||
} | ||
function _assertThisInitialized(self) { | ||
if (self === void 0) { | ||
throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); | ||
} | ||
return self; | ||
} | ||
function _possibleConstructorReturn(self, call) { | ||
if (call && (typeof call === "object" || typeof call === "function")) { | ||
return call; | ||
} | ||
return _assertThisInitialized(self); | ||
} | ||
var styles = {"ul":"styles_ul__37R59","li":"styles_li__2kGXu","suggestion":"styles_suggestion__1bOge","suggestionFocused":"styles_suggestionFocused__2ZMxu"}; | ||
var Suggestions = | ||
/*#__PURE__*/ | ||
function (_Component) { | ||
_inherits(Suggestions, _Component); | ||
function Suggestions() { | ||
var _getPrototypeOf2; | ||
var possibleConstructorReturn = function (self, call) { | ||
if (!self) { | ||
throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); | ||
} | ||
var _this; | ||
return call && (typeof call === "object" || typeof call === "function") ? call : self; | ||
}; | ||
_classCallCheck(this, Suggestions); | ||
// import Draft from 'draft-js' | ||
// const { EditorState, Modifier } = Draft | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
var Suggestions = function (_Component) { | ||
inherits(Suggestions, _Component); | ||
_this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(Suggestions)).call.apply(_getPrototypeOf2, [this].concat(args))); | ||
function Suggestions(props) { | ||
classCallCheck(this, Suggestions); | ||
var _this = possibleConstructorReturn(this, (Suggestions.__proto__ || Object.getPrototypeOf(Suggestions)).call(this, props)); | ||
_this.state = { | ||
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "state", { | ||
isOpen: false, | ||
isSearching: false, | ||
suggestions: [], | ||
selectedItem: 0, | ||
isSearching: false | ||
}; | ||
searchText: null | ||
}); | ||
_this.onBlur = function () { | ||
return _this.setState({ isOpen: false }); | ||
}; | ||
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "_unregister", void 0); | ||
_this.handleReturn = function () { | ||
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "onBlur", function () { | ||
return _this.setState({ | ||
isOpen: false | ||
}); | ||
}); | ||
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleReturn", function () { | ||
if (_this.state.isOpen && _this.props.suggestions.length > 0) { | ||
_this.onSelect(_this.props.suggestions[_this.state.selectedItem]); | ||
return editor.constants.HANDLED; | ||
return core.constants.HANDLED; | ||
} | ||
return editor.constants.NOT_HANDLED; | ||
}; | ||
return core.constants.NOT_HANDLED; | ||
}); | ||
_this.onDownArrow = function (e) { | ||
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "onDownArrow", function (e) { | ||
if (_this.state.isOpen && _this.props.suggestions.length > 0) { | ||
e.preventDefault(); | ||
var _selectedItem = _this.state.selectedItem >= _this.props.suggestions.length - 1 ? 0 : _this.state.selectedItem + 1; | ||
var selectedItem = _this.state.selectedItem >= _this.props.suggestions.length - 1 ? 0 : _this.state.selectedItem + 1; | ||
_this.setState({ selectedItem: _selectedItem }); | ||
return editor.constants.HANDLED; | ||
_this.setState({ | ||
selectedItem: selectedItem | ||
}); | ||
return core.constants.HANDLED; | ||
} | ||
return editor.constants.NOT_HANDLED; | ||
}; | ||
_this.onUpArrow = function (e) { | ||
return core.constants.NOT_HANDLED; | ||
}); | ||
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "onUpArrow", function (e) { | ||
if (_this.state.isOpen && _this.props.suggestions.length > 0) { | ||
e.preventDefault(); | ||
var _selectedItem2 = _this.state.selectedItem === 0 ? _this.props.suggestions.length - 1 : _this.state.selectedItem - 1; | ||
var selectedItem = _this.state.selectedItem === 0 ? _this.props.suggestions.length - 1 : _this.state.selectedItem - 1; | ||
_this.setState({ selectedItem: _selectedItem2 }); | ||
return editor.constants.HANDLED; | ||
_this.setState({ | ||
selectedItem: selectedItem | ||
}); | ||
return core.constants.HANDLED; | ||
} | ||
return editor.constants.NOT_HANDLED; | ||
}; | ||
_this.onSelect = function (item) { | ||
return core.constants.NOT_HANDLED; | ||
}); | ||
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "onSelect", function (item) { | ||
_this.props.onSelect(item, _this.state.searchText); | ||
}; | ||
}); | ||
_this.list = React__default.createRef(); | ||
return _this; | ||
} | ||
createClass(Suggestions, [{ | ||
key: 'componentDidMount', | ||
_createClass(Suggestions, [{ | ||
key: "componentDidMount", | ||
value: function componentDidMount() { | ||
var registerPlugin = this.props.pluginProps.registerPlugin; | ||
this._unregister = registerPlugin({ | ||
@@ -168,3 +210,3 @@ onBlur: this.onBlur, | ||
}, { | ||
key: 'componentWillUnmount', | ||
key: "componentWillUnmount", | ||
value: function componentWillUnmount() { | ||
@@ -174,3 +216,3 @@ this._unregister(); | ||
}, { | ||
key: 'componentDidUpdate', | ||
key: "componentDidUpdate", | ||
value: function componentDidUpdate(prevProps, prevState) { | ||
@@ -182,34 +224,28 @@ if (this.state.searchText != null && prevState.searchText !== this.state.searchText) { | ||
}, { | ||
key: 'render', | ||
key: "render", | ||
value: function render() { | ||
var _this2 = this; | ||
var _props = this.props, | ||
suggestions = _props.suggestions, | ||
renderSuggestion = _props.renderSuggestion; | ||
var _state = this.state, | ||
isOpen = _state.isOpen, | ||
selectedItem = _state.selectedItem; | ||
var _this$props = this.props, | ||
suggestions = _this$props.suggestions, | ||
renderSuggestion = _this$props.renderSuggestion; | ||
var _this$state = this.state, | ||
isOpen = _this$state.isOpen, | ||
selectedItem = _this$state.selectedItem; | ||
if (isOpen === true && suggestions.length > 0) { | ||
return React__default.createElement( | ||
'ul', | ||
{ ref: this.list, className: styles.ul }, | ||
suggestions.map(function (suggestion, index) { | ||
return React__default.createElement( | ||
'li', | ||
{ | ||
className: styles.li, | ||
key: 'autocomplete-option-' + index, | ||
onMouseDown: function onMouseDown() { | ||
return _this2.onSelect(suggestion); | ||
} }, | ||
renderSuggestion({ | ||
isFocused: selectedItem === index, | ||
suggestion: suggestion | ||
}) | ||
); | ||
}) | ||
); | ||
return React__default.createElement("ul", { | ||
className: styles.ul | ||
}, suggestions.map(function (suggestion, index) { | ||
return React__default.createElement("li", { | ||
className: styles.li, | ||
key: "autocomplete-option-".concat(index), | ||
onMouseDown: function onMouseDown() { | ||
return _this2.onSelect(suggestion); | ||
} | ||
}, renderSuggestion({ | ||
isFocused: selectedItem === index, | ||
suggestion: suggestion | ||
})); | ||
})); | ||
} | ||
@@ -220,3 +256,3 @@ | ||
}], [{ | ||
key: 'getDerivedStateFromProps', | ||
key: "getDerivedStateFromProps", | ||
value: function getDerivedStateFromProps(props, state) { | ||
@@ -226,4 +262,2 @@ var trigger = props.trigger, | ||
editorState = props.editorProps.editorState; | ||
var selection = editorState.getSelection(); | ||
@@ -236,3 +270,2 @@ | ||
var textUntilCursor = editorState.getCurrentContent().getBlockForKey(selection.getStartKey()).getText().slice(0, selection.getStartOffset()); | ||
var searchText = null; | ||
@@ -242,2 +275,3 @@ | ||
var lastWord = textUntilCursor.split(/\s/).slice(-1)[0]; | ||
if (lastWord[0] === trigger) { | ||
@@ -250,3 +284,3 @@ searchText = lastWord; | ||
return _extends({}, state, { | ||
return _objectSpread({}, state, { | ||
selectedItem: state.selectedItem > suggestions.length - 1 ? 0 : state.selectedItem, | ||
@@ -258,23 +292,20 @@ searchText: searchText, | ||
}]); | ||
return Suggestions; | ||
}(React.Component); | ||
Suggestions.defaultProps = { | ||
_defineProperty(Suggestions, "defaultProps", { | ||
renderSuggestion: function renderSuggestion(_ref) { | ||
var suggestion = _ref.suggestion, | ||
isFocused = _ref.isFocused; | ||
var classNames = [styles.suggestion]; | ||
if (isFocused) classNames.push(styles.suggestionFocused); | ||
return React__default.createElement( | ||
'span', | ||
{ className: '' + classNames.join(' ') }, | ||
suggestion.label | ||
); | ||
return React__default.createElement("span", { | ||
className: "".concat(classNames.join(' ')) | ||
}, suggestion.label); | ||
} | ||
}; | ||
}); | ||
var index = core.withPluginContext(Suggestions); | ||
var index = editor.withEditorContext(Suggestions); | ||
module.exports = index; |
{ | ||
"name": "@djsp/autocomplete", | ||
"version": "0.0.3", | ||
"version": "0.0.5", | ||
"description": "Autocomplete plugin", | ||
@@ -20,5 +20,7 @@ "author": "juliankrispel", | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"dependencies": { | ||
"@djsp/editor": "^0.0.3", | ||
"emojis-list": "^2.1.0" | ||
"@djsp/core": "^0.0.5" | ||
}, | ||
@@ -31,8 +33,4 @@ "peerDependencies": { | ||
"devDependencies": { | ||
"babel-core": "^6.26.0", | ||
"babel-plugin-external-helpers": "^6.22.0", | ||
"babel-preset-env": "^1.6.0", | ||
"babel-preset-react": "^6.24.1", | ||
"babel-preset-stage-0": "^6.24.1", | ||
"cross-env": "^5.1.4", | ||
"flow-bin": "^0.83.0", | ||
"gh-pages": "^1.1.0", | ||
@@ -43,3 +41,3 @@ "react": "^16.2.0", | ||
"rollup": "^0.54.0", | ||
"rollup-plugin-babel": "^3.0.3", | ||
"rollup-plugin-babel": "^4.0.3", | ||
"rollup-plugin-commonjs": "^8.2.1", | ||
@@ -46,0 +44,0 @@ "rollup-plugin-node-resolve": "^3.0.2", |
19908
4
13
513
+ Added@djsp/core@^0.0.5
+ Added@djsp/core@0.0.5(transitive)
- Removed@djsp/editor@^0.0.3
- Removedemojis-list@^2.1.0
- Removed@djsp/editor@0.0.3(transitive)
- Removedemojis-list@2.1.0(transitive)
- Removedflow-bin@0.80.0(transitive)