model.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import { getElementId, randomCode } from "../../plugins/utils";
  2. import { BOOLEAN_TYPE } from "../../enumerate";
  3. const MODEL = {
  4. type: "FILL_QUESTION",
  5. x: 0,
  6. y: 0,
  7. w: 0,
  8. h: 114,
  9. minHeight: 114,
  10. sign: "objective",
  11. topicName: "",
  12. topicNo: null,
  13. nameFontSize: "14px",
  14. nameFontWeight: 400,
  15. startNumber: 1,
  16. questionsCount: 10,
  17. optionCount: 4,
  18. questionCountPerGroup: 5,
  19. groupPerLine: 4, // 小题纵向排列时,表示每行组数。小题横向排列时,表示每行小题数。
  20. optionDirection: "horizontal",
  21. questionDirection: "vertical",
  22. questionGap: 8,
  23. groupGap: 30,
  24. optionGap: 6,
  25. isBoolean: false, // 是否是判断题
  26. booleanType: BOOLEAN_TYPE[0],
  27. isMultiply: false, // 是否是多选题
  28. isCovered: false,
  29. fontSize: "14px",
  30. };
  31. const getModel = (preSetData = {}) => {
  32. const model = Object.assign({}, MODEL, preSetData);
  33. return {
  34. id: getElementId(),
  35. key: randomCode(),
  36. ...model,
  37. };
  38. };
  39. const getOptionStructInfo = (pageSize, columnNumber, model) => {
  40. // 选项纵向排列
  41. if (model.optionDirection === "vertical") {
  42. return { groupGap: 23, groupPerLine: 5 };
  43. }
  44. // 选项横向排列
  45. // 不同栏数,不同选项个数,每一行对应的组数
  46. // 以一行4题,每题5选项为标准展示效果
  47. const numberPerChildren = {
  48. B4: {
  49. 2: [0, 0, 6, 5, 4, 3, 3, 2, 2, 2, 2, 1],
  50. 3: [0, 0, 4, 3, 2, 2, 2, 1],
  51. 4: [0, 0, 3, 2, 2, 1],
  52. },
  53. "8K": {
  54. 2: [0, 0, 6, 5, 4, 4, 3, 3, 2, 2, 2, 2, 1],
  55. 3: [0, 0, 4, 3, 3, 2, 2, 2, 1],
  56. 4: [0, 0, 3, 2, 2, 2, 1],
  57. },
  58. A3: {
  59. 2: [0, 0, 7, 6, 5, 4, 3, 3, 3, 2, 2, 2, 2, 1],
  60. 3: [0, 0, 5, 4, 3, 2, 2, 2, 1],
  61. 4: [0, 0, 3, 3, 2, 2, 1],
  62. },
  63. A4: {
  64. 1: [0, 0, 7, 6, 5, 4, 3, 3, 3, 2, 2, 2, 2, 1],
  65. 2: [0, 0, 3, 3, 2, 2, 1],
  66. },
  67. };
  68. const groupGapSet = {
  69. B4: {
  70. 2: [0, 0, 30, 30, 35, 50, 40, 70, 70, 70, 40, 40],
  71. 3: [0, 0, 30, 30, 60, 60, 35, 25],
  72. 4: [0, 0, 30, 40, 30, 20],
  73. },
  74. "8K": {
  75. 2: [0, 0, 40, 35, 45, 22, 45, 30, 90, 90, 50, 40],
  76. 3: [0, 0, 35, 40, 24, 60, 40, 25],
  77. 4: [0, 0, 33, 40, 40, 20],
  78. },
  79. A3: {
  80. 2: [0, 0, 33, 28, 27, 40, 40, 40, 27, 40, 40],
  81. 3: [0, 0, 27, 27, 40, 40, 40, 40, 30],
  82. 4: [0, 0, 33, 27, 40, 30],
  83. },
  84. A4: {
  85. 1: [0, 0, 33, 28, 27, 40, 40, 40, 27, 40, 40],
  86. 2: [0, 0, 33, 27, 40, 30],
  87. },
  88. };
  89. const groupGapList = groupGapSet[pageSize][columnNumber];
  90. const groupGap =
  91. model.optionCount >= groupGapList.length
  92. ? groupGapList.pop()
  93. : groupGapList[model.optionCount];
  94. const numList = numberPerChildren[pageSize][columnNumber];
  95. const groupPerLine =
  96. model.optionCount >= numList.length
  97. ? numList.pop()
  98. : numList[model.optionCount];
  99. return { groupGap, groupPerLine };
  100. };
  101. const getFullModel = (model, { pageSize, columnNumber }) => {
  102. const parent = { ...model };
  103. const { groupGap, groupPerLine } = getOptionStructInfo(
  104. pageSize,
  105. columnNumber,
  106. model
  107. );
  108. const numPerLine = groupPerLine * model.questionCountPerGroup;
  109. const total = Math.ceil(model.questionsCount / numPerLine);
  110. let elements = [];
  111. for (let i = 0; i < total; i++) {
  112. let child = Object.assign({}, parent, {
  113. id: getElementId(),
  114. key: randomCode(),
  115. groupPerLine,
  116. groupGap,
  117. startNumber: model.startNumber + i * numPerLine,
  118. questionsCount:
  119. i === total - 1 ? model.questionsCount - numPerLine * i : numPerLine,
  120. parent,
  121. isLast: i === total - 1,
  122. });
  123. if (model.optionDirection === "vertical") {
  124. const optionCount = model.optionCount + 1;
  125. const optionsHeight =
  126. 10 * optionCount + (optionCount - 1) * model.optionGap + 20;
  127. child.h = i ? optionsHeight : optionsHeight + 34;
  128. } else {
  129. const optionCount =
  130. model.questionDirection === "vertical"
  131. ? Math.min(child.questionsCount, model.questionCountPerGroup)
  132. : Math.ceil(child.questionsCount / groupPerLine);
  133. const optionsHeight =
  134. 10 * optionCount + (optionCount - 1) * model.questionGap + 20;
  135. child.h = i ? optionsHeight : optionsHeight + 34;
  136. }
  137. child.minHeight = child.h;
  138. elements[i] = child;
  139. }
  140. return elements;
  141. };
  142. export { MODEL, getModel, getFullModel };