@redocly/theme 0.47.0 → 0.47.1

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 (188) hide show
  1. package/lib/components/Buttons/CopyButton.d.ts +8 -3
  2. package/lib/components/Buttons/CopyButton.js +4 -3
  3. package/lib/components/StatusCode/StatusCode.js +11 -14
  4. package/lib/core/constants/code-walkthrough.d.ts +1 -0
  5. package/lib/core/constants/code-walkthrough.js +5 -0
  6. package/lib/core/constants/index.d.ts +1 -0
  7. package/lib/core/constants/index.js +1 -0
  8. package/lib/core/contexts/CodeWalkthrough/CodeWalkthroughControlsContext.d.ts +2 -0
  9. package/lib/core/contexts/CodeWalkthrough/CodeWalkthroughControlsContext.js +18 -0
  10. package/lib/core/contexts/CodeWalkthrough/CodeWalkthroughStepsContext.d.ts +2 -0
  11. package/lib/core/contexts/CodeWalkthrough/CodeWalkthroughStepsContext.js +11 -0
  12. package/lib/core/contexts/index.d.ts +2 -0
  13. package/lib/core/contexts/index.js +2 -0
  14. package/lib/core/hooks/code-walkthrough/__mocks__/MockIntersectionObserver.d.ts +10 -0
  15. package/lib/core/hooks/code-walkthrough/__mocks__/MockIntersectionObserver.js +55 -0
  16. package/lib/core/hooks/code-walkthrough/use-code-panel.d.ts +6 -0
  17. package/lib/core/hooks/code-walkthrough/use-code-panel.js +93 -0
  18. package/lib/core/hooks/code-walkthrough/use-code-walkthrough-controls.d.ts +22 -0
  19. package/lib/core/hooks/code-walkthrough/use-code-walkthrough-controls.js +183 -0
  20. package/lib/core/hooks/code-walkthrough/use-code-walkthrough-steps.d.ts +16 -0
  21. package/lib/core/hooks/code-walkthrough/use-code-walkthrough-steps.js +117 -0
  22. package/lib/core/hooks/code-walkthrough/use-code-walkthrough.d.ts +14 -0
  23. package/lib/core/hooks/code-walkthrough/use-code-walkthrough.js +22 -0
  24. package/lib/core/hooks/index.d.ts +4 -0
  25. package/lib/core/hooks/index.js +4 -0
  26. package/lib/core/styles/global.js +2 -0
  27. package/lib/core/templates/Markdown.d.ts +8 -2
  28. package/lib/core/templates/Markdown.js +5 -2
  29. package/lib/core/types/hooks.d.ts +2 -0
  30. package/lib/core/types/l10n.d.ts +1 -1
  31. package/lib/core/utils/download-code-walkthrough.d.ts +4 -0
  32. package/lib/core/utils/download-code-walkthrough.js +32 -0
  33. package/lib/core/utils/get-code-walkthrough-file-text.d.ts +4 -0
  34. package/lib/core/utils/get-code-walkthrough-file-text.js +22 -0
  35. package/lib/core/utils/get-file-icon.d.ts +2 -0
  36. package/lib/core/utils/get-file-icon.js +31 -0
  37. package/lib/core/utils/index.d.ts +5 -0
  38. package/lib/core/utils/index.js +5 -0
  39. package/lib/core/utils/js-utils.d.ts +30 -0
  40. package/lib/core/utils/js-utils.js +41 -0
  41. package/lib/core/utils/match-code-walkthrough-conditions.d.ts +4 -0
  42. package/lib/core/utils/match-code-walkthrough-conditions.js +23 -0
  43. package/lib/core/utils/replace-inputs-with-value.d.ts +3 -0
  44. package/lib/core/utils/replace-inputs-with-value.js +16 -0
  45. package/lib/icons/DocumentCssIcon/DocumentCssIcon.d.ts +9 -0
  46. package/lib/icons/DocumentCssIcon/DocumentCssIcon.js +23 -0
  47. package/lib/icons/DocumentCssIcon/index.d.ts +1 -0
  48. package/lib/icons/DocumentCssIcon/index.js +6 -0
  49. package/lib/icons/DocumentGraphqlIcon/DocumentGraphqlIcon.d.ts +9 -0
  50. package/lib/icons/DocumentGraphqlIcon/DocumentGraphqlIcon.js +31 -0
  51. package/lib/icons/DocumentGraphqlIcon/index.d.ts +1 -0
  52. package/lib/icons/DocumentGraphqlIcon/index.js +6 -0
  53. package/lib/icons/DocumentHtmlIcon/DocumentHtmlIcon.d.ts +9 -0
  54. package/lib/icons/DocumentHtmlIcon/DocumentHtmlIcon.js +23 -0
  55. package/lib/icons/DocumentHtmlIcon/index.d.ts +1 -0
  56. package/lib/icons/DocumentHtmlIcon/index.js +6 -0
  57. package/lib/icons/DocumentJsIcon/DocumentJsIcon.d.ts +9 -0
  58. package/lib/icons/DocumentJsIcon/DocumentJsIcon.js +23 -0
  59. package/lib/icons/DocumentJsIcon/index.d.ts +1 -0
  60. package/lib/icons/DocumentJsIcon/index.js +6 -0
  61. package/lib/icons/DocumentJsonIcon/DocumentJsonIcon.d.ts +9 -0
  62. package/lib/icons/DocumentJsonIcon/DocumentJsonIcon.js +23 -0
  63. package/lib/icons/DocumentJsonIcon/index.d.ts +1 -0
  64. package/lib/icons/DocumentJsonIcon/index.js +6 -0
  65. package/lib/icons/DocumentMarkdownIcon/DocumentMarkdownIcon.d.ts +9 -0
  66. package/lib/icons/DocumentMarkdownIcon/DocumentMarkdownIcon.js +23 -0
  67. package/lib/icons/DocumentMarkdownIcon/index.d.ts +1 -0
  68. package/lib/icons/DocumentMarkdownIcon/index.js +6 -0
  69. package/lib/icons/DocumentReactIcon/DocumentReactIcon.d.ts +9 -0
  70. package/lib/icons/DocumentReactIcon/DocumentReactIcon.js +23 -0
  71. package/lib/icons/DocumentReactIcon/index.d.ts +1 -0
  72. package/lib/icons/DocumentReactIcon/index.js +6 -0
  73. package/lib/icons/DocumentTsIcon/DocumentTsIcon.d.ts +9 -0
  74. package/lib/icons/DocumentTsIcon/DocumentTsIcon.js +23 -0
  75. package/lib/icons/DocumentTsIcon/index.d.ts +1 -0
  76. package/lib/icons/DocumentTsIcon/index.js +6 -0
  77. package/lib/icons/DocumentYamlIcon/DocumentYamlIcon.d.ts +9 -0
  78. package/lib/icons/DocumentYamlIcon/DocumentYamlIcon.js +23 -0
  79. package/lib/icons/DocumentYamlIcon/index.d.ts +1 -0
  80. package/lib/icons/DocumentYamlIcon/index.js +6 -0
  81. package/lib/index.d.ts +1 -0
  82. package/lib/index.js +1 -0
  83. package/lib/layouts/CodeWalkthroughLayout.d.ts +5 -0
  84. package/lib/layouts/CodeWalkthroughLayout.js +72 -0
  85. package/lib/markdoc/attributes/code-walkthrough-filesets.d.ts +5 -0
  86. package/lib/markdoc/attributes/code-walkthrough-filesets.js +16 -0
  87. package/lib/markdoc/attributes/code-walkthrough-filters.d.ts +5 -0
  88. package/lib/markdoc/attributes/code-walkthrough-filters.js +16 -0
  89. package/lib/markdoc/components/CodeWalkthrough/CodeContainer.d.ts +5 -0
  90. package/lib/markdoc/components/CodeWalkthrough/CodeContainer.js +81 -0
  91. package/lib/markdoc/components/CodeWalkthrough/CodeFilters.d.ts +12 -0
  92. package/lib/markdoc/components/CodeWalkthrough/CodeFilters.js +58 -0
  93. package/lib/markdoc/components/CodeWalkthrough/CodePanel.d.ts +8 -0
  94. package/lib/markdoc/components/CodeWalkthrough/CodePanel.js +64 -0
  95. package/lib/markdoc/components/CodeWalkthrough/CodePanelHeader.d.ts +8 -0
  96. package/lib/markdoc/components/CodeWalkthrough/CodePanelHeader.js +155 -0
  97. package/lib/markdoc/components/CodeWalkthrough/CodePanelPreview.d.ts +2 -0
  98. package/lib/markdoc/components/CodeWalkthrough/CodePanelPreview.js +73 -0
  99. package/lib/markdoc/components/CodeWalkthrough/CodePanelToolbar.d.ts +5 -0
  100. package/lib/markdoc/components/CodeWalkthrough/CodePanelToolbar.js +47 -0
  101. package/lib/markdoc/components/CodeWalkthrough/CodeStep.d.ts +12 -0
  102. package/lib/markdoc/components/CodeWalkthrough/CodeStep.js +128 -0
  103. package/lib/markdoc/components/CodeWalkthrough/CodeToggle.d.ts +9 -0
  104. package/lib/markdoc/components/CodeWalkthrough/CodeToggle.js +69 -0
  105. package/lib/markdoc/components/CodeWalkthrough/CodeWalkthrough.d.ts +4 -0
  106. package/lib/markdoc/components/CodeWalkthrough/CodeWalkthrough.js +121 -0
  107. package/lib/markdoc/components/CodeWalkthrough/Input.d.ts +8 -0
  108. package/lib/markdoc/components/CodeWalkthrough/Input.js +99 -0
  109. package/lib/markdoc/components/CodeWalkthrough/variables.d.ts +1 -0
  110. package/lib/markdoc/components/CodeWalkthrough/variables.js +19 -0
  111. package/lib/markdoc/components/default.d.ts +5 -0
  112. package/lib/markdoc/components/default.js +5 -0
  113. package/lib/markdoc/default.js +8 -0
  114. package/lib/markdoc/tags/code-step.d.ts +2 -0
  115. package/lib/markdoc/tags/code-step.js +28 -0
  116. package/lib/markdoc/tags/code-toggle.d.ts +2 -0
  117. package/lib/markdoc/tags/code-toggle.js +40 -0
  118. package/lib/markdoc/tags/code-walkthrough.d.ts +8 -0
  119. package/lib/markdoc/tags/code-walkthrough.js +242 -0
  120. package/lib/markdoc/tags/input.d.ts +2 -0
  121. package/lib/markdoc/tags/input.js +37 -0
  122. package/lib/plugin.d.ts +2 -1
  123. package/lib/plugin.js +2 -4
  124. package/package.json +9 -2
  125. package/src/components/Buttons/CopyButton.tsx +24 -6
  126. package/src/components/StatusCode/StatusCode.ts +11 -10
  127. package/src/core/constants/code-walkthrough.ts +1 -0
  128. package/src/core/constants/index.ts +1 -0
  129. package/src/core/contexts/CodeWalkthrough/CodeWalkthroughControlsContext.tsx +21 -0
  130. package/src/core/contexts/CodeWalkthrough/CodeWalkthroughStepsContext.tsx +10 -0
  131. package/src/core/contexts/index.ts +2 -0
  132. package/src/core/hooks/code-walkthrough/__mocks__/MockIntersectionObserver.ts +22 -0
  133. package/src/core/hooks/code-walkthrough/use-code-panel.ts +164 -0
  134. package/src/core/hooks/code-walkthrough/use-code-walkthrough-controls.ts +302 -0
  135. package/src/core/hooks/code-walkthrough/use-code-walkthrough-steps.ts +165 -0
  136. package/src/core/hooks/code-walkthrough/use-code-walkthrough.ts +51 -0
  137. package/src/core/hooks/index.ts +4 -0
  138. package/src/core/styles/global.ts +2 -0
  139. package/src/core/templates/Markdown.tsx +16 -5
  140. package/src/core/types/hooks.ts +2 -0
  141. package/src/core/types/l10n.ts +3 -1
  142. package/src/core/utils/download-code-walkthrough.ts +25 -0
  143. package/src/core/utils/get-code-walkthrough-file-text.ts +32 -0
  144. package/src/core/utils/get-file-icon.ts +35 -0
  145. package/src/core/utils/index.ts +5 -0
  146. package/src/core/utils/js-utils.ts +48 -0
  147. package/src/core/utils/match-code-walkthrough-conditions.ts +29 -0
  148. package/src/core/utils/replace-inputs-with-value.ts +11 -0
  149. package/src/icons/DocumentCssIcon/DocumentCssIcon.tsx +36 -0
  150. package/src/icons/DocumentCssIcon/index.ts +1 -0
  151. package/src/icons/DocumentGraphqlIcon/DocumentGraphqlIcon.tsx +64 -0
  152. package/src/icons/DocumentGraphqlIcon/index.ts +1 -0
  153. package/src/icons/DocumentHtmlIcon/DocumentHtmlIcon.tsx +36 -0
  154. package/src/icons/DocumentHtmlIcon/index.ts +1 -0
  155. package/src/icons/DocumentJsIcon/DocumentJsIcon.tsx +36 -0
  156. package/src/icons/DocumentJsIcon/index.ts +1 -0
  157. package/src/icons/DocumentJsonIcon/DocumentJsonIcon.tsx +36 -0
  158. package/src/icons/DocumentJsonIcon/index.ts +1 -0
  159. package/src/icons/DocumentMarkdownIcon/DocumentMarkdownIcon.tsx +36 -0
  160. package/src/icons/DocumentMarkdownIcon/index.ts +1 -0
  161. package/src/icons/DocumentReactIcon/DocumentReactIcon.tsx +36 -0
  162. package/src/icons/DocumentReactIcon/index.ts +1 -0
  163. package/src/icons/DocumentTsIcon/DocumentTsIcon.tsx +38 -0
  164. package/src/icons/DocumentTsIcon/index.ts +1 -0
  165. package/src/icons/DocumentYamlIcon/DocumentYamlIcon.tsx +36 -0
  166. package/src/icons/DocumentYamlIcon/index.ts +1 -0
  167. package/src/index.ts +1 -0
  168. package/src/layouts/CodeWalkthroughLayout.tsx +78 -0
  169. package/src/markdoc/attributes/code-walkthrough-filesets.ts +9 -0
  170. package/src/markdoc/attributes/code-walkthrough-filters.ts +9 -0
  171. package/src/markdoc/components/CodeWalkthrough/CodeContainer.tsx +76 -0
  172. package/src/markdoc/components/CodeWalkthrough/CodeFilters.tsx +87 -0
  173. package/src/markdoc/components/CodeWalkthrough/CodePanel.tsx +68 -0
  174. package/src/markdoc/components/CodeWalkthrough/CodePanelHeader.tsx +192 -0
  175. package/src/markdoc/components/CodeWalkthrough/CodePanelPreview.tsx +53 -0
  176. package/src/markdoc/components/CodeWalkthrough/CodePanelToolbar.tsx +31 -0
  177. package/src/markdoc/components/CodeWalkthrough/CodeStep.tsx +149 -0
  178. package/src/markdoc/components/CodeWalkthrough/CodeToggle.tsx +63 -0
  179. package/src/markdoc/components/CodeWalkthrough/CodeWalkthrough.tsx +111 -0
  180. package/src/markdoc/components/CodeWalkthrough/Input.tsx +101 -0
  181. package/src/markdoc/components/CodeWalkthrough/variables.ts +16 -0
  182. package/src/markdoc/components/default.ts +5 -0
  183. package/src/markdoc/default.ts +8 -0
  184. package/src/markdoc/tags/code-step.ts +27 -0
  185. package/src/markdoc/tags/code-toggle.ts +37 -0
  186. package/src/markdoc/tags/code-walkthrough.ts +315 -0
  187. package/src/markdoc/tags/input.ts +35 -0
  188. package/src/plugin.js +2 -2
@@ -1,9 +1,9 @@
1
1
  import React from 'react';
2
2
  import type { TooltipProps } from '../../components/Tooltip/Tooltip';
3
- import type { ControlItemType } from '../../components/CodeBlock/CodeBlockControls';
3
+ import type { ButtonProps } from '../../components/Button/Button';
4
4
  export type CopyButtonProps = {
5
5
  data: unknown;
6
- type?: ControlItemType;
6
+ type?: 'icon' | 'text' | 'compound';
7
7
  toasterPlacement?: TooltipProps['placement'];
8
8
  toasterText?: string;
9
9
  toasterDuration?: number;
@@ -11,6 +11,11 @@ export type CopyButtonProps = {
11
11
  onCopyClick?: (e: React.MouseEvent<HTMLElement>) => void;
12
12
  dataTestId?: string;
13
13
  className?: string;
14
- variant?: string;
14
+ variant?: ButtonProps['variant'];
15
+ size?: ButtonProps['size'];
16
+ disabled?: ButtonProps['disabled'];
17
+ fullWidth?: ButtonProps['fullWidth'];
18
+ tone?: ButtonProps['tone'];
19
+ extraClass?: ButtonProps['extraClass'];
15
20
  };
16
21
  export declare const CopyButton: React.NamedExoticComponent<CopyButtonProps>;
@@ -30,7 +30,7 @@ const hooks_1 = require("../../core/hooks");
30
30
  const Button_1 = require("../../components/Button/Button");
31
31
  const CopyIcon_1 = require("../../icons/CopyIcon/CopyIcon");
32
32
  const Tooltip_1 = require("../../components/Tooltip/Tooltip");
33
- function CopyButtonComponent({ data, type = 'icon', toasterPlacement = 'top', toasterText, toasterDuration, buttonText, onCopyClick, dataTestId = 'copy-button', className, variant = 'text', }) {
33
+ function CopyButtonComponent({ data, type = 'icon', toasterPlacement = 'top', toasterText, toasterDuration, buttonText, onCopyClick, dataTestId = 'copy-button', className, variant = 'text', size = 'small', disabled, fullWidth, tone, extraClass, }) {
34
34
  const { useTranslate } = (0, hooks_1.useThemeHooks)();
35
35
  const tooltip = (0, hooks_1.useControl)();
36
36
  const { translate } = useTranslate();
@@ -46,8 +46,9 @@ function CopyButtonComponent({ data, type = 'icon', toasterPlacement = 'top', to
46
46
  showTooltip(duration);
47
47
  onCopyClick === null || onCopyClick === void 0 ? void 0 : onCopyClick(e);
48
48
  };
49
- return (react_1.default.createElement(Tooltip_1.Tooltip, { className: `copy-button ${className ? className : ''}`, tip: translate('codeSnippet.copy.buttonText', toasterText || 'Copied!'), isOpen: tooltip.isOpened, placement: toasterPlacement },
50
- react_1.default.createElement(Button_1.Button, { "data-component-name": "Buttons/CopyButton", onClick: (e) => copy(e, toasterDuration), "data-testid": dataTestId, icon: type === 'icon' ? react_1.default.createElement(CopyIcon_1.CopyIcon, null) : undefined, size: "small", variant: variant }, type !== 'icon' && translate('codeSnippet.copy.toasterText', buttonText || 'Copy'))));
49
+ return (react_1.default.createElement(Tooltip_1.Tooltip, { className: `copy-button ${className ? className : ''}`, tip: translate('codeSnippet.copy.buttonText', toasterText || 'Copied!'), isOpen: tooltip.isOpened, placement: toasterPlacement, width: "fit-content" },
50
+ react_1.default.createElement(Button_1.Button, { "data-component-name": "Buttons/CopyButton", onClick: (e) => copy(e, toasterDuration), "data-testid": dataTestId, icon: type === 'icon' || type === 'compound' ? react_1.default.createElement(CopyIcon_1.CopyIcon, null) : undefined, size: size, variant: variant, disabled: disabled, fullWidth: fullWidth, tone: tone, extraClass: extraClass }, (type === 'text' || type === 'compound') &&
51
+ translate('codeSnippet.copy.toasterText', buttonText || 'Copy'))));
51
52
  }
52
53
  exports.CopyButton = (0, react_1.memo)(CopyButtonComponent);
53
54
  //# sourceMappingURL=CopyButton.js.map
@@ -20,20 +20,17 @@ exports.StatusCode = styled_components_1.default.button.attrs({ 'data-component-
20
20
  width: var(--status-code-width);
21
21
  border: var(--status-code-border);
22
22
  background-color: ${({ status }) => {
23
- if (Math.floor(Number(status) / 100) === 2) {
24
- return 'var(--status-code-200-bg-color)';
25
- }
26
- else if (Math.floor(Number(status) / 100) === 4) {
27
- return 'var(--status-code-400-bg-color)';
28
- }
29
- else if (Math.floor(Number(status) / 100) === 5) {
30
- return 'var(--status-code-500-bg-color)';
31
- }
32
- else if (Math.floor(Number(status) / 100) === 3) {
33
- return 'var(--status-code-300-bg-color)';
34
- }
35
- else {
36
- return 'var(--status-code-100-bg-color)';
23
+ switch (true) {
24
+ case status.startsWith('2'):
25
+ return 'var(--status-code-200-bg-color)';
26
+ case status.startsWith('3'):
27
+ return 'var(--status-code-300-bg-color)';
28
+ case status.startsWith('4'):
29
+ return 'var(--status-code-400-bg-color)';
30
+ case status.startsWith('5'):
31
+ return 'var(--status-code-500-bg-color)';
32
+ default:
33
+ return 'var(--status-code-100-bg-color)';
37
34
  }
38
35
  }}
39
36
  `;
@@ -0,0 +1 @@
1
+ export declare const ACTIVE_STEP_QUERY_PARAM = "__step";
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ACTIVE_STEP_QUERY_PARAM = void 0;
4
+ exports.ACTIVE_STEP_QUERY_PARAM = '__step';
5
+ //# sourceMappingURL=code-walkthrough.js.map
@@ -1,2 +1,3 @@
1
1
  export * from '../../core/constants/common';
2
2
  export * from '../../core/constants/environments';
3
+ export * from '../../core/constants/code-walkthrough';
@@ -16,4 +16,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("../../core/constants/common"), exports);
18
18
  __exportStar(require("../../core/constants/environments"), exports);
19
+ __exportStar(require("../../core/constants/code-walkthrough"), exports);
19
20
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,2 @@
1
+ import type { WalkthroughControlsState } from '../../../core/hooks';
2
+ export declare const CodeWalkthroughControlsStateContext: import("react").Context<WalkthroughControlsState>;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CodeWalkthroughControlsStateContext = void 0;
4
+ const react_1 = require("react");
5
+ exports.CodeWalkthroughControlsStateContext = (0, react_1.createContext)({
6
+ activeFilters: [],
7
+ getToggleState: () => null,
8
+ changeToggleState: () => { },
9
+ getInputState: () => null,
10
+ changeInputState: () => { },
11
+ getFilterState: () => null,
12
+ changeFilterState: () => { },
13
+ getFileText: () => '',
14
+ areConditionsMet: () => false,
15
+ handleDownloadCode: () => Promise.resolve(),
16
+ populateInputsWithValue: (node) => node,
17
+ });
18
+ //# sourceMappingURL=CodeWalkthroughControlsContext.js.map
@@ -0,0 +1,2 @@
1
+ import type { WalkthroughStepsState } from '../../../core/hooks';
2
+ export declare const CodeWalkthroughStepsContext: import("react").Context<WalkthroughStepsState>;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CodeWalkthroughStepsContext = void 0;
4
+ const react_1 = require("react");
5
+ exports.CodeWalkthroughStepsContext = (0, react_1.createContext)({
6
+ activeStep: null,
7
+ setActiveStep: () => { },
8
+ register: () => { },
9
+ unregister: () => { },
10
+ });
11
+ //# sourceMappingURL=CodeWalkthroughStepsContext.js.map
@@ -1 +1,3 @@
1
1
  export * from '../../core/contexts/ThemeDataContext';
2
+ export * from '../../core/contexts/CodeWalkthrough/CodeWalkthroughControlsContext';
3
+ export * from '../../core/contexts/CodeWalkthrough/CodeWalkthroughStepsContext';
@@ -15,4 +15,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("../../core/contexts/ThemeDataContext"), exports);
18
+ __exportStar(require("../../core/contexts/CodeWalkthrough/CodeWalkthroughControlsContext"), exports);
19
+ __exportStar(require("../../core/contexts/CodeWalkthrough/CodeWalkthroughStepsContext"), exports);
18
20
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,10 @@
1
+ export declare class MockIntersectionObserver {
2
+ readonly root: Element | Document | null;
3
+ readonly rootMargin: string;
4
+ readonly thresholds: ReadonlyArray<number>;
5
+ observe: (target: HTMLElement) => void;
6
+ unobserve: (target: HTMLElement) => void;
7
+ disconnect: () => void;
8
+ constructor(callback: IntersectionObserverCallback, options?: IntersectionObserverInit);
9
+ takeRecords(): any[];
10
+ }
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MockIntersectionObserver = void 0;
4
+ class MockIntersectionObserver {
5
+ // @ts-ignore
6
+ constructor(callback, options) {
7
+ Object.defineProperty(this, "root", {
8
+ enumerable: true,
9
+ configurable: true,
10
+ writable: true,
11
+ value: void 0
12
+ });
13
+ Object.defineProperty(this, "rootMargin", {
14
+ enumerable: true,
15
+ configurable: true,
16
+ writable: true,
17
+ value: void 0
18
+ });
19
+ Object.defineProperty(this, "thresholds", {
20
+ enumerable: true,
21
+ configurable: true,
22
+ writable: true,
23
+ value: void 0
24
+ });
25
+ Object.defineProperty(this, "observe", {
26
+ enumerable: true,
27
+ configurable: true,
28
+ writable: true,
29
+ value: void 0
30
+ });
31
+ Object.defineProperty(this, "unobserve", {
32
+ enumerable: true,
33
+ configurable: true,
34
+ writable: true,
35
+ value: void 0
36
+ });
37
+ Object.defineProperty(this, "disconnect", {
38
+ enumerable: true,
39
+ configurable: true,
40
+ writable: true,
41
+ value: void 0
42
+ });
43
+ this.root = null;
44
+ this.rootMargin = '0px';
45
+ this.thresholds = [1];
46
+ this.observe = jest.fn;
47
+ this.unobserve = jest.fn;
48
+ this.disconnect = jest.fn;
49
+ }
50
+ takeRecords() {
51
+ return [];
52
+ }
53
+ }
54
+ exports.MockIntersectionObserver = MockIntersectionObserver;
55
+ //# sourceMappingURL=MockIntersectionObserver.js.map
@@ -0,0 +1,6 @@
1
+ import type { CodeWalkthroughFile } from '@redocly/config';
2
+ export declare function useCodePanel(files: CodeWalkthroughFile[]): {
3
+ readonly activeFile: CodeWalkthroughFile;
4
+ readonly handleTabSwitch: (name: string) => void;
5
+ readonly highlightedCode: string;
6
+ };
@@ -0,0 +1,93 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useCodePanel = useCodePanel;
4
+ const react_1 = require("react");
5
+ const contexts_1 = require("../../../core/contexts");
6
+ const hooks_1 = require("../../../core/hooks");
7
+ const ACTIVE_FILE_MOCK = {
8
+ content: [],
9
+ path: '',
10
+ basename: '',
11
+ metadata: {},
12
+ language: '',
13
+ };
14
+ function useCodePanel(files) {
15
+ const { activeStep } = (0, react_1.useContext)(contexts_1.CodeWalkthroughStepsContext);
16
+ const { areConditionsMet, populateInputsWithValue } = (0, react_1.useContext)(contexts_1.CodeWalkthroughControlsStateContext);
17
+ const { useCodeHighlight } = (0, hooks_1.useThemeHooks)();
18
+ const { highlight } = useCodeHighlight();
19
+ const findFileIndexByName = (0, react_1.useCallback)((name) => {
20
+ return files.findIndex((file) => file.path === name);
21
+ }, [files]);
22
+ const findFileIndexByStepId = (0, react_1.useCallback)((id) => files.findIndex((file) => file.metadata.steps.includes(id)), [files]);
23
+ const activeStepFileIndex = activeStep ? findFileIndexByStepId(activeStep) : 0;
24
+ const initialActiveFileIndex = activeStepFileIndex !== -1 ? activeStepFileIndex : 0;
25
+ const [activeFileIndex, setActiveFileIndex] = (0, react_1.useState)(initialActiveFileIndex);
26
+ (0, react_1.useEffect)(() => {
27
+ setActiveFileIndex(initialActiveFileIndex);
28
+ }, [initialActiveFileIndex, activeStep, files]);
29
+ const handleTabSwitch = (0, react_1.useCallback)((name) => {
30
+ const index = findFileIndexByName(name);
31
+ if (index !== -1) {
32
+ setActiveFileIndex(index);
33
+ }
34
+ }, [findFileIndexByName]);
35
+ const activeFile = files[activeFileIndex] ||
36
+ // Fallback to default. Needed when switching from language with more files to a language with less files
37
+ files[initialActiveFileIndex] ||
38
+ // Final fallback for dev mode when no files were added yet
39
+ ACTIVE_FILE_MOCK;
40
+ const highlightedCode = (0, react_1.useMemo)(() => {
41
+ const { highlightedLines, code, isWholeFileSelected } = getRenderableCode(activeFile, activeStep, areConditionsMet, populateInputsWithValue);
42
+ return highlight(code, activeFile.language, {
43
+ withLineNumbers: true,
44
+ // Shiki transformerMetaHighlight meta to highlight lines
45
+ // If the whole file is selected for a step, do not apply highlighting
46
+ highlight: isWholeFileSelected ? '' : `{${Array.from(highlightedLines).join(',')}}`,
47
+ customTransformer: {
48
+ // Add greyed-out class to lines that are not highlighted
49
+ line(hast, number) {
50
+ if (!highlightedLines.has(number)) {
51
+ this.addClassToHast(hast, 'greyed-out');
52
+ }
53
+ },
54
+ },
55
+ });
56
+ }, [activeFile, activeStep, highlight, areConditionsMet, populateInputsWithValue]);
57
+ return { activeFile, handleTabSwitch, highlightedCode };
58
+ }
59
+ function getRenderableCode(activeFile, activeStep, areConditionsMet, populateInputsWithValue) {
60
+ const codeLines = activeFile.content.flatMap((node) => getCodeLinesFromNode(node, activeStep, areConditionsMet, populateInputsWithValue));
61
+ const codeLinesContent = [];
62
+ const highlightedLines = new Set();
63
+ codeLines.forEach(({ lineContent, highlighted }, idx) => {
64
+ codeLinesContent.push(lineContent);
65
+ if (highlighted) {
66
+ highlightedLines.add(idx + 1);
67
+ }
68
+ });
69
+ return {
70
+ highlightedLines,
71
+ code: codeLinesContent.join('\n'),
72
+ isWholeFileSelected: highlightedLines.size === codeLinesContent.length,
73
+ };
74
+ }
75
+ /**
76
+ * Convert code node to code line objects with content to render and their highlighted status
77
+ */
78
+ function getCodeLinesFromNode(node, activeStep, areConditionsMet, populateInputsWithValue, parentHighlighted = false) {
79
+ if (typeof node === 'string') {
80
+ const replacedNode = populateInputsWithValue(node);
81
+ return [{ lineContent: replacedNode, highlighted: parentHighlighted }];
82
+ }
83
+ else {
84
+ const shouldRenderChunk = areConditionsMet(node.condition);
85
+ const isHighlighted = activeStep != null &&
86
+ node.condition.steps.length > 0 &&
87
+ node.condition.steps.includes(activeStep);
88
+ return shouldRenderChunk
89
+ ? node.children.flatMap((child) => getCodeLinesFromNode(child, activeStep, areConditionsMet, populateInputsWithValue, isHighlighted))
90
+ : [];
91
+ }
92
+ }
93
+ //# sourceMappingURL=use-code-panel.js.map
@@ -0,0 +1,22 @@
1
+ import type { CodeWalkthroughFile, CodeWalkthroughFilter, InputsMarkdocAttr, TogglesMarkdocAttr, CodeWalkthroughFilterItem, ControlState, ControlTypeValue, ControlType, CodeWalkthroughConditionsObject } from '@redocly/config';
2
+ export type getState<T extends ControlType> = (id: string) => Omit<ControlState<T>, 'type'> | null;
3
+ export type changeState<T extends ControlType> = (id: string, value: ControlTypeValue<T>) => void;
4
+ export type ActiveFilter = {
5
+ id: string;
6
+ label?: string;
7
+ items: CodeWalkthroughFilterItem[];
8
+ };
9
+ export type WalkthroughControlsState = {
10
+ activeFilters: ActiveFilter[];
11
+ getToggleState: getState<'toggle'>;
12
+ changeToggleState: changeState<'toggle'>;
13
+ getInputState: getState<'input'>;
14
+ changeInputState: changeState<'input'>;
15
+ getFilterState: getState<'filter'>;
16
+ changeFilterState: changeState<'filter'>;
17
+ areConditionsMet: (conditions: CodeWalkthroughConditionsObject) => boolean;
18
+ handleDownloadCode: (files: CodeWalkthroughFile[]) => Promise<void>;
19
+ getFileText: (file: CodeWalkthroughFile) => string;
20
+ populateInputsWithValue: (node: string) => string;
21
+ };
22
+ export declare function useCodeWalkthroughControls(filters: Record<string, CodeWalkthroughFilter>, inputs: InputsMarkdocAttr, toggles: TogglesMarkdocAttr): WalkthroughControlsState;
@@ -0,0 +1,183 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useCodeWalkthroughControls = useCodeWalkthroughControls;
4
+ const react_1 = require("react");
5
+ const react_router_dom_1 = require("react-router-dom");
6
+ const utils_1 = require("../../../core/utils");
7
+ const defaultControlsValues = {
8
+ input: '',
9
+ toggle: false,
10
+ filter: '',
11
+ };
12
+ function useCodeWalkthroughControls(filters, inputs, toggles) {
13
+ const location = (0, react_router_dom_1.useLocation)();
14
+ const navigate = (0, react_router_dom_1.useNavigate)();
15
+ const searchParams = (0, react_1.useMemo)(() => new URLSearchParams(location.search), [location.search]);
16
+ const [togglesState, setTogglesState] = (0, react_1.useState)(() => {
17
+ const initialState = {};
18
+ for (const [id, toggle] of Object.entries(toggles)) {
19
+ initialState[id] = Object.assign(Object.assign({}, toggle), { render: true, type: 'toggle', value: searchParams.get(id) === 'true' });
20
+ }
21
+ return initialState;
22
+ });
23
+ const changeToggleState = (toggleId, checked) => {
24
+ setTogglesState((prev) => {
25
+ const toggle = prev[toggleId];
26
+ if (toggle) {
27
+ return Object.assign(Object.assign({}, prev), { [toggleId]: Object.assign(Object.assign({}, toggle), { value: checked }) });
28
+ }
29
+ else {
30
+ return prev;
31
+ }
32
+ });
33
+ };
34
+ const getToggleState = (toggleId) => {
35
+ const toggleState = togglesState[toggleId];
36
+ if (toggleState) {
37
+ return {
38
+ render: toggleState.render,
39
+ value: toggleState.value,
40
+ };
41
+ }
42
+ else {
43
+ return null;
44
+ }
45
+ };
46
+ const [inputsState, setInputsState] = (0, react_1.useState)(() => {
47
+ const initialState = {};
48
+ for (const [id, input] of Object.entries(inputs)) {
49
+ initialState[id] = Object.assign(Object.assign({}, input), { render: true, type: 'input', value: searchParams.get(id) || input.value });
50
+ }
51
+ return initialState;
52
+ });
53
+ const changeInputState = (inputId, value) => {
54
+ setInputsState((prev) => {
55
+ const input = prev[inputId];
56
+ if (input) {
57
+ return Object.assign(Object.assign({}, prev), { [inputId]: Object.assign(Object.assign({}, input), { value }) });
58
+ }
59
+ else {
60
+ return prev;
61
+ }
62
+ });
63
+ };
64
+ const getInputState = (inputId) => {
65
+ const inputState = inputsState[inputId];
66
+ if (inputState) {
67
+ return {
68
+ render: inputState.render,
69
+ value: inputState.value,
70
+ };
71
+ }
72
+ else {
73
+ return null;
74
+ }
75
+ };
76
+ const [filtersState, setFiltersState] = (0, react_1.useState)(() => {
77
+ var _a, _b;
78
+ const initialState = {};
79
+ for (const [id, filter] of Object.entries(filters)) {
80
+ initialState[id] = Object.assign(Object.assign({}, filter), { render: true, type: 'filter', value: searchParams.get(id) || ((_b = (_a = filter === null || filter === void 0 ? void 0 : filter.items) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.value) || '' });
81
+ }
82
+ return initialState;
83
+ });
84
+ const changeFilterState = (filterId, value) => {
85
+ setFiltersState((prev) => {
86
+ const filter = prev[filterId];
87
+ if (filter) {
88
+ return Object.assign(Object.assign({}, prev), { [filterId]: Object.assign(Object.assign({}, filter), { value }) });
89
+ }
90
+ else {
91
+ return prev;
92
+ }
93
+ });
94
+ };
95
+ const getFilterState = (filterId) => {
96
+ const filterState = filtersState[filterId];
97
+ if (filterState) {
98
+ return {
99
+ render: filterState.render,
100
+ value: filterState.value,
101
+ };
102
+ }
103
+ else {
104
+ return null;
105
+ }
106
+ };
107
+ const state = Object.assign(Object.assign(Object.assign({}, filtersState), togglesState), inputsState);
108
+ const walkthroughContext = (0, react_1.useMemo)(() => {
109
+ const areConditionsMet = (conditions) => (0, utils_1.matchCodeWalkthroughConditions)(conditions, state);
110
+ for (const [id, element] of Object.entries(state)) {
111
+ if (element && !areConditionsMet(element)) {
112
+ state[id].render = false;
113
+ state[id].value = defaultControlsValues[element.type];
114
+ }
115
+ }
116
+ const activeFilters = [];
117
+ for (const [id, filter] of Object.entries(filters)) {
118
+ if (!filtersState[id].render) {
119
+ continue;
120
+ }
121
+ // code-walk-todo: need to check if we have a default fallback
122
+ const items = Array.isArray(filter === null || filter === void 0 ? void 0 : filter.items) ? filter.items : [];
123
+ const activeItems = items.filter((item) => areConditionsMet(item));
124
+ if (activeItems.length === 0) {
125
+ filtersState[id].render = false;
126
+ filtersState[id].value = defaultControlsValues['filter'];
127
+ continue;
128
+ }
129
+ const currentValue = filtersState[id].value;
130
+ if (currentValue) {
131
+ const isValueInActiveItems = activeItems.findIndex(({ value }) => value === currentValue) !== -1;
132
+ filtersState[id].value = isValueInActiveItems ? currentValue : activeItems[0].value;
133
+ }
134
+ else {
135
+ filtersState[id].value = activeItems[0].value;
136
+ }
137
+ activeFilters.push({
138
+ id,
139
+ label: filter.label,
140
+ items: activeItems,
141
+ });
142
+ }
143
+ const handleDownloadCode = (files) => (0, utils_1.downloadCodeWalkthrough)(files, state, inputsState);
144
+ const getFileText = (file) => (0, utils_1.getCodeWalkthroughFileText)(file, state, inputsState);
145
+ const populateInputsWithValue = (node) => (0, utils_1.replaceInputsWithValue)(node, inputsState);
146
+ return {
147
+ activeFilters,
148
+ areConditionsMet,
149
+ handleDownloadCode,
150
+ getFileText,
151
+ populateInputsWithValue,
152
+ };
153
+ // Ignore state in dependency array as it's simply a combination of toggles, filters and inputs.
154
+ // eslint-disable-next-line react-hooks/exhaustive-deps
155
+ }, [filters, filtersState, togglesState, inputsState]);
156
+ /**
157
+ * Update the URL search params with the current state of the filters and inputs
158
+ */
159
+ (0, react_1.useEffect)(() => {
160
+ const newSearchParams = new URLSearchParams(Array.from(searchParams.entries()));
161
+ for (const [id, { value }] of Object.entries(state)) {
162
+ if (value) {
163
+ newSearchParams.set(id, value.toString());
164
+ }
165
+ else {
166
+ newSearchParams.delete(id);
167
+ }
168
+ }
169
+ const newSearch = newSearchParams.toString();
170
+ if (newSearch === location.search.substring(1))
171
+ return;
172
+ navigate({ search: newSearch });
173
+ // Ignore searchParams in dependency array to avoid infinite re-renders
174
+ // eslint-disable-next-line react-hooks/exhaustive-deps
175
+ }, [filters, filtersState, togglesState, inputsState, navigate, location, state]);
176
+ return Object.assign({ getInputState,
177
+ changeInputState,
178
+ getToggleState,
179
+ changeToggleState,
180
+ getFilterState,
181
+ changeFilterState }, walkthroughContext);
182
+ }
183
+ //# sourceMappingURL=use-code-walkthrough-controls.js.map
@@ -0,0 +1,16 @@
1
+ import type { CodeWalkthroughStepAttr } from '@redocly/config';
2
+ type ActiveStep = string | null;
3
+ type CodeWalkthroughStep = CodeWalkthroughStepAttr & {
4
+ active?: boolean;
5
+ compRef?: HTMLElement;
6
+ };
7
+ export type WalkthroughStepsState = {
8
+ activeStep: ActiveStep;
9
+ setActiveStep: (stepId: ActiveStep) => void;
10
+ register: (element: HTMLElement) => void;
11
+ unregister: (element: HTMLElement) => void;
12
+ lockObserver?: React.MutableRefObject<boolean>;
13
+ filtersElementRef?: React.RefObject<HTMLDivElement>;
14
+ };
15
+ export declare function useCodeWalkthroughSteps(steps: CodeWalkthroughStep[]): WalkthroughStepsState;
16
+ export {};