model.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import {
  2. getElementId,
  3. randomCode,
  4. objAssign,
  5. deepCopy,
  6. } from "../../plugins/utils";
  7. const FILL_LINE_PROP = {
  8. type: "FILL_LINE",
  9. sign: "subjective",
  10. topicName: "",
  11. topicNo: null,
  12. fillCountPerLine: 4,
  13. fillCount: null,
  14. lineSpacing: 40,
  15. questionLineType: "custom",
  16. paperStruct: {},
  17. };
  18. const MODEL = {
  19. type: "FILL_LINE",
  20. x: 0,
  21. y: 0,
  22. w: 0,
  23. h: 40,
  24. minHeight: 40,
  25. sign: "subjective",
  26. topicName: "",
  27. topicNo: null,
  28. fillCountPerLine: 4,
  29. lineSpacing: 40,
  30. questionNo: "1",
  31. fillCount: 1,
  32. isCovered: false,
  33. isLast: false,
  34. isFirst: false,
  35. };
  36. const getModel = (presetData) => {
  37. const model = objAssign(deepCopy(FILL_LINE_PROP), presetData);
  38. model.id = getElementId();
  39. model.key = randomCode();
  40. return model;
  41. };
  42. // questions:[{questionNo,fillCount}]
  43. const getFullModel = (modelProp) => {
  44. // console.log(modelProp);
  45. const parent = deepCopy(modelProp);
  46. const childModel = objAssign(MODEL, parent);
  47. const numPerLine = childModel.fillCountPerLine;
  48. let elements = [];
  49. const questions = parent.paperStruct.questions || [];
  50. questions.forEach((question, index) => {
  51. const questionFillCount = parent.fillCount || question.fillCount;
  52. const maxLineNumberPerQuestion = Math.ceil(questionFillCount / numPerLine);
  53. const questionHeight = childModel.lineSpacing * maxLineNumberPerQuestion;
  54. let child = Object.assign({}, childModel, {
  55. id: getElementId(),
  56. h: index ? questionHeight : questionHeight + 34,
  57. isLast: false,
  58. isFirst: !index,
  59. questionNo: question.questionNo,
  60. fillCount: questionFillCount,
  61. parent,
  62. });
  63. child.minHeight = child.h;
  64. elements[index] = child;
  65. });
  66. elements[elements.length - 1].isLast = true;
  67. return elements;
  68. };
  69. export { MODEL, getModel, getFullModel };