Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/editors/slate/elements/details/index.tsx
1702 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 { CSSProperties } from "react";
7
import { load } from "cheerio";
8
9
import { register, SlateElement } from "../register";
10
import { dict } from "@cocalc/util/misc";
11
12
export const STYLE = {
13
cursor: "pointer",
14
background: "#f6f6f6",
15
color: "black",
16
padding: "0.5rem 1rem",
17
borderRadius: "5px",
18
} as CSSProperties;
19
20
export interface Details extends SlateElement {
21
type: "details";
22
isInline?: boolean;
23
open?: boolean;
24
summary: string;
25
}
26
27
register({
28
slateType: "details",
29
30
StaticElement: ({ attributes, children, element }) => {
31
const node = element as Details;
32
return (
33
<details {...{ ...attributes, ...{ open: node.open } }} style={STYLE}>
34
{node.summary && (
35
<summary style={{ cursor: "pointer", display: "list-item" }}>
36
{node.summary}
37
</summary>
38
)}
39
{children}
40
</details>
41
);
42
},
43
44
toSlate: ({ children, state, token }) => {
45
const attrs = dict(state.attrs as any);
46
const $ = load("");
47
const x = $(token.content);
48
const summary = x.find("summary").text().trim();
49
return {
50
type: "details",
51
children,
52
isInline: token.type == "html_inline",
53
open: attrs.open,
54
summary,
55
};
56
},
57
});
58
59