Untitled diff
0 removals
Words removed | 0 |
Total words | 1458 |
Words removed (%) | 0.00 |
467 lines
0 additions
Words added | 0 |
Total words | 1458 |
Words added (%) | 0.00 |
467 lines
"use strict";
"use strict";
Object.defineProperty(exports, "__esModule", {
Object.defineProperty(exports, "__esModule", {
value: true
value: true
});
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _react = require("react");
var _react = require("react");
var _react2 = _interopRequireDefault(_react);
var _react2 = _interopRequireDefault(_react);
var _draftJsCheckableListItem = require("draft-js-checkable-list-item");
var _draftJsCheckableListItem = require("draft-js-checkable-list-item");
var _immutable = require("immutable");
var _immutable = require("immutable");
var _Code = require("./components/Code");
var _Code = require("./components/Code");
var _Code2 = _interopRequireDefault(_Code);
var _Code2 = _interopRequireDefault(_Code);
var _draftJs = require("draft-js");
var _draftJs = require("draft-js");
var _adjustBlockDepth = require("./modifiers/adjustBlockDepth");
var _adjustBlockDepth = require("./modifiers/adjustBlockDepth");
var _adjustBlockDepth2 = _interopRequireDefault(_adjustBlockDepth);
var _adjustBlockDepth2 = _interopRequireDefault(_adjustBlockDepth);
var _handleBlockType = require("./modifiers/handleBlockType");
var _handleBlockType = require("./modifiers/handleBlockType");
var _handleBlockType2 = _interopRequireDefault(_handleBlockType);
var _handleBlockType2 = _interopRequireDefault(_handleBlockType);
var _handleInlineStyle = require("./modifiers/handleInlineStyle");
var _handleInlineStyle = require("./modifiers/handleInlineStyle");
var _handleInlineStyle2 = _interopRequireDefault(_handleInlineStyle);
var _handleInlineStyle2 = _interopRequireDefault(_handleInlineStyle);
var _splitBlockAndChange = require("./modifiers/splitBlockAndChange");
var _splitBlockAndChange = require("./modifiers/splitBlockAndChange");
var _splitBlockAndChange2 = _interopRequireDefault(_splitBlockAndChange);
var _splitBlockAndChange2 = _interopRequireDefault(_splitBlockAndChange);
var _handleNewCodeBlock = require("./modifiers/handleNewCodeBlock");
var _handleNewCodeBlock = require("./modifiers/handleNewCodeBlock");
var _handleNewCodeBlock2 = _interopRequireDefault(_handleNewCodeBlock);
var _handleNewCodeBlock2 = _interopRequireDefault(_handleNewCodeBlock);
var _resetInlineStyle = require("./modifiers/resetInlineStyle");
var _resetInlineStyle = require("./modifiers/resetInlineStyle");
var _resetInlineStyle2 = _interopRequireDefault(_resetInlineStyle);
var _resetInlineStyle2 = _interopRequireDefault(_resetInlineStyle);
var _insertEmptyBlock = require("./modifiers/insertEmptyBlock");
var _insertEmptyBlock = require("./modifiers/insertEmptyBlock");
var _insertEmptyBlock2 = _interopRequireDefault(_insertEmptyBlock);
var _insertEmptyBlock2 = _interopRequireDefault(_insertEmptyBlock);
var _handleLink = require("./modifiers/handleLink");
var _handleLink = require("./modifiers/handleLink");
var _handleLink2 = _interopRequireDefault(_handleLink);
var _handleLink2 = _interopRequireDefault(_handleLink);
var _handleImage = require("./modifiers/handleImage");
var _handleImage = require("./modifiers/handleImage");
var _handleImage2 = _interopRequireDefault(_handleImage);
var _handleImage2 = _interopRequireDefault(_handleImage);
var _leaveList = require("./modifiers/leaveList");
var _leaveList = require("./modifiers/leaveList");
var _leaveList2 = _interopRequireDefault(_leaveList);
var _leaveList2 = _interopRequireDefault(_leaveList);
var _insertText = require("./modifiers/insertText");
var _insertText = require("./modifiers/insertText");
var _insertText2 = _interopRequireDefault(_insertText);
var _insertText2 = _interopRequireDefault(_insertText);
var _changeCurrentBlockType = require("./modifiers/changeCurrentBlockType");
var _changeCurrentBlockType = require("./modifiers/changeCurrentBlockType");
var _changeCurrentBlockType2 = _interopRequireDefault(_changeCurrentBlockType);
var _changeCurrentBlockType2 = _interopRequireDefault(_changeCurrentBlockType);
var _link = require("./decorators/link");
var _link = require("./decorators/link");
var _link2 = _interopRequireDefault(_link);
var _link2 = _interopRequireDefault(_link);
var _image = require("./decorators/image");
var _image = require("./decorators/image");
var _image2 = _interopRequireDefault(_image);
var _image2 = _interopRequireDefault(_image);
var _utils = require("./utils");
var _utils = require("./utils");
var _constants = require("./constants");
var _constants = require("./constants");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var defaultLanguages = {
var defaultLanguages = {
bash: "Bash",
bash: "Bash",
c: "C",
c: "C",
cpp: "C++",
cpp: "C++",
css: "CSS",
css: "CSS",
go: "Go",
go: "Go",
html: "HTML",
html: "HTML",
java: "Java",
java: "Java",
js: "JavaScript",
js: "JavaScript",
kotlin: "Kotlin",
kotlin: "Kotlin",
mathml: "MathML",
mathml: "MathML",
perl: "Perl",
perl: "Perl",
ruby: "Ruby",
ruby: "Ruby",
scala: "Scala",
scala: "Scala",
sql: "SQL",
sql: "SQL",
svg: "SVG",
svg: "SVG",
swift: "Swift"
swift: "Swift"
};
};
var INLINE_STYLE_CHARACTERS = ["*", "_", "`", "~"];
var INLINE_STYLE_CHARACTERS = ["*", "_", "`", "~"];
var defaultRenderSelect = function defaultRenderSelect(_ref) {
var defaultRenderSelect = function defaultRenderSelect(_ref) {
var options = _ref.options,
var options = _ref.options,
onChange = _ref.onChange,
onChange = _ref.onChange,
selectedValue = _ref.selectedValue;
selectedValue = _ref.selectedValue;
return _react2.default.createElement(
return _react2.default.createElement(
"select",
"select",
{ value: selectedValue, onChange: onChange },
{ value: selectedValue, onChange: onChange },
options.map(function (_ref2) {
options.map(function (_ref2) {
var label = _ref2.label,
var label = _ref2.label,
value = _ref2.value;
value = _ref2.value;
return _react2.default.createElement(
return _react2.default.createElement(
"option",
"option",
{ key: value, value: value },
{ key: value, value: value },
label
label
);
);
})
})
);
);
};
};
function inLink(editorState) {
function inLink(editorState) {
var selection = editorState.getSelection();
var selection = editorState.getSelection();
var contentState = editorState.getCurrentContent();
var contentState = editorState.getCurrentContent();
var block = contentState.getBlockForKey(selection.getAnchorKey());
var block = contentState.getBlockForKey(selection.getAnchorKey());
var entityKey = block.getEntityAt(selection.getFocusOffset());
var entityKey = block.getEntityAt(selection.getFocusOffset());
return entityKey != null && contentState.getEntity(entityKey).getType() === "LINK";
return entityKey != null && contentState.getEntity(entityKey).getType() === "LINK";
}
}
function inCodeBlock(editorState) {
function inCodeBlock(editorState) {
var startKey = editorState.getSelection().getStartKey();
var startKey = editorState.getSelection().getStartKey();
if (startKey) {
if (startKey) {
var currentBlockType = editorState.getCurrentContent().getBlockForKey(startKey).getType();
var currentBlockType = editorState.getCurrentContent().getBlockForKey(startKey).getType();
if (currentBlockType === "code-block") return true;
if (currentBlockType === "code-block") return true;
}
}
return false;
return false;
}
}
function checkCharacterForState(config, editorState, character) {
function checkCharacterForState(config, editorState, character) {
var newEditorState = (0, _handleBlockType2.default)(config.features.block, editorState, character);
var newEditorState = (0, _handleBlockType2.default)(config.features.block, editorState, character);
if (editorState === newEditorState && config.features.inline.includes("IMAGE")) {
if (editorState === newEditorState && config.features.inline.includes("IMAGE")) {
newEditorState = (0, _handleImage2.default)(editorState, character, config.entityType.IMAGE);
newEditorState = (0, _handleImage2.default)(editorState, character, config.entityType.IMAGE);
}
}
if (editorState === newEditorState && config.features.inline.includes("LINK")) {
if (editorState === newEditorState && config.features.inline.includes("LINK")) {
newEditorState = (0, _handleLink2.default)(editorState, character, config.entityType.LINK);
newEditorState = (0, _handleLink2.default)(editorState, character, config.entityType.LINK);
}
}
if (newEditorState === editorState && config.features.block.includes("CODE")) {
if (newEditorState === editorState && config.features.block.includes("CODE")) {
var contentState = editorState.getCurrentContent();
var contentState = editorState.getCurrentContent();
var selection = editorState.getSelection();
var selection = editorState.getSelection();
var key = selection.getStartKey();
var key = selection.getStartKey();
var currentBlock = contentState.getBlockForKey(key);
var currentBlock = contentState.getBlockForKey(key);
var text = currentBlock.getText();
var text = currentBlock.getText();
var type = currentBlock.getType();
var type = currentBlock.getType();
if (type !== "code-block" && _constants.CODE_BLOCK_REGEX.test(text)) newEditorState = (0, _handleNewCodeBlock2.default)(editorState);
if (type !== "code-block" && _constants.CODE_BLOCK_REGEX.test(text)) newEditorState = (0, _handleNewCodeBlock2.default)(editorState);
}
}
if (editorState === newEditorState) {
if (editorState === newEditorState) {
newEditorState = (0, _handleInlineStyle2.default)(config.features.inline, editorState, character);
newEditorState = (0, _handleInlineStyle2.default)(config.features.inline, editorState, character);
}
}
return newEditorState;
return newEditorState;
}
}
function checkReturnForState(config, editorState, ev) {
function checkReturnForState(config, editorState, ev) {
var newEditorState = editorState;
var newEditorState = editorState;
var contentState = editorState.getCurrentContent();
var contentState = editorState.getCurrentContent();
var selection = editorState.getSelection();
var selection = editorState.getSelection();
var isCollapsed = selection.isCollapsed();
var isCollapsed = selection.isCollapsed();
var key = selection.getStartKey();
var key = selection.getStartKey();
var endOffset = selection.getEndOffset();
var endOffset = selection.getEndOffset();
var currentBlock = contentState.getBlockForKey(key);
var currentBlock = contentState.getBlockForKey(key);
var blockLength = currentBlock.getLength();
var blockLength = currentBlock.getLength();
var type = currentBlock.getType();
var type = currentBlock.getType();
var text = currentBlock.getText();
var text = currentBlock.getText();
if (/-list-item$/.test(type) && text === "") {
if (/-list-item$/.test(type) && text === "") {
newEditorState = (0, _leaveList2.default)(editorState);
newEditorState = (0, _leaveList2.default)(editorState);
}
}
var isHeader = /^header-/.test(type);
var isHeader = /^header-/.test(type);
var isBlockQuote = type === "blockquote";
var isBlockQuote = type === "blockquote";
var isAtEndOfLine = endOffset === blockLength;
var isAtEndOfLine = endOffset === blockLength;
var atEndOfHeader = isHeader && isAtEndOfLine;
var atEndOfHeader = isHeader && isAtEndOfLine;
var atEndOfBlockQuote = isBlockQuote && isAtEndOfLine;
var atEndOfBlockQuote = isBlockQuote && isAtEndOfLine;
if (newEditorState === editorState && isCollapsed && (atEndOfHeader || atEndOfBlockQuote)) {
if (newEditorState === editorState && isCollapsed && (atEndOfHeader || atEndOfBlockQuote)) {
// transform markdown (if we aren't in a codeblock that is)
// transform markdown (if we aren't in a codeblock that is)
if (!inCodeBlock(editorState)) {
if (!inCodeBlock(editorState)) {
newEditorState = checkCharacterForState(config, newEditorState, "\n");
newEditorState = checkCharacterForState(config, newEditorState, "\n");
}
}
if (newEditorState === editorState) {
if (newEditorState === editorState) {
newEditorState = (0, _insertEmptyBlock2.default)(newEditorState);
newEditorState = (0, _insertEmptyBlock2.default)(newEditorState);
} else {
} else {
newEditorState = _draftJs.RichUtils.toggleBlockType(newEditorState, type);
newEditorState = _draftJs.RichUtils.toggleBlockType(newEditorState, type);
}
}
} else if (isCollapsed && (isHeader || isBlockQuote) && !isAtEndOfLine) {
} else if (isCollapsed && (isHeader || isBlockQuote) && !isAtEndOfLine) {
newEditorState = (0, _splitBlockAndChange2.default)(newEditorState);
newEditorState = (0, _splitBlockAndChange2.default)(newEditorState);
}
}
if (newEditorState === editorState && type !== "code-block" && config.features.block.includes("CODE") && _constants.CODE_BLOCK_REGEX.test(text)) {
if (newEditorState === editorState && type !== "code-block" && config.features.block.includes("CODE") && _constants.CODE_BLOCK_REGEX.test(text)) {
newEditorState = (0, _handleNewCodeBlock2.default)(editorState);
newEditorState = (0, _handleNewCodeBlock2.default)(editorState);
}
}
if (newEditorState === editorState && type === "code-block") {
if (newEditorState === editorState && type === "code-block") {
if (/```\s*$/.test(text)) {
if (/```\s*$/.test(text)) {
newEditorState = (0, _changeCurrentBlockType2.default)(newEditorState, type, text.replace(/```\s*$/, ""));
newEditorState = (0, _changeCurrentBlockType2.default)(newEditorState, type, text.replace(/```\s*$/, ""));
newEditorState = (0, _insertEmptyBlock2.default)(newEditorState);
newEditorState = (0, _insertEmptyBlock2.default)(newEditorState);
} else if (ev.shiftKey) {
} else if (ev.shiftKey) {
newEditorState = (0, _insertEmptyBlock2.default)(newEditorState);
newEditorState = (0, _insertEmptyBlock2.default)(newEditorState);
} else {
} else {
newEditorState = (0, _insertText2.default)(editorState, "\n");
newEditorState = (0, _insertText2.default)(editorState, "\n");
}
}
}
}
return newEditorState;
return newEditorState;
}
}
var unstickyInlineStyles = function unstickyInlineStyles(character, editorState) {
var unstickyInlineStyles = function unstickyInlineStyles(character, editorState) {
var selection = editorState.getSelection();
var selection = editorState.getSelection();
if (!selection.isCollapsed()) return editorState;
if (!selection.isCollapsed()) return editorState;
var startOffset = selection.getStartOffset();
var startOffset = selection.getStartOffset();
var content = editorState.getCurrentContent();
var content = editorState.getCurrentContent();
var block = content.getBlockForKey(selection.getStartKey());
var block = content.getBlockForKey(selection.getStartKey());
var entity = block.getEntityAt(startOffset);
var entity = block.getEntityAt(startOffset);
if (entity !== null) return editorState;
if (entity !== null) return editorState;
// If we're currently in a style, but the next character doesn't have a style (or doesn't exist)
// If we're currently in a style, but the next character doesn't have a style (or doesn't exist)
// we insert the characters manually without the inline style to "unsticky" them
// we insert the characters manually without the inline style to "unsticky" them
var style = block.getInlineStyleAt(startOffset - 1);
var style = block.getInlineStyleAt(startOffset - 1);
if (style.size === 0) return editorState;
if (style.size === 0) return editorState;
var nextStyle = block.getInlineStyleAt(startOffset);
var nextStyle = block.getInlineStyleAt(startOffset);
if (nextStyle.size !== 0) return editorState;
if (nextStyle.size !== 0) return editorState;
var newContent = _draftJs.Modifier.insertText(content, selection, character);
var newContent = _draftJs.Modifier.insertText(content, selection, character);
return _draftJs.EditorState.push(editorState, newContent, "insert-characters");
return _draftJs.EditorState.push(editorState, newContent, "insert-characters");
};
};
var defaultConfig = {
var defaultConfig = {
renderLanguageSelect: defaultRenderSelect,
renderLanguageSelect: defaultRenderSelect,
languages: defaultLanguages,
languages: defaultLanguages,
features: {
features: {
inline: _constants.defaultInlineWhitelist,
inline: _constants.defaultInlineWhitelist,
block: _constants.defaultBlockWhitelist
block: _constants.defaultBlockWhitelist
},
},
entityType: _constants.ENTITY_TYPE
entityType: _constants.ENTITY_TYPE
};
};
var createMarkdownPlugin = function createMarkdownPlugin() {
var createMarkdownPlugin = function createMarkdownPlugin() {
var _config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var _config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var store = {};
var store = {};
var config = _extends({}, defaultConfig, _config, {
var config = _extends({}, defaultConfig, _config, {
features: _extends({}, defaultConfig.features, _config.features),
features: _extends({}, defaultConfig.features, _config.features),
entityType: _extends({}, defaultConfig.entityType, _config.entityType)
entityType: _extends({}, defaultConfig.entityType, _config.entityType)
});
});
return {
return {
store: store,
store: store,
blockRenderMap: (0, _immutable.Map)({
blockRenderMap: (0, _immutable.Map)({
"code-block": {
"code-block": {
element: "code",
element: "code",
wrapper: _react2.default.createElement("pre", { spellCheck: "false" })
wrapper: _react2.default.createElement("pre", { spellCheck: "false" })
}
}
}).merge(_draftJsCheckableListItem.blockRenderMap),
}).merge(_draftJsCheckableListItem.blockRenderMap),
decorators: [(0, _link2.default)({
decorators: [(0, _link2.default)({
entityType: config.entityType.LINK
entityType: config.entityType.LINK
}), (0, _image2.default)({
}), (0, _image2.default)({
entityType: config.entityType.IMAGE
entityType: config.entityType.IMAGE
})],
})],
initialize: function initialize(_ref3) {
initialize: function initialize(_ref3) {
var setEditorState = _ref3.setEditorState,
var setEditorState = _ref3.setEditorState,
getEditorState = _ref3.getEditorState;
getEditorState = _ref3.getEditorState;
store.setEditorState = setEditorState;
store.setEditorState = setEditorState;
store.getEditorState = getEditorState;
store.getEditorState = getEditorState;
},
},
blockStyleFn: function blockStyleFn(block) {
blockStyleFn: function blockStyleFn(block) {
switch (block.getType()) {
switch (block.getType()) {
case _draftJsCheckableListItem.CHECKABLE_LIST_ITEM:
case _draftJsCheckableListItem.CHECKABLE_LIST_ITEM:
return _draftJsCheckableListItem.CHECKABLE_LIST_ITEM;
return _draftJsCheckableListItem.CHECKABLE_LIST_ITEM;
default:
default:
break;
break;
}
}
return null;
return null;
},
},
blockRendererFn: function blockRendererFn(block, _ref4) {
blockRendererFn: function blockRendererFn(block, _ref4) {
var setReadOnly = _ref4.setReadOnly,
var setReadOnly = _ref4.setReadOnly,
setEditorState = _ref4.setEditorState,
setEditorState = _ref4.setEditorState,
getEditorState = _ref4.getEditorState,
getEditorState = _ref4.getEditorState,
getEditorRef = _ref4.getEditorRef;
getEditorRef = _ref4.getEditorRef;
switch (block.getType()) {
switch (block.getType()) {
case _draftJsCheckableListItem.CHECKABLE_LIST_ITEM:
case _draftJsCheckableListItem.CHECKABLE_LIST_ITEM:
{
{
return {
return {
component: _draftJsCheckableListItem.CheckableListItem,
component: _draftJsCheckableListItem.CheckableListItem,
props: {
props: {
onChangeChecked: function onChangeChecked() {
onChangeChecked: function onChangeChecked() {
return setEditorState(_draftJsCheckableListItem.CheckableListItemUtils.toggleChecked(getEditorState(), block));
return setEditorState(_draftJsCheckableListItem.CheckableListItemUtils.toggleChecked(getEditorState(), block));
},
},
checked: !!block.getData().get("checked")
checked: !!block.getData().get("checked")
}
}
};
};
}
}
case _constants.CODE_BLOCK_TYPE:
case _constants.CODE_BLOCK_TYPE:
{
{
return {
return {
component: _Code2.default,
component: _Code2.default,
props: {
props: {
setEditorState: setEditorState,
setEditorState: setEditorState,
renderLanguageSelect: config.renderLanguageSelect,
renderLanguageSelect: config.renderLanguageSelect,
languages: config.languages,
languages: config.languages,
setReadOnly: setReadOnly,
setReadOnly: setReadOnly,
language: block.getData().get("language"),
language: block.getData().get("language"),
getEditorState: getEditorState
getEditorState: getEditorState
}
}
};
};
}
}
default:
default:
return null;
return null;
}
}
},
},
onTab: function onTab(ev, _ref5) {
onTab: function onTab(ev, _ref5) {
var getEditorState = _ref5.getEditorState,
var getEditorState = _ref5.getEditorState,
setEditorState = _ref5.setEditorState;
setEditorState = _ref5.setEditorState;
var editorState = getEditorState();
var editorState = getEditorState();
var newEditorState = (0, _adjustBlockDepth2.default)(editorState, ev);
var newEditorState = (0, _adjustBlockDepth2.default)(editorState, ev);
if (newEditorState !== editorState) {
if (newEditorState !== editorState) {
setEditorState(newEditorState);
setEditorState(newEditorState);
return "handled";
return "handled";
}
}
return "not-handled";
return "not-handled";
},
},
handleReturn: function handleReturn(ev, editorState, _ref6) {
handleReturn: function handleReturn(ev, editorState, _ref6) {
var setEditorState = _ref6.setEditorState;
var setEditorState = _ref6.setEditorState;
if (inLink(editorState)) return "not-handled";
if (inLink(editorState)) return "not-handled";
var newEditorState = checkReturnForState(config, editorState, ev);
var newEditorState = checkReturnForState(config, editorState, ev);
var selection = newEditorState.getSelection();
var selection = newEditorState.getSelection();
// exit code blocks
// exit code blocks
if (inCodeBlock(editorState) && !(0, _immutable.is)(editorState.getImmutable(), newEditorState.getImmutable())) {
if (inCodeBlock(editorState) && !(0, _immutable.is)(editorState.getImmutable(), newEditorState.getImmutable())) {
setEditorState(newEditorState);
setEditorState(newEditorState);
return "handled";
return "handled";
}
}
newEditorState = checkCharacterForState(config, newEditorState, "\n");
newEditorState = checkCharacterForState(config, newEditorState, "\n");
var content = newEditorState.getCurrentContent();
var content = newEditorState.getCurrentContent();
// if there are actually no changes but the editorState has a
// if there are actually no changes but the editorState has a
// current inline style we want to split the block
// current inline style we want to split the block
if ((0, _immutable.is)(editorState.getImmutable(), newEditorState.getImmutable()) && editorState.getCurrentInlineStyle().size > 0) {
if ((0, _immutable.is)(editorState.getImmutable(), newEditorState.getImmutable()) && editorState.getCurrentInlineStyle().size > 0) {
content = _draftJs.Modifier.splitBlock(content, selection);
content = _draftJs.Modifier.splitBlock(content, selection);
}
}
newEditorState = (0, _resetInlineStyle2.default)(newEditorState);
newEditorState = (0, _resetInlineStyle2.default)(newEditorState);
if (editorState !== newEditorState) {
if (editorState !== newEditorState) {
setEditorState(_draftJs.EditorState.push(newEditorState, content, "split-block"));
setEditorState(_draftJs.EditorState.push(newEditorState, content, "split-block"));
return "handled";
return "handled";
}
}
return "not-handled";
return "not-handled";
},
},
handleBeforeInput: function handleBeforeInput(character, editorState, _ref7) {
handleBeforeInput: function handleBeforeInput(character, editorState, _ref7) {
var setEditorState = _ref7.setEditorState;
var setEditorState = _ref7.setEditorState;
// If we're in a code block - don't transform markdown
// If we're in a code block - don't transform markdown
if (inCodeBlock(editorState)) return "not-handled";
if (inCodeBlock(editorState)) return "not-handled";
// If we're in a link - don't transform markdown
// If we're in a link - don't transform markdown
if (inLink(editorState)) return "not-handled";
if (inLink(editorState)) return "not-handled";
var unsticky = unstickyInlineStyles(character, editorState);
var unsticky = unstickyInlineStyles(character, editorState);
if (editorState !== unsticky) {
if (editorState !== unsticky) {
setEditorState(unsticky);
setEditorState(unsticky);
return "handled";
return "handled";
}
}
var newEditorState = checkCharacterForState(config, editorState, character);
var newEditorState = checkCharacterForState(config, editorState, character);
if (editorState !== newEditorState) {
if (editorState !== newEditorState) {
setEditorState(newEditorState);
setEditorState(newEditorState);
return "handled";
return "handled";
}
}
return "not-handled";
return "not-handled";
},
},
handleKeyCommand: function handleKeyCommand(command, editorState, _ref8) {
handleKeyCommand: function handleKeyCommand(command, editorState, _ref8) {
var setEditorState = _ref8.setEditorState;
var setEditorState = _ref8.setEditorState;
switch (command) {
switch (command) {
case "backspace":
case "backspace":
{
{
// When a styled block is the first thing in the editor,
// When a styled block is the first thing in the editor,
// you cannot delete it. Typing backspace only deletes the content
// you cannot delete it. Typing backspace only deletes the content
// but never deletes the block styling.
// but never deletes the block styling.
// This piece of code fixes the issue by changing the block type
// This piece of code fixes the issue by changing the block type
// to 'unstyled' if we're on the first block of the editor and it's empty
// to 'unstyled' if we're on the first block of the editor and it's empty
var selection = editorState.getSelection();
var selection = editorState.getSelection();
var currentBlockKey = selection.getStartKey();
var currentBlockKey = selection.getStartKey();
if (!currentBlockKey) return "not-handled";
if (!currentBlockKey) return "not-handled";
var content = editorState.getCurrentContent();
var content = editorState.getCurrentContent();
var currentBlock = content.getBlockForKey(currentBlockKey);
var currentBlock = content.getBlockForKey(currentBlockKey);
var firstBlock = content.getFirstBlock();
var firstBlock = content.getFirstBlock();
if (firstBlock !== currentBlock) return "not-handled";
if (firstBlock !== currentBlock) return "not-handled";
var currentBlockType = currentBlock.getType();
var currentBlockType = currentBlock.getType();
var isEmpty = currentBlock.getLength() === 0;
var isEmpty = currentBlock.getLength() === 0;
if (!isEmpty || currentBlockType === "unstyled") return "not-handled";
if (!isEmpty || currentBlockType === "unstyled") return "not-handled";
setEditorState((0, _changeCurrentBlockType2.default)(editorState, "unstyled", ""));
setEditorState((0, _changeCurrentBlockType2.default)(editorState, "unstyled", ""));
return "handled";
return "handled";
}
}
default:
default:
{
{
return "not-handled";
return "not-handled";
}
}
}
}
},
},
handlePastedText: function handlePastedText(text, html, editorState, _ref9) {
handlePastedText: function handlePastedText(text, html, editorState, _ref9) {
var setEditorState = _ref9.setEditorState;
var setEditorState = _ref9.setEditorState;
if (html) {
if (html) {
return "not-handled";
return "not-handled";
}
}
// If we're in a code block don't add markdown to it
// If we're in a code block don't add markdown to it
if (inCodeBlock(editorState)) return "not-handled";
if (inCodeBlock(editorState)) return "not-handled";
var newEditorState = editorState;
var newEditorState = editorState;
var buffer = [];
var buffer = [];
for (var i = 0; i < text.length; i++) {
for (var i = 0; i < text.length; i++) {
// eslint-disable-line no-plusplus
// eslint-disable-line no-plusplus
if (INLINE_STYLE_CHARACTERS.indexOf(text[i]) >= 0) {
if (INLINE_STYLE_CHARACTERS.indexOf(text[i]) >= 0) {
newEditorState = (0, _utils.replaceText)(newEditorState, buffer.join("") + text[i]);
newEditorState = (0, _utils.replaceText)(newEditorState, buffer.join("") + text[i]);
newEditorState = checkCharacterForState(config, newEditorState, text[i]);
newEditorState = checkCharacterForState(config, newEditorState, text[i]);
buffer = [];
buffer = [];
} else if (text[i].charCodeAt(0) === 10) {
} else if (text[i].charCodeAt(0) === 10) {
newEditorState = (0, _utils.replaceText)(newEditorState, buffer.join(""));
newEditorState = (0, _utils.replaceText)(newEditorState, buffer.join(""));
var tmpEditorState = checkReturnForState(config, newEditorState, {});
var tmpEditorState = checkReturnForState(config, newEditorState, {});
if (newEditorState === tmpEditorState) {
if (newEditorState === tmpEditorState) {
newEditorState = (0, _insertEmptyBlock2.default)(tmpEditorState);
newEditorState = (0, _insertEmptyBlock2.default)(tmpEditorState);
} else {
} else {
newEditorState = tmpEditorState;
newEditorState = tmpEditorState;
}
}
buffer = [];
buffer = [];
} else if (i === text.length - 1) {
} else if (i === text.length - 1) {
newEditorState = (0, _utils.replaceText)(newEditorState, buffer.join("") + text[i]);
newEditorState = (0, _utils.replaceText)(newEditorState, buffer.join("") + text[i]);
buffer = [];
buffer = [];
} else {
} else {
buffer.push(text[i]);
buffer.push(text[i]);
}
}
}
}
if (editorState !== newEditorState) {
if (editorState !== newEditorState) {
setEditorState(newEditorState);
setEditorState(newEditorState);
return "handled";
return "handled";
}
}
return "not-handled";
return "not-handled";
}
}
};
};
};
};
exports.default = createMarkdownPlugin;
exports.default = createMarkdownPlugin;