@accordproject/markdown-editor
Version:
A rich text editor that can read and write markdown text. Based on Slate.js.
116 lines (92 loc) • 2.85 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireDefault(require("react"));
var _constants = require("../constants");
var _toolbarMethods = require("../FormattingToolbar/toolbarMethods");
/**
* This is a plugin into the markdown editor to handle block quotes
*/
function Blockquote() {
const name = 'blockquote';
/**
* @param {Event} event
* @param {Editor} editor
* @param {Function} next
*/
const onEnter = (event, editor, next) => {
const value = editor.value;
const startBlock = value.startBlock;
event.preventDefault(); // Hitting enter on an empty line will break out of the block quote
if ((0, _toolbarMethods.isSelectionInput)(value, _constants.BLOCK_QUOTE) && startBlock.text.length === 0) {
editor.withoutNormalizing(() => {
event.preventDefault();
editor.setBlocks(_constants.PARAGRAPH).unwrapBlock(_constants.BLOCK_QUOTE);
});
return;
}
next();
};
/**
* @param {Event} event
* @param {Editor} editor
* @param {Function} next
*/
const onBackspace = (event, editor, next) => {
const value = editor.value;
const startBlock = value.startBlock;
event.preventDefault(); // Hitting backspace on the last item when empty will break out of the block quote
if ((0, _toolbarMethods.isSelectionInput)(value, _constants.BLOCK_QUOTE) && startBlock.text.length === 0) {
const lastItem = !value.document.getAncestors(value.previousBlock.key).some(a => a.type === _constants.BLOCK_QUOTE);
if (lastItem) {
editor.withoutNormalizing(() => {
event.preventDefault();
editor.setBlocks(_constants.PARAGRAPH).unwrapBlock(_constants.BLOCK_QUOTE);
});
return;
}
}
next();
};
/**
* @param {Event} event
* @param {Editor} editor
* @param {Function} next
*/
const onKeyDown = (event, editor, next) => {
switch (event.key) {
case 'Enter':
return onEnter(event, editor, next);
case 'Backspace':
return onBackspace(event, editor, next);
default:
return next();
}
};
/**
* @param {Object} props
* @param {Editor} editor
* @param {Function} next
*/
const renderBlock = (props, editor, next) => {
const node = props.node,
attributes = props.attributes,
children = props.children;
switch (node.type) {
case _constants.BLOCK_QUOTE:
return _react.default.createElement("blockquote", attributes, children);
default:
return next();
}
};
return {
name,
onKeyDown,
renderBlock
};
}
var _default = Blockquote;
exports.default = _default;