@atlaskit/editor-plugin-date
Version:
Date plugin for @atlaskit/editor-core
40 lines (38 loc) • 1.57 kB
JavaScript
import { isPastDate, timestampToString, timestampToTaskContext } from '@atlaskit/editor-common/utils';
import { findParentNodeOfTypeClosestToPos } from '@atlaskit/editor-prosemirror/utils';
export const getDateInformation = (timestamp, intl, state, pos) => {
if (!state) {
return {
color: undefined,
displayString: timestampToString(timestamp, intl)
};
}
const {
doc,
selection
} = state;
const {
taskItem,
blockTaskItem
} = state.schema.nodes;
// 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
const $nodePos = typeof pos === 'number' ? doc.resolve(pos) : selection.$from;
const parent = $nodePos.parent;
let 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) {
const blockTaskItemParent = findParentNodeOfTypeClosestToPos($nodePos, blockTaskItem);
// If nested in a blockTaskItem that is incomplete
if (blockTaskItemParent) {
withinIncompleteTask = blockTaskItemParent.node.attrs.state !== 'DONE';
}
}
const color = withinIncompleteTask && isPastDate(timestamp) ? 'red' : undefined;
const displayString = withinIncompleteTask ? timestampToTaskContext(timestamp, intl) : timestampToString(timestamp, intl);
return {
displayString,
color
};
};