index.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import { Effect, ModuleControl } from "queenjs";
  2. import { EditorModule } from "../../module";
  3. import { reactive } from "vue";
  4. import { DesignComp } from "../../objects/DesignTemp/DesignComp";
  5. import { RxValue } from "../ReactCtrl/rxValue";
  6. import { CardState } from "../../objects/DesignTemp";
  7. import { isPc } from "@queenjs/utils";
  8. const PCConst = {
  9. minRate: 0.42,
  10. maxRate: 0.8,
  11. height: 1520,
  12. factor: 0.8067,
  13. widths: [1900, 2356, 2920],
  14. ranges: [[0.64537, 0.8], [0.52063, 0.64537], [0.42, 0.52063]]
  15. }
  16. const MobileConst = {
  17. minRate: 0.4495,
  18. maxRate: 0.75,
  19. width: 750,
  20. factor: 0.84312,
  21. heights: [1000, 1186, 1406],
  22. ranges: [[0.63234, 0.75], [0.5331387120830735, 0.632340125298328], [0.4495, 0.5331387120830735]]
  23. }
  24. const AllSize = ["short","normal", "long"];
  25. export const MobleScreenNames = ["短屏", "普通", "长屏幕"];
  26. export class ScreenCtrl extends ModuleControl<EditorModule> {
  27. state = RxValue.create({
  28. screen: {
  29. useFor: "mobile" as "mobile" | "pc", // pc 还是 手机
  30. pageMode: "long" as "long" | "short", //长页面还是短页面(一屏高度)
  31. pageSizeType: "normal" as "normal" | "long" | "short", //适配类型 长屏 正常 短屏幕
  32. },
  33. docWidth: window.innerWidth, //实际屏幕宽度
  34. docHeight: window.innerHeight, //实际屏幕高度
  35. safeFactor: 0.8,
  36. })
  37. get isShortPage () {
  38. return this.state.screen.pageMode == "short";
  39. }
  40. get isLongPage() {
  41. return this.state.screen.pageMode == "long";
  42. }
  43. get currScreenId() {
  44. return this.state.screen.useFor + this.state.screen.pageMode + this.state.screen.pageSizeType;
  45. }
  46. initEvent() {
  47. if ( !this.store.isEditMode ) return;
  48. this.state.onScreenChanged(()=>this.updateAdapterState());
  49. const {factor, sizes, ranges} = this.createSteps(MobileConst.width, MobileConst.minRate, MobileConst.maxRate)
  50. console.log("range=>", ranges, factor, sizes);
  51. console.log( this.getRightScreenId() );
  52. }
  53. createSteps(base: number, min:number, max:number, size = 3) {
  54. const factor = Math.pow((min / max) , 1.0 / size);
  55. const ranges = [];
  56. const sizes = [];
  57. for (let i=0; i<size; i++) {
  58. const w = factor * base / min
  59. let pre = min;
  60. min = base / w;
  61. sizes.push( w )
  62. ranges.push([pre, min])
  63. }
  64. const t = sizes[0]
  65. sizes[0] = sizes[2]
  66. sizes[2] = t;
  67. const t2 = ranges[0]
  68. ranges[0] = ranges[2]
  69. ranges[2] = t2;
  70. return {factor, sizes, ranges}
  71. }
  72. getAdapterIndex() {
  73. if(this.state.screen.pageMode == "long") return 1;
  74. return ["short", "normal", "long"].indexOf(this.state.screen.pageSizeType)
  75. }
  76. applyScreen() {
  77. const option = this.getRightScreenId();
  78. if ( this.store.designData.compScreenMap?.[option.sreenId] ) {
  79. this.restorScreenPage(option.sreenId);
  80. return;
  81. }
  82. let n= AllSize.length;
  83. while(n--) {
  84. const screenId = option.device + option.pageMode + AllSize[n];
  85. if ( this.store.designData.compScreenMap?.[screenId] ) {
  86. this.restorScreenPage(screenId);
  87. return;
  88. }
  89. }
  90. if (option.pageMode == "short") {
  91. option.pageMode = "long";
  92. return;
  93. }
  94. if (option.pageMode == "long") {//如果长页面模式找一个合适端页面拼成长页面
  95. let n= AllSize.length;
  96. while(n--) {
  97. const screenId = option.device + "short" + AllSize[n];
  98. if ( this.store.designData.compScreenMap?.[screenId] ) {
  99. this.restorScreenPage(screenId);
  100. return;
  101. }
  102. }
  103. }
  104. }
  105. getRightScreenId() {
  106. const currScreen = isPc() ? "pc" : "mobile";
  107. const rate = isPc() ? window.innerHeight / window.innerWidth : window.innerWidth / window.innerHeight;
  108. const sizes = currScreen == "pc" ? PCConst.ranges : MobileConst.ranges;
  109. let n = 3;
  110. let index = 1;
  111. while(n--) {
  112. const m = sizes[n]
  113. if (rate >= m[0]) {
  114. index = n;
  115. }
  116. }
  117. if (this.state.screen.pageMode == "long") index = 1;
  118. console.log("rate=>", rate);
  119. const sreenId = currScreen + this.state.screen.pageMode + ["short", "normal", "long"][index];
  120. return {sreenId, index, device: currScreen, pageMode: this.state.screen.pageMode};
  121. }
  122. updateAdapterState() {
  123. if (!this.store.rootPage) return;
  124. this.restorScreenPage();
  125. this.store.streamCardIds.forEach(c=>{
  126. const card = this.helper.findComp(c) as DesignComp;
  127. card.setW(this.getCurrScreenWidth());
  128. this.helper.extendStreamCard(card.id);
  129. })
  130. const w = this.helper.designSizeToPx(this.getCurrScreenWidth());
  131. const h = this.helper.designSizeToPx(this.getCurrScreenHeight());
  132. this.controls.editorCtrl.state.setPage({w, h});
  133. this.store.rootPage.layout.size[0] = this.getCurrScreenWidth();
  134. this.store.rootPage.layout.size[1] = this.getCurrScreenHeight();
  135. this.state.safeFactor = this.state.screen.useFor == "pc" ? PCConst.factor : MobileConst.factor;
  136. }
  137. restorScreenPage(screenId = "") {
  138. if (!this.store.rootPage) return;
  139. //获取当前screen的配置
  140. screenId = screenId ? screenId : this.state.screen.useFor + this.state.screen.pageMode + this.state.screen.pageSizeType;
  141. const screenCards = this.store.designData.compScreenMap[screenId] || [];
  142. console.log("apply screen=>", screenId);
  143. //刷新当前card的配置
  144. this.store.streamCardIds.forEach(c=>{
  145. const card = this.helper.findComp(c) as DesignComp;
  146. const screenCard = screenCards.find(item=>item.id == c)
  147. let newChilds:string[] = [];
  148. let childrs = screenCard?.children || [];
  149. childrs.forEach(item=>{
  150. newChilds.push(item.id);
  151. const screenComp = this.helper.findComp(item.id) as DesignComp;
  152. screenComp.layout.size[0] = item.size[0];
  153. screenComp.layout.size[1] = item.size[1];
  154. screenComp.layout.transformMatrix = item.matrix;
  155. })
  156. card.children.default = newChilds;
  157. })
  158. }
  159. saveScreenPage() {
  160. //获取当前screen的配置
  161. const screenId = this.state.screen.useFor + this.state.screen.pageMode + this.state.screen.pageSizeType;
  162. const screenCards = ScreenCtrl.createScreenCards(this.store.compMap, this.store.rootPage);
  163. if (!this.store.designData.compScreenMap) this.store.designData.compScreenMap = {};
  164. this.store.designData.compScreenMap[screenId] = screenCards;
  165. }
  166. static createScreenCards(compMap:any, rootPage: DesignComp) {
  167. //获取当前screen的配置
  168. const screenCards = [] as CardState [];
  169. const streamCardIds = rootPage.children.default || [];
  170. streamCardIds.forEach(c=>{
  171. const card = compMap[c] as DesignComp;
  172. const screenCard :CardState = {
  173. id: c,
  174. children: []
  175. }
  176. let childrs = card.children.default || [];
  177. childrs.forEach(item=>{
  178. const c = compMap[item] as DesignComp;
  179. screenCard.children.push({id: item, size: c.layout.size, matrix: c.layout.transformMatrix as string} );
  180. })
  181. screenCards.push(screenCard);
  182. })
  183. return screenCards;
  184. }
  185. getCurrScreenHeight() {
  186. const pageValue = this.state
  187. if ( pageValue.screen.useFor == "pc") {
  188. return PCConst.height;
  189. }
  190. const currScreenIndex = this.getAdapterIndex();
  191. return MobileConst.heights[currScreenIndex];
  192. }
  193. getCurrScreenWidth() {
  194. const currScreenIndex = this.getAdapterIndex();
  195. const pageValue = this.state
  196. if ( pageValue.screen.useFor == "pc" ) {
  197. return PCConst.widths[currScreenIndex];
  198. }
  199. return MobileConst.width;
  200. }
  201. }