Skip to content

Commit e4ffad7

Browse files
authored
Merge pull request #25 from atom-ide-community/common-ui
2 parents 8d780fd + b8847cb commit e4ffad7

File tree

10 files changed

+416
-36
lines changed

10 files changed

+416
-36
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
# don't diff machine generated files
44
dist/ -diff
55
package-lock.json -diff
6+
pnpm-lock.yaml -diff

.prettierignore

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package-lock.json
44
pnpm-lock.yaml
55
coverage
66
build
7-
dist
8-
src-package
9-
modules
7+
8+
commons-atom
9+
commons
10+
commons-ui

package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,18 @@
5050
"atom": ">=0.174.0 <2.0.0"
5151
},
5252
"dependencies": {
53-
"atom-package-deps": "^6.0.0"
53+
"atom-package-deps": "^6.0.0",
54+
"dompurify": "^2.1.1",
55+
"etch": "^0.14.1",
56+
"react": "^16.13.1",
57+
"react-dom": "^16.13.1"
5458
},
5559
"devDependencies": {
5660
"@types/atom": "1.40.4",
5761
"@types/node": "^14.11.2",
62+
"@types/dompurify": "^2.0.4",
63+
"@types/react": "^16.9.51",
64+
"@types/react-dom": "^16.9.8",
5865
"typescript": "^4.0.3",
5966
"tslib": "^2.0.1",
6067
"coffeescript": "^1.12.7",

pnpm-lock.yaml

Lines changed: 78 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* a reference to the DOMpurify function to make safe HTML strings
3+
*/
4+
import DOMPurify from "dompurify"
5+
import { MarkdownService } from "../../types-packages/main"
6+
7+
/**
8+
* an etch component that can host already prepared HTML text
9+
*/
10+
export class HTMLView {
11+
rootElement: HTMLElement
12+
13+
/**
14+
* creates the HTML view component and hands over the HTML to embedd
15+
* @param {string} html the HTML string to embedd into the HTML view component
16+
*/
17+
constructor({
18+
html,
19+
containerClassName,
20+
contentClassName,
21+
}: {
22+
html: string
23+
containerClassName: string
24+
contentClassName: string
25+
}) {
26+
this.rootElement = document.createElement("div")
27+
this.rootElement.className = containerClassName
28+
this.rootElement.addEventListener("wheel", this.onMouseWheel, {
29+
passive: true,
30+
})
31+
if (html) {
32+
const innerHTML = DOMPurify.sanitize(html)
33+
this.rootElement.innerHTML = `
34+
<div className='${contentClassName}'>${innerHTML}</div>`
35+
}
36+
}
37+
38+
/**
39+
* cleanup the HTML view component
40+
*/
41+
destroy() {
42+
this.rootElement.removeEventListener("wheel", this.onMouseWheel)
43+
}
44+
45+
/**
46+
* returns the root element of the HTML view component
47+
* @return {HTMLElement} the root element wrapping the HTML content
48+
*/
49+
get element(): HTMLElement {
50+
return this.rootElement
51+
}
52+
53+
/**
54+
* handles the mouse wheel event to enable scrolling over long text
55+
* @param {WheelEvent} evt the mouse wheel event being triggered
56+
*/
57+
onMouseWheel(evt: WheelEvent) {
58+
evt.stopPropagation()
59+
}
60+
}
61+
62+
/**
63+
* convert the markdown documentation to HTML
64+
* @param markdownTexts the documentation text in markdown format to be converted
65+
* @param grammarName the default grammar used for embedded code samples
66+
* @param renderer markdown service to be used for rendering
67+
* @return a promise object to track the asynchronous operation
68+
*/
69+
export async function getDocumentationHtml(
70+
markdownTexts: Array<String>,
71+
grammarName: string,
72+
renderer: MarkdownService
73+
): Promise<string | null> {
74+
if (markdownTexts !== undefined && markdownTexts.length > 0) {
75+
const markdownText = markdownTexts.join("\r\n")
76+
return renderer.render(markdownText, grammarName)
77+
}
78+
return null
79+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { renderToStaticMarkup } from "react-dom/server"
2+
import type { ReactElement } from "react"
3+
4+
/**
5+
* a reference to the DOMpurify function to make safe HTML strings
6+
*/
7+
import DOMPurify from "dompurify"
8+
9+
/**
10+
* an etch component that can host an externally given React component
11+
*/
12+
export class ReactView {
13+
rootElement: HTMLElement
14+
children?: ReactElement
15+
childrenStatic?: string
16+
containerClassName: string
17+
contentClassName: string
18+
19+
/**
20+
* creates a React view component handing over the React code to be rendered
21+
* @param component the React component to be rendered
22+
*/
23+
constructor({
24+
component,
25+
containerClassName,
26+
contentClassName,
27+
}: {
28+
component: () => ReactElement
29+
containerClassName: string
30+
contentClassName: string
31+
}) {
32+
this.containerClassName = containerClassName
33+
this.contentClassName = contentClassName
34+
if (component) {
35+
this.children = component()
36+
}
37+
this.render()
38+
// etch.initialize(this);
39+
}
40+
41+
render() {
42+
this.rootElement = document.createElement("span")
43+
this.rootElement.classList.add(this.containerClassName)
44+
if (this.children) {
45+
this.childrenStatic = DOMPurify.sanitize(renderToStaticMarkup(this.children))
46+
this.rootElement.innerHTML = `
47+
<div className="${this.contentClassName}">${this.childrenStatic}</div>
48+
`
49+
}
50+
return this.rootElement
51+
}
52+
53+
/**
54+
* returns the root element of the React view component
55+
* @return {HTMLElement} the root element wrapping the HTML content
56+
*/
57+
get element(): HTMLElement {
58+
return this.rootElement
59+
}
60+
}

0 commit comments

Comments
 (0)