Skip to content

Commit

Permalink
Don't assume state.lineBreak accurately provides a position offset
Browse files Browse the repository at this point in the history
FIX: Fix a bug in `insertNewlineContinueMarkup` that caused it to put the cursor in
the wrong place when the editor's line break was more than one character long.

Closes codemirror/dev#1273
  • Loading branch information
marijnh committed Oct 2, 2023
1 parent b22ffef commit 49ba40f
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,9 @@ export const insertNewlineContinueMarkup: StateCommand = ({state, dispatch}) =>
for (let i = 0, e = context.length - 2; i <= e; i++) {
insert += context[i].blank(i < e ? countColumn(line.text, 4, context[i + 1].from) - insert.length : null, i < e)
}
insert = normalizeIndent(insert + state.lineBreak, state)
return {range: EditorSelection.cursor(pos + insert.length), changes: {from: line.from, insert}}
insert = normalizeIndent(insert, state)
return {range: EditorSelection.cursor(pos + insert.length + 1),
changes: {from: line.from, insert: insert + state.lineBreak}}
}
}

Expand All @@ -166,9 +167,9 @@ export const insertNewlineContinueMarkup: StateCommand = ({state, dispatch}) =>
}
let from = pos
while (from > line.from && /\s/.test(line.text.charAt(from - line.from - 1))) from--
insert = state.lineBreak + normalizeIndent(insert, state)
changes.push({from, to: pos, insert})
return {range: EditorSelection.cursor(from + insert.length), changes}
insert = normalizeIndent(insert, state)
changes.push({from, to: pos, insert: state.lineBreak + insert})
return {range: EditorSelection.cursor(from + insert.length + 1), changes}
})
if (dont) return false
dispatch(state.update(changes, {scrollIntoView: true, userEvent: "input"}))
Expand Down

0 comments on commit 49ba40f

Please sign in to comment.