Skip to content

Commit

Permalink
Support a persistent option for widgets
Browse files Browse the repository at this point in the history
FEATURE: Widget decorations now take a `persistent` option that can be enabled
to prevent mapping from removing them.

See https://discuss.codemirror.net/t/mapmode-simple-for-block-widget-decorations/9040
  • Loading branch information
marijnh committed Feb 26, 2025
1 parent 68ea201 commit 522bacc
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/decoration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ interface WidgetDecorationSpec {
/// [`requestMeasure`](#view.EditorView.requestMeasure), so that the
/// editor can update its information about its vertical layout.
block?: boolean
/// By default, widgets get removed, during mapping, when the
/// content around them is deleted. Set this to true to make a
/// widget survive such changes.
persistent?: boolean
/// Other properties are allowed.
[other: string]: any
}
Expand Down Expand Up @@ -239,7 +243,7 @@ export abstract class Decoration extends RangeValue {
side += (block && !spec.inlineOrder)
? (side > 0 ? Side.BlockAfter : Side.BlockBefore)
: (side > 0 ? Side.InlineAfter : Side.InlineBefore)
return new PointDecoration(spec, side, side, block, spec.widget || null, false)
return new PointDecoration(spec, side, side, block, !!spec.persistent, spec.widget || null, false)
}

/// Create a replace decoration which replaces the given range with
Expand All @@ -254,7 +258,7 @@ export abstract class Decoration extends RangeValue {
startSide = (start ? (block ? Side.BlockIncStart : Side.InlineIncStart) : Side.NonIncStart) - 1
endSide = (end ? (block ? Side.BlockIncEnd : Side.InlineIncEnd) : Side.NonIncEnd) + 1
}
return new PointDecoration(spec, startSide, endSide, block, spec.widget || null, true)
return new PointDecoration(spec, startSide, endSide, block, false, spec.widget || null, true)
}

/// Create a line decoration, which can add DOM attributes to the
Expand Down Expand Up @@ -332,10 +336,14 @@ export class PointDecoration extends Decoration {
constructor(spec: any,
startSide: number, endSide: number,
public block: boolean,
persistent: boolean,
widget: WidgetType | null,
readonly isReplace: boolean) {
super(startSide, endSide, widget, spec)
this.mapMode = !block ? MapMode.TrackDel : startSide <= 0 ? MapMode.TrackBefore : MapMode.TrackAfter
this.mapMode = persistent ? MapMode.Simple
: !block ? MapMode.TrackDel
: startSide <= 0 ? MapMode.TrackBefore
: MapMode.TrackAfter
}

// Only relevant when this.block == true
Expand Down

0 comments on commit 522bacc

Please sign in to comment.