You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.5 KiB

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