Skip to content

Commit 07422cf

Browse files
committed
Handle code blocks correctly
1 parent cc17210 commit 07422cf

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

playground/src/Editor.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -509,8 +509,8 @@ function headingRule(nodeType: NodeType, maxLevel: number) {
509509
}
510510

511511
function buildInputRules(schema: Schema) {
512-
let rules = smartQuotes.concat(ellipsis, emDash),
513-
type
512+
const rules = smartQuotes.concat(ellipsis, emDash)
513+
let type
514514
if ((type = schema.nodes.blockquote)) rules.push(blockQuoteRule(type))
515515
if ((type = schema.nodes.ordered_list)) rules.push(orderedListRule(type))
516516
if ((type = schema.nodes.bullet_list)) rules.push(bulletListRule(type))

playground/src/Playground.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,15 @@ const rightRepo = new Repo({
2121

2222
const leftHandle = leftRepo.create()
2323
leftHandle.change(d => {
24-
d.text = "Heading"
24+
d.text = ""
25+
//am.splitBlock(d, ["text"], 0, {
26+
//type: new am.RawString("heading"),
27+
//attrs: { level: 1 },
28+
//parents: [],
29+
//})
2530
am.splitBlock(d, ["text"], 0, {
26-
type: new am.RawString("heading"),
27-
attrs: { level: 1 },
31+
type: new am.RawString("code-block"),
32+
attrs: {},
2833
parents: [],
2934
})
3035
})

src/traversal.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,11 @@ export function amSpliceIdxToPmIdx(
117117
return maxInsertableIndex
118118
}
119119
if (state.event.type === "openTag") {
120-
if (state.event.tag === "paragraph" || state.event.tag === "heading") {
120+
if (
121+
state.event.tag === "paragraph" ||
122+
state.event.tag === "heading" ||
123+
state.event.tag === "code_block"
124+
) {
121125
maxInsertableIndex = state.after.pmIdx
122126
}
123127
} else if (state.event.type === "leafNode") {

test/traversal.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,22 @@ describe("the traversal API", () => {
223223
// pm 0 0 1 2
224224
assert.equal(amSpliceIdxToPmIdx(spans, 1), 1)
225225
})
226+
227+
it("should find the first index inside a code block at the start of the document", () => {
228+
const spans: am.Span[] = [
229+
{
230+
type: "block",
231+
value: {
232+
type: new am.RawString("code-block"),
233+
attrs: {},
234+
parents: [],
235+
},
236+
},
237+
]
238+
const events = traverseSpans(spans)
239+
console.log(printIndexTable(events))
240+
assert.equal(amSpliceIdxToPmIdx(spans, 1), 1)
241+
})
226242
})
227243

228244
describe("the pmRangeToAmRange function", () => {

0 commit comments

Comments
 (0)