Skip to content

Commit 0a894c2

Browse files
author
Zaydek Michels-Gualtieri
committed
Added formatsAndPropsAreEqual so spans can be combined -- works with onInput but needs to be integrated with synthetic handlers like backspace, etc.
1 parent 4cbaced commit 0a894c2

3 files changed

Lines changed: 65 additions & 45 deletions

File tree

src/Editor/Editor.js

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -48,41 +48,48 @@ const Editor = () => {
4848
type: Paragraph,
4949
uuid: uuidv4(),
5050
spans: [
51-
// {
52-
// content: "Hey, ",
53-
// formats: [formatsEnum.strong],
54-
// },
55-
// {
56-
// content: "Russ",
57-
// formats: [formatsEnum.strong, formatsEnum.emphasis],
58-
// },
59-
// {
60-
// content: "!",
61-
// formats: [formatsEnum.strong],
62-
// },
63-
// {
64-
// content: " I’m making some ",
65-
// formats: [formatsEnum.strong],
66-
// },
67-
// {
68-
// content: "progress ",
69-
// formats: [formatsEnum.code],
70-
// },
71-
// {
72-
// content: " on making a ",
73-
// formats: [],
74-
// },
75-
// {
76-
// content: "WYSIWYG",
77-
// formats: [formatsEnum.anchor],
78-
// [formatsEnum.anchor]: {
79-
// href: "https://heroicons.dev",
80-
// },
81-
// },
82-
// {
83-
// content: " editor.",
84-
// formats: [],
85-
// },
51+
{
52+
content: "Hey, ",
53+
formats: [formatsEnum.strong],
54+
},
55+
{
56+
content: "Russ",
57+
formats: [formatsEnum.strong, formatsEnum.emphasis],
58+
},
59+
{
60+
content: "!",
61+
formats: [formatsEnum.strong],
62+
},
63+
{
64+
content: " I’m making some ",
65+
formats: [formatsEnum.strong],
66+
},
67+
{
68+
content: "progress ",
69+
formats: [formatsEnum.code],
70+
},
71+
{
72+
content: " on making a ",
73+
formats: [],
74+
},
75+
{
76+
content: "WYSIWYG",
77+
formats: [formatsEnum.anchor],
78+
[formatsEnum.anchor]: {
79+
href: "https://heroicons.dev",
80+
},
81+
},
82+
{
83+
content: " editor.",
84+
formats: [],
85+
},
86+
{
87+
content: "WYSIWYG",
88+
formats: [formatsEnum.anchor],
89+
[formatsEnum.anchor]: {
90+
href: "https://heroicons.dev",
91+
},
92+
},
8693
],
8794
},
8895
])

src/Editor/ReactRenderer.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ function decoratePos(elements) {
3030
}
3131
const [types1, typeMap1] = computeTypeInfo(elements[x - 1])
3232
const [types2, typeMap2] = computeTypeInfo(elements[x])
33-
const common = types1.filter(a => types2.some(b => a === b))
34-
for (const type of common) {
33+
const typesInCommon = types1.filter(a => types2.some(b => a === b))
34+
for (const type of typesInCommon) {
3535
typeMap1[type].props.pos = !typeMap1[type].props.pos ? "at-start" : "at-center"
3636
typeMap2[type].props.pos = "at-end"
3737
}
3838
}
3939
}
4040

41-
// Parses pseudo-React elements from spans.
41+
// Parses spans to pseudo-React elements.
4242
function parseSpans(spans) {
4343
const elements = []
4444
for (const span of spans) {

src/Editor/spans.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,29 @@ function readSpan(domNode) {
3636
// continue
3737
// }
3838

39+
// Returns whether two spans’ formats and props are equal.
40+
function formatsAndPropsAreEqual(spanA, spanB) {
41+
if (spanA.formats.length !== spanB.formats.length) {
42+
return false
43+
}
44+
for (let x = 0; x < spanA.formats.length; x++) {
45+
if (spanA.formats[x] !== spanB.formats[x]) {
46+
return false
47+
} else if (JSON.stringify(spanA[spanA.formats[x]]) !== JSON.stringify(spanB[spanB.formats[x]])) {
48+
return false
49+
}
50+
}
51+
return true
52+
}
53+
3954
// Reads spans from a UUID element.
4055
export function readSpans(uuidElement) {
4156
const spans = []
42-
for (const domNode of uuidElement.childNodes) {
43-
const span = readSpan(domNode)
44-
if (typeof span === "string") {
45-
if (spans.length && typeof spans[spans.length - 1] === "string") {
46-
spans[spans.length - 1] += span
47-
continue
48-
}
57+
for (let x = 0; x < uuidElement.childNodes.length; x++) {
58+
const span = readSpan(uuidElement.childNodes[x])
59+
if (x && formatsAndPropsAreEqual(spans[spans.length - 1], span)) {
60+
spans[spans.length - 1].content += span.content
61+
continue
4962
}
5063
spans.push(span)
5164
}

0 commit comments

Comments
 (0)