props.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.containerProps = exports.nodeProps = exports.defineProps = void 0;
  4. const helper_1 = require("../utils/helper");
  5. function defineValueProp(Node, { name, key = name }) {
  6. Node.prototype[name] = function (value) {
  7. if (arguments.length === 0)
  8. return this.attr(key);
  9. return this.attr(key, value);
  10. };
  11. }
  12. function defineArrayProp(Node, { name, key = name }) {
  13. Node.prototype[name] = function (value) {
  14. if (arguments.length === 0)
  15. return this.attr(key);
  16. if (Array.isArray(value))
  17. return this.attr(key, value);
  18. const array = [...(this.attr(key) || []), value];
  19. return this.attr(key, array);
  20. };
  21. }
  22. function defineObjectProp(Node, { name, key: k = name }) {
  23. Node.prototype[name] = function (key, value) {
  24. if (arguments.length === 0)
  25. return this.attr(k);
  26. if (arguments.length === 1 && typeof key !== 'string') {
  27. return this.attr(k, key);
  28. }
  29. const obj = this.attr(k) || {};
  30. obj[key] = arguments.length === 1 ? true : value;
  31. return this.attr(k, obj);
  32. };
  33. }
  34. function defineMixProp(Node, { name }) {
  35. Node.prototype[name] = function (key) {
  36. if (arguments.length === 0)
  37. return this.attr(name);
  38. if (Array.isArray(key))
  39. return this.attr(name, { items: key });
  40. if ((0, helper_1.isStrictObject)(key) &&
  41. (key.title !== undefined || key.items !== undefined)) {
  42. return this.attr(name, key);
  43. }
  44. if (key === null || key === false)
  45. return this.attr(name, key);
  46. const obj = this.attr(name) || {};
  47. const { items = [] } = obj;
  48. items.push(key);
  49. obj.items = items;
  50. return this.attr(name, obj);
  51. };
  52. }
  53. function defineNodeProp(Node, { name, ctor }) {
  54. Node.prototype[name] = function (hocMark) {
  55. const node = this.append(ctor);
  56. if (name === 'mark') {
  57. node.type = hocMark;
  58. }
  59. return node;
  60. };
  61. }
  62. function defineContainerProp(Node, { name, ctor }) {
  63. Node.prototype[name] = function () {
  64. this.type = null;
  65. return this.append(ctor);
  66. };
  67. }
  68. /**
  69. * A decorator to define different type of attribute setter or
  70. * getter for current node.
  71. */
  72. function defineProps(descriptors) {
  73. return (Node) => {
  74. for (const descriptor of descriptors) {
  75. const { type } = descriptor;
  76. if (type === 'value')
  77. defineValueProp(Node, descriptor);
  78. else if (type === 'array')
  79. defineArrayProp(Node, descriptor);
  80. else if (type === 'object')
  81. defineObjectProp(Node, descriptor);
  82. else if (type === 'node')
  83. defineNodeProp(Node, descriptor);
  84. else if (type === 'container')
  85. defineContainerProp(Node, descriptor);
  86. else if (type === 'mix')
  87. defineMixProp(Node, descriptor);
  88. }
  89. return Node;
  90. };
  91. }
  92. exports.defineProps = defineProps;
  93. function nodeProps(node) {
  94. return Object.entries(node).map(([name, ctor]) => ({
  95. type: 'node',
  96. name,
  97. ctor,
  98. }));
  99. }
  100. exports.nodeProps = nodeProps;
  101. function containerProps(node) {
  102. return Object.entries(node).map(([name, ctor]) => ({
  103. type: 'container',
  104. name,
  105. ctor,
  106. }));
  107. }
  108. exports.containerProps = containerProps;
  109. //# sourceMappingURL=props.js.map