@eccenca/gui-elements 25.0.0-rc.2 → 25.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.
Files changed (37) hide show
  1. package/CHANGELOG.md +25 -18
  2. package/dist/cjs/common/index.js +3 -1
  3. package/dist/cjs/common/index.js.map +1 -1
  4. package/dist/cjs/common/utils/reduceToText.js +26 -1
  5. package/dist/cjs/common/utils/reduceToText.js.map +1 -1
  6. package/dist/cjs/components/ContextOverlay/ContextMenu.js +2 -2
  7. package/dist/cjs/components/ContextOverlay/ContextMenu.js.map +1 -1
  8. package/dist/cjs/components/ContextOverlay/ContextOverlay.js +62 -24
  9. package/dist/cjs/components/ContextOverlay/ContextOverlay.js.map +1 -1
  10. package/dist/cjs/components/TextReducer/TextReducer.js +15 -3
  11. package/dist/cjs/components/TextReducer/TextReducer.js.map +1 -1
  12. package/dist/esm/common/index.js +3 -1
  13. package/dist/esm/common/index.js.map +1 -1
  14. package/dist/esm/common/utils/reduceToText.js +37 -1
  15. package/dist/esm/common/utils/reduceToText.js.map +1 -1
  16. package/dist/esm/components/ContextOverlay/ContextMenu.js +2 -2
  17. package/dist/esm/components/ContextOverlay/ContextMenu.js.map +1 -1
  18. package/dist/esm/components/ContextOverlay/ContextOverlay.js +66 -28
  19. package/dist/esm/components/ContextOverlay/ContextOverlay.js.map +1 -1
  20. package/dist/esm/components/TextReducer/TextReducer.js +14 -3
  21. package/dist/esm/components/TextReducer/TextReducer.js.map +1 -1
  22. package/dist/types/common/index.d.ts +2 -0
  23. package/dist/types/common/utils/reduceToText.d.ts +1 -1
  24. package/dist/types/components/ContextOverlay/ContextMenu.d.ts +1 -1
  25. package/dist/types/components/TextReducer/TextReducer.d.ts +13 -1
  26. package/package.json +5 -3
  27. package/src/common/index.ts +6 -2
  28. package/src/common/utils/reduceToText.tsx +30 -2
  29. package/src/components/ContextOverlay/ContextMenu.tsx +4 -1
  30. package/src/components/ContextOverlay/ContextOverlay.tsx +74 -24
  31. package/src/components/ContextOverlay/tests/ContextMenu.test.tsx +43 -0
  32. package/src/components/ContextOverlay/tests/ContextOverlay.test.tsx +71 -0
  33. package/src/components/Dialog/stories/Modal.stories.tsx +12 -150
  34. package/src/components/Dialog/stories/ModalContext.stories.tsx +153 -0
  35. package/src/components/TextReducer/TextReducer.stories.tsx +2 -1
  36. package/src/components/TextReducer/TextReducer.test.tsx +44 -0
  37. package/src/components/TextReducer/TextReducer.tsx +14 -4
@@ -37,56 +37,94 @@ var __read = (this && this.__read) || function (o, n) {
37
37
  return ar;
38
38
  };
39
39
  import React from "react";
40
- import { Classes as BlueprintClasses, Popover as BlueprintPopover, Utils as BlueprintUtils, } from "@blueprintjs/core";
40
+ import { Classes as BlueprintClasses, Popover as BlueprintPopover, PopoverInteractionKind as InteractionKind, Utils as BlueprintUtils, } from "@blueprintjs/core";
41
41
  import { CLASSPREFIX as eccgui } from "../../configuration/constants.js";
42
42
  /**
43
43
  * Element displays connected content by interacting with a target element.
44
44
  * Full list of available option can be seen at https://blueprintjs.com/docs/#popover2-package/popover2
45
45
  */
46
46
  export var ContextOverlay = function (_a) {
47
- var _b;
48
- var children = _a.children, portalClassName = _a.portalClassName, preventTopPosition = _a.preventTopPosition, _c = _a.className, className = _c === void 0 ? "" : _c, _d = _a.usePlaceholder, usePlaceholder = _d === void 0 ? false : _d, otherPopoverProps = __rest(_a, ["children", "portalClassName", "preventTopPosition", "className", "usePlaceholder"]);
47
+ var _b, _c;
48
+ var children = _a.children, portalClassName = _a.portalClassName, preventTopPosition = _a.preventTopPosition, _d = _a.className, className = _d === void 0 ? "" : _d, _e = _a.usePlaceholder, usePlaceholder = _e === void 0 ? false : _e, otherPopoverProps = __rest(_a, ["children", "portalClassName", "preventTopPosition", "className", "usePlaceholder"]);
49
49
  var placeholderRef = React.useRef(null);
50
50
  var eventMemory = React.useRef(undefined);
51
51
  var swapDelay = React.useRef(null);
52
+ var interactionKind = React.useRef((_b = otherPopoverProps.interactionKind) !== null && _b !== void 0 ? _b : InteractionKind.CLICK);
52
53
  var swapDelayTime = 15;
53
- var _e = __read(React.useState(
54
+ var _f = __read(React.useState(
54
55
  // use placeholder only for "simple" overlays without special states
55
56
  !otherPopoverProps.disabled &&
56
57
  !otherPopoverProps.defaultIsOpen &&
57
58
  !otherPopoverProps.isOpen &&
58
59
  otherPopoverProps.renderTarget === undefined &&
59
- usePlaceholder), 2), placeholder = _e[0], setPlaceholder = _e[1];
60
+ usePlaceholder), 2), placeholder = _f[0], setPlaceholder = _f[1];
61
+ var swap = function (ev) {
62
+ var waitForClick = interactionKind.current === InteractionKind.CLICK ||
63
+ interactionKind.current === InteractionKind.CLICK_TARGET_ONLY;
64
+ if (swapDelay.current) {
65
+ clearTimeout(swapDelay.current);
66
+ }
67
+ var replacePlaceholder = function () {
68
+ eventMemory.current = ev.type;
69
+ setPlaceholder(false);
70
+ };
71
+ if (waitForClick) {
72
+ ev.stopImmediatePropagation();
73
+ replacePlaceholder();
74
+ return;
75
+ }
76
+ swapDelay.current = setTimeout(replacePlaceholder,
77
+ // we delay the swap for hover/focus to prevent unwanted effects
78
+ // (e.g. event hickup after replacing elements when it is not really necessary)
79
+ swapDelayTime);
80
+ };
60
81
  React.useEffect(function () {
82
+ var _a;
83
+ interactionKind.current = (_a = otherPopoverProps.interactionKind) !== null && _a !== void 0 ? _a : InteractionKind.CLICK;
84
+ var waitForClick = interactionKind.current === InteractionKind.CLICK ||
85
+ interactionKind.current === InteractionKind.CLICK_TARGET_ONLY;
86
+ var removeEvents = function () {
87
+ if (placeholderRef.current) {
88
+ placeholderRef.current.removeEventListener("click", swap);
89
+ placeholderRef.current.removeEventListener("mouseenter", swap);
90
+ placeholderRef.current.removeEventListener("focusin", swap);
91
+ }
92
+ return;
93
+ };
61
94
  if (placeholderRef.current) {
62
- var swap_1 = function (ev) {
63
- if (swapDelay.current) {
64
- clearTimeout(swapDelay.current);
65
- }
66
- swapDelay.current = setTimeout(function () {
67
- // we delay the swap to prevent unwanted effects
68
- // (e.g. event hickup after replacing elements when it is not really necessary)
69
- eventMemory.current = ev.type === "focusin" ? "afterfocus" : "afterhover";
70
- setPlaceholder(false);
71
- }, swapDelayTime);
72
- };
73
- placeholderRef.current.addEventListener("mouseenter", swap_1);
74
- placeholderRef.current.addEventListener("focusin", swap_1);
95
+ removeEvents(); // remove events in case of interaction kind changed during existence
96
+ if (waitForClick) {
97
+ placeholderRef.current.addEventListener("click", swap);
98
+ }
99
+ else {
100
+ placeholderRef.current.addEventListener("mouseenter", swap);
101
+ placeholderRef.current.addEventListener("focusin", swap);
102
+ }
75
103
  return function () {
76
- if (placeholderRef.current) {
77
- placeholderRef.current.removeEventListener("mouseenter", swap_1);
78
- placeholderRef.current.removeEventListener("focusin", swap_1);
79
- }
104
+ removeEvents();
80
105
  };
81
106
  }
82
107
  return function () { };
83
- }, [!!placeholderRef.current]);
108
+ }, [!!placeholderRef.current, otherPopoverProps.interactionKind]);
84
109
  var refocus = React.useCallback(function (node) {
85
- if (eventMemory.current === "afterfocus" && node) {
86
- var target = node.targetRef.current.children[0];
87
- if (target) {
110
+ var target = node === null || node === void 0 ? void 0 : node.targetRef.current.children[0];
111
+ if (!eventMemory.current || !target) {
112
+ return;
113
+ }
114
+ switch (eventMemory.current) {
115
+ case "focusin":
88
116
  target.focus();
89
- }
117
+ break;
118
+ case "click":
119
+ target.click();
120
+ break;
121
+ case "mouseenter":
122
+ // re-check if the cursor is still over the element after swapping the placeholder before triggering the event to bubble up
123
+ target.addEventListener("mouseover", function () { return target.dispatchEvent(new MouseEvent("mouseover", { bubbles: true })); }, {
124
+ capture: true,
125
+ once: true,
126
+ });
127
+ break;
90
128
  }
91
129
  }, []);
92
130
  var targetClassName = "".concat(eccgui, "-contextoverlay") + (className ? " ".concat(className) : "");
@@ -101,7 +139,7 @@ export var ContextOverlay = function (_a) {
101
139
  };
102
140
  var portalClassNameFinal = (preventTopPosition ? "".concat(eccgui, "-contextoverlay__portal--lowertop") : "") +
103
141
  (portalClassName ? " ".concat(portalClassName) : "");
104
- return placeholder ? (displayPlaceholder()) : (React.createElement(BlueprintPopover, __assign({ placement: "bottom" }, otherPopoverProps, { className: targetClassName, portalClassName: (_b = portalClassNameFinal.trim()) !== null && _b !== void 0 ? _b : undefined, ref: refocus }), children));
142
+ return placeholder ? (displayPlaceholder()) : (React.createElement(BlueprintPopover, __assign({ placement: "bottom" }, otherPopoverProps, { className: targetClassName, portalClassName: (_c = portalClassNameFinal.trim()) !== null && _c !== void 0 ? _c : undefined, ref: refocus }), children));
105
143
  };
106
144
  export default ContextOverlay;
107
145
  //# sourceMappingURL=ContextOverlay.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"ContextOverlay.js","sourceRoot":"","sources":["../../../../src/components/ContextOverlay/ContextOverlay.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EACH,OAAO,IAAI,gBAAgB,EAC3B,OAAO,IAAI,gBAAgB,EAE3B,KAAK,IAAI,cAAc,GAC1B,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EAAE,WAAW,IAAI,MAAM,EAAE,MAAM,+BAA+B,CAAC;AAmBtE;;;GAGG;AACH,MAAM,CAAC,IAAM,cAAc,GAAG,UAAC,EAOT;;IANlB,IAAA,QAAQ,cAAA,EACR,eAAe,qBAAA,EACf,kBAAkB,wBAAA,EAClB,iBAAc,EAAd,SAAS,mBAAG,EAAE,KAAA,EACd,sBAAsB,EAAtB,cAAc,mBAAG,KAAK,KAAA,EACnB,iBAAiB,cANO,oFAO9B,CADuB;IAEpB,IAAM,cAAc,GAAG,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAC1C,IAAM,WAAW,GAAG,KAAK,CAAC,MAAM,CAA0C,SAAS,CAAC,CAAC;IACrF,IAAM,SAAS,GAAG,KAAK,CAAC,MAAM,CAAwB,IAAI,CAAC,CAAC;IAC5D,IAAM,aAAa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IAEF,IAAM,oBAAoB,GACtB,CAAC,kBAAkB,CAAC,CAAC,CAAC,UAAG,MAAM,sCAAmC,CAAC,CAAC,CAAC,EAAE,CAAC;QACxE,CAAC,eAAe,CAAC,CAAC,CAAC,WAAI,eAAe,CAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;IAEnD,OAAO,WAAW,CAAC,CAAC,CAAC,CACjB,kBAAkB,EAAE,CACvB,CAAC,CAAC,CAAC,CACA,oBAAC,gBAAgB,aACb,SAAS,EAAC,QAAQ,IACd,iBAAiB,IACrB,SAAS,EAAE,eAAe,EAC1B,eAAe,EAAE,MAAA,oBAAoB,CAAC,IAAI,EAAE,mCAAI,SAAS,EACzD,GAAG,EAAE,OAAO,KAEX,QAAQ,CACM,CACtB,CAAC;AACN,CAAC,CAAC;AAEF,eAAe,cAAc,CAAC"}
@@ -9,8 +9,19 @@ var __assign = (this && this.__assign) || function () {
9
9
  };
10
10
  return __assign.apply(this, arguments);
11
11
  };
12
+ var __rest = (this && this.__rest) || function (s, e) {
13
+ var t = {};
14
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
15
+ t[p] = s[p];
16
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
17
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
18
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
19
+ t[p[i]] = s[p[i]];
20
+ }
21
+ return t;
22
+ };
12
23
  import React from "react";
13
- import { reduceToText } from "../../common/utils/reduceToText.js";
24
+ import { utils } from "../../common/index.js";
14
25
  import { CLASSPREFIX as eccgui } from "../../configuration/constants.js";
15
26
  import { OverflowText } from "./../Typography/index.js";
16
27
  /**
@@ -18,11 +29,11 @@ import { OverflowText } from "./../Typography/index.js";
18
29
  * Display can be wrapped easily in `OverflowText`.
19
30
  */
20
31
  export var TextReducer = function (_a) {
21
- var children = _a.children, maxNodes = _a.maxNodes, maxLength = _a.maxLength, useOverflowTextWrapper = _a.useOverflowTextWrapper, overflowTextProps = _a.overflowTextProps;
32
+ var children = _a.children, useOverflowTextWrapper = _a.useOverflowTextWrapper, overflowTextProps = _a.overflowTextProps, reduceToTextOptions = __rest(_a, ["children", "useOverflowTextWrapper", "overflowTextProps"]);
22
33
  if (typeof children === "undefined") {
23
34
  return React.createElement(React.Fragment, null);
24
35
  }
25
- var shrinkedContent = reduceToText(children, { maxLength: maxLength, maxNodes: maxNodes });
36
+ var shrinkedContent = utils.reduceToText(children, reduceToTextOptions);
26
37
  return useOverflowTextWrapper ? (React.createElement(OverflowText, __assign({}, overflowTextProps, { className: "".concat(eccgui, "-textreducer") +
27
38
  (overflowTextProps && overflowTextProps.className ? " ".concat(overflowTextProps.className) : "") }), shrinkedContent)) : (React.createElement(React.Fragment, null, shrinkedContent));
28
39
  };
@@ -1 +1 @@
1
- {"version":3,"file":"TextReducer.js","sourceRoot":"","sources":["../../../../src/components/TextReducer/TextReducer.tsx"],"names":[],"mappings":";;;;;;;;;;;AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,OAAO,EAAE,YAAY,EAAE,MAAM,iCAAiC,CAAC;AAC/D,OAAO,EAAE,WAAW,IAAI,MAAM,EAAE,MAAM,+BAA+B,CAAC;AAEtE,OAAO,EAAE,YAAY,EAAqB,MAAM,iBAAiB,CAAC;AAuBlE;;;GAGG;AACH,MAAM,CAAC,IAAM,WAAW,GAAG,UAAC,EAMT;QALf,QAAQ,cAAA,EACR,QAAQ,cAAA,EACR,SAAS,eAAA,EACT,sBAAsB,4BAAA,EACtB,iBAAiB,uBAAA;IAEjB,IAAI,OAAO,QAAQ,KAAK,WAAW,EAAE,CAAC;QAClC,OAAO,yCAAK,CAAC;IACjB,CAAC;IAED,IAAM,eAAe,GAAG,YAAY,CAAC,QAAQ,EAAE,EAAE,SAAS,WAAA,EAAE,QAAQ,UAAA,EAAE,CAAC,CAAC;IAExE,OAAO,sBAAsB,CAAC,CAAC,CA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C,CAAC,CAAC,CACA,0CAAG,eAAe,CAAI,CACzB,CAAC;AACN,CAAC,CAAC;AAEF,eAAe,WAAW,CAAC"}
@@ -1,4 +1,5 @@
1
1
  import { getEnabledColorsFromPalette, textToColorHash } from "./utils/colorHash";
2
+ export type { DecodeOptions as DecodeHtmlEntitiesOptions } from "he";
2
3
  export type { IntentTypes as IntentBaseTypes } from "./Intent";
3
4
  export declare const utils: {
4
5
  openInNewTab: (event: React.MouseEvent<HTMLAnchorElement>, handler?: (e: React.MouseEvent<HTMLAnchorElement>) => void, url?: string) => void;
@@ -27,4 +28,5 @@ export declare const utils: {
27
28
  getEnabledColorsFromPalette: typeof getEnabledColorsFromPalette;
28
29
  textToColorHash: typeof textToColorHash;
29
30
  reduceToText: import("./utils/reduceToText").ReduceToTextFuncType;
31
+ decodeHtmlEntities: import("he").Decode;
30
32
  };
@@ -5,6 +5,6 @@ export interface ReduceToTextFuncType {
5
5
  /**
6
6
  * Component or text to reduce HTML markup content to plain text.
7
7
  */
8
- input: React.ReactNode | React.ReactNode[] | string, options?: Pick<TextReducerProps, "maxNodes" | "maxLength">): string;
8
+ input: React.ReactNode | React.ReactNode[] | string, options?: Pick<TextReducerProps, "maxNodes" | "maxLength" | "decodeHtmlEntities" | "decodeHtmlEntitiesOptions">): string;
9
9
  }
10
10
  export declare const reduceToText: ReduceToTextFuncType;
@@ -47,5 +47,5 @@ export interface ContextMenuProps extends TestableComponent {
47
47
  /**
48
48
  * Element displays menu items after toggler is clicked.
49
49
  */
50
- export declare const ContextMenu: ({ children, className, togglerElement, togglerText, contextOverlayProps, disabled, togglerLarge, tooltipAsTitle, preventPlaceholder, ...restProps }: ContextMenuProps) => React.JSX.Element;
50
+ export declare const ContextMenu: ({ children, className, togglerElement, togglerText, contextOverlayProps, disabled, togglerLarge, tooltipAsTitle, preventPlaceholder, "data-test-id": dataTestId, "data-testid": dataTestid, ...restProps }: ContextMenuProps) => React.JSX.Element;
51
51
  export default ContextMenu;
@@ -1,4 +1,5 @@
1
1
  import React from "react";
2
+ import { DecodeHtmlEntitiesOptions } from "../../common";
2
3
  import { OverflowTextProps } from "./../Typography";
3
4
  export interface TextReducerProps extends Pick<React.HTMLAttributes<HTMLElement>, "children"> {
4
5
  /**
@@ -19,10 +20,21 @@ export interface TextReducerProps extends Pick<React.HTMLAttributes<HTMLElement>
19
20
  * Specify more `OverflowText` properties used when `useOverflowTextWrapper` is set to `true`.
20
21
  */
21
22
  overflowTextProps?: Omit<OverflowTextProps, "passDown">;
23
+ /**
24
+ * If you transform HTML markup to text then the result could contain HTML entity encoded strings.
25
+ * By enabling this option they are decoded back to it's original char.
26
+ */
27
+ decodeHtmlEntities?: boolean;
28
+ /**
29
+ * Set the options used to decode the HTML entities, if `decodeHtmlEntities` is enabled.
30
+ * Internally we use `he` library, see their [documentation on decode options](https://www.npmjs.com/package/he#hedecodehtml-options).
31
+ * If not set we use `{ isAttributeValue: true, strict: true }` as default value.
32
+ */
33
+ decodeHtmlEntitiesOptions?: DecodeHtmlEntitiesOptions;
22
34
  }
23
35
  /**
24
36
  * Component to reduce HTML markup content to simple text.
25
37
  * Display can be wrapped easily in `OverflowText`.
26
38
  */
27
- export declare const TextReducer: ({ children, maxNodes, maxLength, useOverflowTextWrapper, overflowTextProps, }: TextReducerProps) => React.JSX.Element;
39
+ export declare const TextReducer: ({ children, useOverflowTextWrapper, overflowTextProps, ...reduceToTextOptions }: TextReducerProps) => React.JSX.Element;
28
40
  export default TextReducer;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@eccenca/gui-elements",
3
3
  "description": "GUI elements based on other libraries, usable in React application, written in Typescript.",
4
- "version": "25.0.0-rc.2",
4
+ "version": "25.0.0",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://github.com/eccenca/gui-elements",
7
7
  "bugs": "https://github.com/eccenca/gui-elements/issues",
@@ -87,6 +87,7 @@
87
87
  "codemirror": "^6.0.1",
88
88
  "color": "^4.2.3",
89
89
  "compute-scroll-into-view": "^3.1.1",
90
+ "he": "^1.2.0",
90
91
  "jshint": "^2.13.6",
91
92
  "lodash": "^4.17.21",
92
93
  "n3": "^1.25.1",
@@ -134,6 +135,7 @@
134
135
  "@testing-library/react": "^12.1.5",
135
136
  "@types/codemirror": "^5.60.15",
136
137
  "@types/color": "^3.0.6",
138
+ "@types/he": "^1.2.3",
137
139
  "@types/jest": "^29.5.14",
138
140
  "@types/jshint": "^2.12.4",
139
141
  "@types/lodash": "^4.17.16",
@@ -150,8 +152,8 @@
150
152
  "eslint-plugin-simple-import-sort": "^12.1.1",
151
153
  "husky": "4",
152
154
  "identity-obj-proxy": "^3.0.0",
153
- "jest": "^30.0.5",
154
- "jest-environment-jsdom": "^30.0.5",
155
+ "jest": "^30.2.0",
156
+ "jest-environment-jsdom": "^30.2.0",
155
157
  "jest-pnp-resolver": "^1.2.3",
156
158
  "lint-staged": "^15.5.1",
157
159
  "node-sass-package-importer": "^5.3.3",
@@ -1,3 +1,5 @@
1
+ import { decode } from "he";
2
+
1
3
  import { invisibleZeroWidthCharacters } from "./utils/characters";
2
4
  import { colorCalculateDistance } from "./utils/colorCalculateDistance";
3
5
  import decideContrastColorValue from "./utils/colorDecideContrastvalue";
@@ -6,7 +8,8 @@ import getColorConfiguration from "./utils/getColorConfiguration";
6
8
  import { getScrollParent } from "./utils/getScrollParent";
7
9
  import { getGlobalVar, setGlobalVar } from "./utils/globalVars";
8
10
  import { openInNewTab } from "./utils/openInNewTab";
9
- import { reduceToText } from "./utils/reduceToText"
11
+ import { reduceToText } from "./utils/reduceToText";
12
+ export type { DecodeOptions as DecodeHtmlEntitiesOptions } from "he";
10
13
  export type { IntentTypes as IntentBaseTypes } from "./Intent";
11
14
 
12
15
  export const utils = {
@@ -20,5 +23,6 @@ export const utils = {
20
23
  getScrollParent,
21
24
  getEnabledColorsFromPalette,
22
25
  textToColorHash,
23
- reduceToText
26
+ reduceToText,
27
+ decodeHtmlEntities: decode,
24
28
  };
@@ -3,6 +3,7 @@ import { renderToString } from "react-dom/server";
3
3
  import * as ReactIs from "react-is";
4
4
 
5
5
  import { TextReducerProps } from "./../../components/TextReducer/TextReducer";
6
+ import { DecodeHtmlEntitiesOptions, utils } from "./../";
6
7
 
7
8
  export interface ReduceToTextFuncType {
8
9
  (
@@ -10,12 +11,12 @@ export interface ReduceToTextFuncType {
10
11
  * Component or text to reduce HTML markup content to plain text.
11
12
  */
12
13
  input: React.ReactNode | React.ReactNode[] | string,
13
- options?: Pick<TextReducerProps, "maxNodes" | "maxLength">
14
+ options?: Pick<TextReducerProps, "maxNodes" | "maxLength" | "decodeHtmlEntities" | "decodeHtmlEntitiesOptions">
14
15
  ): string;
15
16
  }
16
17
 
17
18
  export const reduceToText: ReduceToTextFuncType = (input, options) => {
18
- const { maxNodes, maxLength } = options || {};
19
+ const { maxNodes, maxLength, decodeHtmlEntities } = options || {};
19
20
  const content: React.ReactNode | React.ReactNode[] = input;
20
21
  let nodeCount = 0;
21
22
 
@@ -46,6 +47,33 @@ export const reduceToText: ReduceToTextFuncType = (input, options) => {
46
47
  // Basic HTML cleanup
47
48
  text = text.replace(/<[^\s][^>]*>/g, "").replace(/\n/g, " ");
48
49
 
50
+ if (decodeHtmlEntities) {
51
+ const decodeDefaultOptions = {
52
+ isAttributeValue: true,
53
+ strict: true,
54
+ } as DecodeHtmlEntitiesOptions;
55
+ let decodeErrors = 0;
56
+ // we decode in pieces to apply some error tolerance even in strict mode
57
+ text = text
58
+ .split(" ")
59
+ .map((value) => {
60
+ try {
61
+ return utils.decodeHtmlEntities(value, {
62
+ ...decodeDefaultOptions,
63
+ ...options?.decodeHtmlEntitiesOptions,
64
+ });
65
+ } catch {
66
+ decodeErrors++;
67
+ return value;
68
+ }
69
+ })
70
+ .join(" ");
71
+ if (decodeErrors > 0) {
72
+ // eslint-disable-next-line no-console
73
+ console.warn(`${decodeErrors} parse error(s) for decodeHtmlEntities, return un-decoded text`, text);
74
+ }
75
+ }
76
+
49
77
  if (typeof maxLength === "number") {
50
78
  text = text.slice(0, maxLength);
51
79
  }
@@ -66,6 +66,8 @@ export const ContextMenu = ({
66
66
  so by default we use the title attribute instead of Tooltip. */
67
67
  tooltipAsTitle = true,
68
68
  preventPlaceholder = false,
69
+ "data-test-id": dataTestId,
70
+ "data-testid": dataTestid,
69
71
  ...restProps
70
72
  }: ContextMenuProps) => {
71
73
  const toggleButton =
@@ -76,7 +78,8 @@ export const ContextMenu = ({
76
78
  text={togglerText}
77
79
  large={togglerLarge}
78
80
  disabled={!!disabled}
79
- data-test-id={restProps["data-test-id"]}
81
+ data-test-id={dataTestId ?? undefined}
82
+ data-testid={dataTestid ?? undefined}
80
83
  />
81
84
  ) : (
82
85
  (togglerElement as ReactElement)
@@ -2,6 +2,7 @@ import React from "react";
2
2
  import {
3
3
  Classes as BlueprintClasses,
4
4
  Popover as BlueprintPopover,
5
+ PopoverInteractionKind as InteractionKind,
5
6
  PopoverProps as BlueprintPopoverProps,
6
7
  Utils as BlueprintUtils,
7
8
  } from "@blueprintjs/core";
@@ -37,9 +38,10 @@ export const ContextOverlay = ({
37
38
  usePlaceholder = false,
38
39
  ...otherPopoverProps
39
40
  }: ContextOverlayProps) => {
40
- const placeholderRef = React.useRef(null);
41
- const eventMemory = React.useRef<undefined | "afterhover" | "afterfocus">(undefined);
41
+ const placeholderRef = React.useRef<HTMLElement>(null);
42
+ const eventMemory = React.useRef<undefined | "mouseenter" | "focusin" | "click">(undefined);
42
43
  const swapDelay = React.useRef<null | NodeJS.Timeout>(null);
44
+ const interactionKind = React.useRef<InteractionKind>(otherPopoverProps.interactionKind ?? InteractionKind.CLICK);
43
45
  const swapDelayTime = 15;
44
46
  const [placeholder, setPlaceholder] = React.useState<boolean>(
45
47
  // use placeholder only for "simple" overlays without special states
@@ -50,37 +52,85 @@ export const ContextOverlay = ({
50
52
  usePlaceholder
51
53
  );
52
54
 
55
+ const swap = (ev: MouseEvent | globalThis.FocusEvent) => {
56
+ const waitForClick =
57
+ interactionKind.current === InteractionKind.CLICK ||
58
+ interactionKind.current === InteractionKind.CLICK_TARGET_ONLY;
59
+
60
+ if (swapDelay.current) {
61
+ clearTimeout(swapDelay.current);
62
+ }
63
+
64
+ const replacePlaceholder = () => {
65
+ eventMemory.current = ev.type as "mouseenter" | "focusin" | "click";
66
+ setPlaceholder(false);
67
+ };
68
+
69
+ if (waitForClick) {
70
+ ev.stopImmediatePropagation();
71
+ replacePlaceholder();
72
+ return;
73
+ }
74
+
75
+ swapDelay.current = setTimeout(
76
+ replacePlaceholder,
77
+ // we delay the swap for hover/focus to prevent unwanted effects
78
+ // (e.g. event hickup after replacing elements when it is not really necessary)
79
+ swapDelayTime
80
+ );
81
+ };
82
+
53
83
  React.useEffect(() => {
84
+ interactionKind.current = otherPopoverProps.interactionKind ?? InteractionKind.CLICK;
85
+ const waitForClick =
86
+ interactionKind.current === InteractionKind.CLICK ||
87
+ interactionKind.current === InteractionKind.CLICK_TARGET_ONLY;
88
+ const removeEvents = () => {
89
+ if (placeholderRef.current) {
90
+ placeholderRef.current.removeEventListener("click", swap);
91
+ placeholderRef.current.removeEventListener("mouseenter", swap);
92
+ placeholderRef.current.removeEventListener("focusin", swap);
93
+ }
94
+ return;
95
+ };
54
96
  if (placeholderRef.current) {
55
- const swap = (ev: MouseEvent | globalThis.FocusEvent) => {
56
- if (swapDelay.current) {
57
- clearTimeout(swapDelay.current);
58
- }
59
- swapDelay.current = setTimeout(() => {
60
- // we delay the swap to prevent unwanted effects
61
- // (e.g. event hickup after replacing elements when it is not really necessary)
62
- eventMemory.current = ev.type === "focusin" ? "afterfocus" : "afterhover";
63
- setPlaceholder(false);
64
- }, swapDelayTime);
65
- };
66
- (placeholderRef.current as HTMLElement).addEventListener("mouseenter", swap);
67
- (placeholderRef.current as HTMLElement).addEventListener("focusin", swap);
97
+ removeEvents(); // remove events in case of interaction kind changed during existence
98
+ if (waitForClick) {
99
+ placeholderRef.current.addEventListener("click", swap);
100
+ } else {
101
+ placeholderRef.current.addEventListener("mouseenter", swap);
102
+ placeholderRef.current.addEventListener("focusin", swap);
103
+ }
68
104
  return () => {
69
- if (placeholderRef.current) {
70
- (placeholderRef.current as HTMLElement).removeEventListener("mouseenter", swap);
71
- (placeholderRef.current as HTMLElement).removeEventListener("focusin", swap);
72
- }
105
+ removeEvents();
73
106
  };
74
107
  }
75
108
  return () => {};
76
- }, [!!placeholderRef.current]);
109
+ }, [!!placeholderRef.current, otherPopoverProps.interactionKind]);
77
110
 
78
111
  const refocus = React.useCallback((node) => {
79
- if (eventMemory.current === "afterfocus" && node) {
80
- const target = node.targetRef.current.children[0];
81
- if (target) {
112
+ const target = node?.targetRef.current.children[0];
113
+ if (!eventMemory.current || !target) {
114
+ return;
115
+ }
116
+ switch (eventMemory.current) {
117
+ case "focusin":
82
118
  target.focus();
83
- }
119
+ break;
120
+ case "click":
121
+ target.click();
122
+ break;
123
+ case "mouseenter":
124
+ // re-check if the cursor is still over the element after swapping the placeholder before triggering the event to bubble up
125
+ (target as HTMLElement).addEventListener(
126
+ "mouseover",
127
+ () => (target as HTMLElement).dispatchEvent(new MouseEvent("mouseover", { bubbles: true })),
128
+ {
129
+ capture: true,
130
+ once: true,
131
+ }
132
+ );
133
+ break;
84
134
  }
85
135
  }, []);
86
136
 
@@ -0,0 +1,43 @@
1
+ import React from "react";
2
+ import { fireEvent, render, screen } from "@testing-library/react";
3
+
4
+ import "@testing-library/jest-dom";
5
+
6
+ import { CLASSPREFIX as eccgui } from "../../../configuration/constants";
7
+
8
+ import ContextMenu from "./../ContextMenu";
9
+ import { Default as ContextMenuStory } from "./../ContextMenu.stories";
10
+
11
+ const overlayWrapper = `${eccgui}-contextoverlay`;
12
+ const placeholderClass = `${overlayWrapper}__wrapper--placeholder`;
13
+
14
+ const checkForPlaceholderClass = (container: HTMLElement, tobe: number) => {
15
+ expect(container.getElementsByClassName(placeholderClass).length).toBe(tobe);
16
+ };
17
+
18
+ describe("ContextMenu", () => {
19
+ it("should render placeholder automatically", () => {
20
+ const { container } = render(<ContextMenu {...ContextMenuStory.args} />);
21
+ checkForPlaceholderClass(container, 1);
22
+ });
23
+ it("should not render placeholder when `preventPlaceholder===true`", () => {
24
+ const { container } = render(<ContextMenu {...ContextMenuStory.args} preventPlaceholder={true} />);
25
+ checkForPlaceholderClass(container, 0);
26
+ });
27
+ it("should render placeholder when `preventPlaceholder===false`", () => {
28
+ const { container } = render(<ContextMenu {...ContextMenuStory.args} preventPlaceholder={false} />);
29
+ checkForPlaceholderClass(container, 1);
30
+ });
31
+ it("if no placeholder is used the menu should be displayed on click", async () => {
32
+ const { container } = render(<ContextMenu {...ContextMenuStory.args} preventPlaceholder={true} />);
33
+ checkForPlaceholderClass(container, 0);
34
+ fireEvent.click(container.getElementsByClassName(overlayWrapper)[0]);
35
+ expect(await screen.findByText("First option")).toBeVisible();
36
+ });
37
+ it("if placeholder is used the menu should be displayed on click", async () => {
38
+ const { container } = render(<ContextMenu {...ContextMenuStory.args} preventPlaceholder={false} />);
39
+ checkForPlaceholderClass(container, 1);
40
+ fireEvent.click(container.getElementsByClassName(overlayWrapper)[0]);
41
+ expect(await screen.findByText("First option")).toBeVisible();
42
+ });
43
+ });
@@ -0,0 +1,71 @@
1
+ import React from "react";
2
+ import { PopoverInteractionKind } from "@blueprintjs/core";
3
+ import { fireEvent, render, screen, waitFor } from "@testing-library/react";
4
+
5
+ import "@testing-library/jest-dom";
6
+
7
+ import { CLASSPREFIX as eccgui } from "../../../configuration/constants";
8
+
9
+ import ContextOverlay from "./../ContextOverlay";
10
+ import { Default as ContextOverlayStory } from "./../ContextOverlay.stories";
11
+
12
+ const overlayWrapper = `${eccgui}-contextoverlay`;
13
+ const placeholderClass = `${overlayWrapper}__wrapper--placeholder`;
14
+
15
+ const checkForPlaceholderClass = (container: HTMLElement, tobe: number) => {
16
+ expect(container.getElementsByClassName(placeholderClass).length).toBe(tobe);
17
+ };
18
+
19
+ describe("ContextOverlay", () => {
20
+ it("should not render placeholder automatically", () => {
21
+ const { container } = render(<ContextOverlay {...ContextOverlayStory.args} />);
22
+ checkForPlaceholderClass(container, 0);
23
+ });
24
+ it("should render placeholder when `usePlaceholder===true`", () => {
25
+ const { container } = render(<ContextOverlay {...ContextOverlayStory.args} usePlaceholder={true} />);
26
+ checkForPlaceholderClass(container, 1);
27
+ });
28
+ it("should render no placeholder when `usePlaceholder===false`", () => {
29
+ const { container } = render(<ContextOverlay {...ContextOverlayStory.args} usePlaceholder={false} />);
30
+ checkForPlaceholderClass(container, 0);
31
+ });
32
+ it("if no placeholder is used the overlay should be displayed on click", async () => {
33
+ const { container } = render(<ContextOverlay {...ContextOverlayStory.args} usePlaceholder={false} />);
34
+ fireEvent.click(container.getElementsByClassName(overlayWrapper)[0]);
35
+ expect(await screen.findByText("Overlay:")).toBeVisible();
36
+ });
37
+ it("if no placeholder is used the overlay should be displayed on hover (hover interactionKind)", async () => {
38
+ const { container } = render(
39
+ <ContextOverlay
40
+ {...ContextOverlayStory.args}
41
+ usePlaceholder={false}
42
+ interactionKind={PopoverInteractionKind.HOVER}
43
+ />
44
+ );
45
+ fireEvent.mouseEnter(container.getElementsByClassName(overlayWrapper)[0]);
46
+ expect(await screen.findByText("Overlay:")).toBeVisible();
47
+ });
48
+ it("if placeholder is used the overlay should be displayed on click", async () => {
49
+ const { container } = render(<ContextOverlay {...ContextOverlayStory.args} usePlaceholder={true} />);
50
+ fireEvent.click(container.getElementsByClassName(overlayWrapper)[0]);
51
+ expect(await screen.findByText("Overlay:")).toBeVisible();
52
+ });
53
+ it("if placeholder is used the overlay should be displayed on hover (hover interactionKind)", async () => {
54
+ const { container } = render(
55
+ <ContextOverlay
56
+ {...ContextOverlayStory.args}
57
+ usePlaceholder={true}
58
+ interactionKind={PopoverInteractionKind.HOVER}
59
+ />
60
+ );
61
+ checkForPlaceholderClass(container, 1);
62
+ fireEvent.mouseEnter(container.getElementsByClassName(overlayWrapper)[0]);
63
+ await waitFor(async () => {
64
+ expect(screen.queryByDisplayValue("Overlay:")).toBeNull();
65
+ checkForPlaceholderClass(container, 0);
66
+ // we need to emulate another mouseover to simulate real user behaviour
67
+ fireEvent.mouseOver(container.getElementsByClassName(overlayWrapper)[0]);
68
+ expect(await screen.findByText("Overlay:")).toBeVisible();
69
+ });
70
+ });
71
+ });