model.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. "8K": {
  49. 1: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
  50. 2: [0, 0, 6, 5, 4, 3, 3, 2, 2, 2, 2, 2, 1],
  51. 3: [0, 0, 4, 3, 3, 2, 2, 1],
  52. 4: [0, 0, 3, 3, 2, 1],
  53. },
  54. A3: {
  55. 2: [0, 0, 7, 6, 5, 4, 3, 3, 3, 2, 2, 2, 2, 1],
  56. 3: [0, 0, 5, 4, 3, 2, 2, 2, 1],
  57. 4: [0, 0, 3, 3, 2, 2, 1],
  58. },
  59. A4: {
  60. 1: [0, 0, 7, 6, 5, 4, 3, 3, 3, 2, 2, 2, 2, 1],
  61. 2: [0, 0, 3, 3, 2, 2, 1],
  62. },
  63. };
  64. const groupGapSet = {
  65. "8K": {
  66. 2: [0, 0, 35, 30, 38, 70, 45, 90, 70, 60, 50, 20],
  67. 3: [0, 0, 32, 35, 16, 60, 40, 30],
  68. 4: [0, 0, 33, 27, 40, 30],
  69. },
  70. A3: {
  71. 2: [0, 0, 33, 28, 27, 40, 40, 40, 27, 40, 40],
  72. 3: [0, 0, 27, 27, 40, 40, 40, 40, 30],
  73. 4: [0, 0, 33, 27, 40, 30],
  74. },
  75. A4: {
  76. 1: [0, 0, 33, 28, 27, 40, 40, 40, 27, 40, 40],
  77. 2: [0, 0, 33, 27, 40, 30],
  78. },
  79. };
  80. const groupGapList = groupGapSet[pageSize][columnNumber];
  81. const groupGap =
  82. model.optionCount >= groupGapList.length
  83. ? groupGapList.pop()
  84. : groupGapList[model.optionCount];
  85. const numList = numberPerChildren[pageSize][columnNumber];
  86. const groupPerLine =
  87. model.optionCount >= numList.length
  88. ? numList.pop()
  89. : numList[model.optionCount];
  90. return { groupGap, groupPerLine };
  91. };
  92. const getFullModel = (model, { pageSize, columnNumber }) => {
  93. const parent = { ...model };
  94. const { groupGap, groupPerLine } = getOptionStructInfo(
  95. pageSize,
  96. columnNumber,
  97. model
  98. );
  99. const numPerLine = groupPerLine * model.questionCountPerGroup;
  100. const total = Math.ceil(model.questionsCount / numPerLine);
  101. let elements = [];
  102. for (let i = 0; i < total; i++) {
  103. let child = Object.assign({}, parent, {
  104. id: getElementId(),
  105. key: randomCode(),
  106. groupPerLine,
  107. groupGap,
  108. startNumber: model.startNumber + i * numPerLine,
  109. questionsCount:
  110. i === total - 1 ? model.questionsCount - numPerLine * i : numPerLine,
  111. parent,
  112. isLast: i === total - 1,
  113. });
  114. if (model.optionDirection === "vertical") {
  115. const optionCount = model.optionCount + 1;
  116. const optionsHeight =
  117. 10 * optionCount + (optionCount - 1) * model.optionGap + 20;
  118. child.h = i ? optionsHeight : optionsHeight + 34;
  119. } else {
  120. const optionCount =
  121. model.questionDirection === "vertical"
  122. ? Math.min(child.questionsCount, model.questionCountPerGroup)
  123. : Math.ceil(child.questionsCount / groupPerLine);
  124. const optionsHeight =
  125. 10 * optionCount + (optionCount - 1) * model.questionGap + 20;
  126. child.h = i ? optionsHeight : optionsHeight + 34;
  127. }
  128. child.minHeight = child.h;
  129. elements[i] = child;
  130. }
  131. return elements;
  132. };
  133. export { MODEL, getModel, getFullModel };