@atlaskit/adf-schema
Version:
Shared package that contains the ADF-schema (json) and ProseMirror node/mark specs
77 lines (76 loc) • 4.26 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 _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
import { Step, StepResult } from 'prosemirror-transform';
import { Slice, Fragment } from '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) {
_inherits(SetAttrsStep, _Step);
var _super = _createSuper(SetAttrsStep);
function SetAttrsStep(pos, attrs) {
var _this;
_classCallCheck(this, SetAttrsStep);
_this = _super.call(this);
_this.pos = pos;
_this.attrs = attrs;
return _this;
}
_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);
}
}]);
return SetAttrsStep;
}(Step);
Step.jsonID('setAttrs', SetAttrsStep);