model.js 3.9 KB

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