default.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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) => any;
  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. rename: {
  85. component: TipIcons.Rename,
  86. onClick(comp) {
  87. this.actions.resetCompName(comp);
  88. },
  89. },
  90. // 显示/隐藏
  91. visible: {
  92. component: TipIcons.Visible,
  93. getValue: (comp) => (comp.layout.visible !== false ? 0 : 1),
  94. onClick(comp) {
  95. this.actions.setCompVisible(comp);
  96. },
  97. },
  98. // 锁定
  99. lock: {
  100. component: TipIcons.Lock,
  101. getValue: (comp) => (comp ? 0 : 1),
  102. onClick(comp) {
  103. this.actions.setCompLock(comp);
  104. },
  105. },
  106. // 删除
  107. delete: {
  108. component: TipIcons.Delete,
  109. getVisible(comp) {
  110. return this.helper.isCompCanDelete(comp.id);
  111. },
  112. onClick(comp) {
  113. this.actions.removeComp(comp.id);
  114. },
  115. },
  116. // 对齐
  117. align: {
  118. component: TipIcons.Align,
  119. getVisible: (comp) => !comp.isPostioned() && !comp.isFullWidth(),
  120. getValue: (comp) =>
  121. ["start", "center", "end"].indexOf(comp.layout.alignSelf || "start"),
  122. onClick(comp) {
  123. const vals = ["start", "center", "end"];
  124. let index = vals.indexOf(comp.layout.alignSelf || "start");
  125. this.actions.setCompAlign(comp, vals[++index % 3]);
  126. },
  127. },
  128. // 绝对定位
  129. position: {
  130. component: TipIcons.Position,
  131. getVisible(comp) {
  132. return this.helper.isCustomChildComp(comp);
  133. },
  134. getValue: (comp) => (comp.layout.position === "absolute" ? 1 : 0),
  135. onClick(comp) {
  136. this.actions.setCompPosition(comp);
  137. },
  138. },
  139. // 编辑模式
  140. editor: {
  141. component: TipIcons.Edit,
  142. getVisible(comp) {
  143. return false; // comp.compKey == "Polygon" && !this.store.compEditMode
  144. },
  145. onClick(comp) {
  146. console.log("clicked edit");
  147. // this.actions.setCompPosition(comp);
  148. this.store.compEditMode = true;
  149. this.store.compEditReslut = -1;
  150. },
  151. },
  152. ok: {
  153. component: TipIcons.Right,
  154. getVisible(comp) {
  155. return this.store.compEditMode;
  156. },
  157. onClick(comp) {
  158. console.log("clicked ok ");
  159. this.store.compEditReslut = 1;
  160. this.store.compEditMode = false;
  161. },
  162. },
  163. cancel: {
  164. component: TipIcons.Cross,
  165. getVisible(comp) {
  166. return this.store.compEditMode;
  167. },
  168. onClick(comp) {
  169. console.log("clicked cancel ");
  170. // this.actions.setCompPosition(comp);
  171. this.store.compEditReslut = -1;
  172. this.store.compEditMode = false;
  173. },
  174. },
  175. // 全屏尺寸
  176. fullWidth: {
  177. component: TipIcons.FullWidth,
  178. getVisible: (comp) => !!comp && !comp.isTransformed && !comp.isFullWidth,
  179. onClick(comp) {
  180. this.actions.fullCompWidth(comp);
  181. },
  182. },
  183. // 清除变换
  184. clearTransform: {
  185. component: TipIcons.ClearTransform,
  186. getVisible: (comp) => !!comp && comp.isTransformed(),
  187. onClick(comp) {
  188. this.actions.clearCompTransform(comp);
  189. },
  190. },
  191. // 定位图层上移
  192. layerUp: {
  193. component: TipIcons.LayerUp,
  194. getVisible: (comp) => true,
  195. onClick(comp) {
  196. this.actions.setCompLayer(comp, 1);
  197. },
  198. },
  199. // 定位图层下移
  200. layerDown: {
  201. component: TipIcons.LayerDown,
  202. getVisible: (comp) => true,
  203. onClick(comp) {
  204. this.actions.setCompLayer(comp, -1);
  205. },
  206. },
  207. // 切换到父组件
  208. parentComp: {
  209. component: TipIcons.ParentComp,
  210. getVisible(comp) {
  211. return !!comp && !this.helper.isCustomChildComp(comp);
  212. },
  213. onClick(comp) {
  214. this.actions.pickParentComp(comp.id);
  215. },
  216. },
  217. // 保存为组件
  218. saveAsComp: {
  219. component: TipIcons.SaveAsComp,
  220. getVisible(comp) {
  221. return !!comp && this.helper.isShowSaveComp(comp);
  222. },
  223. async onClick(comp) {
  224. if (!comp) return;
  225. await this.actions.saveAsComp(comp);
  226. this.controls.compUICtrl.init();
  227. },
  228. },
  229. // 取消打组
  230. cancelGroup: {
  231. component: TipIcons.CancelGroup,
  232. getVisible(comp) {
  233. return (
  234. !!comp &&
  235. comp.compKey == "Group" &&
  236. this.store.selected.length == 1 &&
  237. this.helper.isStreamCardChild(comp.id)
  238. );
  239. },
  240. onClick(comp) {
  241. if (!comp) return;
  242. this.actions.cancelGroupComps(comp);
  243. },
  244. },
  245. // 打组
  246. createGroup: {
  247. component: TipIcons.CreateGroup,
  248. getVisible() {
  249. return this.store.selected.length > 1;
  250. },
  251. onClick() {
  252. console.log("打组");
  253. this.actions.createGroupComps();
  254. },
  255. },
  256. //图片裁剪
  257. imageCropper: {
  258. component: TipIcons.Cropper,
  259. getVisible(comp) {
  260. return this.store.currComp.compKey == "Image";
  261. },
  262. onClick(comp) {
  263. this.controls.cropCtrl.croppImage(this.store.currComp.id);
  264. },
  265. },
  266. });