@eccenca/gui-elements
Version:
GUI elements based on other libraries, usable in React application, written in Typescript.
72 lines • 3.2 kB
JavaScript
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
import { StateEffect, StateField } from "@codemirror/state";
import { Decoration, EditorView } from "@codemirror/view";
var addMarks = StateEffect === null || StateEffect === void 0 ? void 0 : StateEffect.define(), filterMarks = StateEffect === null || StateEffect === void 0 ? void 0 : StateEffect.define();
// This value must be added to the set of extensions to enable this
export var markField = StateField === null || StateField === void 0 ? void 0 : StateField.define({
// Start with an empty set of decorations
create: function () {
return Decoration.none;
},
// This is called whenever the editor updates—it computes the new set
update: function (value, tr) {
var e_1, _a;
// Move the decorations to account for document changes
value = value.map(tr.changes);
try {
// If this transaction adds or removes decorations, apply those changes
for (var _b = __values(tr.effects), _c = _b.next(); !_c.done; _c = _b.next()) {
var effect = _c.value;
if (effect.is(addMarks))
value = value.update({ add: effect.value, sort: true });
else if (effect.is(filterMarks))
value = value.update({ filter: effect.value });
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
return value;
},
// Indicate that this field provides a set of decorations
provide: function (f) { return EditorView.decorations.from(f); },
});
export var markText = function (config) {
var _a;
var docLength = config.view.state.doc.length;
var strikeMark = Decoration.mark({
class: config.className,
attributes: {
title: (_a = config.title) !== null && _a !== void 0 ? _a : "",
},
});
var stopRange = Math.min(config.to, docLength);
if (!docLength || config.from === stopRange)
return { from: 0, to: 0 };
config.view.dispatch({
effects: addMarks.of([strikeMark.range(config.from, stopRange)]),
});
return { from: config.from, to: stopRange };
};
export var removeMarkFromText = function (config) {
var _a;
var dispatch = (typeof config.view.dispatch === "function" ? (_a = config.view) === null || _a === void 0 ? void 0 : _a.dispatch : function () { });
dispatch({
effects: filterMarks === null || filterMarks === void 0 ? void 0 : filterMarks.of((function (from, to) { return to <= config.from || from >= config.to; })),
});
};
//# sourceMappingURL=markText.js.map