label.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. const { setTooltip, setHidden, isMultipleInvalid, createRadioGroup, setLabel } = require('../utils/prop');
  2. const { template, $, update, close } = require('./base');
  3. const fontStyles = ['isBold', 'isItalic', 'isUnderline'];
  4. exports.template = template;
  5. exports.$ = $;
  6. exports.update = update;
  7. exports.close = close;
  8. exports.style = /* css */`
  9. ui-tab {
  10. flex: none;
  11. }
  12. .fontStyleParent {
  13. display:flex
  14. }
  15. .fontStyle:nth-child(2) {
  16. margin-left: 5px;
  17. margin-right: 5px;
  18. }
  19. .fontStyle {
  20. height: 20px;
  21. width: 42px;
  22. text-align: center;
  23. line-height: 20px;
  24. border: calc(var(--size-normal-border) * 1px) solid var(--color-default-border-weaker);
  25. border-radius: calc(var(--size-normal-radius) * 1px);
  26. }
  27. .fontStyle.invalid {
  28. background-color: var(--color-default-fill);
  29. }
  30. .fontStyle.select {
  31. background-color: var(--color-info-fill-important);
  32. border-color: var(--color-focus-border-emphasis);
  33. }
  34. .fontStyle.italic {
  35. font-style: italic;
  36. }
  37. .fontStyle.bold {
  38. font-weight: bold;
  39. }
  40. .fontStyle.underline {
  41. text-decoration-line: underline;
  42. }
  43. `;
  44. exports.ready = function() {
  45. this.elements = {
  46. horizontalAlign: {
  47. create(dump) {
  48. const prop = document.createElement('ui-prop');
  49. prop.dump = dump;
  50. const label = document.createElement('ui-label');
  51. label.setAttribute('slot', 'label');
  52. setLabel(dump, label);
  53. const content = createRadioGroup({
  54. enumList: dump.enumList,
  55. tooltip: dump.tooltip,
  56. getIconName: (elName) => {
  57. const iconName = elName.toLocaleLowerCase();
  58. if (iconName === 'center') {
  59. return `align-h-${iconName}`;
  60. }
  61. return `align-${iconName}`;
  62. },
  63. onChange: (event) => {
  64. const value = Number(event.target.value);
  65. if (Number.isFinite(value) && value !== -1) {
  66. dump.value = value;
  67. if (dump.values) {
  68. dump.values.forEach((_, index) => dump.values[index] = dump.value);
  69. }
  70. prop.dispatch('change-dump');
  71. prop.dispatch('confirm-dump');
  72. }
  73. },
  74. });
  75. prop.appendChild(label);
  76. prop.appendChild(content);
  77. return prop;
  78. },
  79. update(element, dump) {
  80. const radioGroup = element.querySelector('ui-radio-group');
  81. if (isMultipleInvalid(dump.horizontalAlign)) {
  82. radioGroup.value = -1;
  83. } else {
  84. radioGroup.value = dump.horizontalAlign.value;
  85. }
  86. },
  87. },
  88. verticalAlign: {
  89. create(dump) {
  90. const prop = document.createElement('ui-prop');
  91. prop.dump = dump;
  92. const label = document.createElement('ui-label');
  93. label.setAttribute('slot', 'label');
  94. setLabel(dump, label);
  95. const content = createRadioGroup({
  96. enumList: dump.enumList,
  97. tooltip: dump.tooltip,
  98. getIconName: (elementName) => {
  99. const iconName = elementName.toLocaleLowerCase();
  100. if (iconName === 'center') {
  101. return `align-v-${iconName}`;
  102. }
  103. return `align-${iconName}`;
  104. },
  105. onChange: (e) => {
  106. const enumVal = Number(e.target.value);
  107. if (!Number.isFinite(enumVal) || enumVal === -1) {
  108. return;
  109. }
  110. dump.value = enumVal;
  111. if (dump.values) {
  112. dump.values.forEach((_, index) => (dump.values[index] = dump.value));
  113. }
  114. prop.dispatch('change-dump');
  115. prop.dispatch('confirm-dump');
  116. },
  117. });
  118. prop.appendChild(label);
  119. prop.appendChild(content);
  120. return prop;
  121. },
  122. update(element, dump) {
  123. const radioGroup = element.querySelector('ui-radio-group');
  124. if (isMultipleInvalid(dump.verticalAlign)) {
  125. radioGroup.value = -1;
  126. } else {
  127. radioGroup.value = dump.verticalAlign.value;
  128. }
  129. },
  130. },
  131. isBold: {
  132. create() {
  133. const prop = document.createElement('ui-prop');
  134. const label = document.createElement('ui-label');
  135. label.setAttribute('slot', 'label');
  136. label.value = 'i18n:ENGINE.components.label.font_style';
  137. label.setAttribute('tooltip', 'i18n:ENGINE.components.label.font_style_tooltip');
  138. prop.appendChild(label);
  139. const content = document.createElement('div');
  140. content.setAttribute('slot', 'content');
  141. content.classList.add('fontStyleParent');
  142. const styleDisplayNames = ['B', 'I', 'U'];
  143. const styleClassNames = ['bold', 'italic', 'underline'];
  144. for (let index = 0; index < fontStyles.length; index++) {
  145. const style = fontStyles[index];
  146. const label = document.createElement('ui-label');
  147. label.innerHTML = styleDisplayNames[index];
  148. label.setAttribute('key', style);
  149. setTooltip(this.dump.value[style], label);
  150. label.classList.add('fontStyle', styleClassNames[index]);
  151. label.addEventListener('click', () => {
  152. prop.dump = this.dump.value[style];
  153. prop.dump.value = !prop.dump.value;
  154. if (prop.dump.values) {
  155. prop.dump.values.forEach((_, index) => prop.dump.values[index] = prop.dump.value);
  156. }
  157. prop.dispatch('change-dump');
  158. prop.dispatch('confirm-dump');
  159. this.dump.value[style].value ? label.classList.add('select') : label.classList.remove('select');
  160. });
  161. content.appendChild(label);
  162. }
  163. prop.appendChild(content);
  164. return prop;
  165. },
  166. update(element, dump) {
  167. const parent = element.querySelector('[slot="content"]');
  168. for (let index = 0; index < fontStyles.length; index++) {
  169. const div = parent.querySelector(`[key="${fontStyles[index]}"]`);
  170. const key = fontStyles[index];
  171. if (div) {
  172. const multipleInvalid = isMultipleInvalid(this.dump.value[key]);
  173. if (multipleInvalid) {
  174. div.classList.remove('select');
  175. div.classList.add('invalid');
  176. } else {
  177. div.classList.remove('invalid');
  178. this.dump.value[key].value ? div.classList.add('select') : div.classList.remove('select');
  179. }
  180. }
  181. }
  182. },
  183. },
  184. isItalic: {
  185. update(element, dump) {
  186. setHidden(true, element);
  187. },
  188. },
  189. isUnderline: {
  190. update(element, dump) {
  191. setHidden(true, element);
  192. },
  193. },
  194. };
  195. };