@atlaskit/editor-plugin-highlight
Version:
Highlight plugin for @atlaskit/editor-core
65 lines (63 loc) • 2.41 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.highlightPaddingPluginKey = exports.createHighlightPaddingPlugin = void 0;
var _safePlugin = require("@atlaskit/editor-common/safe-plugin");
var _state = require("@atlaskit/editor-prosemirror/state");
var _view = require("@atlaskit/editor-prosemirror/view");
var _addPaddingDecorations = require("./add-padding-decorations");
var _updatePaddingDecorations = require("./update-padding-decorations");
var highlightPaddingPluginKey = exports.highlightPaddingPluginKey = new _state.PluginKey('highlightPaddingPluginKey');
/**
* Plugin to add padding decorations around highlighted text.
*
* Padding is added to the left and/or right of highlighted text
* only when it is at the start or end of a block, or when it is adjacent
* to whitespace.
*/
var createHighlightPaddingPlugin = exports.createHighlightPaddingPlugin = function createHighlightPaddingPlugin() {
return new _safePlugin.SafePlugin({
key: highlightPaddingPluginKey,
state: {
init: function init(_, state) {
// Initially scan the entire doc to create all highlight padding decorations
// after which updates will only apply to changed ranges
var initialDecorationSet = _view.DecorationSet.empty;
var decorationSet = (0, _addPaddingDecorations.addPaddingDecorations)({
decorationSet: initialDecorationSet,
state: state,
from: 0,
to: state.doc.content.size
});
return {
decorationSet: decorationSet
};
},
apply: function apply(tr, pluginState, _oldState, state) {
if (!tr.docChanged) {
return pluginState;
}
var decorationSet = pluginState.decorationSet.map(tr.mapping, tr.doc);
tr.mapping.maps.forEach(function (stepMap) {
stepMap.forEach(function (_oldStart, _oldEnd, start, end) {
decorationSet = (0, _updatePaddingDecorations.updatePaddingDecorations)({
decorationSet: decorationSet,
state: state,
start: start,
end: end
});
});
});
return {
decorationSet: decorationSet
};
}
},
props: {
decorations: function decorations(state) {
return highlightPaddingPluginKey.getState(state).decorationSet;
}
}
});
};