model.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { getElementId, randomCode } from "../../plugins/utils";
  2. const MODEL = {
  3. type: "FILL_LINE",
  4. x: 0,
  5. y: 0,
  6. w: 0,
  7. h: 40,
  8. minHeight: 40,
  9. sign: "subjective",
  10. topicName: "",
  11. topicNo: null,
  12. startNumber: 1,
  13. questionsCount: 4,
  14. questionNumberPerLine: 2,
  15. lineNumberPerQuestion: 1,
  16. lineSpacing: 40,
  17. questionDirection: "vertical",
  18. questionLineType: "norm",
  19. questionLineNums: [],
  20. numberPre: "",
  21. isCovered: false,
  22. };
  23. const getModel = (preSetData = {}) => {
  24. const model = Object.assign({}, MODEL, preSetData);
  25. return {
  26. id: getElementId(),
  27. key: randomCode(),
  28. ...model,
  29. };
  30. };
  31. const getFullModel = (model) => {
  32. const parent = { ...model };
  33. const numPerLine = model.questionNumberPerLine;
  34. let elements = [];
  35. let questionLineNums = model.questionLineNums;
  36. if (model.questionLineType === "norm") {
  37. questionLineNums = [];
  38. for (
  39. let j = model.startNumber;
  40. j < model.startNumber + model.questionsCount;
  41. j++
  42. ) {
  43. questionLineNums.push({
  44. no: j,
  45. count: model.lineNumberPerQuestion,
  46. });
  47. }
  48. }
  49. if (model.questionDirection === "vertical") {
  50. const total = Math.ceil(model.questionsCount / numPerLine);
  51. for (let i = 0; i < total; i++) {
  52. const childQuestionLineNums = questionLineNums.slice(
  53. i * numPerLine,
  54. (i + 1) * numPerLine
  55. );
  56. const maxLineNumberPerQuestion = Math.max.apply(
  57. null,
  58. childQuestionLineNums.map((item) => item.count)
  59. );
  60. const questionHeight = model.lineSpacing * maxLineNumberPerQuestion;
  61. let child = Object.assign({}, parent, {
  62. id: getElementId(),
  63. key: randomCode(),
  64. h: i ? questionHeight : questionHeight + 34,
  65. startNumber: model.startNumber + i * numPerLine,
  66. questionsCount:
  67. i === total - 1 ? model.questionsCount - numPerLine * i : numPerLine,
  68. parent: parent,
  69. isLast: i === total - 1,
  70. questionLineNums: childQuestionLineNums,
  71. });
  72. child.minHeight = child.h;
  73. elements[i] = child;
  74. }
  75. } else {
  76. for (let i = 0; i < model.questionsCount; i++) {
  77. const childQuestionLineNums = questionLineNums[i];
  78. const maxLineNumberPerQuestion = Math.ceil(
  79. childQuestionLineNums.count / numPerLine
  80. );
  81. const questionHeight = model.lineSpacing * maxLineNumberPerQuestion;
  82. let child = Object.assign({}, parent, {
  83. id: getElementId(),
  84. h: i ? questionHeight : questionHeight + 34,
  85. startNumber: model.startNumber + i,
  86. questionsCount: 1,
  87. parent: parent,
  88. isLast: i === model.questionsCount - 1,
  89. questionLineNums: [childQuestionLineNums],
  90. });
  91. child.minHeight = child.h;
  92. elements[i] = child;
  93. }
  94. }
  95. return elements;
  96. };
  97. export { MODEL, getModel, getFullModel };