refs-context.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. const GRID_DEFAULT_TRACK_WIDTH = 64;
  2. const GRID_AVAILABLE_OPTIONS_LIST = [
  3. 'autofill',
  4. 'autofill-xs',
  5. 'autofill-sm',
  6. 'autofill-md',
  7. 'autofill-lg',
  8. 'autofill-xl',
  9. 'col-2',
  10. 'col-3',
  11. 'col-4',
  12. 'col-5',
  13. 'col-6',
  14. ];
  15. type tags = 'ref' | 'refs' | 'refimg' | 'refsimg'
  16. /**
  17. * Context Object class for $ref() and $refimg()
  18. */
  19. export class RefsContext {
  20. tag: tags;
  21. options?: Record<string, string|undefined>;
  22. constructor(tag: tags, options: Record<string, string|undefined>) {
  23. this.tag = tag;
  24. // remove undefined keys
  25. Object.keys(options).forEach(key => options[key] === undefined && delete options[key]);
  26. this.options = options;
  27. }
  28. getStringifiedAttributes(separator = ', '): string {
  29. const attributeStrs: string[] = [];
  30. if (this.options != null) {
  31. const optionEntries = Object.entries(this.options).sort();
  32. attributeStrs.push(
  33. ...optionEntries.map(([key, val]) => `${key}=${val || 'true'}`),
  34. );
  35. }
  36. return attributeStrs.join(separator);
  37. }
  38. /**
  39. * for printing errors
  40. * @returns
  41. */
  42. toString(): string {
  43. return `$${this.tag}(${this.getStringifiedAttributes()})`;
  44. }
  45. get isSingle(): boolean {
  46. return this.tag === 'ref' || this.tag === 'refimg';
  47. }
  48. getOptGrid(): string | undefined {
  49. return GRID_AVAILABLE_OPTIONS_LIST.find(item => item === this.options?.grid);
  50. }
  51. isOptGridColumnEnabled(): boolean {
  52. const optGrid = this.getOptGrid();
  53. return (optGrid != null) && optGrid.startsWith('col-');
  54. }
  55. /**
  56. * return auto-calculated grid width
  57. * rules:
  58. * 1. when column mode (e.g. col-6, col5, ...), the width specification is disabled
  59. * 2. when width option is set, return it
  60. * 3. otherwise, the mode should be autofill and the width will be calculated according to the size
  61. */
  62. getOptGridWidth(): string | undefined {
  63. const grid = this.getOptGrid();
  64. const width = this.options?.width;
  65. // when Grid column mode
  66. if (this.isOptGridColumnEnabled()) {
  67. // not specify and ignore width
  68. return undefined;
  69. }
  70. // when width is specified
  71. if (width != null) {
  72. return width;
  73. }
  74. // when Grid autofill mode
  75. let autofillMagnification = 1;
  76. switch (grid) {
  77. case 'autofill-xl':
  78. autofillMagnification = 3;
  79. break;
  80. case 'autofill-lg':
  81. autofillMagnification = 2;
  82. break;
  83. case 'autofill-sm':
  84. autofillMagnification = 0.75;
  85. break;
  86. case 'autofill-xs':
  87. autofillMagnification = 0.5;
  88. break;
  89. case 'autofill':
  90. case 'autofill-md':
  91. break;
  92. }
  93. return `${GRID_DEFAULT_TRACK_WIDTH * autofillMagnification}px`;
  94. }
  95. /**
  96. * return auto-calculated grid height
  97. * rules:
  98. * 1. when height option is set, return it
  99. * 2. otherwise, the same value to the width will be returned
  100. */
  101. getOptGridHeight(): string | undefined {
  102. const height = this.options?.height;
  103. // when height is specified
  104. if (height != null) {
  105. return height;
  106. }
  107. // return the value which is same to width
  108. return this.getOptGridWidth();
  109. }
  110. getOptGridColumnsNum(): number | null {
  111. let columnsNum: number | null = null;
  112. switch (this.options?.grid) {
  113. case 'col-2':
  114. columnsNum = 2;
  115. break;
  116. case 'col-3':
  117. columnsNum = 3;
  118. break;
  119. case 'col-4':
  120. columnsNum = 4;
  121. break;
  122. case 'col-5':
  123. columnsNum = 5;
  124. break;
  125. case 'col-6':
  126. columnsNum = 6;
  127. break;
  128. }
  129. return columnsNum;
  130. }
  131. }