@atlaskit/editor-plugin-date
Version:
Date plugin for @atlaskit/editor-core
79 lines (76 loc) • 3.13 kB
JavaScript
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
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 var DateNodeView = /*#__PURE__*/function () {
/**
*
* @param node
* @param view
* @param getPos
* @param intl
* @param decorations
* @example
*/
function DateNodeView(node, view, getPos, intl, decorations) {
var _this = this;
_classCallCheck(this, DateNodeView);
_defineProperty(this, "parentTaskState", '');
this.node = node;
this.intl = intl;
this.view = view;
this.getPos = getPos;
var spec = dateToDOM(node, this.view.state, this.getPos, this.intl);
var _DOMSerializer$render = DOMSerializer.renderSpec(document, spec),
dom = _DOMSerializer$render.dom;
this.dom = dom;
// eslint-disable-next-line @repo/internal/dom-events/no-unsafe-event-listeners
dom.addEventListener('click', function (event) {
event.stopImmediatePropagation();
var _this$view = _this.view,
state = _this$view.state,
dispatch = _this$view.dispatch;
setDatePickerAt(state.selection.from)(state, dispatch);
});
this.parentTaskState = DateNodeView.getParentTaskState(decorations);
}
/**
*
* @param node
* @param decorations
* @example
*/
return _createClass(DateNodeView, [{
key: "update",
value: function 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
var hasDateChanged = node.attrs.timestamp !== this.node.attrs.timestamp;
var parentTaskState = DateNodeView.getParentTaskState(decorations);
var parentTaskStateChanged = parentTaskState !== this.parentTaskState;
// update local state after comparisons ...
this.parentTaskState = parentTaskState;
this.node = node;
var skipProseMirrorDomUpdate = !hasDateChanged && !parentTaskStateChanged;
return skipProseMirrorDomUpdate;
}
}], [{
key: "getParentTaskState",
value: function getParentTaskState(decorations) {
var _parentTaskDecoration, _parentTaskDecoration2;
var parentTaskDecoration = decorations.find(function (d) {
return d.spec.dataTaskNodeCheckState !== undefined;
});
return (_parentTaskDecoration = parentTaskDecoration === null || parentTaskDecoration === void 0 || (_parentTaskDecoration2 = parentTaskDecoration.spec) === null || _parentTaskDecoration2 === void 0 ? void 0 : _parentTaskDecoration2.dataTaskNodeCheckState) !== null && _parentTaskDecoration !== void 0 ? _parentTaskDecoration : '';
}
}]);
}();