1 | function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
|
---|
2 |
|
---|
3 | function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
---|
4 |
|
---|
5 | function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
---|
6 |
|
---|
7 | function findParent(_ref, cb) {
|
---|
8 | var parentPath = _ref.parentPath;
|
---|
9 |
|
---|
10 | if (parentPath == null) {
|
---|
11 | throw new Error("node is root");
|
---|
12 | }
|
---|
13 |
|
---|
14 | var currentPath = parentPath;
|
---|
15 |
|
---|
16 | while (cb(currentPath) !== false) {
|
---|
17 | // Hit the root node, stop
|
---|
18 | // $FlowIgnore
|
---|
19 | if (currentPath.parentPath == null) {
|
---|
20 | return null;
|
---|
21 | } // $FlowIgnore
|
---|
22 |
|
---|
23 |
|
---|
24 | currentPath = currentPath.parentPath;
|
---|
25 | }
|
---|
26 |
|
---|
27 | return currentPath.node;
|
---|
28 | }
|
---|
29 |
|
---|
30 | function insertBefore(context, newNode) {
|
---|
31 | return insert(context, newNode);
|
---|
32 | }
|
---|
33 |
|
---|
34 | function insertAfter(context, newNode) {
|
---|
35 | return insert(context, newNode, 1);
|
---|
36 | }
|
---|
37 |
|
---|
38 | function insert(_ref2, newNode) {
|
---|
39 | var node = _ref2.node,
|
---|
40 | inList = _ref2.inList,
|
---|
41 | parentPath = _ref2.parentPath,
|
---|
42 | parentKey = _ref2.parentKey;
|
---|
43 | var indexOffset = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
|
---|
44 |
|
---|
45 | if (!inList) {
|
---|
46 | throw new Error('inList' + " error: " + ("insert can only be used for nodes that are within lists" || "unknown"));
|
---|
47 | }
|
---|
48 |
|
---|
49 | if (!(parentPath != null)) {
|
---|
50 | throw new Error('parentPath != null' + " error: " + ("Can not remove root node" || "unknown"));
|
---|
51 | }
|
---|
52 |
|
---|
53 | // $FlowIgnore
|
---|
54 | var parentList = parentPath.node[parentKey];
|
---|
55 | var indexInList = parentList.findIndex(function (n) {
|
---|
56 | return n === node;
|
---|
57 | });
|
---|
58 | parentList.splice(indexInList + indexOffset, 0, newNode);
|
---|
59 | }
|
---|
60 |
|
---|
61 | function remove(_ref3) {
|
---|
62 | var node = _ref3.node,
|
---|
63 | parentKey = _ref3.parentKey,
|
---|
64 | parentPath = _ref3.parentPath;
|
---|
65 |
|
---|
66 | if (!(parentPath != null)) {
|
---|
67 | throw new Error('parentPath != null' + " error: " + ("Can not remove root node" || "unknown"));
|
---|
68 | }
|
---|
69 |
|
---|
70 | // $FlowIgnore
|
---|
71 | var parentNode = parentPath.node; // $FlowIgnore
|
---|
72 |
|
---|
73 | var parentProperty = parentNode[parentKey];
|
---|
74 |
|
---|
75 | if (Array.isArray(parentProperty)) {
|
---|
76 | // $FlowIgnore
|
---|
77 | parentNode[parentKey] = parentProperty.filter(function (n) {
|
---|
78 | return n !== node;
|
---|
79 | });
|
---|
80 | } else {
|
---|
81 | // $FlowIgnore
|
---|
82 | delete parentNode[parentKey];
|
---|
83 | }
|
---|
84 |
|
---|
85 | node._deleted = true;
|
---|
86 | }
|
---|
87 |
|
---|
88 | function stop(context) {
|
---|
89 | context.shouldStop = true;
|
---|
90 | }
|
---|
91 |
|
---|
92 | function replaceWith(context, newNode) {
|
---|
93 | // $FlowIgnore
|
---|
94 | var parentNode = context.parentPath.node; // $FlowIgnore
|
---|
95 |
|
---|
96 | var parentProperty = parentNode[context.parentKey];
|
---|
97 |
|
---|
98 | if (Array.isArray(parentProperty)) {
|
---|
99 | var indexInList = parentProperty.findIndex(function (n) {
|
---|
100 | return n === context.node;
|
---|
101 | });
|
---|
102 | parentProperty.splice(indexInList, 1, newNode);
|
---|
103 | } else {
|
---|
104 | // $FlowIgnore
|
---|
105 | parentNode[context.parentKey] = newNode;
|
---|
106 | }
|
---|
107 |
|
---|
108 | context.node._deleted = true;
|
---|
109 | context.node = newNode;
|
---|
110 | } // bind the context to the first argument of node operations
|
---|
111 |
|
---|
112 |
|
---|
113 | function bindNodeOperations(operations, context) {
|
---|
114 | var keys = Object.keys(operations);
|
---|
115 | var boundOperations = {};
|
---|
116 | keys.forEach(function (key) {
|
---|
117 | boundOperations[key] = operations[key].bind(null, context);
|
---|
118 | });
|
---|
119 | return boundOperations;
|
---|
120 | }
|
---|
121 |
|
---|
122 | function createPathOperations(context) {
|
---|
123 | // $FlowIgnore
|
---|
124 | return bindNodeOperations({
|
---|
125 | findParent: findParent,
|
---|
126 | replaceWith: replaceWith,
|
---|
127 | remove: remove,
|
---|
128 | insertBefore: insertBefore,
|
---|
129 | insertAfter: insertAfter,
|
---|
130 | stop: stop
|
---|
131 | }, context);
|
---|
132 | }
|
---|
133 |
|
---|
134 | export function createPath(context) {
|
---|
135 | var path = _objectSpread({}, context); // $FlowIgnore
|
---|
136 |
|
---|
137 |
|
---|
138 | Object.assign(path, createPathOperations(path)); // $FlowIgnore
|
---|
139 |
|
---|
140 | return path;
|
---|
141 | } |
---|