@pushwoosh/dumb-components 4.18.2 → 4.19.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,3 +1,6 @@
1
- import { type Placement } from '@floating-ui/react-dom';
2
- import { type ArrowDirection } from './RichMessageEditor.types';
1
+ import { type Placement, type ReferenceElement } from '@floating-ui/react-dom';
2
+ import { type ArrowDirection, type DropValue } from './RichMessageEditor.types';
3
+ import { type CustomEditor } from './slate/custom-types';
3
4
  export declare function placementToDirection(placement: Placement): ArrowDirection;
5
+ /** Element the drop is positioned against: the edited chip, the emoji action or the caret where a new chip goes. */
6
+ export declare function getDropAnchor(editor: CustomEditor, drop: DropValue, box: HTMLElement, emojiAction: HTMLElement | null): ReferenceElement;
@@ -1,3 +1,5 @@
1
+ import { Node as SlateNode } from 'slate';
2
+ import { ReactEditor } from 'slate-react';
1
3
  export function placementToDirection(placement) {
2
4
  if (placement.startsWith('left')) {
3
5
  return 'right';
@@ -9,4 +11,33 @@ export function placementToDirection(placement) {
9
11
  return 'bottom';
10
12
  }
11
13
  return 'top';
14
+ }
15
+ function attempt(read) {
16
+ try {
17
+ return read();
18
+ } catch {
19
+ return undefined;
20
+ }
21
+ }
22
+ function caretReference(editor, box) {
23
+ return {
24
+ contextElement: box,
25
+ getBoundingClientRect: () => {
26
+ const {
27
+ selection
28
+ } = editor;
29
+ const rect = selection ? attempt(() => ReactEditor.toDOMRange(editor, selection).getBoundingClientRect()) : undefined;
30
+ return rect && rect.height > 0 ? rect : box.getBoundingClientRect();
31
+ }
32
+ };
33
+ }
34
+ function nodeReference(editor, path, box) {
35
+ return attempt(() => ReactEditor.toDOMNode(editor, SlateNode.get(editor, path))) ?? box;
36
+ }
37
+ /** Element the drop is positioned against: the edited chip, the emoji action or the caret where a new chip goes. */
38
+ export function getDropAnchor(editor, drop, box, emojiAction) {
39
+ if (drop.type === 'emoji') {
40
+ return emojiAction ?? box;
41
+ }
42
+ return drop.path ? nodeReference(editor, drop.path, box) : caretReference(editor, box);
12
43
  }
@@ -1,18 +1,16 @@
1
- import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
- import { arrow, autoPlacement, offset, useFloating } from '@floating-ui/react-dom';
1
+ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
3
2
  import { isEqual } from 'es-toolkit';
4
3
  import { useEffect, useState } from 'react';
5
4
  import { Transforms } from 'slate';
6
5
  import { Collapse, elementAddListener, Horizontal, usePersistentFunction } from '@pushwoosh/kit-helpers';
7
6
  import { FieldBox } from '../FieldBox';
8
- import ArrowIcon from './assets/arrow.svg';
9
7
  import EmojiIcon from './assets/EmojiIcon.svg';
10
8
  import PersonalizationIcon from './assets/PersonalizationIcon.svg';
11
9
  import { EditorEmojiPicker } from './EditorEmojiPicker';
12
- import { placementToDirection } from './RichMessageEditor.helpers';
10
+ import { RichMessageEditorDrop } from './RichMessageEditorDrop';
13
11
  import { getDcByPath } from './slate/helpers';
14
12
  import { SlateEditable, SlateEditor } from './slate/SlateEditor';
15
- import { Arrow, DropArea, DropContent, ImSpan } from './styles';
13
+ import { DropContent, ImSpan } from './styles';
16
14
  /**
17
15
  * Slate-based message editor with optional personalization (dynamic-content) tokens
18
16
  * and an emoji picker; serializes to a string via `onChange`. Render the result with
@@ -38,25 +36,8 @@ export const RichMessageEditor = ({
38
36
  }) => {
39
37
  const [showDrop, setShowDrop] = useState();
40
38
  const [isFocused, setIsFocused] = useState(false);
41
- const [arrowElement, setArrowElement] = useState(null);
42
- const {
43
- refs: {
44
- reference,
45
- setReference,
46
- setFloating
47
- },
48
- floatingStyles,
49
- update,
50
- middlewareData,
51
- placement
52
- } = useFloating({
53
- strategy: 'fixed',
54
- middleware: [offset(8), autoPlacement({
55
- allowedPlacements: ['right', 'right-start', 'right-end', 'left', 'left-start', 'left-end']
56
- }), arrow({
57
- element: arrowElement
58
- })]
59
- });
39
+ const [box, setBox] = useState(null);
40
+ const [emojiAction, setEmojiAction] = useState(null);
60
41
  const onSelectionChange = (selection, isFocusing = false) => {
61
42
  if (disabled) {
62
43
  return;
@@ -103,11 +84,11 @@ export const RichMessageEditor = ({
103
84
  return;
104
85
  }
105
86
  return elementAddListener(document, ['mousedown', 'touchstart', 'focusin'], event => {
106
- if (reference.current instanceof HTMLElement && !reference.current.contains(event.target)) {
87
+ if (box && !box.contains(event.target)) {
107
88
  clickAwayHandler();
108
89
  }
109
90
  });
110
- }, [clickAwayHandler, isFocused, reference]);
91
+ }, [clickAwayHandler, isFocused, box]);
111
92
  return _jsx(SlateEditor, {
112
93
  initialValue: initialValue,
113
94
  onChange: onChange,
@@ -116,7 +97,7 @@ export const RichMessageEditor = ({
116
97
  editor
117
98
  }) => _jsxs(FieldBox, {
118
99
  title: title,
119
- boxRef: setReference,
100
+ boxRef: setBox,
120
101
  error: error,
121
102
  actions: _jsx(Collapse, {
122
103
  expanded: isFocused,
@@ -128,6 +109,7 @@ export const RichMessageEditor = ({
128
109
  }),
129
110
  children: [_jsx(PersonalizationIcon, {}), "Personalization"]
130
111
  }), canUseEmoji && _jsxs(ImSpan, {
112
+ ref: setEmojiAction,
131
113
  onClick: () => setShowDrop({
132
114
  type: 'emoji'
133
115
  }),
@@ -145,37 +127,33 @@ export const RichMessageEditor = ({
145
127
  onDelete: () => {
146
128
  setShowDrop(undefined);
147
129
  }
148
- }), showDrop && _jsxs(DropArea, {
149
- style: floatingStyles,
150
- ref: setFloating,
151
- children: [showDrop.type === 'personalization' && _jsx(DropContent, {
152
- children: renderDynamicContent?.({
153
- dc: showDrop.path ? getDcByPath(editor.children, showDrop.path)?.dc : undefined,
154
- onChange: newDC => {
155
- if (showDrop.path) {
156
- Transforms.setNodes(editor, {
157
- dc: newDC
158
- }, {
159
- at: showDrop.path
160
- });
161
- } else {
162
- insertDynamicContent(editor, newDC);
163
- }
164
- },
165
- onClose,
166
- update
167
- })
168
- }), showDrop.type === 'emoji' && _jsx(EditorEmojiPicker, {
169
- onEmojiSelect: s => editor.insertText(s)
170
- }), _jsx(Arrow, {
171
- "$direction": placementToDirection(placement),
172
- ref: setArrowElement,
173
- style: {
174
- left: middlewareData.arrow?.x,
175
- top: middlewareData.arrow?.y
176
- },
177
- children: _jsx(ArrowIcon, {})
178
- })]
130
+ }), showDrop && box && _jsx(RichMessageEditorDrop, {
131
+ editor: editor,
132
+ drop: showDrop,
133
+ box: box,
134
+ emojiAction: emojiAction,
135
+ children: update => _jsxs(_Fragment, {
136
+ children: [showDrop.type === 'personalization' && _jsx(DropContent, {
137
+ children: renderDynamicContent?.({
138
+ dc: showDrop.path ? getDcByPath(editor.children, showDrop.path)?.dc : undefined,
139
+ onChange: newDC => {
140
+ if (showDrop.path) {
141
+ Transforms.setNodes(editor, {
142
+ dc: newDC
143
+ }, {
144
+ at: showDrop.path
145
+ });
146
+ } else {
147
+ insertDynamicContent(editor, newDC);
148
+ }
149
+ },
150
+ onClose,
151
+ update
152
+ })
153
+ }), showDrop.type === 'emoji' && _jsx(EditorEmojiPicker, {
154
+ onEmojiSelect: s => editor.insertText(s)
155
+ })]
156
+ })
179
157
  }, showDrop.type)]
180
158
  })
181
159
  });
@@ -0,0 +1,12 @@
1
+ import { type FC, type ReactNode } from 'react';
2
+ import { type DropValue } from './RichMessageEditor.types';
3
+ import { type CustomEditor } from './slate/custom-types';
4
+ type RichMessageEditorDropProps = {
5
+ editor: CustomEditor;
6
+ drop: DropValue;
7
+ box: HTMLElement;
8
+ emojiAction: HTMLElement | null;
9
+ children: (update: () => void) => ReactNode;
10
+ };
11
+ export declare const RichMessageEditorDrop: FC<RichMessageEditorDropProps>;
12
+ export {};
@@ -0,0 +1,52 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { arrow, autoUpdate, flip, offset, shift, useFloating } from '@floating-ui/react-dom';
3
+ import { useMemo, useState } from 'react';
4
+ import ArrowIcon from './assets/arrow.svg';
5
+ import { getDropAnchor, placementToDirection } from './RichMessageEditor.helpers';
6
+ import { Arrow, DropArea } from './styles';
7
+ export const RichMessageEditorDrop = ({
8
+ editor,
9
+ drop,
10
+ box,
11
+ emojiAction,
12
+ children
13
+ }) => {
14
+ const [arrowElement, setArrowElement] = useState(null);
15
+ const anchor = useMemo(() => getDropAnchor(editor, drop, box, emojiAction), [editor, drop, box, emojiAction]);
16
+ const {
17
+ refs: {
18
+ setFloating
19
+ },
20
+ floatingStyles,
21
+ update,
22
+ middlewareData,
23
+ placement
24
+ } = useFloating({
25
+ strategy: 'fixed',
26
+ placement: 'bottom',
27
+ elements: {
28
+ reference: anchor
29
+ },
30
+ whileElementsMounted: autoUpdate,
31
+ middleware: [offset(8), flip({
32
+ fallbackPlacements: ['top', 'right', 'left']
33
+ }), shift({
34
+ padding: 8
35
+ }), arrow({
36
+ element: arrowElement
37
+ })]
38
+ });
39
+ return _jsxs(DropArea, {
40
+ style: floatingStyles,
41
+ ref: setFloating,
42
+ children: [children(update), _jsx(Arrow, {
43
+ "$direction": placementToDirection(placement),
44
+ ref: setArrowElement,
45
+ style: {
46
+ left: middlewareData.arrow?.x,
47
+ top: middlewareData.arrow?.y
48
+ },
49
+ children: _jsx(ArrowIcon, {})
50
+ })]
51
+ });
52
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pushwoosh/dumb-components",
3
- "version": "4.18.2",
3
+ "version": "4.19.0",
4
4
  "description": "React components to build Pushwoosh products",
5
5
  "main": "index.js",
6
6
  "module": "index.js",