default.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import { TipIcons } from "../../components/TipIcons";
  2. import { EditorModule } from "../../module";
  3. import { ICompKeys } from "../../typings";
  4. import { DesignComp } from "../DesignTemp/DesignComp";
  5. function getVisible(this: EditorModule, comp: DesignComp) {
  6. return !!comp;
  7. }
  8. type ItemParams = Pick<ToolbarItem, "getValue" | "component" | "onClick"> & {
  9. getVisible?: typeof getVisible;
  10. };
  11. class ToolbarItem {
  12. component: any;
  13. getValue?: (c: DesignComp) => number;
  14. onClick: (this: EditorModule, c: DesignComp) => void;
  15. getVisible!: typeof getVisible;
  16. constructor(data: ItemParams) {
  17. this.component = data.component;
  18. this.getValue = data.getValue;
  19. this.onClick = data.onClick;
  20. this.getVisible = data.getVisible || getVisible;
  21. return;
  22. }
  23. setVisible(cb: typeof getVisible) {
  24. const item = new ToolbarItem(this);
  25. item.getVisible = cb;
  26. return item;
  27. }
  28. }
  29. export type ICompToolbars = { [name in ICompKeys]?: ToolbarItem[] } & {
  30. default: ToolbarItem[];
  31. };
  32. function createToolbars<T extends Record<string, ItemParams>>(obj: T) {
  33. const data: any = {};
  34. Object.entries(obj).forEach(([key, value]) => {
  35. data[key] = new ToolbarItem(value);
  36. });
  37. return data as { [name in keyof T]: ToolbarItem };
  38. }
  39. export const toolbars = createToolbars({
  40. // 显示/隐藏
  41. visible: {
  42. component: TipIcons.Visible,
  43. getValue: (comp) => (comp.layout.visible !== false ? 0 : 1),
  44. onClick(comp) {
  45. this.actions.setCompVisible(comp);
  46. },
  47. },
  48. // 锁定
  49. lock: {
  50. component: TipIcons.Lock,
  51. getValue: (comp) => (comp ? 0 : 1),
  52. onClick(comp) {
  53. this.actions.setCompLock(comp);
  54. },
  55. },
  56. // 删除
  57. delete: {
  58. component: TipIcons.Delete,
  59. getVisible(comp) {
  60. return this.helper.isCompCanDelete(comp.id);
  61. },
  62. onClick(comp) {
  63. this.actions.removeComp(comp.id);
  64. },
  65. },
  66. // 对齐
  67. align: {
  68. component: TipIcons.Align,
  69. getVisible: (comp) => !comp.isPostioned() && !comp.isFullWidth(),
  70. getValue: (comp) =>
  71. ["start", "center", "end"].indexOf(comp.layout.alignSelf || "start"),
  72. onClick(comp) {
  73. const vals = ["start", "center", "end"];
  74. let index = vals.indexOf(comp.layout.alignSelf || "start");
  75. this.actions.setCompAlign(comp, vals[++index % 3]);
  76. },
  77. },
  78. // 绝对定位
  79. position: {
  80. component: TipIcons.Position,
  81. getVisible(comp) {
  82. return this.helper.isCustomChildComp(comp);
  83. },
  84. getValue: (comp) => (comp.layout.position === "absolute" ? 1 : 0),
  85. onClick(comp) {
  86. this.actions.setCompPosition(comp);
  87. },
  88. },
  89. // 全屏尺寸
  90. fullWidth: {
  91. component: TipIcons.FullWidth,
  92. getVisible: (comp) => !comp.isTransformed && !comp.isFullWidth,
  93. onClick(comp) {
  94. this.actions.fullCompWidth(comp);
  95. },
  96. },
  97. // 清除变换
  98. clearTransform: {
  99. component: TipIcons.ClearTransform,
  100. getVisible: (comp) => comp.isTransformed(),
  101. onClick(comp) {
  102. this.actions.clearCompTransform(comp);
  103. },
  104. },
  105. // 定位图层上移
  106. layerUp: {
  107. component: TipIcons.LayerUp,
  108. getVisible: (comp) => comp.isPostioned(),
  109. onClick(comp) {
  110. this.actions.setCompLayer(comp, 1);
  111. },
  112. },
  113. // 定位图层下移
  114. layerDown: {
  115. component: TipIcons.LayerDown,
  116. getVisible: (comp) => comp.isPostioned(),
  117. onClick(comp) {
  118. this.actions.setCompLayer(comp, -1);
  119. },
  120. },
  121. // 切换到父组件
  122. parentComp: {
  123. component: TipIcons.ParentComp,
  124. getVisible(comp) {
  125. return !this.helper.isCustomChildComp(comp);
  126. },
  127. onClick(comp) {
  128. this.actions.pickParentComp(comp.id);
  129. },
  130. },
  131. // 保存为组件
  132. saveAsComp: {
  133. component: TipIcons.SaveAsComp,
  134. getVisible(comp) {
  135. return this.helper.isShowSaveComp(comp);
  136. },
  137. onClick(comp) {
  138. this.actions.saveAsComp(comp);
  139. },
  140. },
  141. // 取消打组
  142. cancelGroup: {
  143. component: TipIcons.CancelGroup,
  144. onClick(comp) {
  145. this.actions.cancelGroupComps(comp);
  146. },
  147. }
  148. });