default.ts 6.8 KB

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