Skip to content

Commit 85f5565

Browse files
author
Zaydek Michels-Gualtieri
committed
Changed editor hooks to just useRichTextEditorFromMarkup and useRichTextEditorFromChildren
1 parent 0a8c669 commit 85f5565

5 files changed

Lines changed: 52 additions & 39 deletions

File tree

src/App.js

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from "react"
2-
import { RichTextEditor } from "./RichTextEditor"
2+
import * as RTE from "./RichTextEditor"
33

44
const markup = `
55
<p>
@@ -19,28 +19,37 @@ const markup = `
1919
</p>
2020
`
2121

22-
const App = () => (
23-
<div className="px-6 py-24 flex flex-row justify-center">
24-
<div className="w-full max-w-3xl">
25-
<RichTextEditor markup={markup}>
26-
{/* <p> */}
27-
{/* <code>Go</code> is <strong>expressive, concise, clean, and efficient</strong>. */}
28-
{/* Its <a href="https://google.com">concurrency mechanisms</a> make it easy to write programs that get the most out of <code>multicore and networked machines</code>, while its novel type system enables <em>flexible and modular</em> program construction. */}
29-
{/* Go compiles quickly to machine code yet has the convenience of <strike>garbage</strike> collection and the power of run-time reflection. */}
30-
{/* It's a fast, statically typed, <em>compiled language</em> that feels like a <em>dynamically typed, interpreted language</em>. */}
31-
{/* </p> */}
32-
{/* <p> */}
33-
{/* <br /> */}
34-
{/* </p> */}
35-
{/* <p> */}
36-
{/* <code>Go</code> is <strong>expressive, concise, clean, and efficient</strong>. */}
37-
{/* Its <a href="https://google.com">concurrency mechanisms</a> make it easy to write programs that get the most out of <code>multicore and networked machines</code>, while its novel type system enables <em>flexible and modular</em> program construction. */}
38-
{/* Go compiles quickly to machine code yet has the convenience of <strike>garbage</strike> collection and the power of run-time reflection. */}
39-
{/* It's a fast, statically typed, <em>compiled language</em> that feels like a <em>dynamically typed, interpreted language</em>. */}
40-
{/* </p> */}
41-
</RichTextEditor>
22+
// const children = <React.Fragment>
23+
// <p>
24+
// <code>Go</code> is <strong>expressive, concise, clean, and efficient</strong>.\u0020
25+
// Its <a href="https://google.com">concurrency mechanisms</a> make it easy to write programs that get the most out of <code>multicore and networked machines</code>, while its novel type system enables <em>flexible and modular</em> program construction.\u0020
26+
// Go compiles quickly to machine code yet has the convenience of <strike>garbage</strike> collection and the power of run-time reflection.\u0020
27+
// It's a fast, statically typed, <em>compiled language</em> that feels like a <em>dynamically typed, interpreted language</em>.
28+
// </p>
29+
// <p>
30+
// <br />
31+
// </p>
32+
// <p>
33+
// <code>Go</code> is <strong>expressive, concise, clean, and efficient</strong>.\u0020
34+
// Its <a href="https://google.com">concurrency mechanisms</a> make it easy to write programs that get the most out of <code>multicore and networked machines</code>, while its novel type system enables <em>flexible and modular</em> program construction.\u0020
35+
// Go compiles quickly to machine code yet has the convenience of <strike>garbage</strike> collection and the power of run-time reflection.\u0020
36+
// It's a fast, statically typed, <em>compiled language</em> that feels like a <em>dynamically typed, interpreted language</em>.
37+
// </p>
38+
// </React.Fragment>
39+
40+
const App = () => {
41+
// const [state, dispatch] = RTE.useRichTextEditorFromChildren(children.props.children)
42+
const [state, dispatch] = RTE.useRichTextEditorFromMarkup(markup)
43+
return (
44+
<div className="px-6 py-24 flex flex-row justify-center">
45+
<div className="w-full max-w-3xl">
46+
<RTE.RichTextEditor
47+
state={state}
48+
dispatch={dispatch}
49+
/>
50+
</div>
4251
</div>
43-
</div>
44-
)
52+
)
53+
}
4554

4655
export default App

src/RichTextEditor/RichTextEditor.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,14 @@ import Debugger from "./components/Debugger"
33
import React from "react"
44
import Renderer from "./components/Renderer"
55
import useDOMContentLoadedCallback from "lib/x/useDOMContentLoadedCallback"
6-
import useRichTextEditor from "./useRichTextEditor"
76
import { parseRenderedChildren } from "./parsers"
87

98
import "./RichTextEditor.css"
109

11-
const RichTextEditor = ({ markup, children }) => {
10+
const RichTextEditor = ({ state, dispatch }) => {
1211
const ref = React.useRef(null)
1312
const pointerdownRef = React.useRef(false)
1413

15-
const [state, dispatch] = useRichTextEditor({ markup, children })
16-
1714
// Disables read-only mode on DOMContentLoaded.
1815
useDOMContentLoadedCallback(dispatch.disableReadOnlyMode)
1916

src/RichTextEditor/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
export { default as RichTextEditor } from "./RichTextEditor"
2-
export { default as useRichTextEditor } from "./useRichTextEditor"
2+
export * from "./useRichTextEditor"

src/RichTextEditor/useRichTextEditor.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const newInitialState = elements => ({
1010
lastActionTimestamp: "init",
1111
lastAction: Date.now(),
1212
readOnlyModeEnabled: true, // DOMContentLoaded disables read-only mode
13-
displayMarkdownModeEnabled: false,
1413
focused: false,
1514
elements,
1615
range: {
@@ -49,14 +48,21 @@ function parseElements({ markup, children }) {
4948
return parseSemanticElements(tree)
5049
}
5150

52-
// TODO: Add support for read-only mode enabled and display
53-
// markdown mode enabled
54-
function useRichTextEditor({ markup, children }) {
51+
// Instantiates from markup.
52+
export function useRichTextEditorFromMarkup(markup) {
5553
const initialState = React.useMemo(() => {
56-
const elements = parseElements({ markup, children })
54+
const elements = parseElements({ markup })
5755
return newInitialState(elements)
58-
}, [markup, children])
56+
}, [markup])
5957
return useMethods(methods, initialState)
6058
}
6159

62-
export default useRichTextEditor
60+
// Instantiates from React children. Note that children is
61+
// expected to be an array of React elements.
62+
export function useRichTextEditorFromChildren(children) {
63+
const initialState = React.useMemo(() => {
64+
const elements = parseElements({ children })
65+
return newInitialState(elements)
66+
}, [children])
67+
return useMethods(methods, initialState)
68+
}

src/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ import "stylesheets/tailwind-em-context-extended.css"
1010
import "stylesheets/tailwind-em-context.css"
1111
import "stylesheets/tailwind-group-hover-text.css"
1212

13-
;(() => {
14-
serviceWorker.unregister()
15-
})()
16-
1713
ReactDOM.render(
1814
<App />,
1915
document.getElementById("root"),
2016
)
17+
18+
// If you want your app to work offline and load faster, you can change
19+
// unregister() to register() below. Note this comes with some pitfalls.
20+
// Learn more about service workers: https://bit.ly/CRA-PWA
21+
serviceWorker.unregister()

0 commit comments

Comments
 (0)