Skip to content

Commit

Permalink
Handle code blocks correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjg committed May 4, 2024
1 parent cc17210 commit 07422cf
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
4 changes: 2 additions & 2 deletions playground/src/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -509,8 +509,8 @@ function headingRule(nodeType: NodeType, maxLevel: number) {
}

function buildInputRules(schema: Schema) {
let rules = smartQuotes.concat(ellipsis, emDash),
type
const rules = smartQuotes.concat(ellipsis, emDash)
let type
if ((type = schema.nodes.blockquote)) rules.push(blockQuoteRule(type))
if ((type = schema.nodes.ordered_list)) rules.push(orderedListRule(type))
if ((type = schema.nodes.bullet_list)) rules.push(bulletListRule(type))
Expand Down
11 changes: 8 additions & 3 deletions playground/src/Playground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@ const rightRepo = new Repo({

const leftHandle = leftRepo.create()
leftHandle.change(d => {
d.text = "Heading"
d.text = ""
//am.splitBlock(d, ["text"], 0, {
//type: new am.RawString("heading"),
//attrs: { level: 1 },
//parents: [],
//})
am.splitBlock(d, ["text"], 0, {
type: new am.RawString("heading"),
attrs: { level: 1 },
type: new am.RawString("code-block"),
attrs: {},
parents: [],
})
})
Expand Down
6 changes: 5 additions & 1 deletion src/traversal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,11 @@ export function amSpliceIdxToPmIdx(
return maxInsertableIndex
}
if (state.event.type === "openTag") {
if (state.event.tag === "paragraph" || state.event.tag === "heading") {
if (
state.event.tag === "paragraph" ||
state.event.tag === "heading" ||
state.event.tag === "code_block"
) {
maxInsertableIndex = state.after.pmIdx
}
} else if (state.event.type === "leafNode") {
Expand Down
16 changes: 16 additions & 0 deletions test/traversal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,22 @@ describe("the traversal API", () => {
// pm 0 0 1 2
assert.equal(amSpliceIdxToPmIdx(spans, 1), 1)
})

it("should find the first index inside a code block at the start of the document", () => {
const spans: am.Span[] = [
{
type: "block",
value: {
type: new am.RawString("code-block"),
attrs: {},
parents: [],
},
},
]
const events = traverseSpans(spans)
console.log(printIndexTable(events))
assert.equal(amSpliceIdxToPmIdx(spans, 1), 1)
})
})

describe("the pmRangeToAmRange function", () => {
Expand Down

0 comments on commit 07422cf

Please sign in to comment.