model.js 2.8 KB

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