Skip to content

Commit c2e89d3

Browse files
author
Zaydek Michels-Gualtieri
committed
Small tweaks and comments
1 parent c9693a3 commit c2e89d3

11 files changed

Lines changed: 52 additions & 63 deletions

File tree

src/Editor/components/components.js

Lines changed: 0 additions & 53 deletions
This file was deleted.
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import attrs from "./attrs"
22
import React from "react"
33
import T from "./T"
4-
import types from "../types"
54

65
export const Em = ({ children }) => (
7-
<T type={types.enum.em}>
6+
<T type="em">
87
<span className="italic">
98
{children}
109
</span>
1110
</T>
1211
)
1312

1413
export const Strong = ({ children }) => (
15-
<T type={types.enum.strong}>
14+
<T type="strong">
1615
<span className="font-semibold">
1716
{children}
1817
</span>
@@ -21,23 +20,23 @@ export const Strong = ({ children }) => (
2120

2221
// mx-px px-1 py-px font-mono text-sm text-blue-600 bg-white border border-cool-gray-300 rounded
2322
export const Code = ({ children }) => (
24-
<T type={types.enum.code}>
23+
<T type="code">
2524
<span className="mx-px py-1 text-sm font-mono text-blue-600 border border-cool-gray-300" {...attrs.code}>
2625
{children}
2726
</span>
2827
</T>
2928
)
3029

3130
export const Strike = ({ children }) => (
32-
<T type={types.enum.strike}>
31+
<T type="strike">
3332
<span className="line-through text-gray-400">
3433
{children}
3534
</span>
3635
</T>
3736
)
3837

3938
export const A = ({ href, children }) => (
40-
<T type={types.enum.a} props={{ href }}>
39+
<T type="a" props={{ href }}>
4140
<span className="mx-px underline text-blue-600" {...attrs.a}>
4241
{children}
4342
</span>

src/Editor/components/elements.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import deeplySyncTrees from "../utils/deeplySyncTrees"
2+
import parseTree from "lib/parseTree"
3+
import React from "react"
4+
import ReactDOMServer from "react-dom/server"
5+
import T from "./T"
6+
import toReact from "./toReact"
7+
8+
const Element = ({ id, style, children, ...props }) => {
9+
const ref = React.useRef(null)
10+
11+
// NOTE: React **does not** manage rendered children.
12+
React.useLayoutEffect(() => {
13+
if (!ref.current) {
14+
// No-op
15+
return
16+
}
17+
// NOTE: Uses ReactDOMServer.renderToStaticMarkup
18+
// because ReactDOM.render is asynchronous.
19+
const markup = ReactDOMServer.renderToStaticMarkup(children)
20+
const tree = parseTree("<div>" + markup + "</div>")
21+
deeplySyncTrees(tree, ref.current)
22+
}, [children])
23+
24+
// Imperative styles:
25+
const style = {
26+
// https://github.com/codex-src/codex-wysiwyg/commit/f0755661d24e900804ab43b9657ec584c00bbbca
27+
...style, // Takes precedence
28+
whiteSpace: "pre-wrap",
29+
overflowWrap: "break-word",
30+
}
31+
32+
return <div ref={ref} id={id} style={style} {...props} />
33+
}
34+
35+
export const P = React.memo(({ id, children }) => (
36+
<T type="p">
37+
<Element id={id}>
38+
{toReact(children) || (
39+
<br />
40+
)}
41+
</Element>
42+
</T>
43+
))
File renamed without changes.

0 commit comments

Comments
 (0)