UNPKG

kedao

Version:

Rich Text Editor Based On Draft.js

102 lines (101 loc) 4.21 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { classNameParser } from '../../utils/style'; import React from 'react'; import Menu from '../Menu'; import MenuItem from '../MenuItem'; import styles from "./style.module.css"; import { defaultHeadings } from '../../constants'; import loadable from '@loadable/component'; import useLanguage from '../../hooks/use-language'; const DropDown = loadable(() => __awaiter(void 0, void 0, void 0, function* () { return yield import('../DropDown'); })); const cls = classNameParser(styles); const getHeadings = (lang) => [ { key: 'header-one', title: `${lang.controls.header} 1`, text: React.createElement("h1", null, lang.controls.header, " 1"), type: 'block-type', command: 'header-one' }, { key: 'header-two', title: `${lang.controls.header} 2`, text: React.createElement("h2", null, lang.controls.header, " 2"), type: 'block-type', command: 'header-two' }, { key: 'header-three', title: `${lang.controls.header} 3`, text: React.createElement("h3", null, lang.controls.header, " 3"), type: 'block-type', command: 'header-three' }, { key: 'header-four', title: `${lang.controls.header} 4`, text: React.createElement("h4", null, lang.controls.header, " 4"), type: 'block-type', command: 'header-four' }, { key: 'header-five', title: `${lang.controls.header} 5`, text: React.createElement("h5", null, lang.controls.header, " 5"), type: 'block-type', command: 'header-five' }, { key: 'header-six', title: `${lang.controls.header} 6`, text: React.createElement("h6", null, lang.controls.header, " 6"), type: 'block-type', command: 'header-six' }, { key: 'unstyled', title: lang.controls.normal, text: lang.controls.normal, type: 'block-type', command: 'unstyled' } ]; const HeadingsPicker = ({ headings = defaultHeadings, current, getContainerNode, disabled, onChange }) => { const dropDownInstance = React.createRef(); const language = useLanguage(); const innerHeadings = getHeadings(language).filter((item) => headings.includes(item.key)); const currentHeadingIndex = innerHeadings.findIndex((item) => item.command === current); const caption = innerHeadings[currentHeadingIndex] ? innerHeadings[currentHeadingIndex].title : language.controls.normal; return (React.createElement(DropDown, { caption: caption, autoHide: true, disabled: disabled, getContainerNode: getContainerNode, title: language.controls.headings, arrowActive: currentHeadingIndex === 0, ref: dropDownInstance, className: cls('headings-dropdown') }, React.createElement(Menu, { className: cls('headings-menu') }, innerHeadings.map((item) => { const isActive = current === item.command; return (React.createElement(MenuItem, { key: item.key, role: "presentation", className: cls(`headings-menu-item${isActive ? ' active' : ''}`), onClick: () => { var _a; onChange(item.command, item.type); (_a = dropDownInstance.current) === null || _a === void 0 ? void 0 : _a.hide(); } }, item.text)); })))); }; export default HeadingsPicker;