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