default.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. export 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. MultiSelector: ToolbarItem[];
  32. };
  33. export function createToolbars<T extends Record<string, ItemParams>>(obj: T) {
  34. const data: any = {};
  35. Object.entries(obj).forEach(([key, value]) => {
  36. data[key] = new ToolbarItem(value);
  37. });
  38. return data as { [name in keyof T]: ToolbarItem };
  39. }
  40. export const multiSelToolbar = createToolbars({
  41. // 删除
  42. delete: {
  43. component: TipIcons.Delete,
  44. getVisible() {
  45. return true;
  46. },
  47. onClick() {
  48. this.actions.removeSelectComps();
  49. },
  50. },
  51. // // 清除变换
  52. // clearTransform: {
  53. // component: TipIcons.ClearTransform,
  54. // getVisible() {
  55. // return true;
  56. // },
  57. // onClick() {
  58. // //this.actions.clearCompTransform(comp);
  59. // },
  60. // },
  61. // // 定位图层上移
  62. // layerUp: {
  63. // component: TipIcons.LayerUp,
  64. // getVisible() {
  65. // return true;
  66. // },
  67. // onClick() {
  68. // // this.actions.setCompLayer(comp, 1);
  69. // },
  70. // },
  71. // // 定位图层下移
  72. // layerDown: {
  73. // component: TipIcons.LayerDown,
  74. // getVisible() {
  75. // return true;
  76. // },
  77. // onClick() {
  78. // // this.actions.setCompLayer(comp, -1);
  79. // },
  80. // },
  81. })
  82. export const toolbars = createToolbars({
  83. // 显示/隐藏
  84. visible: {
  85. component: TipIcons.Visible,
  86. getValue: (comp) => (comp.layout.visible !== false ? 0 : 1),
  87. onClick(comp) {
  88. this.actions.setCompVisible(comp);
  89. },
  90. },
  91. // 锁定
  92. lock: {
  93. component: TipIcons.Lock,
  94. getValue: (comp) => (comp ? 0 : 1),
  95. onClick(comp) {
  96. this.actions.setCompLock(comp);
  97. },
  98. },
  99. // 删除
  100. delete: {
  101. component: TipIcons.Delete,
  102. getVisible(comp) {
  103. return this.helper.isCompCanDelete(comp.id);
  104. },
  105. onClick(comp) {
  106. this.actions.removeComp(comp.id);
  107. },
  108. },
  109. // 对齐
  110. align: {
  111. component: TipIcons.Align,
  112. getVisible: (comp) => !comp.isPostioned() && !comp.isFullWidth(),
  113. getValue: (comp) =>
  114. ["start", "center", "end"].indexOf(comp.layout.alignSelf || "start"),
  115. onClick(comp) {
  116. const vals = ["start", "center", "end"];
  117. let index = vals.indexOf(comp.layout.alignSelf || "start");
  118. this.actions.setCompAlign(comp, vals[++index % 3]);
  119. },
  120. },
  121. // 绝对定位
  122. position: {
  123. component: TipIcons.Position,
  124. getVisible(comp) {
  125. return this.helper.isCustomChildComp(comp);
  126. },
  127. getValue: (comp) => (comp.layout.position === "absolute" ? 1 : 0),
  128. onClick(comp) {
  129. this.actions.setCompPosition(comp);
  130. },
  131. },
  132. // 全屏尺寸
  133. fullWidth: {
  134. component: TipIcons.FullWidth,
  135. getVisible: (comp) => !!comp && !comp.isTransformed && !comp.isFullWidth,
  136. onClick(comp) {
  137. this.actions.fullCompWidth(comp);
  138. },
  139. },
  140. // 清除变换
  141. clearTransform: {
  142. component: TipIcons.ClearTransform,
  143. getVisible: (comp) => !!comp && comp.isTransformed(),
  144. onClick(comp) {
  145. this.actions.clearCompTransform(comp);
  146. },
  147. },
  148. // 定位图层上移
  149. layerUp: {
  150. component: TipIcons.LayerUp,
  151. getVisible: (comp) =>true,
  152. onClick(comp) {
  153. this.actions.setCompLayer(comp, 1);
  154. },
  155. },
  156. // 定位图层下移
  157. layerDown: {
  158. component: TipIcons.LayerDown,
  159. getVisible: (comp) => true,
  160. onClick(comp) {
  161. this.actions.setCompLayer(comp, -1);
  162. },
  163. },
  164. // 切换到父组件
  165. parentComp: {
  166. component: TipIcons.ParentComp,
  167. getVisible(comp) {
  168. return !!comp && !this.helper.isCustomChildComp(comp);
  169. },
  170. onClick(comp) {
  171. this.actions.pickParentComp(comp.id);
  172. },
  173. },
  174. // 保存为组件
  175. saveAsComp: {
  176. component: TipIcons.SaveAsComp,
  177. getVisible(comp) {
  178. return !!comp && this.helper.isShowSaveComp(comp);
  179. },
  180. async onClick(comp) {
  181. if (!comp) return;
  182. await this.actions.saveAsComp(comp);
  183. this.controls.compUICtrl.init();
  184. },
  185. },
  186. // 取消打组
  187. cancelGroup: {
  188. component: TipIcons.CancelGroup,
  189. getVisible(comp) {
  190. return !!comp && comp.compKey == "Group" && this.store.selected.length == 1 && this.helper.isStreamCardChild(comp.id);
  191. },
  192. onClick(comp) {
  193. if (!comp) return;
  194. this.actions.cancelGroupComps(comp);
  195. },
  196. },
  197. // 打组
  198. createGroup: {
  199. component: TipIcons.CreateGroup,
  200. getVisible() {
  201. return this.store.selected.length > 1;
  202. },
  203. onClick() {
  204. console.log("打组");
  205. this.actions.createGroupComps();
  206. },
  207. },
  208. //图片裁剪
  209. imageCropper: {
  210. component: TipIcons.Cropper,
  211. getVisible(comp) {
  212. return this.store.currComp.compKey == 'Image';
  213. },
  214. onClick(comp) {
  215. this.controls.cropCtrl.croppImage(this.store.currComp.id);
  216. },
  217. },
  218. });