model.js 3.5 KB

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