UNPKG

3.94 kBJavaScriptView Raw
1import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
2import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
3import _createClass from "@babel/runtime/helpers/createClass";
4import _inherits from "@babel/runtime/helpers/inherits";
5import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
6import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
7var _excluded = ["sourceEvent"];
8function _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); }; }
9function _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; } }
10import { Slice } from 'prosemirror-model';
11import { ReplaceStep, Step, StepMap, StepResult } from 'prosemirror-transform';
12export var stepType = 'editor-linking-meta';
13export var invertStepType = 'editor-linking-meta-invert';
14/**
15 * Custom Prosemirror Step to attach metadata about user interactions with links
16 * Using a Step means that it will work with prosemirror-history and we get utilise when
17 * firing events on history change
18 */
19export var LinkMetaStep = /*#__PURE__*/function (_Step) {
20 _inherits(LinkMetaStep, _Step);
21 var _super = _createSuper(LinkMetaStep);
22 function LinkMetaStep(pos, metadata) {
23 var _this;
24 var isInverted = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
25 _classCallCheck(this, LinkMetaStep);
26 _this = _super.call(this);
27 _this.pos = pos;
28 _this.metadata = metadata;
29 _this.isInverted = isInverted;
30 return _this;
31 }
32 _createClass(LinkMetaStep, [{
33 key: "getMetadata",
34 value: function getMetadata() {
35 return this.metadata;
36 }
37
38 /**
39 * Generate new undo/redo analytics event when step is inverted
40 */
41 }, {
42 key: "invert",
43 value: function invert() {
44 /**
45 * Omit sourceEvent in history
46 */
47 var _this$metadata = this.metadata,
48 sourceEvent = _this$metadata.sourceEvent,
49 metadata = _objectWithoutProperties(_this$metadata, _excluded);
50 return new LinkMetaStep(this.pos, metadata, true);
51 }
52
53 // Should make no modifications to the doc
54 }, {
55 key: "apply",
56 value: function apply(doc) {
57 return StepResult.ok(doc);
58 }
59 }, {
60 key: "map",
61 value: function map(mapping) {
62 var newPos = this.pos;
63 if (typeof newPos === 'number') {
64 newPos = mapping.map(newPos);
65 }
66 // Return the same events, this step will never be removed
67 return new LinkMetaStep(newPos, this.metadata, this.isInverted);
68 }
69 }, {
70 key: "getMap",
71 value: function getMap() {
72 return new StepMap([this.pos || 0, 0, 0]);
73 }
74
75 // Return null to avoid merging events
76 }, {
77 key: "merge",
78 value: function merge() {
79 return null;
80 }
81 }, {
82 key: "toJSON",
83 value: function toJSON() {
84 // When serialized we should create a noop Replace step
85 return {
86 stepType: 'replace',
87 from: 0,
88 to: 0
89 };
90 }
91 }], [{
92 key: "fromJSON",
93 value: function fromJSON(_, __) {
94 // This is a "local custom step" once serialized
95 // we need to transform it in a no-operation action
96 return new ReplaceStep(0, 0, Slice.empty);
97 }
98 }]);
99 return LinkMetaStep;
100}(Step);
101
102/** Register this step with Prosemirror */
103Step.jsonID(stepType, LinkMetaStep);
\No newline at end of file