123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501 |
- import { cloneDeep, pick } from "lodash";
- import { Exception, queenApi } from "queenjs";
- import { EditorModule } from "..";
- import { ScreenshotCtrl } from "../../controllers/ScreenshotCtrl";
- import { CompObject } from "../../controllers/SelectCtrl/compObj";
- import { DesignComp } from "../../objects/DesignTemp/DesignComp";
- import { ICompKeys, Layout } from "../../typings";
- import CompSave from "../../components/CompSave";
- export const editActions = EditorModule.action({
- pickComp(compId: string, selected = true) {
- if (compId == "") {
-
- compId = "root";
- }
- const selectCardChild = (id: string) => {
- const paths = this.helper.getCompTrees(id);
- const cardChilds = paths[2];
- if (cardChilds) {
- this.actions.selectObjs([cardChilds.id]);
- } else {
- this.actions.selectObjs([]);
- if (id != "root") {
- this.store.setCurrComp(this.store.currStreamCardId);
- }
- }
- };
- if (this.store.currCompId == compId) {
- return;
- }
- this.store.setCurrComp(compId);
- if (selected) {
- selectCardChild(compId);
- }
- },
-
- async clickCompToDesign(compKey: ICompKeys, cb?: (comp: DesignComp) => void) {
- if (!this.store.currStreamCardId) {
- queenApi.messageError("请先选中一个卡片");
- return;
- }
-
-
- const isCreatCard =
- compKey != "Text" &&
- compKey != "Image" &&
- compKey != "Rectage" &&
- compKey != "Line" &&
- compKey != "Arc" &&
- compKey != "Triangle" &&
- compKey != "Ellipse" &&
- compKey != "Polygon" &&
- compKey != "Curve"
- let yOffset = 0;
- if (
- this.store.currCompId != this.store.currStreamCardId &&
- !isCreatCard &&
- this.store.currCompId != "root"
- ) {
- const bound = this.helper.getCardCompBound(this.store.currCompId);
- yOffset = bound.y + bound.h;
- }
- let currCard = this.store.currStreamCard;
- if (isCreatCard) {
-
- const currCardIndex =
- this.store.streamCardIds.indexOf(this.store.currStreamCardId) + 1;
- const compId = await this.store.insertDesignContent(
- "Container",
- currCardIndex
- );
- currCard = this.helper.findComp(compId) as DesignComp;
- }
- const compId = await this.store.insertCompContainer(compKey, currCard);
- const addedComp = this.store.compMap[compId];
- addedComp.layout.position = "absolute";
- const currComp = this.helper.findComp(compId) as DesignComp;
- cb?.(currComp);
-
- let xOffset = this.helper.designSizeToPx(
- 375 - (currComp.layout.size?.[0] || 750) / 2
- );
- const obj = new CompObject(currComp);
-
- const children = currCard.children.default || [];
- if (yOffset == 0 && children.length >= 2) {
- const prevCompIndex = children.indexOf(compId) - 1;
- const bound = this.helper.getCardCompBound(children[prevCompIndex]);
- yOffset = bound.y + bound.h;
- }
- obj.worldTransform.translate(xOffset, yOffset);
- currComp.layout.transformMatrix = obj.worldTransform.getMatrixStr();
- this.actions.pickComp(compId);
- this.helper.extendStreamCard(currCard.id);
- if (compKey == "Text") {
- this.actions.textFocus(compId, true);
- }
- this.controls.cropCtrl.close();
- },
-
- async dragCompToDesign(event: MouseEvent, compKey: ICompKeys) {
- await this.actions.addCompToDesign(compKey);
- const cardPoints = this.helper.getPointOffsetWith(
- event,
- this.store.currStreamCard.$el
- );
- const { currComp } = this.store;
- let selCtrl = this.controls.selectCtrl;
- selCtrl.translate(
- this.helper.designSizeToPx(375 - (currComp.layout.size?.[0] || 750) / 2),
- cardPoints.y
- );
- this.helper.extendStreamCard(this.store.currStreamCardId);
- if (compKey == "Text") {
- this.actions.selectObjs([]);
- this.actions.textFocus(currComp.id, true);
- }
- this.controls.cropCtrl.close();
- },
- async selectObjs(ids: string[]) {
- this.store.selected = ids;
- this.store.selectId = ids.length > 1 ? Date.now() + "" : "";
- },
-
- async addCompToDesign(compKey: ICompKeys, index?: number) {
- if (!this.store.currStreamCardId) {
-
- return;
- }
- if (compKey == "Container") {
-
- const compId = await this.store.insertDesignContent(compKey, index);
- this.actions.pickComp(compId);
- return;
- }
- const compId = await this.store.insertCompContainer(
- compKey,
- this.store.currStreamCard
- );
- const addedComp = this.store.compMap[compId];
- this.actions.setCompPositionFloat(addedComp);
- this.actions.pickComp(compId);
- },
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- pickParentComp(compId: string) {
- const parentComp = this.helper.findParentComp(compId);
- parentComp && this.store.setCurrComp(parentComp.id);
- },
-
- removeSelectComps() {
- const selected = this.store.selected.slice(0);
- this.actions.selectObjs([]);
- let n = selected.length;
- while (n--) {
- this.actions.removeComp(selected[n]);
- }
- },
-
- removeComp(compId: string) {
- if (this.helper.isCompCanDelete(compId)) {
- if (this.helper.isStreamCard(compId)) {
- this.actions.removeStreamCard(compId);
- return;
- }
- const cardid = this.store.currStreamCardId;
- if (compId === this.store.currCompId) {
- this.store.setCurrComp(this.store.currStreamCardId);
- }
- this.store.deleteComp(compId);
- this.helper.extendStreamCard(cardid);
- this.actions.selectObjs([]);
- }
- },
- async removeStreamCard(compId: string) {
- await queenApi.showConfirm({ title: "删除", content: "确认删除当前内容?" });
-
-
-
-
- let nextCard = this.store.currStreamCardId;
- if (compId == this.store.currStreamCardId) {
- const i = this.store.streamCardIds.indexOf(compId);
- nextCard = this.store.streamCardIds[i + 1];
- if (!nextCard) {
- nextCard = this.store.streamCardIds[i - 1];
- }
- }
- this.controls.selectCtrl.selecteObjs([]);
- this.store.deleteComp(compId);
- this.store.setCurrComp(nextCard);
- },
-
- moveComp(selIndex: number, targetIndex: number) {
- if (selIndex === targetIndex) return;
- this.store.moveComp(selIndex, targetIndex);
- },
-
- async saveAsComp(comp: DesignComp) {
- try {
- const CompSave = this.components.CompSave as any;
- let title = "";
- let type = "comp";
- try {
- const ret:any = await queenApi.dialog(<CompSave />, {width:"300px", title: "保存到我的"});
- if (!ret) {
- return;
- }
- title = ret.title;
- type = ret.type;
- } catch (error) {
- return;
- }
- console.log( title, type);
-
- const blob = await new ScreenshotCtrl().snap({
- element: comp.$el,
- });
- const thumbnail = URL.createObjectURL(blob);
-
-
-
-
-
-
-
- const data = {
- title,
- type,
- thumbnail,
- compMap: cloneDeep(this.store.designData.compMap),
- };
- this.helper.clearUnusedComps(data.compMap, comp.id);
- data.compMap.root = data.compMap[comp.id];
- data.compMap.root.id = "root";
- delete data.compMap[comp.id];
- queenApi.showLoading("保存中");
- await this.controls.uploader.uploadBlobs(data);
- await this.https.createComp(data);
- queenApi.messageSuccess("保存成功");
- } catch (error: any) {
- throw Exception.error(error.toString());
- } finally {
- queenApi.hideLoading();
- }
- },
-
- async saveDesign() {
- try {
-
- this.helper.clearUnusedComps(this.store.designData.compMap);
-
- if (!this.store.designData.thumbnail) {
- await this.actions.updateThumbnailByScreenshot();
- }
- queenApi.showLoading("保存中");
- await this.controls.uploader.uploadBlobs(this.store.designData);
- await this.https[this.store.isEditPage ? "saveDesign" : "saveComp"](
- this.store.designData
- );
- queenApi.messageSuccess("保存成功");
- } catch (error: any) {
- throw Exception.error(error.toString());
- } finally {
- queenApi.hideLoading();
- }
- },
-
- async updateThumbnailByScreenshot(autoSave?: boolean) {
- try {
- const rootComp = this.helper.findRootComp();
- if (!rootComp) return;
- queenApi.showLoading("截屏中");
- const blob = await new ScreenshotCtrl().snap({
- element: rootComp.$el,
- ratio: this.store.isEditComp ? 0 : 1,
- });
- const thumbnail = URL.createObjectURL(blob);
- this.store.setDesignThumbnail(thumbnail);
- if (autoSave) {
- await this.controls.uploader.uploadBlobs(this.store.designData);
- await this.https[this.store.isEditPage ? "saveDesign" : "saveComp"](
- pick(this.store.designData, ["_id", "thumbnail"])
- );
- queenApi.messageSuccess("保存成功");
- }
- } catch (error: any) {
- throw Exception.error(error.toString());
- } finally {
- queenApi.hideLoading();
- }
- },
-
- setCompTransform(comp: DesignComp, transform: Layout["transform"]) {
- if (!comp) return;
- comp.layout.transform = transform;
- console.log(comp);
- },
-
- setCompTransformMatrix(comp: DesignComp, transformMatrix: string) {
- if (!comp) return;
- comp.layout.transformMatrix = transformMatrix;
- },
-
- setCompPositionFloat(comp: DesignComp) {
- comp.layout.position = "absolute";
- },
-
- setCompPosition(comp: DesignComp) {
- comp.layout.position =
- comp.layout.position === "absolute" ? undefined : "absolute";
- },
-
- setCompVisible(comp: DesignComp) {
- comp.layout.visible = comp.layout.visible === false ? true : false;
- },
-
- clearCompTransform(comp: DesignComp) {
- comp.layout.margin = "";
- comp.layout.transform = undefined;
- },
-
- setCompLock(comp: DesignComp) {
- console.log(comp);
- },
-
- setCompLayer(comp: DesignComp, offset: number) {
- comp.layout.zIndex = Math.min(
- Math.max((comp.layout.zIndex || 0) + offset, 0),
- 99
- );
- },
-
- fullCompWidth(comp: DesignComp) {
- comp.layout.size || (comp.layout.size = []);
- comp.layout.size[0] = 750;
- },
-
- setCompAlign(comp: DesignComp, align: string) {
- comp.layout.alignSelf = align;
- if (comp.layout.transform?.x) {
- comp.layout.transform.x = 0;
- }
- },
-
- enableGroupMode() {
- this.store.setGroupIds(
- this.store.currCompId ? [this.store.currCompId] : []
- );
- this.store.setGroupMode(true);
- },
-
- async disableGroupMode() {
- const groupId = await this.controls.transferCtrl.groupCtrl.combineGroup();
- if (groupId) {
- this.store.setCurrComp(groupId);
- }
- this.store.setGroupIds([]);
- this.store.setGroupMode(false);
- },
-
- cancelGroupComps(groupComp: DesignComp) {
- this.controls.transferCtrl.groupCtrl.cancelGroup(groupComp);
- this.store.setCurrComp(groupComp.children.default?.[0] as string);
- },
- handleSelectMoving(key: string) {
- if (this.store.selected.length < 1) return;
- let x = 0,
- y = 0;
- switch (key) {
- case "left":
- x = -1;
- break;
- case "right":
- x = 1;
- break;
- case "up":
- y = -1;
- break;
- case "down":
- y = 1;
- break;
- }
- this.controls.selectCtrl.translate(x * 0.5, y * 0.5);
- this.controls.selectCtrl.assistCtrl?.flashDrawCardDists();
- },
-
- async clickFrameToDesign(record) {
- const res = await queenApi.showConfirm({
- title: "",
- content: "要替换正在编辑的内容?",
- });
- if (!res) return;
- const frameData = await this.https.getDesignDetail(record._id, {
- isSys: true,
- });
- const { compMap, content, desc, thumbnail, title } = frameData.result;
- const designData = {
- ...this.store.designData,
- compMap,
- content,
- desc,
- thumbnail,
- title,
- };
- this.actions.selectObjs([]);
- this.store.setCurrComp("root");
- this.store.setDesignData(designData);
- this.store.currStreamCardId = this.store.streamCardIds[0];
- },
- });
|