node-path.js 4.3 KB

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