default.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const path_1 = require("path");
  4. const fs_1 = require("fs");
  5. const Vue = require('vue/dist/vue.common.prod');
  6. const languageContentTemplate = `
  7. const win = window as any;
  8. export const languages = {
  9. // Data
  10. };
  11. if (!win.languages) {
  12. win.languages = {};
  13. }
  14. win.languages.{{name}} = languages;
  15. `;
  16. module.exports = Editor.Panel.define({
  17. template: `
  18. <div class="content">
  19. <header>
  20. <ui-button class="transparent add"
  21. @confirm="add()"
  22. >
  23. <ui-icon value="add"></ui-icon>
  24. </ui-button>
  25. <ui-button class="transparent refresh"
  26. @confirm="refresh()"
  27. >
  28. <ui-icon value="refresh"></ui-icon>
  29. </ui-button>
  30. </header>
  31. <section>
  32. <div
  33. v-for="item of list"
  34. >
  35. <ui-icon value="eye-open"
  36. v-if="item === current"
  37. ></ui-icon>
  38. <ui-icon value="eye-close"
  39. v-else
  40. @click="select(item)"
  41. ></ui-icon>
  42. <span>{{item}}</span>
  43. <ui-icon class="option" value="del"
  44. @click="del(item)"
  45. ></ui-icon>
  46. </div>
  47. <div
  48. v-if="showAddInput"
  49. >
  50. <ui-input ref="addInput"
  51. @confirm="generateLanguageFile($event)"
  52. ></ui-input>
  53. </div>
  54. </section>
  55. <div>
  56. `,
  57. style: `
  58. :host { display: flex; padding: 6px; flex-direction: column; }
  59. :host .content { flex: 1; display: flex; flex-direction: column; }
  60. header { margin-bottom: 6px; }
  61. header > ui-button.refresh { float: right; }
  62. section { flex: 1; background: var(--color-normal-fill-emphasis); border-radius: calc( var(--size-normal-radius)* 1px); padding: 4px; }
  63. section > div { padding: 0 10px; }
  64. section > div > .slider { margin-right: 4px; }
  65. section > div > .option { float: right; display: none; }
  66. section > div > ui-icon { cursor: pointer; color: var(--color-warn-fill-normal); }
  67. section > div > ui-icon[value=eye-open] { color: var(--color-success-fill-normal); }
  68. section > div > ui-icon[value=del] { color: var(--color-danger-fill-normal); }
  69. section > div:hover { background: var(--color-normal-fill); border-radius: calc( var(--size-normal-radius)* 1px); }
  70. section > div:hover > .option { display: inline; }
  71. `,
  72. $: {
  73. content: '.content',
  74. },
  75. ready() {
  76. const vm = new Vue({
  77. el: this.$.content,
  78. data: {
  79. current: '',
  80. list: [],
  81. showAddInput: false,
  82. },
  83. watch: {
  84. current() {
  85. const vm = this;
  86. Editor.Message.send('scene', 'execute-scene-script', {
  87. name: 'i18n',
  88. method: 'changeCurrentLanguage',
  89. args: [vm.current || ''],
  90. });
  91. },
  92. },
  93. methods: {
  94. add() {
  95. vm.showAddInput = true;
  96. requestAnimationFrame(() => {
  97. vm.$refs.addInput.focus();
  98. });
  99. },
  100. select(language) {
  101. vm.current = language;
  102. },
  103. async del(name) {
  104. const result = await Editor.Dialog.info(`确定删除 ${name} 语言文件?`, {
  105. buttons: ['确认', '取消'],
  106. default: 0,
  107. cancel: 1,
  108. });
  109. if (result.response === 0) {
  110. await Editor.Message.request('asset-db', 'delete-asset', `db://assets/resources/i18n/${name}.ts`);
  111. vm.refresh();
  112. }
  113. },
  114. async refresh() {
  115. const dir = path_1.join(Editor.Project.path, 'assets/resources/i18n');
  116. if (!fs_1.existsSync(dir)) {
  117. return;
  118. }
  119. vm.current = await Editor.Message.request('scene', 'execute-scene-script', {
  120. name: 'i18n',
  121. method: 'queryCurrentLanguage',
  122. args: [],
  123. }) || '';
  124. const names = fs_1.readdirSync(dir);
  125. vm.$set(vm, 'list', []);
  126. names.forEach((name) => {
  127. const language = name.replace(/\.[^\.]+$/, '');
  128. if (!/\./.test(language)) {
  129. vm.list.push(language);
  130. }
  131. });
  132. },
  133. async generateLanguageFile(event) {
  134. // @ts-ignore
  135. const language = event.target.value;
  136. if (!/[a-zA-Z]/.test(language)) {
  137. console.warn(`语言名称只允许使用 a-z A-Z, ${language} 不合法`);
  138. return;
  139. }
  140. const languageContent = languageContentTemplate.replace(/{{name}}/g, language);
  141. vm.showAddInput = false;
  142. await Editor.Message.request('asset-db', 'create-asset', `db://assets/resources/i18n/${language}.ts`, languageContent);
  143. vm.refresh();
  144. },
  145. },
  146. });
  147. vm.refresh();
  148. },
  149. });