-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
292 lines (258 loc) · 7.22 KB
/
script.js
File metadata and controls
292 lines (258 loc) · 7.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/**
* 読み込んだファイルハンドル
* @type {FileSystemFileHandle}
*/
let _fileHandle;
/**
* データベースのテーブルデータ
* @type {{key: string, value: string}}
*/
let _dbRecords;
/**
* データベースのテーブルキーを表示する親要素
* @type {Element}
*/
let _dbKeysElement;
/**
* データベースの編集中のキー要素
* @type {Element}
*/
let _activeDbElement;
/**
* データベース
* @type {SQL.Database}
*/
let _db;
/**
* データベース
* @type {SQL.Database}
*/
let _editor;
window.addEventListener('load', loadedPage);
function loadedPage() {
initEditor();
_dbKeysElement = document.getElementById('db-keys');
eventHooks();
}
/**
* エディタ初期化
*/
function initEditor() {
_editor = ace.edit("editor");
_editor.setTheme("ace/theme/twilight");
updateEditorLanguageHighlight(false);
}
/**
* イベントフック
*/
function eventHooks() {
/**
* ドラッグ
*/
$(document).on('dragover', '#db-keys, #editor', function (_e) {
var e = _e;
if (_e.originalEvent) {
e = _e.originalEvent;
}
e.stopPropagation();
e.preventDefault();
e.dataTransfer.dropEffect = 'copy';
});
/**
* ドラック&ドロップでファイルを読み込み
*/
$(document).on('drop', '#db-keys, #editor', async function (_e) {
var e = _e;
if (_e.originalEvent) {
e = _e.originalEvent;
}
e.stopPropagation();
e.preventDefault();
initSession();
var items = e.dataTransfer.items;
for (var i = 0; i < items.length; i++) {
let item = items[i];
if (item.kind != 'file') { continue; }
_fileHandle = item.getAsFileSystemHandle();
let file = item.getAsFile();
if (!file.name.endsWith('db')) { continue; }
// ocsのみ読み込み
var reader = new FileReader();
reader.onload = (function (loadFile) {
return function (e) { loadDb(e); };
})(file);
reader.readAsArrayBuffer(file);
$('#filename').text(file.name);
break;
}
});
/**
* key切り替え
*/
$(document).on('click', '.db-key', async function (_e) {
// 現在のvalueを保持
storeCurrentEditor();
const key = _e.currentTarget.dataset.key;
showSelectedValue(key);
switchActiveKeyElement(_e.currentTarget);
_activeDbElement = _e.currentTarget;
});
/**
* キーイベント
*/
$(window).bind('keydown', async function (e) {
if (e.ctrlKey || e.metaKey) {
switch (String.fromCharCode(e.which).toLowerCase()) {
case 's':
e.preventDefault();
await saveDb();
break;
}
}
});
}
/**
* 初期化
*/
function initSession() {
_db = null;
_dbRecords = {};
_dbKeysElement.innerHTML = '';
_activeDbElement = null;
_editor.setValue('');
}
/**
* データベースロード
* @param {ProgressEvent<FileReader>} e
*/
function loadDb(e) {
initSqlJs({ locateFile: file => `https://cdnjs.cloudflare.com/ajax/libs/sql.js/1.8.0/${file}` }).then(function (SQL) {
const uint8 = new Uint8Array(e.target.result);
_db = new SQL.Database(uint8);
const contents = _db.exec("SELECT * FROM Entity;");
if (contents.length == 0) { return; }
for (const pair of contents[0].values) {
const key = pair[0];
const value = pair[1];
_dbRecords[key] = value;
const element = document.createElement('div');
element.className = 'db-key';
element.dataset.key = key;
element.innerText = key;
_dbKeysElement.appendChild(element);
}
});
}
/**
* 編集中の値を一時保存
*/
function storeCurrentEditor() {
const isReseted = _activeDbElement == null;
if (isReseted) { return; }
const isClean = _editor.session.getUndoManager().isClean();
if (isClean) { return; }
const value = _editor.getValue();
const isJson = isJsonText(value)
// jsonの場合は改行等を削除
const text = isJson ? JSON.stringify(JSON.parse(value)) : value;
const key = _activeDbElement.dataset.key;
_dbRecords[key] = text;
}
/**
* 選択したKeyの値をエディタに表示
* @param {string} key
*/
function showSelectedValue(key) {
const value = _dbRecords[key];
const isJson = isJsonText(value);
updateEditorLanguageHighlight(isJson);
const text = isJson ? JSON.stringify(JSON.parse(value), null, 4) : value;
_editor.setValue(text);
}
/**
* アクティブなキーを変更
* @param {Element} selectElement
*/
function switchActiveKeyElement(selectElement) {
if (_activeDbElement != null) { _activeDbElement.classList.remove('active'); }
selectElement.classList.add('active');
}
/**
* データベースを保存
*/
async function saveDb() {
if (_fileHandle == null) {
showToastr('warning', 'ファイルが開かれていません。', null);
return;
}
storeCurrentEditor();
try {
for (const key in _dbRecords) {
const value = _dbRecords[key];
_db.run(`UPDATE Entity SET value = '${value}' WHERE id = '${key}';`)
}
const binary = _db.export();
const handle = await _fileHandle;
const writer = await handle.createWritable();
await writer.write(binary);
await writer.close();
showToastr('success', 'ファイルを保存しました。', null);
} catch (error) {
showToastr('error', 'ファイルの保存に失敗しました。', error);
}
}
/**
* エディタのハイライトモードを更新
* @param {bool} toJsonMode Jsonモードに変更するか
*/
function updateEditorLanguageHighlight(toJsonMode) {
if (toJsonMode) {
const jsonMode = ace.require("ace/mode/json").Mode;
_editor.session.setMode(new jsonMode());
} else {
const plainMode = ace.require("ace/mode/plain_text").Mode;
_editor.session.setMode(new plainMode());
}
}
/**
* Json形式か
* @param {string} text
*/
function isJsonText(text) {
try {
JSON.parse(text);
return true;
} catch (error) {
return false;
}
}
/**
* トースト通知
* @param {string} status success, info, warning, error
* @param {string} title
* @param {string} message
*/
function showToastr(status, title, message) {
if (['success', 'info', 'warning', 'error'].indexOf(status) < 0) {
console.log(`不明なstatus: ${status}`);
return;
}
Command: toastr[status](message, title);
toastr.options = {
"closeButton": true,
"debug": false,
"newestOnTop": false,
"progressBar": false,
"positionClass": "toast-top-right",
"preventDuplicates": false,
"onclick": null,
"showDuration": "300",
"hideDuration": "1000",
"timeOut": "5000",
"extendedTimeOut": "1000",
"showEasing": "swing",
"hideEasing": "linear",
"showMethod": "fadeIn",
"hideMethod": "fadeOut"
}
}