Path: blob/master/src/packages/frontend/editors/slate/elements/details/editable.tsx
1702 views
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { useEffect, useRef, useState } from "react";6import { register } from "../register";7import { Details } from "./index";8import { useSlate } from "../hooks";9import { useSetElement } from "../set-element";1011import { STYLE } from "./index";1213register({14slateType: "details",1516Element: ({ attributes, children, element }) => {17const node = element as Details;18const ref = useRef<any>(undefined);19const [open, setOpen] = useState<boolean>(!!node.open);20useEffect(() => {21if (open != node.open) {22setOpen(!!node.open);23}24}, [node.open]);25const editor = useSlate();26const setElement = useSetElement(editor, element);27const details = (28<details29ref={ref}30style={STYLE}31open={open}32onToggle={() => {33if (ref.current?.open != open) {34setOpen(!open);35setElement({ open: !open });36}37}}38>39{node.summary /* whiteSpace inherits something weird from some css */ && (40<summary41contentEditable={false}42style={{43whiteSpace: "normal",44cursor: "pointer",45display: "list-item",46}}47>48{node.summary}49</summary>50)}51{children}52</details>53);54return node.isInline ? (55<span {...attributes}>{details}</span>56) : (57<div {...attributes}>{details}</div>58);59},6061fromSlate: ({ node, children }) => {62return `<details${node.open ? " open" : ""}>${63node.summary ? "\n <summary>" + node.summary + "</summary>" : ""64}${node.isInline ? "" : "\n\n"}${children.trim()}${65node.isInline ? "" : "\n\n"66}</details>${node.isInline ? "" : "\n\n"}`;67},68});697071