|
8 | 8 | // etc. This is an assumption that makes things much more efficient,
|
9 | 9 | // and should be fine for everything in cocalc.
|
10 | 10 |
|
11 |
| -import React, { CSSProperties as CSS, useEffect, useRef } from "react"; |
| 11 | +import { CSSProperties as CSS, useEffect, useRef } from "react"; |
12 | 12 | import ReactDOM from "react-dom";
|
13 | 13 | import useIsMountedRef from "@cocalc/frontend/app-framework/is-mounted-hook";
|
14 | 14 | import { is_share_server } from "./share-server";
|
@@ -66,175 +66,180 @@ export interface Props {
|
66 | 66 |
|
67 | 67 | id?: string;
|
68 | 68 |
|
69 |
| - // if given, only run mathjax on result of jquery select with this |
70 |
| - // selector and never use katex. |
71 |
| - mathjax_selector?: string; |
72 |
| - |
73 | 69 | onClick?: (event?: any) => void;
|
74 | 70 | onDoubleClick?: (event?: any) => void;
|
75 | 71 | }
|
76 | 72 |
|
77 |
| -export const HTML: React.FC<Props> = React.memo( |
78 |
| - (props) => { |
79 |
| - const isMountedRef = useIsMountedRef(); |
80 |
| - const ref = useRef(null); |
81 |
| - |
82 |
| - function jq(): any { |
83 |
| - if (!isMountedRef.current) return; |
84 |
| - return $(ReactDOM.findDOMNode(ref.current)) as any; |
| 73 | +export function HTML({ |
| 74 | + value, |
| 75 | + style, |
| 76 | + auto_render_math = true, |
| 77 | + preProcessMath, |
| 78 | + project_id, |
| 79 | + file_path, |
| 80 | + className, |
| 81 | + safeHTML = true, |
| 82 | + href_transform, |
| 83 | + post_hook, |
| 84 | + content_editable, |
| 85 | + reload_images, |
| 86 | + smc_image_scaling, |
| 87 | + highlight_code = true, |
| 88 | + id, |
| 89 | + onClick, |
| 90 | + onDoubleClick, |
| 91 | +}: Props) { |
| 92 | + const isMountedRef = useIsMountedRef(); |
| 93 | + const ref = useRef(null); |
| 94 | + |
| 95 | + function jq(): any { |
| 96 | + if (!isMountedRef.current) return; |
| 97 | + return $(ReactDOM.findDOMNode(ref.current)) as any; |
| 98 | + } |
| 99 | + |
| 100 | + function update_mathjax(): void { |
| 101 | + if (!isMountedRef.current) { |
| 102 | + // see https://github.com/sagemathinc/cocalc/issues/1689 |
| 103 | + return; |
85 | 104 | }
|
86 |
| - |
87 |
| - function update_mathjax(): void { |
88 |
| - if (!isMountedRef.current) { |
89 |
| - // see https://github.com/sagemathinc/cocalc/issues/1689 |
90 |
| - return; |
91 |
| - } |
92 |
| - if (!props.auto_render_math) { |
93 |
| - return; |
94 |
| - } |
95 |
| - jq()?.katex({ preProcess: props.preProcessMath ?? true }); |
| 105 | + if (!auto_render_math) { |
| 106 | + return; |
96 | 107 | }
|
| 108 | + jq()?.katex({ preProcess: preProcessMath ?? true }); |
| 109 | + } |
97 | 110 |
|
98 |
| - function update_links(): void { |
99 |
| - if (!isMountedRef.current) { |
100 |
| - return; |
101 |
| - } |
102 |
| - jq()?.process_smc_links({ |
103 |
| - project_id: props.project_id, |
104 |
| - file_path: props.file_path, |
105 |
| - href_transform: props.href_transform, |
106 |
| - }); |
| 111 | + function update_links(): void { |
| 112 | + if (!isMountedRef.current) { |
| 113 | + return; |
107 | 114 | }
|
108 |
| - |
109 |
| - function update_tables(): void { |
110 |
| - if (!isMountedRef.current) { |
111 |
| - return; |
112 |
| - } |
113 |
| - jq()?.find("table").addClass("table"); |
| 115 | + jq()?.process_smc_links({ |
| 116 | + project_id, |
| 117 | + file_path, |
| 118 | + href_transform, |
| 119 | + }); |
| 120 | + } |
| 121 | + |
| 122 | + function update_tables(): void { |
| 123 | + if (!isMountedRef.current) { |
| 124 | + return; |
114 | 125 | }
|
| 126 | + jq()?.find("table").addClass("table"); |
| 127 | + } |
115 | 128 |
|
116 |
| - function update_images(): void { |
117 |
| - if (!isMountedRef.current) { |
118 |
| - return; |
119 |
| - } |
120 |
| - if (props.reload_images) { |
121 |
| - jq()?.reload_images(); |
122 |
| - } |
123 |
| - if (props.smc_image_scaling) { |
124 |
| - jq()?.smc_image_scaling(); |
125 |
| - } |
| 129 | + function update_images(): void { |
| 130 | + if (!isMountedRef.current) { |
| 131 | + return; |
126 | 132 | }
|
127 |
| - |
128 |
| - function update_code(): void { |
129 |
| - if (isMountedRef.current && props.highlight_code) { |
130 |
| - // note that the highlight_code plugin might not be defined. |
131 |
| - jq()?.highlight_code?.(); |
132 |
| - } |
| 133 | + if (reload_images) { |
| 134 | + jq()?.reload_images(); |
| 135 | + } |
| 136 | + if (smc_image_scaling) { |
| 137 | + jq()?.smc_image_scaling(); |
133 | 138 | }
|
| 139 | + } |
134 | 140 |
|
135 |
| - function do_updates(): void { |
136 |
| - if (is_share_server()) { |
137 |
| - return; |
138 |
| - } |
139 |
| - update_mathjax(); |
140 |
| - update_links(); |
141 |
| - update_tables(); |
142 |
| - update_code(); |
143 |
| - update_images(); |
| 141 | + function update_code(): void { |
| 142 | + if (isMountedRef.current && highlight_code) { |
| 143 | + // note that the highlight_code plugin might not be defined. |
| 144 | + jq()?.highlight_code?.(); |
144 | 145 | }
|
| 146 | + } |
145 | 147 |
|
146 |
| - function update_content(): void { |
147 |
| - if (!isMountedRef.current) { |
148 |
| - return; |
149 |
| - } |
150 |
| - do_updates(); |
| 148 | + function do_updates(): void { |
| 149 | + if (is_share_server()) { |
| 150 | + return; |
151 | 151 | }
|
| 152 | + update_mathjax(); |
| 153 | + update_links(); |
| 154 | + update_tables(); |
| 155 | + update_code(); |
| 156 | + update_images(); |
| 157 | + } |
| 158 | + |
| 159 | + function update_content(): void { |
| 160 | + if (!isMountedRef.current) { |
| 161 | + return; |
| 162 | + } |
| 163 | + do_updates(); |
| 164 | + } |
152 | 165 |
|
153 |
| - useEffect(update_content); |
| 166 | + useEffect(update_content); |
154 | 167 |
|
155 |
| - function render_html(): { __html: string } { |
156 |
| - let html; |
157 |
| - if (!props.value) { |
158 |
| - return { __html: "" }; |
159 |
| - } |
| 168 | + function render_html(): { __html: string } { |
| 169 | + let html; |
| 170 | + if (!value) { |
| 171 | + return { __html: "" }; |
| 172 | + } |
160 | 173 |
|
161 |
| - if (is_share_server()) { |
162 |
| - /* No sanitization at all for share server. For now we |
| 174 | + if (is_share_server()) { |
| 175 | + /* No sanitization at all for share server. For now we |
163 | 176 | have set things up so that the share server is served
|
164 | 177 | from a different subdomain and user can't sign into it,
|
165 | 178 | so XSS is not an issue. Note that the sanitizing
|
166 | 179 | in the else below (on non-share server) is expensive and
|
167 | 180 | can crash on "big" documents (e.g., 500K).
|
168 | 181 | */
|
169 |
| - const elt = $("<div>") as any; |
170 |
| - elt.html(props.value); |
171 |
| - if (props.auto_render_math) { |
172 |
| - elt.katex({ preProcess: props.preProcessMath ?? true }); |
173 |
| - } |
174 |
| - elt.find("table").addClass("table"); |
175 |
| - if (props.highlight_code) { |
176 |
| - elt.highlight_code(); |
177 |
| - } |
178 |
| - elt.process_smc_links({ |
179 |
| - project_id: props.project_id, |
180 |
| - file_path: props.file_path, |
181 |
| - href_transform: props.href_transform, |
182 |
| - }); |
183 |
| - html = elt.html(); |
| 182 | + const elt = $("<div>") as any; |
| 183 | + elt.html(value); |
| 184 | + if (auto_render_math) { |
| 185 | + elt.katex({ preProcess: preProcessMath ?? true }); |
| 186 | + } |
| 187 | + elt.find("table").addClass("table"); |
| 188 | + if (highlight_code) { |
| 189 | + elt.highlight_code(); |
| 190 | + } |
| 191 | + elt.process_smc_links({ |
| 192 | + project_id, |
| 193 | + file_path, |
| 194 | + href_transform, |
| 195 | + }); |
| 196 | + html = elt.html(); |
| 197 | + } else { |
| 198 | + if (safeHTML) { |
| 199 | + html = sanitize_html_safe(value, post_hook); |
184 | 200 | } else {
|
185 |
| - if (props.safeHTML) { |
186 |
| - html = sanitize_html_safe(props.value, props.post_hook); |
187 |
| - } else { |
188 |
| - html = sanitize_html(props.value, true, true, props.post_hook); |
189 |
| - } |
| 201 | + html = sanitize_html(value, true, true, post_hook); |
190 | 202 | }
|
191 |
| - |
192 |
| - return { __html: html }; |
193 | 203 | }
|
194 | 204 |
|
195 |
| - /* The random key is the whole span (hence the html) does |
| 205 | + return { __html: html }; |
| 206 | + } |
| 207 | + |
| 208 | + /* The random key is the whole span (hence the html) does |
196 | 209 | get rendered whenever this component is updated. Otherwise,
|
197 | 210 | it will NOT re-render except when the value changes.
|
198 | 211 | */
|
199 |
| - if (props.content_editable) { |
200 |
| - return ( |
201 |
| - <div |
202 |
| - ref={ref} |
203 |
| - id={props.id} |
204 |
| - contentEditable={true} |
205 |
| - key={Math.random()} |
206 |
| - className={`${props.className ?? ""} cocalc-html-component`} |
207 |
| - dangerouslySetInnerHTML={render_html()} |
208 |
| - style={props.style} |
209 |
| - onClick={props.onClick} |
210 |
| - onDoubleClick={props.onDoubleClick} |
211 |
| - ></div> |
212 |
| - ); |
213 |
| - } else { |
214 |
| - return ( |
215 |
| - <span |
216 |
| - ref={ref} |
217 |
| - id={props.id} |
218 |
| - contentEditable={false} |
219 |
| - key={Math.random()} |
220 |
| - className={`${props.className ?? ""} cocalc-html-component`} |
221 |
| - dangerouslySetInnerHTML={render_html()} |
222 |
| - style={props.style} |
223 |
| - onClick={props.onClick} |
224 |
| - onDoubleClick={props.onDoubleClick} |
225 |
| - ></span> |
226 |
| - ); |
227 |
| - } |
228 |
| - }, |
229 |
| - (props, prev) => props.value == prev.value |
230 |
| -); |
| 212 | + if (content_editable) { |
| 213 | + return ( |
| 214 | + <div |
| 215 | + ref={ref} |
| 216 | + id={id} |
| 217 | + contentEditable={true} |
| 218 | + key={Math.random()} |
| 219 | + className={`${className ?? ""} cocalc-html-component`} |
| 220 | + dangerouslySetInnerHTML={render_html()} |
| 221 | + style={style} |
| 222 | + onClick={onClick} |
| 223 | + onDoubleClick={onDoubleClick} |
| 224 | + ></div> |
| 225 | + ); |
| 226 | + } else { |
| 227 | + return ( |
| 228 | + <span |
| 229 | + ref={ref} |
| 230 | + id={id} |
| 231 | + contentEditable={false} |
| 232 | + key={Math.random()} |
| 233 | + className={`${className ?? ""} cocalc-html-component`} |
| 234 | + dangerouslySetInnerHTML={render_html()} |
| 235 | + style={style} |
| 236 | + onClick={onClick} |
| 237 | + onDoubleClick={onDoubleClick} |
| 238 | + ></span> |
| 239 | + ); |
| 240 | + } |
| 241 | +} |
231 | 242 |
|
232 | 243 | // this displayName is assumed and USED in the packages/hub/share/mathjax-support
|
233 | 244 | // to identify this component; do NOT change or remove!!
|
234 | 245 | HTML.displayName = "Misc-HTML";
|
235 |
| - |
236 |
| -HTML.defaultProps = { |
237 |
| - auto_render_math: true, |
238 |
| - safeHTML: true, |
239 |
| - highlight_code: true, |
240 |
| -}; |
|
0 commit comments