Skip to content

Commit

Permalink
Clip context positions to the size of the edit context
Browse files Browse the repository at this point in the history
FIX: Work around another crash caused by incorrect composition positions
reported by `EditContext`.

See https://discuss.codemirror.net/t/editor-crashing-when-used-as-controlled-component-in-react/8457
Issue codemirror/dev#1472
  • Loading branch information
marijnh committed Dec 4, 2024
1 parent f445c3c commit e7d6eeb
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/domobserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,11 +592,13 @@ class EditContextManager {
for (let format of e.getTextFormats()) {
let lineStyle = format.underlineStyle, thickness = format.underlineThickness
if (lineStyle != "None" && thickness != "None") {
let style = `text-decoration: underline ${
lineStyle == "Dashed" ? "dashed " : lineStyle == "Squiggle" ? "wavy " : ""
}${thickness == "Thin" ? 1 : 2}px`
deco.push(Decoration.mark({attributes: {style}})
.range(this.toEditorPos(format.rangeStart), this.toEditorPos(format.rangeEnd)))
let from = this.toEditorPos(format.rangeStart), to = this.toEditorPos(format.rangeEnd)
if (from < to) {
let style = `text-decoration: underline ${
lineStyle == "Dashed" ? "dashed " : lineStyle == "Squiggle" ? "wavy " : ""
}${thickness == "Thin" ? 1 : 2}px`
deco.push(Decoration.mark({attributes: {style}}).range(from, to))
}
}
}
view.dispatch({effects: setEditContextFormatting.of(Decoration.set(deco))})
Expand Down Expand Up @@ -713,6 +715,7 @@ class EditContextManager {
}

toEditorPos(contextPos: number) {
contextPos = Math.min(contextPos, this.to - this.from)
let c = this.composing
return c && c.drifted ? c.editorBase + (contextPos - c.contextBase) : contextPos + this.from
}
Expand Down

0 comments on commit e7d6eeb

Please sign in to comment.