Skip to content

Commit 7ff239c

Browse files
author
Zaydek Michels-Gualtieri
committed
Added emojiClassName so that we don’t need to do <span>{children}</span> and heuristic for emoji-input events
1 parent 17c80ae commit 7ff239c

File tree

4 files changed

+33
-99
lines changed

4 files changed

+33
-99
lines changed

src/Editor/Elements/Elements.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import attrs from "./attrs"
22
import dedupeSpaces from "lib/dedupeSpaces"
3+
import emojiClassName from "./emojiClassName"
34
import escape from "lodash/escape"
45
import IfWrapper from "lib/IfWrapper"
56
import Markdown from "./Markdown"
@@ -14,7 +15,6 @@ import { Strikethrough } from "./InlineElements"
1415
// React components.
1516
function toReact(children) {
1617
if (children === null || typeof children === "string") {
17-
// return !children ? null : <span>{children}</span>
1818
return children
1919
}
2020
const components = []
@@ -52,7 +52,7 @@ export const Header = React.memo(({ tag: Tag, id, syntax, hash, children }) => (
5252
))
5353

5454
export const Paragraph = React.memo(({ id, children }) => (
55-
<p id={id}>
55+
<p id={id} className={emojiClassName(children)}>
5656
{toReact(children) || (
5757
<br />
5858
)}
@@ -223,13 +223,13 @@ export const Image = React.memo(({ id, syntax, src, alt, href, children }) => {
223223
<img className="mx-auto" style={{ minHeight: "1.5em", maxHeight: "24em" }} src={src} alt={alt} />
224224
{/* </IfWrapper> */}
225225
{/* {(!readOnly || (readOnly && children)) && ( */}
226-
<figcaption className="px-6 py-2 text-center text-sm text-gray-600">
227-
<Markdown syntax={syntax} {...attrs.disableAutoCorrect}>
228-
{toReact(children) || (
229-
<br />
230-
)}
231-
</Markdown>
232-
</figcaption>
226+
<figcaption className="px-6 py-2 text-center text-sm text-gray-600">
227+
<Markdown syntax={syntax} {...attrs.disableAutoCorrect}>
228+
{toReact(children) || (
229+
<br />
230+
)}
231+
</Markdown>
232+
</figcaption>
233233
{/* )} */}
234234
</figure>
235235
)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import typeEnum from "./typeEnum"
2+
3+
// Returns an `emojis--${count}` class name.
4+
function emojiClassName(children) {
5+
const count = (
6+
children &&
7+
children.every &&
8+
children.every(each => (
9+
each &&
10+
each.type &&
11+
each.type === typeEnum.Emoji
12+
)) &&
13+
children.length
14+
)
15+
return !count ? undefined : `emojis--${count}`
16+
}
17+
18+
export default emojiClassName

src/Editor/stylesheets/core.css

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,9 @@
1717
line-height: 1;
1818
}
1919

20-
/* https://stackoverflow.com/a/12198561 */
21-
.codex-editor p [aria-label][role="img"]:first-child:nth-last-child(1),
22-
.codex-editor p [aria-label][role="img"]:first-child:nth-last-child(2),
23-
.codex-editor p [aria-label][role="img"]:first-child:nth-last-child(2) ~ [aria-label][role="img"],
24-
.codex-editor p [aria-label][role="img"]:first-child:nth-last-child(3),
25-
.codex-editor p [aria-label][role="img"]:first-child:nth-last-child(3) ~ [aria-label][role="img"] {
20+
.codex-editor p.emojis--1 [aria-label][role="img"],
21+
.codex-editor p.emojis--2 [aria-label][role="img"],
22+
.codex-editor p.emojis--3 [aria-label][role="img"] {
2623
font-size: 2em;
2724
}
2825

src/Editor/useEditor.js

Lines changed: 3 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -370,100 +370,19 @@ const methods = state => ({
370370
const range = selection.getRangeAt(0)
371371
rerenderNeeded = (
372372
(range.startContainer.nodeType === Node.TEXT_NODE && range.startOffset <= 1) ||
373-
// TODO: Add data-codex-rerender?
374373
ascendToElement(range.startContainer).getAttribute("data-codex-markdown") ||
375-
ascendToElement(range.startContainer).getAttribute("aria-label")
374+
(range.startContainer.nodeType === Node.TEXT_NODE && emojiTrie.atEnd(range.startContainer.nodeValue))
376375
)
377376
const root = ascendToElement(range.startContainer).closest("[data-codex-editor] > *")
378377
id = root.id || root.querySelector("[id]").id
379378
}
380-
381-
// if (rerenderNeeded) {
379+
if (rerenderNeeded) {
382380
const nextElement = nextElements.find(each => each.id === id)
383381
if (nextElement) {
384382
nextElement.reactKey = uuidv4().slice(0, 8)
385383
}
386-
// }
387-
388-
// const elements = parseInlineElements(state.nodes[state.pos1.y].data)
389-
// console.log(elements)
384+
}
390385

391-
// // Compares whether two children are equal. Character
392-
// // data is not compared in order to preserve
393-
// // spellcheck highlighting.
394-
// const isString = value => {
395-
// return typeof value === "string"
396-
// }
397-
// const isObject = value => {
398-
// const ok = (
399-
// typeof value === "object" &&
400-
// !Array.isArray(value)
401-
// )
402-
// return ok
403-
// }
404-
// const isArray = value => {
405-
// const ok = (
406-
// typeof value === "object" &&
407-
// Array.isArray(value)
408-
// )
409-
// return ok
410-
// }
411-
// const areEqualChildren = (childrenA, childrenB) => {
412-
// if (childrenA === null || childrenB === null) {
413-
// return childrenA === childrenB
414-
// }
415-
// if (isString(childrenA) && isString(childrenB)) {
416-
// return true
417-
// } else if (isObject(childrenA) && isObject(childrenB)) {
418-
// const ok = (
419-
// childrenA.type === childrenB.type &&
420-
// JSON.stringify(childrenA.syntax) === JSON.stringify(childrenB.syntax) &&
421-
// areEqualChildren(childrenA.children, childrenB.children)
422-
// )
423-
// return ok
424-
// }
425-
// if (!isArray(childrenA) || !isArray(childrenB) || childrenA.length !== childrenB.length) {
426-
// return false
427-
// }
428-
// for (let x = 0; x < childrenA.length; x++) {
429-
// if (!areEqualChildren(childrenA[x], childrenB[x])) {
430-
// return false
431-
// }
432-
// }
433-
// return true
434-
// }
435-
// // Compares whether two elements are equal. Elements
436-
// // are considered to be equal if their types, ID, and
437-
// // children are equal.
438-
// const areEqualElements = (elementA, elementB) => {
439-
// const ok = (
440-
// elementA.type === elementB.type &&
441-
// elementA.id === elementB.id &&
442-
// JSON.stringify(elementA.syntax) === JSON.stringify(elementB.syntax) &&
443-
// areEqualChildren(elementA.children, elementB.children)
444-
// )
445-
// return ok
446-
// }
447-
// let id = ""
448-
// const selection = document.getSelection()
449-
// if (selection.rangeCount) {
450-
// const range = selection.getRangeAt(0)
451-
// const root = ascendToElement(range.startContainer).closest("[data-codex-editor] > *")
452-
// id = root.id || root.querySelector("[id]").id
453-
// }
454-
// const substr = state.nodes[state.pos1.y].data.slice(state.pos1.x - 2, state.pos1.x + 1)
455-
// const mustRerender = false // substr
456-
// // .split("")
457-
// // .some(each => ascii.isPunctuation(each))
458-
// const prevElement = state.elements.find(each => each.id === id)
459-
// const nextElement = nextElements.find(each => each.id === id)
460-
// if (prevElement && nextElement) {
461-
// if (areEqualElements(prevElement, nextElement)) {
462-
// // No-op
463-
// return
464-
// }
465-
// nextElement.reactKey = uuidv4().slice(0, 8)
466-
// }
467386
}
468387

469388
Object.assign(state, {

0 commit comments

Comments
 (0)