Skip to content

Commit 5085cf8

Browse files
author
Zaydek Michels-Gualtieri
committed
Reworked resolvers implementation
1 parent 47bc37a commit 5085cf8

File tree

10 files changed

+101
-118
lines changed

10 files changed

+101
-118
lines changed
File renamed without changes.
File renamed without changes.

src/EditorApp/EditorApp.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import ContextDispatch from "./ContextDispatch"
2+
import ContextPrefsDispatch from "./ContextPrefsDispatch"
13
import ctrlOrCmd from "lib/Client/ctrlOrCmd"
2-
import DispatchContext from "./DispatchContext"
34
import keyCodeFor from "lib/Client/keyCodeFor"
45
import MemoFixedBottomWYSIWYGMenu from "./MemoFixedBottomWYSIWYGMenu"
56
import MemoFixedTopPreferences from "./MemoFixedTopPreferences"
6-
import PrefsDispatchContext from "./PrefsDispatchContext"
77
import React from "react"
88
import useKeydown from "lib/x/handlers/useKeydown"
99
import usePreferences from "./usePreferences"
@@ -83,8 +83,8 @@ const App = () => {
8383
})
8484

8585
return (
86-
<DispatchContext.Provider value={dispatch}>
87-
<PrefsDispatchContext.Provider value={prefsDispatch}>
86+
<ContextDispatch.Provider value={dispatch}>
87+
<ContextPrefsDispatch.Provider value={prefsDispatch}>
8888

8989
<div className="px-6 py-32 flex flex-row justify-center h-full">
9090
<div className="w-full max-w-2xl h-full">
@@ -134,8 +134,8 @@ const App = () => {
134134
</div>
135135
</div>
136136

137-
</PrefsDispatchContext.Provider>
138-
</DispatchContext.Provider>
137+
</ContextPrefsDispatch.Provider>
138+
</ContextDispatch.Provider>
139139
)
140140
}
141141

src/EditorApp/MemoFixedBottomWYSIWYGMenu/WYSIWYGMenu.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import DispatchContext from "../DispatchContext"
1+
import ContextDispatch from "../ContextDispatch"
22
import React from "react"
33
import userAgent from "lib/Client/userAgent"
44
import { TopTooltip as Tooltip } from "../Tooltips"
55

66
const WYSIWYGMenu = ({ rangeTypes }) => {
7-
const dispatch = React.useContext(DispatchContext)
7+
const dispatch = React.useContext(ContextDispatch)
88

99
const [tooltip, setTooltip] = React.useState("")
1010

src/EditorApp/MemoFixedTopPreferences/MemoFixedTopPreferences.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import ContextPrefsDispatch from "../ContextPrefsDispatch"
12
import MemoHighlight from "lib/PrismJS/MemoHighlight"
2-
import PrefsDispatchContext from "../PrefsDispatchContext"
33
import React from "react"
44
import Releases from "./Releases"
55
import tabSize from "lib/x/tabSize"
@@ -11,7 +11,7 @@ import {
1111
} from "../Tooltips"
1212

1313
const MemoFixedTopPreferences = React.memo(({ prefs }) => {
14-
const dispatch = React.useContext(PrefsDispatchContext)
14+
const dispatch = React.useContext(ContextPrefsDispatch)
1515

1616
const [tooltip, setTooltip] = React.useState("")
1717

src/EditorApp/resolvers.js

Lines changed: 0 additions & 105 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from "./usePreferences"
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import escape from "lodash/escape"
2+
import toArray from "lib/x/toArray"
3+
import toTree from "Editor/components/toReactTree/toTree"
4+
5+
const markdown = {
6+
em: element => `_${toText(element.props.children, markdown)}_`,
7+
strong: element => `**${toText(element.props.children, markdown)}**`,
8+
code: element => `\`${text(element.props.children)}\``,
9+
strike: element => `~~${toText(element.props.children, markdown)}~~`,
10+
a: element => `[${toText(element.props.children, markdown)}](${element.props.href || "TODO"})`,
11+
"h1": element => `# ${toText(toTree(element.props.children, markdown))}`,
12+
"h2": element => `## ${toText(toTree(element.props.children, markdown))}`,
13+
"h3": element => `### ${toText(toTree(element.props.children, markdown))}`,
14+
"h4": element => `#### ${toText(toTree(element.props.children, markdown))}`,
15+
"h5": element => `##### ${toText(toTree(element.props.children, markdown))}`,
16+
"h6": element => `###### ${toText(toTree(element.props.children, markdown))}`,
17+
"p": element => toText(toTree(element.props.children), markdown),
18+
}
19+
20+
const markup = {
21+
em: element => `<em>${toText(element.props.children, markup)}</em>`,
22+
strong: element => `<strong>${toText(element.props.children, markup)}</strong>`,
23+
code: element => `<code>${toText(element.props.children, markup)}</code>`,
24+
strike: element => `<strike>${toText(element.props.children, markup)}</strike>`,
25+
a: element => `<a href="${element.props.href}">${toText(element.props.children, markup)}</a>`,
26+
"h1": element => `<h1>\n\t${toText(toTree(element.props.children), markup) || "<br />"}\n</h1>`,
27+
"h2": element => `<h2>\n\t${toText(toTree(element.props.children), markup) || "<br />"}\n</h2>`,
28+
"h3": element => `<h3>\n\t${toText(toTree(element.props.children), markup) || "<br />"}\n</h3>`,
29+
"h4": element => `<h4>\n\t${toText(toTree(element.props.children), markup) || "<br />"}\n</h4>`,
30+
"h5": element => `<h5>\n\t${toText(toTree(element.props.children), markup) || "<br />"}\n</h5>`,
31+
"h6": element => `<h6>\n\t${toText(toTree(element.props.children), markup) || "<br />"}\n</h6>`,
32+
"p": element => `<p>\n\t${toText(toTree(element.props.children), markup) || "<br />"}\n</p>`,
33+
}
34+
35+
// Reads text from tree-shaped text nodes. Does not use a
36+
// resolver-type.
37+
function text(treeTextNodes) {
38+
let str = ""
39+
for (const each of toArray(treeTextNodes)) {
40+
if (typeof each === "string") {
41+
str += each
42+
continue
43+
}
44+
str += each.props.children &&
45+
text(each.props.children)
46+
}
47+
return str
48+
}
49+
50+
// Reads text from non-tree-shaped text nodes and a
51+
// resolver-type.
52+
function toText(children, resolver) {
53+
let str = ""
54+
for (const each of toArray(children)) {
55+
if (typeof each === "string") {
56+
str += resolver !== markup ? each : escape(each)
57+
continue
58+
}
59+
str += resolver[each.type](each)
60+
}
61+
return str
62+
}
63+
64+
// Converts to GFM.
65+
export function toMarkdown(elements) {
66+
let str = ""
67+
for (const each of elements) {
68+
str += markdown[each.type](each)
69+
if (each !== elements[elements.length - 1]) {
70+
str += "\n"
71+
}
72+
}
73+
return str
74+
}
75+
76+
// Converts to HTML.
77+
export function toMarkup(elements) {
78+
let str = ""
79+
for (const each of elements) {
80+
str += markup[each.type](each)
81+
if (each !== elements[elements.length - 1]) {
82+
str += "\n"
83+
}
84+
}
85+
return str
86+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// TODO

src/EditorApp/usePreferences.js renamed to src/EditorApp/usePreferences/usePreferences.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { useImmerReducer } from "use-immer"
22

3-
import { // Unsorted
4-
toGFM as toMarkdown, // TODO
5-
toHTML as toMarkup, // TODO
3+
import {
4+
toMarkdown,
5+
toMarkup,
66
} from "./resolvers"
77

88
const init = elements => ({

0 commit comments

Comments
 (0)