UNPKG

@atlaskit/editor-plugin-date

Version:

Date plugin for @atlaskit/editor-core

72 lines (69 loc) 2.64 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; import { DOMSerializer } from '@atlaskit/editor-prosemirror/model'; import { setDatePickerAt } from '../pm-plugins/actions'; import { dateToDOM } from './dateNodeSpec'; /** * */ export class DateNodeView { /** * * @param node * @param view * @param getPos * @param intl * @param decorations * @example */ constructor(node, view, getPos, intl, decorations) { _defineProperty(this, "parentTaskState", ''); this.node = node; this.intl = intl; this.view = view; this.getPos = getPos; const spec = dateToDOM(node, this.view.state, this.getPos, this.intl); const { dom } = DOMSerializer.renderSpec(document, spec); this.dom = dom; // eslint-disable-next-line @repo/internal/dom-events/no-unsafe-event-listeners dom.addEventListener('click', event => { event.stopImmediatePropagation(); const { state, dispatch } = this.view; setDatePickerAt(state.selection.from)(state, dispatch); }); this.parentTaskState = DateNodeView.getParentTaskState(decorations); } /** * * @param node * @param decorations * @example */ update(node, decorations) { // we're only interested in two scenarios to trigger a DOM update: // 1. the date value (timestamp) has changed // 2. A wrapping taskitem state (if present) has changed // in all other cases, we tell prosemirror to ignore DOM updates // if new changes are added to the DOM structure, they will need to be // coded here const hasDateChanged = node.attrs.timestamp !== this.node.attrs.timestamp; const parentTaskState = DateNodeView.getParentTaskState(decorations); const parentTaskStateChanged = parentTaskState !== this.parentTaskState; // update local state after comparisons ... this.parentTaskState = parentTaskState; this.node = node; const skipProseMirrorDomUpdate = !hasDateChanged && !parentTaskStateChanged; return skipProseMirrorDomUpdate; } static getParentTaskState(decorations) { var _parentTaskDecoration, _parentTaskDecoration2; const parentTaskDecoration = decorations.find(d => { return d.spec.dataTaskNodeCheckState !== undefined; }); return (_parentTaskDecoration = parentTaskDecoration === null || parentTaskDecoration === void 0 ? void 0 : (_parentTaskDecoration2 = parentTaskDecoration.spec) === null || _parentTaskDecoration2 === void 0 ? void 0 : _parentTaskDecoration2.dataTaskNodeCheckState) !== null && _parentTaskDecoration !== void 0 ? _parentTaskDecoration : ''; } }