UNPKG

@eccenca/gui-elements

Version:

GUI elements based on other libraries, usable in React application, written in Typescript.

55 lines 2.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.removeMarkFromText = exports.markText = exports.markField = void 0; const state_1 = require("@codemirror/state"); const view_1 = require("@codemirror/view"); const addMarks = state_1.StateEffect === null || state_1.StateEffect === void 0 ? void 0 : state_1.StateEffect.define(), filterMarks = state_1.StateEffect === null || state_1.StateEffect === void 0 ? void 0 : state_1.StateEffect.define(); // This value must be added to the set of extensions to enable this exports.markField = state_1.StateField === null || state_1.StateField === void 0 ? void 0 : state_1.StateField.define({ // Start with an empty set of decorations create() { return view_1.Decoration.none; }, // This is called whenever the editor updates—it computes the new set update(value, tr) { // Move the decorations to account for document changes value = value.map(tr.changes); // If this transaction adds or removes decorations, apply those changes for (const effect of tr.effects) { if (effect.is(addMarks)) value = value.update({ add: effect.value, sort: true }); else if (effect.is(filterMarks)) value = value.update({ filter: effect.value }); } return value; }, // Indicate that this field provides a set of decorations provide: (f) => view_1.EditorView.decorations.from(f), }); const markText = (config) => { var _a; const docLength = config.view.state.doc.length; const strikeMark = view_1.Decoration.mark({ class: config.className, attributes: { title: (_a = config.title) !== null && _a !== void 0 ? _a : "", }, }); const 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 }; }; exports.markText = markText; const removeMarkFromText = (config) => { var _a; const dispatch = (typeof config.view.dispatch === "function" ? (_a = config.view) === null || _a === void 0 ? void 0 : _a.dispatch : () => { }); dispatch({ effects: filterMarks === null || filterMarks === void 0 ? void 0 : filterMarks.of(((from, to) => to <= config.from || from >= config.to)), }); }; exports.removeMarkFromText = removeMarkFromText; //# sourceMappingURL=markText.js.map