Skip to content

Commit 3964196

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

File tree

10 files changed

+110
-118
lines changed

10 files changed

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