Skip to content

Commit c81c072

Browse files
author
Zaydek Michels-Gualtieri
committed
Partially deprecated part of queryCollection’s API
1 parent 014e365 commit c81c072

3 files changed

Lines changed: 18 additions & 33 deletions

File tree

src/Editor/Types/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
} from "../components/components-text"
1616

1717
// Enumerates types.
18-
export const enumerated = new Enum(
18+
const enumerated = new Enum(
1919
"p",
2020

2121
"em",
@@ -36,7 +36,6 @@ export const components = {
3636
[enumerated.a]: A,
3737
}
3838

39-
// TODO: Use NumberEnum
4039
const i = iota()
4140

4241
// Maps types to sort orders for text components; render
@@ -54,5 +53,5 @@ export function sort(span) {
5453
span.types.sort((T1, T2) => sortOrder[T1] - sortOrder[T2])
5554
}
5655

57-
// Re-exports as enumerated as enum for Types.enum.
56+
// Exports enumerated as Types.enum.
5857
export { enumerated as enum }

src/Editor/useEditor/applyFormat.js

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
11
import * as Types from "../Types"
22
import methods from "./methods"
3-
import NumberEnum from "lib/enums/NumberEnum"
43
import queryCollection from "./queryCollection"
54

6-
// Enumerates how to apply a format.
7-
const enumerated = new NumberEnum(
8-
"plaintext",
9-
"shouldNotApply",
10-
"shouldApply",
11-
)
12-
135
// Tests whether to apply a format.
146
const testShouldApply = collection => (T, P = {}) => {
157
if (T === "plaintext") {
16-
return enumerated.plaintext
8+
return "plaintext"
179
}
18-
const didApply = collection.every(each => each.spans.every(each => each.types.indexOf(T) >= 0))
19-
return didApply ? enumerated.shouldNotApply : enumerated.shouldApply
10+
const didApply = collection.every(each => each.refs.spans.every(each => each.types.indexOf(T) >= 0))
11+
return didApply ? "should-not-apply" : "should-apply"
2012
}
2113

2214
// Applies a format to the current range.
@@ -26,34 +18,34 @@ const applyFormat = state => (T, P = {}) => {
2618
const shouldApply = testShouldApply(collection)(T, P)
2719
switch (shouldApply) {
2820
// Plaintext:
29-
case enumerated.plaintext:
21+
case "plaintext":
3022
for (const c of collection) {
31-
for (const s of c.spans) {
23+
for (const s of c.refs.spans) {
3224
for (const T of s.types) {
3325
s[T] = undefined
3426
}
3527
s.types.splice(0)
3628
}
37-
c.spans.map(each => Types.sort(each))
29+
c.refs.spans.map(each => Types.sort(each))
3830
}
3931
break
40-
// Should not apply:
41-
case enumerated.shouldNotApply:
32+
// Should not apply a format:
33+
case "should-not-apply":
4234
for (const c of collection) {
43-
for (const s of c.spans) {
35+
for (const s of c.refs.spans) {
4436
const x = s.types.indexOf(T)
4537
if (x >= 0) {
4638
s.types.splice(x, 1)
4739
s[T] = undefined
4840
}
4941
}
50-
c.spans.map(each => Types.sort(each))
42+
c.refs.spans.map(each => Types.sort(each))
5143
}
5244
break
53-
// Should apply:
54-
case enumerated.shouldApply:
45+
// Should apply a format:
46+
case "should-apply":
5547
for (const c of collection) {
56-
for (const s of c.spans) {
48+
for (const s of c.refs.spans) {
5749
const x = s.types.indexOf(T)
5850
if (x === -1) {
5951
s.types.push(T)
@@ -62,7 +54,7 @@ const applyFormat = state => (T, P = {}) => {
6254
}
6355
}
6456
}
65-
c.spans.map(each => Types.sort(each))
57+
c.refs.spans.map(each => Types.sort(each))
6658
}
6759
break
6860
default:

src/Editor/useEditor/queryCollection.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import offset from "./offset"
22

3-
// Queries a collection based on the current range.
3+
// Queries the current range as a collection. A collection
4+
// is an array of normalized elements and spans.
45
function queryCollection(state) {
56
const e1 = state.elements.findIndex(each => each.key === state.range[0].key)
67
const e2 = state.elements.findIndex(each => each.key === state.range[1].key)
@@ -15,22 +16,15 @@ function queryCollection(state) {
1516
// Span offsets:
1617
const s1 = offset(each.props.spans, t1)
1718
const s2 = offset(each.props.spans, t2)
18-
// Push references and offsets:
1919
collection.push({
2020
refs: {
2121
element: each,
2222
spans: each.props.spans.slice(s1, s2),
23-
// text: each.props.spans.reduce((acc, each) => acc += each.text, "").slice(t1, t2),
2423
},
2524
offsets: {
2625
element: x,
2726
spans: [s1, s2],
28-
// text: [t1, t2],
2927
},
30-
31-
// TODO: Deprecate
32-
ref: each, // TODO: Rename to element?
33-
spans: each.props.spans.slice(s1, s2),
3428
})
3529
}
3630
return collection

0 commit comments

Comments
 (0)