123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- import { Effect, ModuleControl } from "queenjs";
- import { EditorModule } from "../../module";
- import { reactive } from "vue";
- import { DesignComp } from "../../objects/DesignTemp/DesignComp";
- import { RxValue } from "../ReactCtrl/rxValue";
- import { CardState } from "../../objects/DesignTemp";
- import { isPc } from "@queenjs/utils";
- const PCConst = {
- minRate: 0.42,
- maxRate: 0.8,
- height: 1520,
- factor: 0.8067,
- widths: [1900, 2356, 2920],
- ranges: [[0.64537, 0.8], [0.52063, 0.64537], [0.42, 0.52063]]
- }
- const MobileConst = {
- minRate: 0.4495,
- maxRate: 0.75,
- width: 750,
- factor: 0.84312,
- heights: [1000, 1186, 1406],
- ranges: [[0.63234, 0.75], [0.5331387120830735, 0.632340125298328], [0.4495, 0.5331387120830735]]
- }
- const AllSize = ["short","normal", "long"];
- export const MobleScreenNames = ["短屏", "普通", "长屏幕"];
- export class ScreenCtrl extends ModuleControl<EditorModule> {
- state = RxValue.create({
- screen: {
- useFor: "mobile" as "mobile" | "pc", // pc 还是 手机
- pageMode: "long" as "long" | "short", //长页面还是短页面(一屏高度)
- pageSizeType: "normal" as "normal" | "long" | "short", //适配类型 长屏 正常 短屏幕
- },
- docWidth: window.innerWidth, //实际屏幕宽度
- docHeight: window.innerHeight, //实际屏幕高度
- safeFactor: 0.8,
- })
- get isShortPage () {
- return this.state.screen.pageMode == "short";
- }
- get isLongPage() {
- return this.state.screen.pageMode == "long";
- }
- get currScreenId() {
- return this.state.screen.useFor + this.state.screen.pageMode + this.state.screen.pageSizeType;
- }
-
- initEvent() {
- if ( !this.store.isEditMode ) return;
- this.state.onScreenChanged(()=>this.updateAdapterState());
-
- const {factor, sizes, ranges} = this.createSteps(MobileConst.width, MobileConst.minRate, MobileConst.maxRate)
- console.log("range=>", ranges, factor, sizes);
- console.log( this.getRightScreenId() );
- }
- createSteps(base: number, min:number, max:number, size = 3) {
- const factor = Math.pow((min / max) , 1.0 / size);
- const ranges = [];
- const sizes = [];
- for (let i=0; i<size; i++) {
- const w = factor * base / min
- let pre = min;
- min = base / w;
- sizes.push( w )
- ranges.push([pre, min])
- }
- const t = sizes[0]
- sizes[0] = sizes[2]
- sizes[2] = t;
-
- const t2 = ranges[0]
- ranges[0] = ranges[2]
- ranges[2] = t2;
- return {factor, sizes, ranges}
- }
- getAdapterIndex() {
- if(this.state.screen.pageMode == "long") return 1;
- return ["short", "normal", "long"].indexOf(this.state.screen.pageSizeType)
- }
- applyScreen() {
- const option = this.getRightScreenId();
- if ( this.isScreenExit(option.sreenId) ) {
- this.restorScreenPage(option.sreenId);
- return;
- }
- let n= AllSize.length;
- while(n--) {
- const screenId = option.device + option.pageMode + AllSize[n];
- if ( this.isScreenExit(screenId) ) {
- this.restorScreenPage(screenId);
- return;
- }
- }
- if (option.pageMode == "short") {
- option.pageMode = "long";
- return;
- }
- if (option.pageMode == "long") {//如果长页面模式找一个合适端页面拼成长页面
- let n= AllSize.length;
- while(n--) {
- const screenId = option.device + "short" + AllSize[n];
- if ( this.isScreenExit(screenId) ) {
- this.restorScreenPage(screenId);
- return;
- }
- }
- }
- }
- getRightScreenId() {
- const currScreen = isPc() ? "pc" : "mobile";
- const rate = isPc() ? window.innerHeight / window.innerWidth : window.innerWidth / window.innerHeight;
- const sizes = currScreen == "pc" ? PCConst.ranges : MobileConst.ranges;
- let n = 3;
- let index = 1;
- while(n--) {
- const m = sizes[n]
- if (rate >= m[0]) {
- index = n;
- }
- }
- if (this.state.screen.pageMode == "long") index = 1;
- console.log("rate=>", rate);
- const sreenId = currScreen + this.state.screen.pageMode + ["short", "normal", "long"][index];
- return {sreenId, index, device: currScreen, pageMode: this.state.screen.pageMode};
- }
- updateAdapterState() {
- if (!this.store.rootPage) return;
-
- this.restorScreenPage();
- this.store.streamCardIds.forEach(c=>{
- const card = this.helper.findComp(c) as DesignComp;
- card.setW(this.getCurrScreenWidth());
- this.helper.extendStreamCard(card.id);
- })
- const w = this.helper.designSizeToPx(this.getCurrScreenWidth());
- const h = this.helper.designSizeToPx(this.getCurrScreenHeight());
- this.controls.editorCtrl.state.setPage({w, h});
- this.store.rootPage.layout.size[0] = this.getCurrScreenWidth();
- this.store.rootPage.layout.size[1] = this.getCurrScreenHeight();
- this.state.safeFactor = this.state.screen.useFor == "pc" ? PCConst.factor : MobileConst.factor;
- }
- isScreenExit(screenId:string) {
- return !!this.store.designData.compScreenMap[screenId] &&
- this.store.designData.compScreenMap[screenId].length > 0 &&
- this.store.designData.compScreenMap[screenId][1].children.length > 0;
- }
- restorScreenPage(screenId = "") {
- if (!this.store.rootPage) return;
-
- //获取当前screen的配置
- screenId = screenId ? screenId : this.state.screen.useFor + this.state.screen.pageMode + this.state.screen.pageSizeType;
- const screenCards = this.store.designData.compScreenMap[screenId] || [];
- console.log("apply screen=>", screenId);
- //刷新当前card的配置
- this.store.streamCardIds.forEach(c=>{
- const card = this.helper.findComp(c) as DesignComp;
- const screenCard = screenCards.find(item=>item.id == c)
- let newChilds:string[] = [];
- let childrs = screenCard?.children || [];
- childrs.forEach(item=>{
- newChilds.push(item.id);
- const screenComp = this.helper.findComp(item.id) as DesignComp;
- screenComp.layout.size[0] = item.size[0];
- screenComp.layout.size[1] = item.size[1];
- screenComp.layout.transformMatrix = item.matrix;
- })
- card.children.default = newChilds;
- })
- }
- saveScreenPage() {
- //获取当前screen的配置
- const screenId = this.state.screen.useFor + this.state.screen.pageMode + this.state.screen.pageSizeType;
- const screenCards = ScreenCtrl.createScreenCards(this.store.compMap, this.store.rootPage);
- if (!this.store.designData.compScreenMap) this.store.designData.compScreenMap = {};
- this.store.designData.compScreenMap[screenId] = screenCards;
- }
- static createScreenCards(compMap:any, rootPage: DesignComp) {
- //获取当前screen的配置
- const screenCards = [] as CardState [];
- const streamCardIds = rootPage.children.default || [];
- streamCardIds.forEach(c=>{
- const card = compMap[c] as DesignComp;
- const screenCard :CardState = {
- id: c,
- children: []
- }
- let childrs = card.children.default || [];
- childrs.forEach(item=>{
- const c = compMap[item] as DesignComp;
- screenCard.children.push({id: item, size: c.layout.size, matrix: c.layout.transformMatrix as string} );
- })
- screenCards.push(screenCard);
- })
- return screenCards;
- }
- getCurrScreenHeight() {
- const pageValue = this.state
- if ( pageValue.screen.useFor == "pc") {
- return PCConst.height;
- }
- const currScreenIndex = this.getAdapterIndex();
- return MobileConst.heights[currScreenIndex];
- }
- getCurrScreenWidth() {
- const currScreenIndex = this.getAdapterIndex();
- const pageValue = this.state
- if ( pageValue.screen.useFor == "pc" ) {
- return PCConst.widths[currScreenIndex];
- }
- return MobileConst.width;
- }
- }
|