UNPKG

@atlaskit/editor-plugin-date

Version:

Date plugin for @atlaskit/editor-core

37 lines (35 loc) 1.7 kB
import { isPastDate, timestampToString, timestampToTaskContext } from '@atlaskit/editor-common/utils'; import { findParentNodeOfTypeClosestToPos } from '@atlaskit/editor-prosemirror/utils'; export var getDateInformation = function getDateInformation(timestamp, intl, state, pos) { if (!state) { return { color: undefined, displayString: 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 = findParentNodeOfTypeClosestToPos($nodePos, blockTaskItem); // If nested in a blockTaskItem that is incomplete if (blockTaskItemParent) { withinIncompleteTask = blockTaskItemParent.node.attrs.state !== 'DONE'; } } var color = withinIncompleteTask && isPastDate(timestamp) ? 'red' : undefined; var displayString = withinIncompleteTask ? timestampToTaskContext(timestamp, intl) : timestampToString(timestamp, intl); return { displayString: displayString, color: color }; };