Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/editors/slate/elements/generic.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 React, { CSSProperties as CSS } from "react";
7
import { string_to_style } from "../util";
8
import { register, SlateElement } from "./register";
9
import { dict } from "@cocalc/util/misc";
10
11
const VOID_TAGS = new Set([
12
"area",
13
"base",
14
"br",
15
"col",
16
"hr",
17
"img",
18
"input",
19
"link",
20
"meta",
21
"param",
22
"command",
23
" keygen",
24
"source",
25
]);
26
27
export interface Generic extends SlateElement {
28
type: "generic";
29
isInline: boolean;
30
tag: string | undefined;
31
attrs: object | undefined;
32
}
33
34
register({
35
slateType: "generic", // this is the generic plugin
36
37
toSlate: ({ token, state, children }) => {
38
let attrs: object | undefined;
39
if (state.attrs != null) {
40
const a: any = dict(state.attrs as any);
41
if (a.style != null) {
42
a.style = string_to_style(a.style as any);
43
}
44
attrs = a;
45
} else {
46
attrs = undefined;
47
}
48
return {
49
type: "generic",
50
isInline: !state.block,
51
tag: token.tag ? (token.tag as string) : undefined,
52
attrs,
53
children,
54
};
55
},
56
57
Element: ({ attributes, children, element }) => {
58
const elt = element as Generic;
59
if (elt.tag) {
60
if (VOID_TAGS.has(elt.tag)) {
61
return React.createElement(elt.tag as string, {
62
...attributes,
63
...(elt.attrs as object),
64
});
65
}
66
let style = {} as CSS;
67
if (elt.tag == "ol" || elt.tag == "ul") {
68
// NOTE: this is done correctly of course in the list plugin.
69
// doing it here is just redundant...
70
style.marginBottom = "1em";
71
}
72
73
return React.createElement(
74
elt.tag as string,
75
{
76
...attributes,
77
...(elt.attrs as object),
78
...{ style },
79
},
80
children
81
);
82
}
83
return (
84
<p {...attributes} {...elt.attrs}>
85
{children}
86
</p>
87
);
88
},
89
90
fromSlate: ({ children }) => `${children}\n`,
91
});
92
93