@atlaskit/editor-plugin-date
Version:
Date plugin for @atlaskit/editor-core
43 lines (40 loc) • 1.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getDateInformation = void 0;
var _utils = require("@atlaskit/editor-common/utils");
var _utils2 = require("@atlaskit/editor-prosemirror/utils");
var getDateInformation = exports.getDateInformation = function getDateInformation(timestamp, intl, state, pos) {
if (!state) {
return {
color: undefined,
displayString: (0, _utils.timestampToString)(timestamp, intl)
};
}
var doc = state.doc,
selection = state.selection;
var _state$schema$nodes = state.schema.nodes,
taskItem = _state$schema$nodes.taskItem,
blockTaskItem = _state$schema$nodes.blockTaskItem;
// We fall back to selection.$from even though it does not cover all use cases
// eg. upon Editor init, selection is at the start, not at the Date node
var $nodePos = typeof pos === 'number' ? doc.resolve(pos) : selection.$from;
var parent = $nodePos.parent;
var withinIncompleteTask = parent.type === taskItem && parent.attrs.state !== 'DONE';
// If there is blockTaskItem in the schema and it's not nested in an incomplete task item,
// check if it's nested in an incomplete block task item
if (blockTaskItem && !withinIncompleteTask) {
var blockTaskItemParent = (0, _utils2.findParentNodeOfTypeClosestToPos)($nodePos, blockTaskItem);
// If nested in a blockTaskItem that is incomplete
if (blockTaskItemParent) {
withinIncompleteTask = blockTaskItemParent.node.attrs.state !== 'DONE';
}
}
var color = withinIncompleteTask && (0, _utils.isPastDate)(timestamp) ? 'red' : undefined;
var displayString = withinIncompleteTask ? (0, _utils.timestampToTaskContext)(timestamp, intl) : (0, _utils.timestampToString)(timestamp, intl);
return {
displayString: displayString,
color: color
};
};