node-path.js 4.4 KB

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