element.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import computeLayout from "./css-layout";
  2. import { getDefaultStyle, scalableStyles, layoutAffectedStyles } from "./style";
  3. type LayoutData = {
  4. left: number,
  5. top: number,
  6. width: number,
  7. height: number
  8. };
  9. type LayoutNode = {
  10. id: number,
  11. style: Object,
  12. children: LayoutNode[],
  13. layout?: LayoutData
  14. };
  15. let uuid = 0;
  16. class Element {
  17. public static uuid(): number {
  18. return uuid++;
  19. }
  20. public parent: Element | null = null;
  21. public id: number = Element.uuid();
  22. public style: { [key: string]: any } = {};
  23. public computedStyle: { [key: string]: any } = {};
  24. public lastComputedStyle: { [key: string]: any } = {};
  25. public children: { [key: string]: Element } = {};
  26. public layoutBox: LayoutData = { left: 0, top: 0, width: 0, height: 0 };
  27. constructor(style: { [key: string]: any } = {}) {
  28. // 拷贝一份,防止被外部逻辑修改
  29. style = Object.assign(getDefaultStyle(), style);
  30. this.computedStyle = Object.assign(getDefaultStyle(), style);
  31. this.lastComputedStyle = Object.assign(getDefaultStyle(), style);
  32. Object.keys(style).forEach(key => {
  33. Object.defineProperty(this.style, key, {
  34. configurable: true,
  35. enumerable: true,
  36. get: () => style[key],
  37. set: (value: any) => {
  38. if (value === style[key] || value === undefined) {
  39. return;
  40. }
  41. this.lastComputedStyle = this.computedStyle[key]
  42. style[key] = value
  43. this.computedStyle[key] = value
  44. // 如果设置的是一个可缩放的属性, 计算自己
  45. if (scalableStyles.includes(key) && this.style.scale) {
  46. this.computedStyle[key] = value * this.style.scale
  47. }
  48. // 如果设置的是 scale, 则把所有可缩放的属性计算
  49. if (key === "scale") {
  50. scalableStyles.forEach(prop => {
  51. if (style[prop]) {
  52. this.computedStyle[prop] = style[prop] * value
  53. }
  54. })
  55. }
  56. if (key === "hidden") {
  57. if (value) {
  58. layoutAffectedStyles.forEach((key: string) => {
  59. this.computedStyle[key] = 0;
  60. });
  61. } else {
  62. layoutAffectedStyles.forEach((key: string) => {
  63. this.computedStyle[key] = this.lastComputedStyle[key];
  64. });
  65. }
  66. }
  67. }
  68. })
  69. })
  70. if (this.style.scale) {
  71. scalableStyles.forEach((key: string) => {
  72. if (this.style[key]) {
  73. const computedValue = this.style[key] * this.style.scale;
  74. this.computedStyle[key] = computedValue;
  75. }
  76. });
  77. }
  78. if (style.hidden) {
  79. layoutAffectedStyles.forEach((key: string) => {
  80. this.computedStyle[key] = 0;
  81. });
  82. }
  83. }
  84. getAbsolutePosition(element: Element) {
  85. if (!element) {
  86. return this.getAbsolutePosition(this)
  87. }
  88. if (!element.parent) {
  89. return {
  90. left: 0,
  91. top: 0
  92. }
  93. }
  94. const {left, top} = this.getAbsolutePosition(element.parent)
  95. return {
  96. left: left + element.layoutBox.left,
  97. top: top + element.layoutBox.top
  98. }
  99. }
  100. public add(element: Element) {
  101. element.parent = this;
  102. this.children[element.id] = element;
  103. }
  104. public remove(element?: Element) {
  105. // 删除自己
  106. if (!element) {
  107. Object.keys(this.children).forEach(id => {
  108. const child = this.children[id]
  109. child.remove()
  110. delete this.children[id]
  111. })
  112. } else if (this.children[element.id]) {
  113. // 是自己的子节点才删除
  114. element.remove()
  115. delete this.children[element.id];
  116. }
  117. }
  118. public getNodeTree(): LayoutNode {
  119. return {
  120. id: this.id,
  121. style: this.computedStyle,
  122. children: Object.keys(this.children).map((id: string) => {
  123. const child = this.children[id];
  124. return child.getNodeTree();
  125. })
  126. }
  127. }
  128. public applyLayout(layoutNode: LayoutNode) {
  129. ["left", "top", "width", "height"].forEach((key: string) => {
  130. if (layoutNode.layout && typeof layoutNode.layout[key] === "number") {
  131. this.layoutBox[key] = layoutNode.layout[key];
  132. if (this.parent && (key === "left" || key === "top")) {
  133. this.layoutBox[key] += this.parent.layoutBox[key];
  134. }
  135. }
  136. });
  137. layoutNode.children.forEach((child: LayoutNode) => {
  138. this.children[child.id].applyLayout(child);
  139. });
  140. }
  141. layout() {
  142. const nodeTree = this.getNodeTree();
  143. computeLayout(nodeTree);
  144. this.applyLayout(nodeTree);
  145. }
  146. }
  147. export default Element;