@entur-partner/rich-text-editor 6.3.5 → 7.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,872 +0,0 @@
1
- import { ExpandablePanel } from '@entur/expand';
2
- import { TextField, FeedbackText } from '@entur/form';
3
- import { Unbutton, Stack } from '@entur-partner/common';
4
- import React, { Component, createRef, useState } from 'react';
5
- import { Label } from '@entur/typography';
6
- import cx from 'classnames';
7
- import { EditorState, CompositeDecorator, Editor, RichUtils, getDefaultKeyBinding, Modifier, convertToRaw, convertFromRaw } from 'draft-js';
8
- import PropTypes from 'prop-types';
9
- import { useTranslation } from 'react-i18next';
10
- import { SecondarySquareButton } from '@entur/button';
11
- import { LinkIcon, CloseIcon, CheckIcon, UnlinkIcon, DownArrowIcon, BulletListIcon, NumberListIcon, BoldIcon, ItalicIcon, UnderlineIcon } from '@entur/icons';
12
- import { Dropdown } from '@entur/dropdown';
13
- import { OverflowMenuItem, OverflowMenu } from '@entur/menu';
14
- import { Popover } from '@entur/tooltip';
15
- import { draftToMarkdown, markdownToDraft } from 'markdown-draft-js';
16
- import showdown from 'showdown';
17
- import { convertFromHTML, convertToHTML } from 'draft-convert';
18
-
19
- function _extends() {
20
- return _extends = Object.assign ? Object.assign.bind() : function (n) {
21
- for (var e = 1; e < arguments.length; e++) {
22
- var t = arguments[e];
23
- for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]);
24
- }
25
- return n;
26
- }, _extends.apply(null, arguments);
27
- }
28
- function _inheritsLoose(t, o) {
29
- t.prototype = Object.create(o.prototype), t.prototype.constructor = t, _setPrototypeOf(t, o);
30
- }
31
- function _objectWithoutPropertiesLoose(r, e) {
32
- if (null == r) return {};
33
- var t = {};
34
- for (var n in r) if ({}.hasOwnProperty.call(r, n)) {
35
- if (-1 !== e.indexOf(n)) continue;
36
- t[n] = r[n];
37
- }
38
- return t;
39
- }
40
- function _setPrototypeOf(t, e) {
41
- return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) {
42
- return t.__proto__ = e, t;
43
- }, _setPrototypeOf(t, e);
44
- }
45
-
46
- var Key = /*#__PURE__*/Object.freeze({
47
- ENTER: 13
48
- });
49
- function getKey(event) {
50
- return event.keyCode || event.which;
51
- }
52
-
53
- function useI18N() {
54
- var _useTranslation = useTranslation(),
55
- t = _useTranslation.t,
56
- language = _useTranslation.i18n.language;
57
- if (language !== "nb" && language !== "en" && language !== "nb-NO" && language !== "en-GB") {
58
- throw Error("Language must be either nb or en.");
59
- }
60
- return t;
61
- }
62
- function withI18n(Component) {
63
- return function WrappedComponent(props) {
64
- var t = useI18N();
65
- return /*#__PURE__*/React.createElement(Component, _extends({}, props, {
66
- i18n: t
67
- }));
68
- };
69
- }
70
-
71
- var StyleButton = /*#__PURE__*/function (_Component) {
72
- function StyleButton() {
73
- var _this;
74
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
75
- args[_key] = arguments[_key];
76
- }
77
- _this = _Component.call.apply(_Component, [this].concat(args)) || this;
78
- _this.onToggle = function (e) {
79
- var _this$props = _this.props,
80
- style = _this$props.style,
81
- onToggle = _this$props.onToggle;
82
- e.preventDefault();
83
- onToggle(style);
84
- };
85
- _this.onToggleNoEvent = function () {
86
- var _this$props2 = _this.props,
87
- style = _this$props2.style,
88
- onToggle = _this$props2.onToggle;
89
- onToggle(style);
90
- };
91
- return _this;
92
- }
93
- _inheritsLoose(StyleButton, _Component);
94
- var _proto = StyleButton.prototype;
95
- _proto.render = function render() {
96
- var _this2 = this;
97
- var _this$props3 = this.props,
98
- i18n = _this$props3.i18n,
99
- active = _this$props3.active,
100
- label = _this$props3.label,
101
- title = _this$props3.title,
102
- hotKey = _this$props3.hotKey;
103
- var classNames = cx("editor-stylebutton", {
104
- active: active
105
- });
106
- return (
107
- /*#__PURE__*/
108
- // biome-ignore lint/a11y/noStaticElementInteractions: Ignore for now.
109
- React.createElement("div", {
110
- className: classNames,
111
- onMouseDown: this.onToggle,
112
- title: i18n(title) + hotKey
113
- // biome-ignore lint/a11y/noNoninteractiveTabindex: Ignore for now.
114
- ,
115
- tabIndex: 0,
116
- onKeyPress: function onKeyPress(event) {
117
- if (getKey(event) === Key.ENTER) {
118
- _this2.onToggleNoEvent();
119
- }
120
- }
121
- }, label)
122
- );
123
- };
124
- return StyleButton;
125
- }(Component);
126
- var StyleButton$1 = /*#__PURE__*/withI18n(StyleButton);
127
-
128
- var BlockTypeButtonControls = function BlockTypeButtonControls(props) {
129
- var editorState = props.editorState,
130
- blockTypes = props.blockTypes,
131
- onToggle = props.onToggle;
132
- var selection = editorState.getSelection();
133
- var blockTypeStyle = editorState.getCurrentContent().getBlockForKey(selection.getStartKey()).getType();
134
- return /*#__PURE__*/React.createElement("div", {
135
- className: "controls-group block-editor-controls"
136
- }, blockTypes.map(function (type) {
137
- return /*#__PURE__*/React.createElement(StyleButton$1, {
138
- key: type.style + type.label,
139
- active: type.style === blockTypeStyle,
140
- label: type.label,
141
- onToggle: onToggle,
142
- style: type.style,
143
- title: type.tooltip
144
- });
145
- }));
146
- };
147
-
148
- var BlockTypeLinkControls = /*#__PURE__*/function (_Component) {
149
- function BlockTypeLinkControls() {
150
- var _this;
151
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
152
- args[_key] = arguments[_key];
153
- }
154
- _this = _Component.call.apply(_Component, [this].concat(args)) || this;
155
- _this.state = {
156
- urlValue: "",
157
- showUrlInput: false,
158
- hasLink: false
159
- };
160
- _this.onUrlChange = function (e) {
161
- _this.setState({
162
- urlValue: e.target.value
163
- });
164
- };
165
- _this.addLink = function (e) {
166
- e.preventDefault();
167
- var editorState = _this.props.editorState;
168
- var selection = editorState.getSelection();
169
- if (!selection.isCollapsed()) {
170
- var currentContent = editorState.getCurrentContent();
171
- var startKey = selection.getStartKey();
172
- var startOffset = selection.getStartOffset();
173
- var blockWithLinkAtBeginning = currentContent.getBlockForKey(startKey);
174
- var linkKey = blockWithLinkAtBeginning.getEntityAt(startOffset);
175
- var url = "";
176
- if (linkKey) {
177
- var linkInstance = currentContent.getEntity(linkKey);
178
- url = linkInstance.getData().url;
179
- }
180
- _this.setState({
181
- showUrlInput: true,
182
- urlValue: url,
183
- hasLink: !!url
184
- });
185
- }
186
- };
187
- _this.confirmLink = function (e) {
188
- e.preventDefault();
189
- var urlValue = _this.state.urlValue;
190
- var _this$props = _this.props,
191
- editorState = _this$props.editorState,
192
- onToggle = _this$props.onToggle;
193
- var contentState = editorState.getCurrentContent();
194
- var contentStateWithEntity = contentState.createEntity("LINK", "MUTABLE", {
195
- url: urlValue
196
- });
197
- var entityKey = contentStateWithEntity.getLastCreatedEntityKey();
198
- var newEditorState = EditorState.set(editorState, {
199
- currentContent: contentStateWithEntity
200
- });
201
- onToggle(newEditorState, entityKey);
202
- _this.setState({
203
- showUrlInput: false,
204
- urlValue: ""
205
- });
206
- };
207
- _this.cancelLink = function (e) {
208
- e.preventDefault();
209
- _this.setState({
210
- showUrlInput: false,
211
- urlValue: ""
212
- });
213
- };
214
- _this.onLinkInputKeyDown = function (e) {
215
- if (getKey(e) === Key.ENTER) {
216
- _this.confirmLink(e);
217
- }
218
- };
219
- _this.removeLink = function (e) {
220
- e.preventDefault();
221
- var _this$props2 = _this.props,
222
- editorState = _this$props2.editorState,
223
- onToggle = _this$props2.onToggle;
224
- var hasLink = _this.state.hasLink;
225
- var selection = editorState.getSelection();
226
- if (!selection.isCollapsed() && hasLink) {
227
- onToggle(editorState, null);
228
- _this.setState({
229
- showUrlInput: false
230
- });
231
- }
232
- };
233
- return _this;
234
- }
235
- _inheritsLoose(BlockTypeLinkControls, _Component);
236
- var _proto = BlockTypeLinkControls.prototype;
237
- _proto.render = function render() {
238
- var i18n = this.props.i18n;
239
- var _this$state = this.state,
240
- showUrlInput = _this$state.showUrlInput,
241
- hasLink = _this$state.hasLink;
242
- return /*#__PURE__*/React.createElement("div", {
243
- className: "controls-group block-editor-controls link-controls"
244
- }, /*#__PURE__*/React.createElement("div", {
245
- className: "editor-stylebutton",
246
- title: i18n("styles.link"),
247
- onClick: this.addLink,
248
- tabIndex: 0
249
- }, /*#__PURE__*/React.createElement(LinkIcon, null)), showUrlInput && /*#__PURE__*/React.createElement("div", {
250
- className: "url-input-container"
251
- }, /*#__PURE__*/React.createElement("span", {
252
- className: "exit-button",
253
- onMouseDown: this.cancelLink
254
- }, /*#__PURE__*/React.createElement(CloseIcon, null)), /*#__PURE__*/React.createElement("div", {
255
- className: "link-label"
256
- }, i18n("common.from")), /*#__PURE__*/React.createElement("div", {
257
- className: "url-input-form"
258
- }, /*#__PURE__*/React.createElement(TextField, {
259
- style: {
260
- width: 250,
261
- margin: 5
262
- },
263
- onChange: this.onUrlChange,
264
- value: this.state.urlValue,
265
- onKeyDown: this.onLinkInputKeyDown,
266
- placeholder: "https://example.com"
267
- }), /*#__PURE__*/React.createElement(SecondarySquareButton, {
268
- onMouseDown: this.confirmLink
269
- }, /*#__PURE__*/React.createElement(CheckIcon, {
270
- width: 15,
271
- height: 15
272
- }))), hasLink && /*#__PURE__*/React.createElement("div", {
273
- className: "remove-link"
274
- }, /*#__PURE__*/React.createElement(Unbutton, {
275
- onClick: this.removeLink,
276
- title: i18n("styles.unlink")
277
- }, /*#__PURE__*/React.createElement(UnlinkIcon, null), i18n("styles.unlink")))));
278
- };
279
- return BlockTypeLinkControls;
280
- }(Component);
281
- BlockTypeLinkControls.propTypes = {
282
- editorState: PropTypes.object,
283
- onToggle: PropTypes.func
284
- };
285
- var BlockTypeLinkControls$1 = /*#__PURE__*/withI18n(BlockTypeLinkControls);
286
-
287
- var BlockTypeDropdown = /*#__PURE__*/function (_Component) {
288
- function BlockTypeDropdown() {
289
- var _this;
290
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
291
- args[_key] = arguments[_key];
292
- }
293
- _this = _Component.call.apply(_Component, [this].concat(args)) || this;
294
- _this.handleOnClick = function (selectedOption) {
295
- var onChange = _this.props.onChange;
296
- onChange(selectedOption);
297
- };
298
- return _this;
299
- }
300
- _inheritsLoose(BlockTypeDropdown, _Component);
301
- var _proto = BlockTypeDropdown.prototype;
302
- _proto.toItem = function toItem(option) {
303
- return {
304
- value: option.style,
305
- label: this.props.i18n(option.label)
306
- };
307
- };
308
- _proto.render = function render() {
309
- var _this2 = this;
310
- var _this$props = this.props,
311
- options = _this$props.options,
312
- selected = _this$props.selected,
313
- i18n = _this$props.i18n;
314
- return /*#__PURE__*/React.createElement(Dropdown, {
315
- className: "block-type-dropdown",
316
- items: options.map(function (option) {
317
- return _this2.toItem(option);
318
- }),
319
- selectedItem: this.toItem(selected),
320
- label: i18n("styles.style"),
321
- onChange: function onChange(selectedItem) {
322
- return _this2.handleOnClick(selectedItem.value);
323
- }
324
- });
325
- };
326
- return BlockTypeDropdown;
327
- }(Component);
328
- var BlockTypeDropdown$1 = /*#__PURE__*/withI18n(BlockTypeDropdown);
329
-
330
- var BlockTypeControls = function BlockTypeControls(_ref) {
331
- var editorState = _ref.editorState,
332
- blockTypes = _ref.blockTypes,
333
- onToggle = _ref.onToggle;
334
- var selection = editorState.getSelection();
335
- var blockTypeStyle = editorState.getCurrentContent().getBlockForKey(selection.getStartKey()).getType();
336
- var selectedOption = blockTypes.find(function (option) {
337
- return option.style === blockTypeStyle;
338
- }) || blockTypes[0];
339
- return /*#__PURE__*/React.createElement(BlockTypeDropdown$1, {
340
- options: blockTypes,
341
- selected: selectedOption,
342
- onChange: onToggle
343
- });
344
- };
345
-
346
- var InlineStyleControls = function InlineStyleControls(props) {
347
- var editorState = props.editorState,
348
- inlineStyles = props.inlineStyles;
349
- var currentStyle = editorState.getCurrentInlineStyle();
350
- return /*#__PURE__*/React.createElement("div", {
351
- className: "controls-group inline-editor-controls"
352
- }, inlineStyles.map(function (type) {
353
- return /*#__PURE__*/React.createElement(StyleButton$1, {
354
- key: type.style + type.label,
355
- active: currentStyle.has(type.style),
356
- label: type.label,
357
- onToggle: props.onToggle,
358
- style: type.style,
359
- title: type.tooltip,
360
- hotKey: type.hotKey
361
- });
362
- }));
363
- };
364
-
365
- var TagControls = /*#__PURE__*/function (_Component) {
366
- function TagControls() {
367
- var _this;
368
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
369
- args[_key] = arguments[_key];
370
- }
371
- _this = _Component.call.apply(_Component, [this].concat(args)) || this;
372
- _this.tagControlRef = /*#__PURE__*/createRef();
373
- _this.state = {
374
- isOpen: false
375
- };
376
- _this.handleClickOutside = function (e) {
377
- if (_this.tagControlRef.current && !_this.tagControlRef.current.contains(e.target)) {
378
- _this.setState({
379
- isOpen: false
380
- });
381
- }
382
- };
383
- _this.handleTagClick = function (tag, delimiter) {
384
- var _this$props = _this.props,
385
- editorState = _this$props.editorState,
386
- onInsertTag = _this$props.onInsertTag;
387
- var delimiterStart = delimiter[0];
388
- var delimiterEnd = delimiter[1];
389
- var fullTag = "" + delimiterStart + tag + delimiterEnd;
390
- onInsertTag(editorState, fullTag);
391
- };
392
- return _this;
393
- }
394
- _inheritsLoose(TagControls, _Component);
395
- var _proto = TagControls.prototype;
396
- _proto.componentDidMount = function componentDidMount() {
397
- document.addEventListener("mousedown", this.handleClickOutside);
398
- };
399
- _proto.componentWillUnmount = function componentWillUnmount() {
400
- document.removeEventListener("mousedown", this.handleClickOutside);
401
- };
402
- _proto.render = function render() {
403
- var _this2 = this;
404
- var _this$props2 = this.props,
405
- i18n = _this$props2.i18n,
406
- tags = _this$props2.tags;
407
- var isOpen = this.state.isOpen;
408
- var menuItems = tags.map(function (tag) {
409
- var delimiter = tag.delimiter || "{}";
410
- return /*#__PURE__*/React.createElement(OverflowMenuItem, {
411
- key: tag.tag,
412
- className: "tag-control",
413
- id: "tag-control-" + tag.tag,
414
- onClick: function onClick() {
415
- return _this2.handleTagClick(tag.tag, delimiter);
416
- }
417
- }, tag.icon, /*#__PURE__*/React.createElement("span", {
418
- className: "tag-control-text"
419
- }, tag.label));
420
- });
421
- return /*#__PURE__*/React.createElement("div", {
422
- className: "tag-control-dropdown",
423
- ref: this.tagControlRef
424
- }, /*#__PURE__*/React.createElement("div", {
425
- className: "tag-control-header",
426
- onClick: function onClick() {
427
- return _this2.setState(function (_ref) {
428
- var isOpen = _ref.isOpen;
429
- return {
430
- isOpen: !isOpen
431
- };
432
- });
433
- }
434
- }, i18n("common.insert"), /*#__PURE__*/React.createElement(DownArrowIcon, {
435
- className: "tag-control-chevron",
436
- inline: true
437
- })), /*#__PURE__*/React.createElement(Popover, {
438
- open: isOpen,
439
- className: "tag-control-content"
440
- }, /*#__PURE__*/React.createElement(OverflowMenu, {
441
- className: "action-menu"
442
- }, menuItems)));
443
- };
444
- return TagControls;
445
- }(Component);
446
- TagControls.propTypes = {
447
- editorState: PropTypes.object,
448
- tags: PropTypes.arrayOf(PropTypes.object).isRequired,
449
- onInsertTag: PropTypes.func.isRequired
450
- };
451
- var TagControls$1 = /*#__PURE__*/withI18n(TagControls);
452
-
453
- var HtmlLink = function HtmlLink(_ref) {
454
- var url = _ref.url,
455
- children = _ref.children;
456
- return /*#__PURE__*/React.createElement("a", {
457
- href: url
458
- }, children);
459
- };
460
- function findLinkEntities(contentBlock, callback, contentState) {
461
- contentBlock.findEntityRanges(function (character) {
462
- var entityKey = character.getEntity();
463
- return entityKey !== null && contentState.getEntity(entityKey).getType() === "LINK";
464
- }, callback);
465
- }
466
- var Link = function Link(props) {
467
- var _props$contentState$g = props.contentState.getEntity(props.entityKey).getData(),
468
- url = _props$contentState$g.url;
469
- return /*#__PURE__*/React.createElement(HtmlLink, {
470
- url: url
471
- }, props.children);
472
- };
473
-
474
- var EditorConfigTypes = /*#__PURE__*/Object.freeze({
475
- FONT_LIST: {
476
- UNSTYLED: "unstyled",
477
- HEADER_ONE: "header-one",
478
- HEADER_TWO: "header-two",
479
- HEADER_THREE: "header-three",
480
- HEADER_FOUR: "header-four",
481
- HEADER_FIVE: "header-five",
482
- HEADER_SIX: "header-six"
483
- },
484
- BLOCK_TYPES: {
485
- UNORDERED_LIST: "unordered-list",
486
- ORDERED_LIST: "ordered-list"
487
- },
488
- INLINE_STYLES: {
489
- BOLD: "bold",
490
- ITALIC: "italic",
491
- UNDERLINE: "underline"
492
- }
493
- });
494
- var FontList = /*#__PURE__*/Object.freeze({
495
- unstyled: {
496
- label: "styles.p",
497
- style: "unstyled"
498
- },
499
- "header-one": {
500
- label: "styles.h1",
501
- style: "header-one"
502
- },
503
- "header-two": {
504
- label: "styles.h2",
505
- style: "header-two"
506
- },
507
- "header-three": {
508
- label: "styles.h3",
509
- style: "header-three"
510
- },
511
- "header-four": {
512
- label: "styles.h4",
513
- style: "header-four"
514
- },
515
- "header-five": {
516
- label: "styles.h5",
517
- style: "header-five"
518
- },
519
- "header-six": {
520
- label: "styles.h6",
521
- style: "header-six"
522
- }
523
- });
524
- var BlockTypeList = /*#__PURE__*/Object.freeze({
525
- "unordered-list": {
526
- label: /*#__PURE__*/React.createElement(BulletListIcon, null),
527
- style: "unordered-list-item",
528
- tooltip: "styles.unordered_list_item"
529
- },
530
- "ordered-list": {
531
- label: /*#__PURE__*/React.createElement(NumberListIcon, null),
532
- style: "ordered-list-item",
533
- tooltip: "styles.ordered_list_item"
534
- }
535
- });
536
- var InlineStylesList = /*#__PURE__*/Object.freeze({
537
- bold: {
538
- label: /*#__PURE__*/React.createElement(BoldIcon, null),
539
- style: "BOLD",
540
- tooltip: "styles.bold",
541
- hotKey: "(Ctrl+B)"
542
- },
543
- italic: {
544
- label: /*#__PURE__*/React.createElement(ItalicIcon, null),
545
- style: "ITALIC",
546
- tooltip: "styles.italic",
547
- hotKey: "(Ctrl+I)"
548
- },
549
- underline: {
550
- label: /*#__PURE__*/React.createElement(UnderlineIcon, null),
551
- style: "UNDERLINE",
552
- tooltip: "styles.underline",
553
- hotKey: "(Ctrl+U)"
554
- }
555
- });
556
- var MinifiedConfigTypes = /*#__PURE__*/Object.freeze({
557
- fontList: [{
558
- type: "unstyled",
559
- label: "styles.p",
560
- style: "unstyled"
561
- }, {
562
- type: "header-two",
563
- label: "styles.h1",
564
- style: "header-two"
565
- }],
566
- blockTypes: /*#__PURE__*/Object.values(EditorConfigTypes.BLOCK_TYPES),
567
- inlineStyles: [EditorConfigTypes.INLINE_STYLES.BOLD, EditorConfigTypes.INLINE_STYLES.ITALIC]
568
- });
569
- var decorator = /*#__PURE__*/new CompositeDecorator([{
570
- strategy: findLinkEntities,
571
- component: Link
572
- }]);
573
-
574
- var RichTextEditor = /*#__PURE__*/function (_Component) {
575
- function RichTextEditor() {
576
- var _this;
577
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
578
- args[_key] = arguments[_key];
579
- }
580
- _this = _Component.call.apply(_Component, [this].concat(args)) || this;
581
- _this.wrapperRef = /*#__PURE__*/createRef();
582
- _this.editor = /*#__PURE__*/createRef();
583
- _this.handleKeyCommand = function (command, editorState) {
584
- var newState = RichUtils.handleKeyCommand(editorState, command);
585
- if (newState) {
586
- _this.props.onChange(newState);
587
- return true;
588
- }
589
- return false;
590
- };
591
- _this.keyBindingFn = function (evt) {
592
- switch (evt.keyCode) {
593
- case 9:
594
- return _this.onTab(evt);
595
- default:
596
- return getDefaultKeyBinding(evt);
597
- }
598
- };
599
- _this.onTab = function (evt) {
600
- evt.stopPropagation();
601
- var _this$props = _this.props,
602
- maxTabDepth = _this$props.maxTabDepth,
603
- editorState = _this$props.editorState,
604
- onChange = _this$props.onChange;
605
- onChange(RichUtils.onTab(evt, editorState, maxTabDepth));
606
- };
607
- _this.toggleButtonBlockType = function (blockType) {
608
- var _this$props2 = _this.props,
609
- editorState = _this$props2.editorState,
610
- onChange = _this$props2.onChange;
611
- onChange(RichUtils.toggleBlockType(editorState, blockType));
612
- };
613
- _this.toggleSelectBlockType = function (blockType) {
614
- var _this$props3 = _this.props,
615
- editorState = _this$props3.editorState,
616
- onChange = _this$props3.onChange;
617
- onChange(RichUtils.toggleBlockType(editorState, blockType));
618
- };
619
- _this.toggleInlineStyle = function (inlineStyle) {
620
- var _this$props4 = _this.props,
621
- editorState = _this$props4.editorState,
622
- onChange = _this$props4.onChange;
623
- onChange(RichUtils.toggleInlineStyle(editorState, inlineStyle));
624
- };
625
- _this.toggleLink = function (newEditorState, entityKey) {
626
- _this.props.onChange(RichUtils.toggleLink(newEditorState, newEditorState.getSelection(), entityKey));
627
- };
628
- _this.insertText = function (editorState, text) {
629
- var newContent = Modifier.insertText(editorState.getCurrentContent(), editorState.getSelection(), text);
630
- _this.props.onChange(EditorState.push(editorState, newContent, "insert-characters"), function () {
631
- return _this.focusEditor();
632
- });
633
- };
634
- _this.getFontListConfigForPanelType = function (panelType) {
635
- var controlConfig = _this.getControlConfigBySet(_this.props.controlConfigSet);
636
- return controlConfig[panelType];
637
- };
638
- _this.getControlConfigBySet = function (config) {
639
- var fontList = ((config == null ? void 0 : config.fontList) || Object.keys(FontList)).filter(function (f) {
640
- return typeof f === "object" ? FontList[f.type] : FontList[f];
641
- }).map(function (f) {
642
- return f.label && f.style ? f : FontList[f];
643
- });
644
- var blockTypes = ((config == null ? void 0 : config.blockTypes) || Object.keys(BlockTypeList)).filter(function (f) {
645
- return typeof f === "object" ? BlockTypeList[f.type] : BlockTypeList[f];
646
- }).map(function (f) {
647
- return f.label && f.style ? f : BlockTypeList[f];
648
- });
649
- var inlineStyles = ((config == null ? void 0 : config.inlineStyles) || Object.keys(InlineStylesList)).filter(function (f) {
650
- return typeof f === "object" ? InlineStylesList[f.type] : InlineStylesList[f];
651
- }).map(function (f) {
652
- return f.label && f.style ? f : InlineStylesList[f];
653
- });
654
- return {
655
- fontList: fontList,
656
- blockTypes: blockTypes,
657
- inlineStyles: inlineStyles
658
- };
659
- };
660
- return _this;
661
- }
662
- _inheritsLoose(RichTextEditor, _Component);
663
- var _proto = RichTextEditor.prototype;
664
- _proto.focusEditor = function focusEditor() {
665
- var _this$editor$current;
666
- (_this$editor$current = this.editor.current) == null || _this$editor$current.focus();
667
- };
668
- _proto.render = function render() {
669
- var _this$props5 = this.props,
670
- className = _this$props5.className,
671
- showFontMenu = _this$props5.showFontMenu,
672
- showInlineStyleMenu = _this$props5.showInlineStyleMenu,
673
- showTextFormatMenu = _this$props5.showTextFormatMenu,
674
- showLinkMenu = _this$props5.showLinkMenu,
675
- label = _this$props5.label,
676
- tags = _this$props5.tags,
677
- editorState = _this$props5.editorState,
678
- onChange = _this$props5.onChange,
679
- handleBeforeInput = _this$props5.handleBeforeInput;
680
- var classNames = cx("editor-root", className);
681
- return /*#__PURE__*/React.createElement("div", {
682
- ref: this.wrapperRef
683
- }, label && /*#__PURE__*/React.createElement(Label, null, label), /*#__PURE__*/React.createElement("div", {
684
- className: classNames
685
- }, /*#__PURE__*/React.createElement("div", {
686
- className: "controls-wrapper"
687
- }, showFontMenu && /*#__PURE__*/React.createElement(BlockTypeControls, {
688
- editorState: editorState,
689
- onToggle: this.toggleSelectBlockType,
690
- blockTypes: this.getFontListConfigForPanelType("fontList")
691
- }), showInlineStyleMenu && /*#__PURE__*/React.createElement(InlineStyleControls, {
692
- editorState: editorState,
693
- onToggle: this.toggleInlineStyle,
694
- inlineStyles: this.getFontListConfigForPanelType("inlineStyles")
695
- }), showTextFormatMenu && /*#__PURE__*/React.createElement(BlockTypeButtonControls, {
696
- editorState: editorState,
697
- onToggle: this.toggleButtonBlockType,
698
- blockTypes: this.getFontListConfigForPanelType("blockTypes")
699
- }), showLinkMenu && /*#__PURE__*/React.createElement(BlockTypeLinkControls$1, {
700
- editorState: editorState,
701
- onToggle: this.toggleLink
702
- }), tags && tags.length > 0 && /*#__PURE__*/React.createElement(TagControls$1, {
703
- editorState: editorState,
704
- tags: tags,
705
- onInsertTag: this.insertText
706
- })), /*#__PURE__*/React.createElement("div", {
707
- className: "editor-wrapper"
708
- }, /*#__PURE__*/React.createElement(Editor, {
709
- ref: this.editor,
710
- editorState: editorState,
711
- handleKeyCommand: this.handleKeyCommand,
712
- onChange: onChange,
713
- handleBeforeInput: handleBeforeInput,
714
- keyBindingFn: this.keyBindingFn,
715
- spellCheck: true
716
- }))));
717
- };
718
- return RichTextEditor;
719
- }(Component);
720
- RichTextEditor.decorator = decorator;
721
- var styleShape = function styleShape(configList) {
722
- return PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOf([].concat(Object.values(configList)))), PropTypes.arrayOf(PropTypes.shape({
723
- type: PropTypes.oneOf([].concat(Object.values(configList))),
724
- label: PropTypes.string,
725
- style: PropTypes.string,
726
- tooltip: PropTypes.string,
727
- hotKey: PropTypes.string
728
- }))]);
729
- };
730
- RichTextEditor.propTypes = {
731
- editorState: PropTypes.object.isRequired,
732
- onChange: PropTypes.func.isRequired,
733
- maxTabDepth: PropTypes.number,
734
- showFontMenu: PropTypes.bool,
735
- showInlineStyleMenu: PropTypes.bool,
736
- showTextFormatMenu: PropTypes.bool,
737
- showLinkMenu: PropTypes.bool,
738
- label: PropTypes.string,
739
- tags: /*#__PURE__*/PropTypes.arrayOf(PropTypes.object),
740
- controlConfigSet: /*#__PURE__*/PropTypes.shape({
741
- fontList: /*#__PURE__*/styleShape(EditorConfigTypes.FONT_LIST),
742
- blockTypes: /*#__PURE__*/styleShape(EditorConfigTypes.BLOCK_TYPES),
743
- inlineStyles: /*#__PURE__*/styleShape(EditorConfigTypes.INLINE_STYLES)
744
- })
745
- };
746
- RichTextEditor.defaultProps = {
747
- maxTabDepth: 4,
748
- showFontMenu: true,
749
- showInlineStyleMenu: true,
750
- showTextFormatMenu: true,
751
- showLinkMenu: true,
752
- label: ""
753
- };
754
-
755
- var editorStateToMarkdown = function editorStateToMarkdown(editorState) {
756
- var rawState = convertToRaw(editorState.getCurrentContent());
757
- return draftToMarkdown(rawState, {
758
- preserveNewlines: true
759
- });
760
- };
761
- var markdownToEditorState = function markdownToEditorState(markdown) {
762
- if (!markdown) {
763
- return EditorState.createEmpty(decorator);
764
- }
765
- var rawState = markdownToDraft(markdown, {
766
- preserveNewlines: true
767
- });
768
- var contentState = convertFromRaw(rawState);
769
- return EditorState.createWithContent(contentState, decorator);
770
- };
771
- var converter = /*#__PURE__*/new showdown.Converter({
772
- underline: true
773
- });
774
- var markdownToHtml = function markdownToHtml(markdown) {
775
- return converter.makeHtml(markdown);
776
- };
777
-
778
- var _excluded = ["name", "title", "languages"];
779
- var RichTextEditorForLanguage = function RichTextEditorForLanguage(_ref) {
780
- var _ref$language = _ref.language,
781
- langKey = _ref$language.value,
782
- label = _ref$language.label,
783
- required = _ref$language.required,
784
- values = _ref.values,
785
- onChange = _ref.onChange,
786
- variant = _ref.variant,
787
- feedback = _ref.feedback,
788
- tags = _ref.tags;
789
- var _useState = useState(markdownToEditorState(values[langKey])),
790
- editorState = _useState[0],
791
- setEditorState = _useState[1];
792
- var handleOnChange = function handleOnChange(changedState) {
793
- var newValues = _extends({}, values);
794
- var content = editorStateToMarkdown(changedState);
795
- if (values[langKey] && content === "\n") {
796
- delete newValues[langKey];
797
- } else {
798
- newValues[langKey] = content;
799
- }
800
- setEditorState(changedState);
801
- onChange(newValues);
802
- };
803
- var feedbackText = feedback == null ? void 0 : feedback(langKey);
804
- return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(RichTextEditor, {
805
- label: required ? label + "*" : label,
806
- editorState: editorState,
807
- onChange: function onChange(changedState) {
808
- return handleOnChange(changedState);
809
- },
810
- controlConfigSet: MinifiedConfigTypes,
811
- tags: tags
812
- }), variant && feedbackText && feedbackText.length > 0 && (/*#__PURE__*/React.createElement(FeedbackText, {
813
- variant: variant(langKey)
814
- }, feedbackText)));
815
- };
816
- var ExpandableMultiLanguageRichTextEditor = function ExpandableMultiLanguageRichTextEditor(_ref2) {
817
- var name = _ref2.name,
818
- title = _ref2.title,
819
- languages = _ref2.languages,
820
- rest = _objectWithoutPropertiesLoose(_ref2, _excluded);
821
- return /*#__PURE__*/React.createElement(ExpandablePanel, {
822
- title: title,
823
- defaultOpen: true,
824
- contentStyle: {
825
- padding: "4px 4px 4px 4px",
826
- marginTop: "16px",
827
- marginBottom: "16px"
828
- }
829
- }, /*#__PURE__*/React.createElement(Stack, {
830
- space: "medium"
831
- }, languages.map(function (lang) {
832
- return /*#__PURE__*/React.createElement("div", {
833
- key: name + lang.value,
834
- "data-testid": "multi-lang-rich-text-" + name + "-" + lang.value
835
- }, /*#__PURE__*/React.createElement(RichTextEditorForLanguage, _extends({
836
- language: lang
837
- }, rest)));
838
- })));
839
- };
840
-
841
- var htmlToEntity = {
842
- htmlToEntity: function htmlToEntity(nodeName, node, createEntity) {
843
- if (nodeName === "a") {
844
- return createEntity("LINK", "MUTABLE", {
845
- url: node.href
846
- });
847
- }
848
- }
849
- };
850
- var entityToHtml = {
851
- entityToHTML: function entityToHTML(entity, originalText) {
852
- if (entity.type === "LINK") {
853
- return /*#__PURE__*/React.createElement(HtmlLink, {
854
- url: entity.data.url
855
- }, originalText);
856
- }
857
- return originalText;
858
- }
859
- };
860
- var htmlToEditorState = function htmlToEditorState(html) {
861
- if (!html) {
862
- return EditorState.createEmpty(decorator);
863
- }
864
- var rawState = convertFromHTML(htmlToEntity)(html);
865
- return EditorState.createWithContent(rawState, decorator);
866
- };
867
- var editorStateToHtml = function editorStateToHtml(editorState) {
868
- return convertToHTML(entityToHtml)(editorState.getCurrentContent());
869
- };
870
-
871
- export { EditorConfigTypes, ExpandableMultiLanguageRichTextEditor, MinifiedConfigTypes, RichTextEditor, editorStateToHtml, editorStateToMarkdown, htmlToEditorState, markdownToEditorState, markdownToHtml };
872
- //# sourceMappingURL=rich-text-editor.esm.js.map