Skip to content

Commit 522bacc

Browse files
committed
Support a persistent option for widgets
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
1 parent 68ea201 commit 522bacc

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/decoration.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ interface WidgetDecorationSpec {
6666
/// [`requestMeasure`](#view.EditorView.requestMeasure), so that the
6767
/// editor can update its information about its vertical layout.
6868
block?: boolean
69+
/// By default, widgets get removed, during mapping, when the
70+
/// content around them is deleted. Set this to true to make a
71+
/// widget survive such changes.
72+
persistent?: boolean
6973
/// Other properties are allowed.
7074
[other: string]: any
7175
}
@@ -239,7 +243,7 @@ export abstract class Decoration extends RangeValue {
239243
side += (block && !spec.inlineOrder)
240244
? (side > 0 ? Side.BlockAfter : Side.BlockBefore)
241245
: (side > 0 ? Side.InlineAfter : Side.InlineBefore)
242-
return new PointDecoration(spec, side, side, block, spec.widget || null, false)
246+
return new PointDecoration(spec, side, side, block, !!spec.persistent, spec.widget || null, false)
243247
}
244248

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

260264
/// Create a line decoration, which can add DOM attributes to the
@@ -332,10 +336,14 @@ export class PointDecoration extends Decoration {
332336
constructor(spec: any,
333337
startSide: number, endSide: number,
334338
public block: boolean,
339+
persistent: boolean,
335340
widget: WidgetType | null,
336341
readonly isReplace: boolean) {
337342
super(startSide, endSide, widget, spec)
338-
this.mapMode = !block ? MapMode.TrackDel : startSide <= 0 ? MapMode.TrackBefore : MapMode.TrackAfter
343+
this.mapMode = persistent ? MapMode.Simple
344+
: !block ? MapMode.TrackDel
345+
: startSide <= 0 ? MapMode.TrackBefore
346+
: MapMode.TrackAfter
339347
}
340348

341349
// Only relevant when this.block == true

0 commit comments

Comments
 (0)