@scm-manager/ui-components 2.31.0 → 2.31.1-20220118-142503

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scm-manager/ui-components",
3
- "version": "2.31.0",
3
+ "version": "2.31.1-20220118-142503",
4
4
  "description": "UI Components for SCM-Manager and its plugins",
5
5
  "main": "src/index.ts",
6
6
  "files": [
@@ -25,7 +25,7 @@
25
25
  "@scm-manager/jest-preset": "^2.13.0",
26
26
  "@scm-manager/prettier-config": "^2.10.1",
27
27
  "@scm-manager/tsconfig": "^2.12.0",
28
- "@scm-manager/ui-tests": "^2.31.0",
28
+ "@scm-manager/ui-tests": "^2.31.1-20220118-142503",
29
29
  "@storybook/addon-actions": "^6.3.12",
30
30
  "@storybook/addon-storyshots": "^6.3.12",
31
31
  "@storybook/builder-webpack5": "^6.3.12",
@@ -66,9 +66,9 @@
66
66
  },
67
67
  "dependencies": {
68
68
  "@headlessui/react": "^1.4.3",
69
- "@scm-manager/ui-api": "^2.31.0",
70
- "@scm-manager/ui-extensions": "^2.31.0",
71
- "@scm-manager/ui-types": "^2.31.0",
69
+ "@scm-manager/ui-api": "^2.31.1-20220118-142503",
70
+ "@scm-manager/ui-extensions": "^2.31.1-20220118-142503",
71
+ "@scm-manager/ui-types": "^2.31.1-20220118-142503",
72
72
  "classnames": "^2.2.6",
73
73
  "date-fns": "^2.4.1",
74
74
  "deepmerge": "^4.2.2",
@@ -0,0 +1,230 @@
1
+ /*
2
+ * MIT License
3
+ *
4
+ * Copyright (c) 2020-present Cloudogu GmbH and Contributors
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+ import React, { FC } from "react";
25
+ import { RouteComponentProps, withRouter } from "react-router-dom";
26
+ import unified from "unified";
27
+ import parseMarkdown from "remark-parse";
28
+ import sanitize from "rehype-sanitize";
29
+ import remark2rehype from "remark-rehype";
30
+ import rehype2react from "rehype-react";
31
+ import gfm from "remark-gfm";
32
+ import { BinderContext } from "@scm-manager/ui-extensions";
33
+ import ErrorBoundary from "../ErrorBoundary";
34
+ import { create as createMarkdownHeadingRenderer } from "./MarkdownHeadingRenderer";
35
+ import { create as createMarkdownLinkRenderer } from "./MarkdownLinkRenderer";
36
+ import { useTranslation, WithTranslation, withTranslation } from "react-i18next";
37
+ import Notification from "../Notification";
38
+ import { createTransformer as createChangesetShortlinkParser } from "./remarkChangesetShortLinkParser";
39
+ import { createTransformer as createValuelessTextAdapter } from "./remarkValuelessTextAdapter";
40
+ import MarkdownCodeRenderer from "./MarkdownCodeRenderer";
41
+ import { AstPlugin } from "./PluginApi";
42
+ import createMdastPlugin from "./createMdastPlugin";
43
+ // @ts-ignore
44
+ import gh from "hast-util-sanitize/lib/github";
45
+ import raw from "rehype-raw";
46
+ import slug from "rehype-slug";
47
+ import merge from "deepmerge";
48
+ import { createComponentList } from "./createComponentList";
49
+ import { ProtocolLinkRendererExtension, ProtocolLinkRendererExtensionMap } from "./markdownExtensions";
50
+
51
+ export type MarkdownProps = {
52
+ content: string;
53
+ renderContext?: object;
54
+ renderers?: any;
55
+ skipHtml?: boolean;
56
+ enableAnchorHeadings?: boolean;
57
+ // basePath for markdown links
58
+ basePath?: string;
59
+ permalink?: string;
60
+ mdastPlugins?: AstPlugin[];
61
+ };
62
+
63
+ type Props = RouteComponentProps & WithTranslation & MarkdownProps;
64
+
65
+ type State = {
66
+ contentRef: HTMLDivElement | null | undefined;
67
+ };
68
+
69
+ const xmlMarkupSample = `\`\`\`xml
70
+ <your>
71
+ <xml>
72
+ <content/>
73
+ </xml>
74
+ </your>
75
+ \`\`\``;
76
+
77
+ const MarkdownErrorNotification: FC = () => {
78
+ const [t] = useTranslation("commons");
79
+ return (
80
+ <Notification type="danger">
81
+ <div className="content">
82
+ <p className="subtitle">{t("markdownErrorNotification.title")}</p>
83
+ <p>{t("markdownErrorNotification.description")}</p>
84
+ <pre>
85
+ <code>{xmlMarkupSample}</code>
86
+ </pre>
87
+ <p>
88
+ {t("markdownErrorNotification.spec")}:{" "}
89
+ <a href="https://github.github.com/gfm/" target="_blank" rel="noreferrer">
90
+ GitHub Flavored Markdown Spec
91
+ </a>
92
+ </p>
93
+ </div>
94
+ </Notification>
95
+ );
96
+ };
97
+
98
+ class LazyMarkdownView extends React.Component<Props, State> {
99
+ static contextType = BinderContext;
100
+
101
+ static defaultProps: Partial<Props> = {
102
+ enableAnchorHeadings: false,
103
+ skipHtml: false
104
+ };
105
+
106
+ constructor(props: Props) {
107
+ super(props);
108
+ this.state = {
109
+ contentRef: null
110
+ };
111
+ }
112
+
113
+ shouldComponentUpdate(nextProps: Readonly<Props>, nextState: Readonly<State>): boolean {
114
+ // We have check if the contentRef changed and update afterwards so the page can scroll to the anchor links.
115
+ // Otherwise it can happen that componentDidUpdate is never executed depending on how fast the markdown got rendered
116
+ // We also have to check if props have changed, because we also want to rerender if one of our props has changed
117
+ return this.state.contentRef !== nextState.contentRef || this.props !== nextProps;
118
+ }
119
+
120
+ componentDidUpdate() {
121
+ const { contentRef } = this.state;
122
+ // we have to use componentDidUpdate, because we have to wait until all
123
+ // children are rendered and componentDidMount is called before the
124
+ // markdown content was rendered.
125
+ const hash = this.props.location.hash;
126
+ if (contentRef && hash) {
127
+ // we query only child elements, to avoid strange scrolling with multiple
128
+ // markdown elements on one page.
129
+ const elementId = decodeURIComponent(hash.substring(1) /* remove # */);
130
+ const element = contentRef.querySelector(`[id="${elementId}"]`);
131
+ if (element && element.scrollIntoView) {
132
+ element.scrollIntoView();
133
+ }
134
+ }
135
+ }
136
+
137
+ render() {
138
+ const {
139
+ content,
140
+ renderers,
141
+ renderContext,
142
+ enableAnchorHeadings,
143
+ skipHtml,
144
+ basePath,
145
+ permalink,
146
+ t,
147
+ mdastPlugins = []
148
+ } = this.props;
149
+
150
+ const rendererFactory = this.context.getExtension("markdown-renderer-factory");
151
+ let remarkRendererList = renderers;
152
+
153
+ if (rendererFactory) {
154
+ remarkRendererList = rendererFactory(renderContext);
155
+ }
156
+
157
+ if (!remarkRendererList) {
158
+ remarkRendererList = {};
159
+ }
160
+
161
+ if (enableAnchorHeadings && permalink && !remarkRendererList.heading) {
162
+ remarkRendererList.heading = createMarkdownHeadingRenderer(permalink);
163
+ }
164
+
165
+ let protocolLinkRendererExtensions: ProtocolLinkRendererExtensionMap = {};
166
+ if (!remarkRendererList.link) {
167
+ const extensionPoints = this.context.getExtensions(
168
+ "markdown-renderer.link.protocol"
169
+ ) as ProtocolLinkRendererExtension[];
170
+ protocolLinkRendererExtensions = extensionPoints.reduce<ProtocolLinkRendererExtensionMap>(
171
+ (prev, { protocol, renderer }) => {
172
+ prev[protocol] = renderer;
173
+ return prev;
174
+ },
175
+ {}
176
+ );
177
+ remarkRendererList.link = createMarkdownLinkRenderer(basePath, protocolLinkRendererExtensions);
178
+ }
179
+
180
+ if (!remarkRendererList.code) {
181
+ remarkRendererList.code = MarkdownCodeRenderer;
182
+ }
183
+
184
+ const remarkPlugins = [...mdastPlugins, createChangesetShortlinkParser(t), createValuelessTextAdapter()].map(
185
+ createMdastPlugin
186
+ );
187
+
188
+ let processor = unified()
189
+ .use(parseMarkdown)
190
+ .use(gfm)
191
+ .use(remarkPlugins)
192
+ .use(remark2rehype, { allowDangerousHtml: true });
193
+
194
+ if (!skipHtml) {
195
+ processor = processor.use(raw);
196
+ }
197
+
198
+ processor = processor
199
+ .use(slug)
200
+ .use(
201
+ sanitize,
202
+ merge(gh, {
203
+ attributes: {
204
+ code: ["className"] // Allow className for code elements, this is necessary to extract the code language
205
+ },
206
+ clobberPrefix: "", // Do not prefix user-provided ids and class names,
207
+ protocols: {
208
+ href: Object.keys(protocolLinkRendererExtensions)
209
+ }
210
+ })
211
+ )
212
+ .use(rehype2react, {
213
+ createElement: React.createElement,
214
+ passNode: true,
215
+ components: createComponentList(remarkRendererList, { permalink })
216
+ });
217
+
218
+ const renderedMarkdown: any = processor.processSync(content).result;
219
+
220
+ return (
221
+ <ErrorBoundary fallback={MarkdownErrorNotification}>
222
+ <div ref={el => this.setState({ contentRef: el })} className="content is-word-break">
223
+ {renderedMarkdown}
224
+ </div>
225
+ </ErrorBoundary>
226
+ );
227
+ }
228
+ }
229
+
230
+ export default withRouter(withTranslation("repos")(LazyMarkdownView));
@@ -21,209 +21,16 @@
21
21
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
22
  * SOFTWARE.
23
23
  */
24
- import React, { FC } from "react";
25
- import { RouteComponentProps, withRouter } from "react-router-dom";
26
- import unified from "unified";
27
- import parseMarkdown from "remark-parse";
28
- import sanitize from "rehype-sanitize";
29
- import remark2rehype from "remark-rehype";
30
- import rehype2react from "rehype-react";
31
- import gfm from "remark-gfm";
32
- import { BinderContext } from "@scm-manager/ui-extensions";
33
- import ErrorBoundary from "../ErrorBoundary";
34
- import { create as createMarkdownHeadingRenderer } from "./MarkdownHeadingRenderer";
35
- import { create as createMarkdownLinkRenderer } from "./MarkdownLinkRenderer";
36
- import { useTranslation, WithTranslation, withTranslation } from "react-i18next";
37
- import Notification from "../Notification";
38
- import { createTransformer as createChangesetShortlinkParser } from "./remarkChangesetShortLinkParser";
39
- import { createTransformer as createValuelessTextAdapter } from "./remarkValuelessTextAdapter";
40
- import MarkdownCodeRenderer from "./MarkdownCodeRenderer";
41
- import { AstPlugin } from "./PluginApi";
42
- import createMdastPlugin from "./createMdastPlugin";
43
- // @ts-ignore
44
- import gh from "hast-util-sanitize/lib/github";
45
- import raw from "rehype-raw";
46
- import slug from "rehype-slug";
47
- import merge from "deepmerge";
48
- import { createComponentList } from "./createComponentList";
49
- import { ProtocolLinkRendererExtension, ProtocolLinkRendererExtensionMap } from "./markdownExtensions";
24
+ import React, { FC, Suspense } from "react";
25
+ import { MarkdownProps } from "./LazyMarkdownView";
26
+ import Loading from "../Loading";
50
27
 
51
- type Props = RouteComponentProps &
52
- WithTranslation & {
53
- content: string;
54
- renderContext?: object;
55
- renderers?: any;
56
- skipHtml?: boolean;
57
- enableAnchorHeadings?: boolean;
58
- // basePath for markdown links
59
- basePath?: string;
60
- permalink?: string;
61
- mdastPlugins?: AstPlugin[];
62
- };
28
+ const LazyMarkdownView = React.lazy(() => import("./LazyMarkdownView"));
63
29
 
64
- type State = {
65
- contentRef: HTMLDivElement | null | undefined;
66
- };
30
+ const MarkdownView: FC<MarkdownProps> = props => (
31
+ <Suspense fallback={<Loading />}>
32
+ <LazyMarkdownView {...props} />
33
+ </Suspense>
34
+ );
67
35
 
68
- const xmlMarkupSample = `\`\`\`xml
69
- <your>
70
- <xml>
71
- <content/>
72
- </xml>
73
- </your>
74
- \`\`\``;
75
-
76
- const MarkdownErrorNotification: FC = () => {
77
- const [t] = useTranslation("commons");
78
- return (
79
- <Notification type="danger">
80
- <div className="content">
81
- <p className="subtitle">{t("markdownErrorNotification.title")}</p>
82
- <p>{t("markdownErrorNotification.description")}</p>
83
- <pre>
84
- <code>{xmlMarkupSample}</code>
85
- </pre>
86
- <p>
87
- {t("markdownErrorNotification.spec")}:{" "}
88
- <a href="https://github.github.com/gfm/" target="_blank" rel="noreferrer">
89
- GitHub Flavored Markdown Spec
90
- </a>
91
- </p>
92
- </div>
93
- </Notification>
94
- );
95
- };
96
-
97
- class MarkdownView extends React.Component<Props, State> {
98
- static contextType = BinderContext;
99
-
100
- static defaultProps: Partial<Props> = {
101
- enableAnchorHeadings: false,
102
- skipHtml: false,
103
- };
104
-
105
- constructor(props: Props) {
106
- super(props);
107
- this.state = {
108
- contentRef: null,
109
- };
110
- }
111
-
112
- shouldComponentUpdate(nextProps: Readonly<Props>, nextState: Readonly<State>): boolean {
113
- // We have check if the contentRef changed and update afterwards so the page can scroll to the anchor links.
114
- // Otherwise it can happen that componentDidUpdate is never executed depending on how fast the markdown got rendered
115
- // We also have to check if props have changed, because we also want to rerender if one of our props has changed
116
- return this.state.contentRef !== nextState.contentRef || this.props !== nextProps;
117
- }
118
-
119
- componentDidUpdate() {
120
- const { contentRef } = this.state;
121
- // we have to use componentDidUpdate, because we have to wait until all
122
- // children are rendered and componentDidMount is called before the
123
- // markdown content was rendered.
124
- const hash = this.props.location.hash;
125
- if (contentRef && hash) {
126
- // we query only child elements, to avoid strange scrolling with multiple
127
- // markdown elements on one page.
128
- const elementId = decodeURIComponent(hash.substring(1) /* remove # */);
129
- const element = contentRef.querySelector(`[id="${elementId}"]`);
130
- if (element && element.scrollIntoView) {
131
- element.scrollIntoView();
132
- }
133
- }
134
- }
135
-
136
- render() {
137
- const {
138
- content,
139
- renderers,
140
- renderContext,
141
- enableAnchorHeadings,
142
- skipHtml,
143
- basePath,
144
- permalink,
145
- t,
146
- mdastPlugins = [],
147
- } = this.props;
148
-
149
- const rendererFactory = this.context.getExtension("markdown-renderer-factory");
150
- let remarkRendererList = renderers;
151
-
152
- if (rendererFactory) {
153
- remarkRendererList = rendererFactory(renderContext);
154
- }
155
-
156
- if (!remarkRendererList) {
157
- remarkRendererList = {};
158
- }
159
-
160
- if (enableAnchorHeadings && permalink && !remarkRendererList.heading) {
161
- remarkRendererList.heading = createMarkdownHeadingRenderer(permalink);
162
- }
163
-
164
- let protocolLinkRendererExtensions: ProtocolLinkRendererExtensionMap = {};
165
- if (!remarkRendererList.link) {
166
- const extensionPoints = this.context.getExtensions(
167
- "markdown-renderer.link.protocol"
168
- ) as ProtocolLinkRendererExtension[];
169
- protocolLinkRendererExtensions = extensionPoints.reduce<ProtocolLinkRendererExtensionMap>(
170
- (prev, { protocol, renderer }) => {
171
- prev[protocol] = renderer;
172
- return prev;
173
- },
174
- {}
175
- );
176
- remarkRendererList.link = createMarkdownLinkRenderer(basePath, protocolLinkRendererExtensions);
177
- }
178
-
179
- if (!remarkRendererList.code) {
180
- remarkRendererList.code = MarkdownCodeRenderer;
181
- }
182
-
183
- const remarkPlugins = [...mdastPlugins, createChangesetShortlinkParser(t), createValuelessTextAdapter()].map(
184
- createMdastPlugin
185
- );
186
-
187
- let processor = unified()
188
- .use(parseMarkdown)
189
- .use(gfm)
190
- .use(remarkPlugins)
191
- .use(remark2rehype, { allowDangerousHtml: true });
192
-
193
- if (!skipHtml) {
194
- processor = processor.use(raw);
195
- }
196
-
197
- processor = processor
198
- .use(slug)
199
- .use(
200
- sanitize,
201
- merge(gh, {
202
- attributes: {
203
- code: ["className"], // Allow className for code elements, this is necessary to extract the code language
204
- },
205
- clobberPrefix: "", // Do not prefix user-provided ids and class names,
206
- protocols: {
207
- href: Object.keys(protocolLinkRendererExtensions),
208
- },
209
- })
210
- )
211
- .use(rehype2react, {
212
- createElement: React.createElement,
213
- passNode: true,
214
- components: createComponentList(remarkRendererList, { permalink }),
215
- });
216
-
217
- const renderedMarkdown: any = processor.processSync(content).result;
218
-
219
- return (
220
- <ErrorBoundary fallback={MarkdownErrorNotification}>
221
- <div ref={(el) => this.setState({ contentRef: el })} className="content is-word-break">
222
- {renderedMarkdown}
223
- </div>
224
- </ErrorBoundary>
225
- );
226
- }
227
- }
228
-
229
- export default withRouter(withTranslation("repos")(MarkdownView));
36
+ export default MarkdownView;
@@ -64,14 +64,14 @@ const Diff: FC<Props> = ({ diff, ...fileProps }) => {
64
64
  {diff.length === 0 ? (
65
65
  <Notification type="info">{t("diff.noDiffFound")}</Notification>
66
66
  ) : (
67
- diff.map((file) => <DiffFile key={createKey(file)} file={file} {...fileProps} />)
67
+ diff.map(file => <DiffFile key={createKey(file)} file={file} {...fileProps} />)
68
68
  )}
69
69
  </div>
70
70
  );
71
71
  };
72
72
 
73
73
  Diff.defaultProps = {
74
- sideBySide: false,
74
+ sideBySide: false
75
75
  };
76
76
 
77
77
  export default Diff;