Skip to content

Commit

Permalink
Add a priority option to addOverlay
Browse files Browse the repository at this point in the history
  • Loading branch information
marijnh committed Aug 15, 2016
1 parent c5c1e92 commit 85888eb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
20 changes: 15 additions & 5 deletions doc/manual.html
Original file line number Diff line number Diff line change
Expand Up @@ -1435,11 +1435,21 @@ <h3 id="api_configuration">Configuration methods</h3>
spec</a> or a mode object (an object with
a <a href="#token"><code>token</code></a> method).
The <code>options</code> parameter is optional. If given, it
should be an object. Currently, only the <code>opaque</code>
option is recognized. This defaults to off, but can be given to
allow the overlay styling, when not <code>null</code>, to
override the styling of the base mode entirely, instead of the
two being applied together.</dd>
should be an object, optionally containing the following options:
<dl>
<dt><code><strong>opaque</strong>: bool</code></dt>
<dd>Defaults to off, but can be given to allow the overlay
styling, when not <code>null</code>, to override the styling of
the base mode entirely, instead of the two being applied
together.</dd>
<dt><code><strong>priority</strong>: number</code></dt>
<dd>Determines the ordering in which the overlays are
applied. Those with high priority are applied after those
with lower priority, and able to override the opaqueness of
the ones that come before. Defaults to 0.</dd>
</dl>
</dd>

<dt id="removeOverlay"><code><strong>cm.removeOverlay</strong>(mode: string|object)</code></dt>
<dd>Pass this the exact value passed for the <code>mode</code>
parameter to <a href="#addOverlay"><code>addOverlay</code></a>,
Expand Down
12 changes: 11 additions & 1 deletion lib/codemirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -4952,7 +4952,10 @@
addOverlay: methodOp(function(spec, options) {
var mode = spec.token ? spec : CodeMirror.getMode(this.options, spec);
if (mode.startState) throw new Error("Overlays may not be stateful.");
this.state.overlays.push({mode: mode, modeSpec: spec, opaque: options && options.opaque});
insertSorted(this.state.overlays,
{mode: mode, modeSpec: spec, opaque: options && options.opaque,
priority: (options && options.priority) || 0},
function(overlay) { return overlay.priority })
this.state.modeGen++;
regChange(this);
}),
Expand Down Expand Up @@ -8370,6 +8373,13 @@
return out;
}

function insertSorted(array, value, score) {
console.log(array)
var pos = 0, priority = score(value)
while (pos < array.length && score(array[pos]) <= priority) pos++
array.splice(pos, 0, value)
}

function nothing() {}

function createObj(base, props) {
Expand Down

0 comments on commit 85888eb

Please sign in to comment.