@atlaskit/editor-plugin-mentions
Version:
Mentions plugin for @atlaskit/editor-core
73 lines (69 loc) • 2.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.nextRunStateDecorations = exports.buildAgentRunStateDecorations = void 0;
var _view = require("@atlaskit/editor-prosemirror/view");
var AGENT_MENTION_USER_TYPES = new Set(['APP', 'AGENT']);
var isAgentMentionUserType = function isAgentMentionUserType(userType) {
return typeof userType === 'string' && AGENT_MENTION_USER_TYPES.has(userType);
};
var AGENT_RUN_STATE_ANALYSING_CLASS = 'agent-mention-analysing-state';
/**
* Map the bridge's run-state to a mention node visual class.
* Currently only the "analysing" state changes the chip, applying a shimmer to the pill.
*/
var getRunStateClass = function getRunStateClass(runState) {
switch (runState) {
case 'thinking':
case 'working':
return AGENT_RUN_STATE_ANALYSING_CLASS;
default:
return undefined;
}
};
/**
* Build the node-decoration set that styles agent mentions by their run-state.
*/
var buildAgentRunStateDecorations = exports.buildAgentRunStateDecorations = function buildAgentRunStateDecorations(state, runStateByLocalId) {
if (!runStateByLocalId || Object.keys(runStateByLocalId).length === 0) {
return _view.DecorationSet.empty;
}
var mentionNodeType = state.schema.nodes.mention;
if (!mentionNodeType) {
return _view.DecorationSet.empty;
}
var decorations = [];
state.doc.descendants(function (node, pos) {
if (node.type !== mentionNodeType || !isAgentMentionUserType(node.attrs.userType)) {
return true;
}
var localId = node.attrs.localId;
var runState = typeof localId === 'string' ? runStateByLocalId[localId] : undefined;
if (!runState) {
return true;
}
var stateClass = getRunStateClass(runState);
if (!stateClass) {
return true;
}
decorations.push(_view.Decoration.node(pos, pos + node.nodeSize, {
class: stateClass
}));
return true;
});
return decorations.length ? _view.DecorationSet.create(state.doc, decorations) : _view.DecorationSet.empty;
};
/**
* Computes the next cached run-state `DecorationSet` for a plugin's `apply`, so that
* `props.decorations` can return a stored set instead of walking the doc on every transaction:
*/
var nextRunStateDecorations = exports.nextRunStateDecorations = function nextRunStateDecorations(previous, tr, newState, runStateByLocalId) {
if (runStateByLocalId !== undefined) {
return buildAgentRunStateDecorations(newState, runStateByLocalId);
}
if (tr.docChanged && previous) {
return previous.map(tr.mapping, tr.doc);
}
return previous;
};