Path: blob/master/src/packages/frontend/editors/slate/elements/generic.tsx
1698 views
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import React, { CSSProperties as CSS } from "react";6import { string_to_style } from "../util";7import { register, SlateElement } from "./register";8import { dict } from "@cocalc/util/misc";910const VOID_TAGS = new Set([11"area",12"base",13"br",14"col",15"hr",16"img",17"input",18"link",19"meta",20"param",21"command",22" keygen",23"source",24]);2526export interface Generic extends SlateElement {27type: "generic";28isInline: boolean;29tag: string | undefined;30attrs: object | undefined;31}3233register({34slateType: "generic", // this is the generic plugin3536toSlate: ({ token, state, children }) => {37let attrs: object | undefined;38if (state.attrs != null) {39const a: any = dict(state.attrs as any);40if (a.style != null) {41a.style = string_to_style(a.style as any);42}43attrs = a;44} else {45attrs = undefined;46}47return {48type: "generic",49isInline: !state.block,50tag: token.tag ? (token.tag as string) : undefined,51attrs,52children,53};54},5556Element: ({ attributes, children, element }) => {57const elt = element as Generic;58if (elt.tag) {59if (VOID_TAGS.has(elt.tag)) {60return React.createElement(elt.tag as string, {61...attributes,62...(elt.attrs as object),63});64}65let style = {} as CSS;66if (elt.tag == "ol" || elt.tag == "ul") {67// NOTE: this is done correctly of course in the list plugin.68// doing it here is just redundant...69style.marginBottom = "1em";70}7172return React.createElement(73elt.tag as string,74{75...attributes,76...(elt.attrs as object),77...{ style },78},79children80);81}82return (83<p {...attributes} {...elt.attrs}>84{children}85</p>86);87},8889fromSlate: ({ children }) => `${children}\n`,90});919293