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