@mirrormedia/lilith-draft-editor
Version:
## Installation
135 lines (110 loc) • 4.86 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.LinkButton = LinkButton;
var _react = _interopRequireWildcard(require("react"));
var _styledComponents = _interopRequireDefault(require("styled-components"));
var _modals = require("@keystone-ui/modals");
var _draftJs = require("draft-js");
var _fields = require("@keystone-ui/fields");
var _util = require("../util");
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 StyledTextInput = (0, _styledComponents.default)(_fields.TextInput)`
fontfamily: Georgia, serif;
marginright: 10;
padding: 10;
`;
function LinkButton(props) {
const {
editorState,
className,
onChange
} = props;
const entityData = (0, _util.getSelectionEntityData)(editorState);
const url = (entityData === null || entityData === void 0 ? void 0 : entityData.url) || '';
(0, _react.useEffect)(() => {
setLinkUrl(url);
}, [url]);
const [toShowUrlInput, setToShowUrlInput] = (0, _react.useState)(false);
const [linkUrl, setLinkUrl] = (0, _react.useState)(url || '');
const promptForLink = e => {
e.preventDefault();
const selection = editorState.getSelection();
if (!selection.isCollapsed()) {
setToShowUrlInput(true);
}
};
const confirmLink = () => {
const contentState = editorState.getCurrentContent();
const contentStateWithEntity = contentState.createEntity('LINK', 'MUTABLE', {
url: linkUrl
});
const entityKey = contentStateWithEntity.getLastCreatedEntityKey();
const newEditorState = _draftJs.EditorState.set(editorState, {
currentContent: contentStateWithEntity
});
const selection = newEditorState.getSelection(); // RichUtils.toggleLink will reset selection back to first block and cause
// the editor scroll to top. Use `forceSelection` to hold the selection.
onChange(_draftJs.EditorState.forceSelection(_draftJs.RichUtils.toggleLink(newEditorState, selection, entityKey), selection));
setToShowUrlInput(false);
};
const onLinkInputKeyDown = e => {
if (e.which === 13) {
e.preventDefault();
confirmLink();
}
};
const removeLink = () => {
const selection = editorState.getSelection();
if (!selection.isCollapsed()) {
// RichUtils.toggleLink will reset selection back to first block and cause
// the editor scroll to top. Use `forceSelection` to hold the selection.
onChange(_draftJs.EditorState.forceSelection(_draftJs.RichUtils.toggleLink(editorState, selection, null), selection));
}
setToShowUrlInput(false);
};
const alertProps = linkUrl ? {
title: 'Update Link',
actions: {
cancel: {
label: 'Remove',
action: removeLink
},
confirm: {
label: 'Update',
action: confirmLink
}
}
} : {
title: 'Insert Link',
actions: {
cancel: {
label: 'Cancel',
action: removeLink
},
confirm: {
label: 'Confirm',
action: confirmLink
}
}
};
const urlInput = /*#__PURE__*/_react.default.createElement(_modals.AlertDialog, {
title: alertProps.title,
isOpen: toShowUrlInput,
actions: alertProps.actions
}, /*#__PURE__*/_react.default.createElement(StyledTextInput, {
onChange: e => setLinkUrl(e.target.value),
value: linkUrl,
type: "text",
onKeyDown: onLinkInputKeyDown
}));
return /*#__PURE__*/_react.default.createElement(_react.Fragment, null, urlInput, /*#__PURE__*/_react.default.createElement("div", {
className: className,
onMouseDown: promptForLink
}, /*#__PURE__*/_react.default.createElement("i", {
className: "fas fa-link"
})));
}