@atlaskit/adf-schema
Version:
Shared package that contains the ADF-schema (json) and ProseMirror node/mark specs
75 lines (74 loc) • 3.79 kB
JavaScript
import _typeof from "@babel/runtime/helpers/typeof";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _inherits from "@babel/runtime/helpers/inherits";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
import { Step, StepResult } from '@atlaskit/editor-prosemirror/transform';
import { Slice, Fragment } from '@atlaskit/editor-prosemirror/model';
/**
* For more context on what this is about:
* @see https://discuss.prosemirror.net/t/preventing-image-placeholder-replacement-from-being-undone/1394
*/
export var SetAttrsStep = /*#__PURE__*/function (_Step) {
function SetAttrsStep(pos, attrs) {
var _this;
_classCallCheck(this, SetAttrsStep);
_this = _callSuper(this, SetAttrsStep);
_this.pos = pos;
_this.attrs = attrs;
return _this;
}
_inherits(SetAttrsStep, _Step);
return _createClass(SetAttrsStep, [{
key: "apply",
value: function apply(doc) {
var target = doc.nodeAt(this.pos);
if (!target) {
return StepResult.fail('No node at given position');
}
if (target.isText) {
return StepResult.fail('Target is a text node. Attributes are not allowed.');
}
var attrs = _objectSpread(_objectSpread({}, target.attrs || {}), this.attrs || {});
var newNode = target.type.create(attrs, Fragment.empty, target.marks);
var slice = new Slice(Fragment.from(newNode), 0, target.isLeaf ? 0 : 1);
return StepResult.fromReplace(doc, this.pos, this.pos + 1, slice);
}
}, {
key: "invert",
value: function invert(doc) {
var target = doc.nodeAt(this.pos);
return new SetAttrsStep(this.pos, target ? target.attrs : {});
}
}, {
key: "map",
value: function map(mapping) {
var result = mapping.mapResult(this.pos, 1);
return result.deleted ? null : new SetAttrsStep(result.pos, this.attrs);
}
}, {
key: "toJSON",
value: function toJSON() {
return {
stepType: 'setAttrs',
pos: this.pos,
attrs: this.attrs
};
}
}], [{
key: "fromJSON",
value: function fromJSON(_schema, json) {
if (typeof json.pos !== 'number' || json.attrs !== null && _typeof(json.attrs) !== 'object') {
throw new RangeError('Invalid input for SetAttrsStep.fromJSON');
}
return new SetAttrsStep(json.pos, json.attrs);
}
}]);
}(Step);
Step.jsonID('setAttrs', SetAttrsStep);