Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/editors/slate/elements/html/index.tsx
1698 views
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { register, SlateElement } from "../register";
7
import { toSlate as toSlateImage } from "../image";
8
import HTML from "@cocalc/frontend/components/html-ssr";
9
10
export interface HtmlInline extends SlateElement {
11
type: "html_inline";
12
isInline: true;
13
isVoid: true;
14
html: string;
15
}
16
17
export interface HtmlBlock extends SlateElement {
18
type: "html_block";
19
isInline: false;
20
isVoid: true;
21
html: string;
22
}
23
24
const StaticElement = ({ attributes, element }) => {
25
const html = ((element.html as string) ?? "").trim();
26
if (element.type == "html_inline") {
27
return (
28
<span {...attributes} style={{ display: "inline" }}>
29
<HTML inline value={html} />
30
</span>
31
);
32
} else {
33
return (
34
<div {...attributes}>
35
<HTML value={html} />
36
</div>
37
);
38
}
39
};
40
41
register({
42
slateType: ["html_inline", "html_block"],
43
44
toSlate: ({ type, token, children }) => {
45
// Special case of images (one line, img tag);
46
// we use a completely different function.
47
if (
48
token.content.startsWith("<img ") &&
49
token.content.trim().split("\n").length <= 1
50
) {
51
return toSlateImage({ type, token, children });
52
}
53
return {
54
type: token.type,
55
isVoid: true,
56
isInline: token.type == "html_inline",
57
html: token.content,
58
children,
59
};
60
},
61
62
StaticElement,
63
});
64
65