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