Skip to content

Commit ab3b43d

Browse files
author
Zaydek Michels-Gualtieri
committed
Added basic support for formatting a selection of spans; implementation is not well behaved
1 parent 41e4247 commit ab3b43d

File tree

1 file changed

+39
-38
lines changed

1 file changed

+39
-38
lines changed

src/Editor/useEditor.js

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -184,50 +184,51 @@ const methods = state => ({
184184
// Computes an array of spans from an array of spans and
185185
// offsets. Offsets must be from shared keys.
186186
const computeSpansBetweenOffsets = (spans, offset1, offset2) => {
187-
const [s1, t1] = must(computeSpanOffsets(spans, offset1))
188-
const [s2, t2] = must(computeSpanOffsets(spans, offset2))
187+
let [s1, t1] = must(computeSpanOffsets(spans, offset1))
188+
let [s2, t2] = must(computeSpanOffsets(spans, offset2))
189189

190-
console.log([s1, t1], [s2, t2])
190+
// console.log([s1, t1], [s2, t2])
191191

192-
// if (t1) {
193-
spans.splice(s1, 1, ...[
194-
{
195-
...spans[s1],
196-
props: {
197-
...spans[s1].props,
198-
children: spans[s1].props.children.slice(0, t1),
199-
}
200-
},
201-
{
202-
...spans[s1],
203-
props: {
204-
...spans[s1].props,
205-
children: spans[s1].props.children.slice(t1),
206-
}
207-
},
208-
])
209-
spans.splice(s2, 1, ...[
210-
{
211-
...spans[s2],
212-
props: {
213-
...spans[s2].props,
214-
children: spans[s2].props.children.slice(0, t2),
215-
}
216-
},
217-
{
218-
...spans[s2],
219-
props: {
220-
...spans[s2].props,
221-
children: spans[s2].props.children.slice(t2),
222-
}
223-
},
224-
])
192+
// ...
193+
if (t1 && t1 < spans[s1].props.children.length) {
194+
const cloned1A = JSONClone(spans[s1])
195+
const cloned1B = JSONClone(spans[s1])
196+
cloned1A.props.children = cloned1A.props.children.slice(0, t1)
197+
cloned1B.props.children = cloned1B.props.children.slice(t1)
198+
spans.splice(s1, 1, cloned1A, cloned1B)
199+
s1++
200+
s2++
201+
}
202+
// ...
203+
if (t2 && t2 < spans[s2].props.children.length) {
204+
const cloned2A = JSONClone(spans[s2])
205+
const cloned2B = JSONClone(spans[s2])
206+
cloned2A.props.children = cloned2A.props.children.slice(0, t2)
207+
cloned2B.props.children = cloned2B.props.children.slice(t2)
208+
spans.splice(s2, 1, cloned2A, cloned2B)
209+
s2++
210+
}
211+
212+
const shouldApply = !spans.slice(s1, s2).every(each => each.types.some(each => each === typeEnum.strong))
213+
spans.slice(s1, s2).map(each => {
214+
const x = each.types.findIndex(each => each === typeEnum.strong)
215+
if (!shouldApply && x >= 0) {
216+
each.types.splice(x, 1)
217+
} else if (shouldApply && x === -1) {
218+
each.types.push(typeEnum.strong)
219+
}
220+
})
225221

226-
// console.log([JSONClone(spans[s1]), t1], [JSONClone(spans[s2]), t2])
227-
console.log(JSONClone(spans))
222+
// for (; s1 < s2; s1++) {
223+
// spans[s1].types.push(typeEnum.strong)
224+
// }
225+
// Spans.defer(spans)
226+
// state.shouldRenderElements++
228227
}
229228

230229
computeSpansBetweenOffsets(spans, state.cursors[0].offset, state.cursors[1].offset)
230+
Spans.defer(spans)
231+
state.shouldRenderElements++
231232

232233
// const shouldApply = !spans.every(each => each.types.some(each => each === typeEnum.strong))
233234
// // console.log({ shouldApply }) // DEBUG

0 commit comments

Comments
 (0)