model.js 1.7 KB

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