@mirrormedia/lilith-draft-editor
Version:
## Installation
116 lines (89 loc) • 4.33 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.FontColorButton = FontColorButton;
var _react = _interopRequireWildcard(require("react"));
var _modals = require("@keystone-ui/modals");
var _draftJs = require("draft-js");
var _fields = require("@keystone-ui/fields");
var _styledComponents = _interopRequireDefault(require("styled-components"));
var _modifier = require("../modifier");
var _const = require("../const");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
const ColorHexInput = (0, _styledComponents.default)(_fields.TextInput)`
font-family: Georgia, serif;
margin-right: 10px;
padding: 10px;
`;
function FontColorButton(props) {
const {
isActive,
editorState,
onChange,
className
} = props;
const [toShowColorInput, setToShowColorInput] = (0, _react.useState)(false);
const [colorValue, setColorValue] = (0, _react.useState)('');
const promptForColor = e => {
e.preventDefault();
const selection = editorState.getSelection();
if (!selection.isCollapsed()) {
setToShowColorInput(true);
}
};
const confirmColor = () => {
const selection = editorState.getSelection();
const contentState = editorState.getCurrentContent();
let newContentState = _modifier.Modifier.removeInlineStyleByPrefix(contentState, selection, _const.CUSTOM_STYLE_PREFIX_FONT_COLOR);
if (colorValue) {
newContentState = _modifier.Modifier.applyInlineStyle(newContentState, selection, _const.CUSTOM_STYLE_PREFIX_FONT_COLOR + colorValue);
}
onChange(_draftJs.EditorState.push(editorState, newContentState, 'change-inline-style'));
setToShowColorInput(false);
setColorValue('');
};
const onColorInputKeyDown = e => {
if (e.which === 13) {
e.preventDefault();
confirmColor();
}
};
const removeColor = () => {
const selection = editorState.getSelection();
if (!selection.isCollapsed()) {
const contentState = editorState.getCurrentContent();
const newContentState = _modifier.Modifier.removeInlineStyleByPrefix(contentState, selection, _const.CUSTOM_STYLE_PREFIX_FONT_COLOR);
onChange(_draftJs.EditorState.push(editorState, newContentState, 'change-inline-style'));
}
setToShowColorInput(false);
setColorValue('');
};
const colorInput = /*#__PURE__*/_react.default.createElement(_modals.AlertDialog, {
title: "Hex Color Code (#ffffff)",
isOpen: toShowColorInput,
actions: {
cancel: {
label: 'Cancel',
action: removeColor
},
confirm: {
label: 'Confirm',
action: confirmColor
}
}
}, /*#__PURE__*/_react.default.createElement(ColorHexInput, {
onChange: e => setColorValue(e.target.value),
type: "text",
value: colorValue,
onKeyDown: onColorInputKeyDown
}));
return /*#__PURE__*/_react.default.createElement(_react.Fragment, null, colorInput, /*#__PURE__*/_react.default.createElement("div", {
className: className,
onMouseDown: isActive ? removeColor : promptForColor
}, /*#__PURE__*/_react.default.createElement("i", {
className: "fas fa-palette"
})));
}