Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

node-path.js 3.6KB

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