商城国际化JSON编辑器,支持中日对照
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

index.html 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>JSON Editor</title>
  5. <style>
  6. body {
  7. font-family: Arial, sans-serif;
  8. background-color: #f5f5f5;
  9. margin: 0;
  10. padding: 20px;
  11. }
  12. .container {
  13. max-width: 1000px;
  14. margin: 0 auto;
  15. background-color: white;
  16. padding: 30px;
  17. border-radius: 8px;
  18. box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  19. margin-bottom: 80px;
  20. }
  21. .form-group {
  22. margin-bottom: 20px;
  23. padding: 15px;
  24. border: 1px solid #eee;
  25. border-radius: 4px;
  26. background-color: #fafafa;
  27. }
  28. .form-group:hover {
  29. background-color: #f0f0f0;
  30. }
  31. .form-group label {
  32. display: block;
  33. margin-bottom: 8px;
  34. font-weight: bold;
  35. color: #333;
  36. word-break: break-word;
  37. }
  38. .index {
  39. display: inline-block;
  40. background-color: #007bff;
  41. color: white;
  42. padding: 2px 8px;
  43. border-radius: 3px;
  44. margin-right: 8px;
  45. font-size: 0.9em;
  46. }
  47. textarea {
  48. width: 100%;
  49. min-height: 50px;
  50. padding: 10px;
  51. border: 1px solid #ddd;
  52. border-radius: 4px;
  53. font-size: 14px;
  54. resize: vertical;
  55. box-sizing: border-box;
  56. }
  57. textarea:focus {
  58. outline: none;
  59. border-color: #007bff;
  60. box-shadow: 0 0 0 2px rgba(0,123,255,0.25);
  61. }
  62. button {
  63. background-color: #28a745;
  64. color: white;
  65. border: none;
  66. padding: 12px 24px;
  67. border-radius: 4px;
  68. cursor: pointer;
  69. font-size: 16px;
  70. transition: background-color 0.2s;
  71. }
  72. button:hover {
  73. background-color: #218838;
  74. }
  75. fieldset {
  76. border: 1px solid #ddd;
  77. border-radius: 4px;
  78. padding: 20px;
  79. margin-bottom: 20px;
  80. }
  81. legend {
  82. font-weight: bold;
  83. padding: 0 10px;
  84. color: #333;
  85. }
  86. .save-button-container {
  87. position: fixed;
  88. bottom: 0;
  89. left: 0;
  90. right: 0;
  91. background-color: rgba(255, 255, 255, 0.9);
  92. padding: 15px;
  93. text-align: center;
  94. box-shadow: 0 -2px 10px rgba(0,0,0,0.1);
  95. z-index: 1000;
  96. }
  97. .reference-text {
  98. margin-top: 8px;
  99. padding: 10px;
  100. background-color: #f8f9fa;
  101. border: 1px solid #e9ecef;
  102. border-radius: 4px;
  103. color: #666;
  104. font-size: 0.9em;
  105. }
  106. .reference-label {
  107. color: #666;
  108. font-size: 0.9em;
  109. margin-bottom: 5px;
  110. font-style: italic;
  111. }
  112. </style>
  113. </head>
  114. <body>
  115. <div class="container">
  116. <h1>JSON 编辑器</h1>
  117. <div id="editor"></div>
  118. </div>
  119. <div class="save-button-container">
  120. <button onclick="saveChanges()">保存更改</button>
  121. </div>
  122. <script>
  123. let jsonData = {};
  124. let referenceData = {};
  125. let indexCounter = 1;
  126. // 创建编辑表单
  127. function createForm(data, parentKey = '') {
  128. const container = document.createElement('div');
  129. for (const [key, value] of Object.entries(data)) {
  130. const currentKey = parentKey ? `${parentKey}.${key}` : key;
  131. if (typeof value === 'object' && value !== null) {
  132. const fieldset = document.createElement('fieldset');
  133. fieldset.innerHTML = `<legend>${key}</legend>`;
  134. fieldset.appendChild(createForm(value, currentKey));
  135. container.appendChild(fieldset);
  136. } else {
  137. const formGroup = document.createElement('div');
  138. formGroup.className = 'form-group';
  139. // 获取对应的参照文本
  140. const referenceValue = getNestedValue(referenceData, currentKey);
  141. const referenceHtml = referenceValue ? `
  142. <div class="reference-label">参照文本:</div>
  143. <div class="reference-text">${referenceValue}</div>
  144. ` : '';
  145. formGroup.innerHTML = `
  146. <label><span class="index">${indexCounter}</span>${key}:</label>
  147. <textarea data-key="${currentKey}">${value}</textarea>
  148. ${referenceHtml}
  149. `;
  150. container.appendChild(formGroup);
  151. indexCounter++;
  152. }
  153. }
  154. return container;
  155. }
  156. // 获取嵌套对象的值
  157. function getNestedValue(obj, path) {
  158. return path.split('.').reduce((current, key) =>
  159. current ? current[key] : undefined, obj);
  160. }
  161. // 获取数据并显示
  162. async function loadData() {
  163. try {
  164. // 并行加载两个文件
  165. const [dataResponse, referenceResponse] = await Promise.all([
  166. fetch('/api/data'),
  167. fetch('/api/reference')
  168. ]);
  169. jsonData = await dataResponse.json();
  170. referenceData = await referenceResponse.json();
  171. const editor = document.getElementById('editor');
  172. editor.innerHTML = '';
  173. indexCounter = 1;
  174. editor.appendChild(createForm(jsonData));
  175. } catch (error) {
  176. console.error('Error loading data:', error);
  177. alert('加载数据失败!');
  178. }
  179. }
  180. // 保存更改
  181. async function saveChanges() {
  182. const textareas = document.querySelectorAll('textarea');
  183. let updatedData = JSON.parse(JSON.stringify(jsonData));
  184. textareas.forEach(textarea => {
  185. const path = textarea.dataset.key.split('.');
  186. let current = updatedData;
  187. // 确保路径存在
  188. for (let i = 0; i < path.length - 1; i++) {
  189. if (current[path[i]] === undefined) {
  190. current[path[i]] = {};
  191. }
  192. current = current[path[i]];
  193. }
  194. // 安全地设置值
  195. if (current && path.length > 0) {
  196. const lastKey = path[path.length - 1];
  197. current[lastKey] = textarea.value;
  198. }
  199. });
  200. try {
  201. const response = await fetch('/api/save', {
  202. method: 'POST',
  203. headers: {
  204. 'Content-Type': 'application/json'
  205. },
  206. body: JSON.stringify(updatedData)
  207. });
  208. const result = await response.json();
  209. if (result.success) {
  210. alert('保存成功!');
  211. } else {
  212. alert('保存失败:' + (result.error || '未知错误'));
  213. }
  214. } catch (error) {
  215. console.error('Error saving data:', error);
  216. alert('保存失败:' + error.message);
  217. }
  218. }
  219. // 页面加载时获取数据
  220. loadData();
  221. </script>
  222. </body>
  223. </html>