| "use strict"; |
|
|
| Object.defineProperty(exports, "__esModule", { |
| value: true |
| }); |
| exports.createPath = createPath; |
|
|
| 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; } |
|
|
| 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; } |
|
|
| 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; } |
|
|
| function findParent(_ref, cb) { |
| var parentPath = _ref.parentPath; |
|
|
| if (parentPath == null) { |
| throw new Error("node is root"); |
| } |
|
|
| var currentPath = parentPath; |
|
|
| while (cb(currentPath) !== false) { |
| |
| |
| if (currentPath.parentPath == null) { |
| return null; |
| } |
|
|
|
|
| currentPath = currentPath.parentPath; |
| } |
|
|
| return currentPath.node; |
| } |
|
|
| function insertBefore(context, newNode) { |
| return insert(context, newNode); |
| } |
|
|
| function insertAfter(context, newNode) { |
| return insert(context, newNode, 1); |
| } |
|
|
| function insert(_ref2, newNode) { |
| var node = _ref2.node, |
| inList = _ref2.inList, |
| parentPath = _ref2.parentPath, |
| parentKey = _ref2.parentKey; |
| var indexOffset = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0; |
|
|
| if (!inList) { |
| throw new Error('inList' + " error: " + ("insert can only be used for nodes that are within lists" || "unknown")); |
| } |
|
|
| if (!(parentPath != null)) { |
| throw new Error('parentPath != null' + " error: " + ("Can not remove root node" || "unknown")); |
| } |
|
|
| |
| var parentList = parentPath.node[parentKey]; |
| var indexInList = parentList.findIndex(function (n) { |
| return n === node; |
| }); |
| parentList.splice(indexInList + indexOffset, 0, newNode); |
| } |
|
|
| function remove(_ref3) { |
| var node = _ref3.node, |
| parentKey = _ref3.parentKey, |
| parentPath = _ref3.parentPath; |
|
|
| if (!(parentPath != null)) { |
| throw new Error('parentPath != null' + " error: " + ("Can not remove root node" || "unknown")); |
| } |
|
|
| |
| var parentNode = parentPath.node; |
|
|
| var parentProperty = parentNode[parentKey]; |
|
|
| if (Array.isArray(parentProperty)) { |
| |
| parentNode[parentKey] = parentProperty.filter(function (n) { |
| return n !== node; |
| }); |
| } else { |
| |
| delete parentNode[parentKey]; |
| } |
|
|
| node._deleted = true; |
| } |
|
|
| function stop(context) { |
| context.shouldStop = true; |
| } |
|
|
| function replaceWith(context, newNode) { |
| |
| var parentNode = context.parentPath.node; |
|
|
| var parentProperty = parentNode[context.parentKey]; |
|
|
| if (Array.isArray(parentProperty)) { |
| var indexInList = parentProperty.findIndex(function (n) { |
| return n === context.node; |
| }); |
| parentProperty.splice(indexInList, 1, newNode); |
| } else { |
| |
| parentNode[context.parentKey] = newNode; |
| } |
|
|
| context.node._deleted = true; |
| context.node = newNode; |
| } |
|
|
|
|
| function bindNodeOperations(operations, context) { |
| var keys = Object.keys(operations); |
| var boundOperations = {}; |
| keys.forEach(function (key) { |
| boundOperations[key] = operations[key].bind(null, context); |
| }); |
| return boundOperations; |
| } |
|
|
| function createPathOperations(context) { |
| |
| return bindNodeOperations({ |
| findParent: findParent, |
| replaceWith: replaceWith, |
| remove: remove, |
| insertBefore: insertBefore, |
| insertAfter: insertAfter, |
| stop: stop |
| }, context); |
| } |
|
|
| function createPath(context) { |
| var path = _objectSpread({}, context); |
|
|
|
|
| Object.assign(path, createPathOperations(path)); |
|
|
| return path; |
| } |