kintone-plugin-ide 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +123 -123
- package/dist/index.js +1 -1
- package/dist/kintone-plugin-ide.iife.js +1 -1
- package/package.json +59 -59
- package/src/build/build.js +87 -87
- package/src/build/http-plugin.js +32 -32
- package/src/build/transformers.js +168 -168
- package/src/build/vfs-plugin.js +60 -60
- package/src/config.js +58 -58
- package/src/deps.js +41 -41
- package/src/ide/actions/misc.js +74 -74
- package/src/ide/actions/npm.js +103 -103
- package/src/ide/actions/release.js +57 -57
- package/src/ide/actions/save.js +54 -54
- package/src/ide/actions/zip.js +59 -59
- package/src/ide/commands.js +96 -96
- package/src/ide/css.js +50 -50
- package/src/ide/dialog.js +51 -51
- package/src/ide/editor.js +155 -155
- package/src/ide/index.js +94 -94
- package/src/ide/layout.js +89 -89
- package/src/ide/sample.js +168 -168
- package/src/ide/state.js +58 -58
- package/src/ide/terminal.js +57 -57
- package/src/ide/tree.js +224 -224
- package/src/ide/workspace-fs.js +173 -173
- package/src/index.js +48 -48
- package/src/kintone/api.js +24 -24
- package/src/kintone/plugin.js +37 -37
- package/src/npm/registry.js +32 -32
- package/src/npm/vendor.js +69 -69
- package/src/packer/packer.js +50 -50
- package/src/util/bytes.js +15 -15
- package/src/util/dom.js +15 -15
- package/src/util/idb.js +18 -18
- package/src/util/path.js +14 -14
- package/src/util/settings.js +5 -5
- package/types/index.d.ts +49 -49
package/src/ide/tree.js
CHANGED
|
@@ -1,224 +1,224 @@
|
|
|
1
|
-
// ファイルツリー(@pierre/trees)と、ファイル・フォルダの操作
|
|
2
|
-
import { NODE_MODULES } from '../config.js';
|
|
3
|
-
import { loadTrees } from '../deps.js';
|
|
4
|
-
import { el } from '../util/dom.js';
|
|
5
|
-
import { fromBytes, kb } from '../util/bytes.js';
|
|
6
|
-
import { dirname, normalize, isVendor } from '../util/path.js';
|
|
7
|
-
import { TREE_CSS } from './css.js';
|
|
8
|
-
import { workspaceFS } from './workspace-fs.js';
|
|
9
|
-
|
|
10
|
-
export async function setupTree(ctx) {
|
|
11
|
-
const { log, ui } = ctx;
|
|
12
|
-
const { files, saved, dirs } = ctx.state;
|
|
13
|
-
const { FileTree } = await loadTrees();
|
|
14
|
-
const treePaths = () => [...files.keys(), ...[...dirs].map((d) => d + '/')].sort();
|
|
15
|
-
|
|
16
|
-
// ---- 右クリック / ⋯ のメニュー ----
|
|
17
|
-
// ツリーの中(行の右端)に出すと、右のエディタの下に潜ってしまうので、IDE の一番上に出す
|
|
18
|
-
let portal = null;
|
|
19
|
-
const closePortal = () => { portal?.remove(); portal = null; };
|
|
20
|
-
function menu(item, c) {
|
|
21
|
-
closePortal();
|
|
22
|
-
const m = el('div', { className: 'kpi-menu' });
|
|
23
|
-
m.dataset.fileTreeContextMenuRoot = 'true';
|
|
24
|
-
const isDir = item.kind === 'directory';
|
|
25
|
-
const path = normalize(item.path);
|
|
26
|
-
const vendor = isVendor(path);
|
|
27
|
-
const add = (label, fn, disabled = false) => {
|
|
28
|
-
const b = el('button', { textContent: label, disabled });
|
|
29
|
-
b.onclick = () => { c.close({ restoreFocus: false }); closePortal(); fn(); };
|
|
30
|
-
m.append(b);
|
|
31
|
-
};
|
|
32
|
-
const dir = isDir ? path : dirname(path);
|
|
33
|
-
add('新しいファイル', () => newFile(dir), vendor);
|
|
34
|
-
add('新しいフォルダ', () => newFolder(dir), vendor);
|
|
35
|
-
add('ファイルをアップロード…', () => uploadFiles(dir), vendor);
|
|
36
|
-
m.append(el('hr'));
|
|
37
|
-
add('名前を変更', () => tree.startRenaming(item.path), vendor);
|
|
38
|
-
add('複製', () => duplicate(path, isDir), vendor);
|
|
39
|
-
if (!isDir) {
|
|
40
|
-
m.append(el('hr'));
|
|
41
|
-
add('保存した内容と比べる', () => ctx.showDiff(path), vendor);
|
|
42
|
-
add('整形(prettier)', () => ctx.run(() => ctx.formatFiles([path])), vendor);
|
|
43
|
-
}
|
|
44
|
-
add('パスをコピー', () => navigator.clipboard.writeText(path));
|
|
45
|
-
m.append(el('hr'));
|
|
46
|
-
if (vendor) {
|
|
47
|
-
const rest = path.slice(NODE_MODULES.length).split('/');
|
|
48
|
-
const pkg = rest[0] && (rest[0].startsWith('@') ? rest.slice(0, 2).join('/') : rest[0]);
|
|
49
|
-
add(pkg ? `npm uninstall ${pkg}` : 'npm uninstall', () => ctx.run(() => ctx.command(`npm uninstall ${pkg}`)), !pkg);
|
|
50
|
-
}
|
|
51
|
-
else add('削除', () => removePath(path, isDir));
|
|
52
|
-
|
|
53
|
-
ui.root.append(m);
|
|
54
|
-
const r = c.anchorRect;
|
|
55
|
-
const w = m.offsetWidth, h = m.offsetHeight;
|
|
56
|
-
m.style.left = `${Math.max(4, Math.min(r.left, innerWidth - w - 8))}px`;
|
|
57
|
-
m.style.top = `${r.bottom + h + 8 > innerHeight ? Math.max(4, r.top - h) : r.bottom}px`;
|
|
58
|
-
portal = m;
|
|
59
|
-
return el('span'); // ツリーに渡すのは空の印だけ
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
const vendorGuard = (paths) => !paths.some((p) => isVendor(normalize(p)));
|
|
63
|
-
const tree = new FileTree({
|
|
64
|
-
paths: treePaths(),
|
|
65
|
-
initialExpansion: 'open',
|
|
66
|
-
flattenEmptyDirectories: false, // node_modules/dayjs のように1行にまとめない
|
|
67
|
-
search: true,
|
|
68
|
-
renaming: { canRename: ({ path }) => !isVendor(normalize(path)) },
|
|
69
|
-
dragAndDrop: {
|
|
70
|
-
canDrag: vendorGuard,
|
|
71
|
-
canDrop: ({ target }) => !JSON.stringify(target ?? '').includes(NODE_MODULES),
|
|
72
|
-
},
|
|
73
|
-
composition: {
|
|
74
|
-
contextMenu: { enabled: true, triggerMode: 'both', buttonVisibility: 'when-needed', render: menu, onClose: closePortal },
|
|
75
|
-
},
|
|
76
|
-
onSelectionChange: (paths) => { const p = paths[0] && normalize(paths[0]); if (p && files.has(p)) ctx.openFile(p); },
|
|
77
|
-
unsafeCSS: TREE_CSS,
|
|
78
|
-
});
|
|
79
|
-
// 名前の変更もドラッグ&ドロップでの移動も、ツリーの "move" を受けて反映する
|
|
80
|
-
tree.onMutation('move', ({ from, to }) => {
|
|
81
|
-
const f = normalize(from), t = normalize(to);
|
|
82
|
-
const isFolder = from.endsWith('/') || dirs.has(f) || (!files.has(f) && [...files.keys()].some((k) => k.startsWith(f + '/')));
|
|
83
|
-
movePath(f, t, isFolder);
|
|
84
|
-
});
|
|
85
|
-
tree.render({ containerWrapper: ui.treeEl });
|
|
86
|
-
const collapse = (p) => { try { (tree.getItem(p + '/') || tree.getItem(p))?.collapse(); } catch {} };
|
|
87
|
-
collapse('node_modules');
|
|
88
|
-
|
|
89
|
-
// ---- 変更の印(git status の見た目を借りる) ----
|
|
90
|
-
let lastStatus = [];
|
|
91
|
-
let syncTimer;
|
|
92
|
-
function refreshStatus() {
|
|
93
|
-
const set = [];
|
|
94
|
-
for (const p of files.keys()) {
|
|
95
|
-
if (isVendor(p)) continue;
|
|
96
|
-
if (!saved.has(p)) set.push({ path: p, status: 'added' });
|
|
97
|
-
else if (files.get(p) !== saved.get(p)) set.push({ path: p, status: 'modified' });
|
|
98
|
-
}
|
|
99
|
-
if ([...files.keys()].some(isVendor)) set.push({ path: NODE_MODULES, status: 'ignored' });
|
|
100
|
-
const remove = lastStatus.map((s) => s.path).filter((p) => !set.some((s) => s.path === p));
|
|
101
|
-
tree.applyGitStatusPatch({ set, remove });
|
|
102
|
-
lastStatus = set;
|
|
103
|
-
ctx.updateStatus();
|
|
104
|
-
ctx.saveDraft?.();
|
|
105
|
-
clearTimeout(syncTimer); syncTimer = setTimeout(() => workspaceFS.sync(), 400); // 言語サービスに知らせる
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
// ---- 操作 ----
|
|
109
|
-
const has = (p) => files.has(p) || dirs.has(p) || [...files.keys()].some((k) => k.startsWith(p + '/'));
|
|
110
|
-
// 入力欄のチェック(空・node_modules・同じ名前)
|
|
111
|
-
const checkPath = (kind) => (v) => {
|
|
112
|
-
const p = normalize(v);
|
|
113
|
-
if (!p) return '名前を入れてください';
|
|
114
|
-
if (isVendor(p)) return 'node_modules の中には作れません(npm install を使ってください)';
|
|
115
|
-
if (/[\\:*?"<>|]/.test(p)) return '使えない文字が入っています';
|
|
116
|
-
if (kind === 'file' && v.endsWith('/')) return null; // 末尾が / ならフォルダとして作る
|
|
117
|
-
if (files.has(p)) return `${p} はもうあります`;
|
|
118
|
-
if (kind === 'folder' && has(p)) return `${p}/ はもうあります`;
|
|
119
|
-
return null;
|
|
120
|
-
};
|
|
121
|
-
// "src/" + "new-file.ts" の、ファイル名の部分だけを選んだ状態で出す
|
|
122
|
-
const nameRange = (prefix, name) => [prefix.length, prefix.length + name.replace(/\.[^.]*$/, '').length];
|
|
123
|
-
async function newFile(dir) {
|
|
124
|
-
const prefix = dir ? `${dir}/` : '';
|
|
125
|
-
const v = await ctx.ask({ title: '新しいファイル', message: 'フォルダは / で区切ります。末尾を / にするとフォルダを作ります', value: prefix + 'new-file.ts', select: nameRange(prefix, 'new-file.ts'), validate: checkPath('file'), ok: '作る' });
|
|
126
|
-
if (v == null) return;
|
|
127
|
-
if (v.endsWith('/')) { makeFolder(normalize(v)); return; }
|
|
128
|
-
const p = normalize(v);
|
|
129
|
-
addFiles([[p, '']]);
|
|
130
|
-
ctx.openFile(p);
|
|
131
|
-
}
|
|
132
|
-
async function newFolder(dir) {
|
|
133
|
-
const prefix = dir ? `${dir}/` : '';
|
|
134
|
-
const v = await ctx.ask({ title: '新しいフォルダ', value: prefix + 'new-folder', select: [prefix.length, prefix.length + 10], validate: checkPath('folder'), ok: '作る' });
|
|
135
|
-
if (v != null) makeFolder(normalize(v));
|
|
136
|
-
}
|
|
137
|
-
function makeFolder(p) {
|
|
138
|
-
if (!p || has(p)) return;
|
|
139
|
-
dirs.add(p);
|
|
140
|
-
tree.add(p + '/');
|
|
141
|
-
refreshStatus();
|
|
142
|
-
log.ok(`${p}/ を作りました`);
|
|
143
|
-
}
|
|
144
|
-
// 画像などをアップロード。1つならパスを、複数なら置き場所のフォルダを聞く
|
|
145
|
-
async function uploadFiles(dir, picked) {
|
|
146
|
-
const list = picked || await new Promise((resolve) => {
|
|
147
|
-
const input = el('input', { type: 'file', multiple: true });
|
|
148
|
-
input.onchange = () => resolve([...input.files]);
|
|
149
|
-
input.click();
|
|
150
|
-
});
|
|
151
|
-
if (!list.length) return;
|
|
152
|
-
const prefix = dir ? `${dir}/` : '';
|
|
153
|
-
let targets;
|
|
154
|
-
if (list.length === 1) {
|
|
155
|
-
const v = await ctx.ask({ title: `${list[0].name} をアップロード`, message: `置く場所(パス)を指定してください(${kb(list[0].size)})`, value: prefix + list[0].name, select: nameRange(prefix, list[0].name), validate: (x) => (normalize(x) ? (isVendor(normalize(x)) ? 'node_modules の中には置けません' : null) : 'パスを入れてください'), ok: 'アップロード' });
|
|
156
|
-
if (v == null) return;
|
|
157
|
-
targets = [[normalize(v), list[0]]];
|
|
158
|
-
} else {
|
|
159
|
-
const v = await ctx.ask({ title: `${list.length} 個のファイルをアップロード`, message: list.map((f) => f.name).join('\n'), value: dir || 'image', validate: (x) => (isVendor(normalize(x)) ? 'node_modules の中には置けません' : null), ok: 'アップロード' });
|
|
160
|
-
if (v == null) return;
|
|
161
|
-
targets = list.map((f) => [normalize(`${normalize(v)}/${f.name}`), f]);
|
|
162
|
-
}
|
|
163
|
-
const exists = targets.filter(([p]) => files.has(p)).map(([p]) => p);
|
|
164
|
-
if (exists.length && !(await ctx.confirm('上書きします', exists.join('\n'), { ok: '上書きする', danger: true }))) return;
|
|
165
|
-
const entries = [];
|
|
166
|
-
for (const [p, f] of targets) entries.push([p, fromBytes(new Uint8Array(await f.arrayBuffer()))]);
|
|
167
|
-
addFiles(entries);
|
|
168
|
-
for (const [p, f] of targets) log.ok(`${p} を追加しました (${kb(f.size)})`);
|
|
169
|
-
if (targets.length === 1) ctx.openFile(targets[0][0]);
|
|
170
|
-
}
|
|
171
|
-
function addFiles(entries, { collapseNew = false } = {}) {
|
|
172
|
-
const added = [];
|
|
173
|
-
for (const [p, text] of entries) {
|
|
174
|
-
if (!files.has(p)) added.push(p);
|
|
175
|
-
files.set(p, text);
|
|
176
|
-
const m = ctx.models?.get(p); if (m && m.getValue() !== text) m.setValue(text);
|
|
177
|
-
}
|
|
178
|
-
if (added.length) tree.batch(added.map((path) => ({ type: 'add', path })));
|
|
179
|
-
for (const d of [...dirs]) if (added.some((p) => p.startsWith(d + '/'))) dirs.delete(d);
|
|
180
|
-
if (collapseNew) collapse('node_modules');
|
|
181
|
-
refreshStatus();
|
|
182
|
-
}
|
|
183
|
-
// ファイルを消す(確認なし。node_modules の入れ替えなどに使う)
|
|
184
|
-
function dropFiles(paths) {
|
|
185
|
-
for (const f of paths) { files.delete(f); ctx.disposeModel?.(f); }
|
|
186
|
-
const gone = paths.filter((p) => !files.has(p));
|
|
187
|
-
if (gone.length) tree.batch(gone.map((path) => ({ type: 'remove', path })));
|
|
188
|
-
if (gone.includes(ctx.current)) ctx.closeEditor();
|
|
189
|
-
refreshStatus();
|
|
190
|
-
}
|
|
191
|
-
function duplicate(p, isDir) {
|
|
192
|
-
const targets = isDir ? [...files.keys()].filter((f) => f.startsWith(p + '/')) : [p];
|
|
193
|
-
const copyName = isDir ? `${p}-copy` : p.replace(/(\.[^./]+)?$/, '-copy$1');
|
|
194
|
-
if (isDir && !targets.length) { makeFolder(copyName); return; }
|
|
195
|
-
addFiles(targets.map((f) => [isDir ? copyName + f.slice(p.length) : copyName, files.get(f)]));
|
|
196
|
-
}
|
|
197
|
-
async function removePath(p, isFolder) {
|
|
198
|
-
const targets = isFolder ? [...files.keys()].filter((f) => f.startsWith(p + '/')) : [p];
|
|
199
|
-
const emptyDirs = isFolder ? [...dirs].filter((d) => d === p || d.startsWith(p + '/')) : [];
|
|
200
|
-
if (!targets.length && !emptyDirs.length) return;
|
|
201
|
-
if (targets.length && !(await ctx.confirm(`${targets.length} 個のファイルを削除します`, `保存するまでレコードには反映されません\n\n${targets.slice(0, 30).join('\n')}${targets.length > 30 ? '\n…' : ''}`, { ok: '削除', danger: true }))) return;
|
|
202
|
-
for (const f of targets) { files.delete(f); ctx.disposeModel(f); }
|
|
203
|
-
for (const d of emptyDirs) dirs.delete(d);
|
|
204
|
-
tree.remove(isFolder ? p + '/' : p, { recursive: isFolder });
|
|
205
|
-
if (targets.includes(ctx.current)) ctx.closeEditor();
|
|
206
|
-
refreshStatus();
|
|
207
|
-
}
|
|
208
|
-
function movePath(from, to, isFolder) {
|
|
209
|
-
if (isFolder) {
|
|
210
|
-
const moves = [...files.keys()].filter((f) => f.startsWith(from + '/')).map((f) => [f, to + f.slice(from.length)]);
|
|
211
|
-
for (const d of [...dirs]) if (d === from || d.startsWith(from + '/')) { dirs.delete(d); dirs.add(to + d.slice(from.length)); }
|
|
212
|
-
for (const [a, b] of moves) moveFile(a, b);
|
|
213
|
-
} else moveFile(from, to);
|
|
214
|
-
refreshStatus();
|
|
215
|
-
}
|
|
216
|
-
function moveFile(a, b) {
|
|
217
|
-
if (!files.has(a)) return;
|
|
218
|
-
files.set(b, files.get(a)); files.delete(a);
|
|
219
|
-
ctx.disposeModel(a);
|
|
220
|
-
if (ctx.current === a) { ctx.current = null; ctx.openFile(b); }
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
Object.assign(ctx, { tree, refreshStatus, newFile, newFolder, makeFolder, uploadFiles, addFiles, dropFiles, removePath, closeMenu: closePortal });
|
|
224
|
-
}
|
|
1
|
+
// ファイルツリー(@pierre/trees)と、ファイル・フォルダの操作
|
|
2
|
+
import { NODE_MODULES } from '../config.js';
|
|
3
|
+
import { loadTrees } from '../deps.js';
|
|
4
|
+
import { el } from '../util/dom.js';
|
|
5
|
+
import { fromBytes, kb } from '../util/bytes.js';
|
|
6
|
+
import { dirname, normalize, isVendor } from '../util/path.js';
|
|
7
|
+
import { TREE_CSS } from './css.js';
|
|
8
|
+
import { workspaceFS } from './workspace-fs.js';
|
|
9
|
+
|
|
10
|
+
export async function setupTree(ctx) {
|
|
11
|
+
const { log, ui } = ctx;
|
|
12
|
+
const { files, saved, dirs } = ctx.state;
|
|
13
|
+
const { FileTree } = await loadTrees();
|
|
14
|
+
const treePaths = () => [...files.keys(), ...[...dirs].map((d) => d + '/')].sort();
|
|
15
|
+
|
|
16
|
+
// ---- 右クリック / ⋯ のメニュー ----
|
|
17
|
+
// ツリーの中(行の右端)に出すと、右のエディタの下に潜ってしまうので、IDE の一番上に出す
|
|
18
|
+
let portal = null;
|
|
19
|
+
const closePortal = () => { portal?.remove(); portal = null; };
|
|
20
|
+
function menu(item, c) {
|
|
21
|
+
closePortal();
|
|
22
|
+
const m = el('div', { className: 'kpi-menu' });
|
|
23
|
+
m.dataset.fileTreeContextMenuRoot = 'true';
|
|
24
|
+
const isDir = item.kind === 'directory';
|
|
25
|
+
const path = normalize(item.path);
|
|
26
|
+
const vendor = isVendor(path);
|
|
27
|
+
const add = (label, fn, disabled = false) => {
|
|
28
|
+
const b = el('button', { textContent: label, disabled });
|
|
29
|
+
b.onclick = () => { c.close({ restoreFocus: false }); closePortal(); fn(); };
|
|
30
|
+
m.append(b);
|
|
31
|
+
};
|
|
32
|
+
const dir = isDir ? path : dirname(path);
|
|
33
|
+
add('新しいファイル', () => newFile(dir), vendor);
|
|
34
|
+
add('新しいフォルダ', () => newFolder(dir), vendor);
|
|
35
|
+
add('ファイルをアップロード…', () => uploadFiles(dir), vendor);
|
|
36
|
+
m.append(el('hr'));
|
|
37
|
+
add('名前を変更', () => tree.startRenaming(item.path), vendor);
|
|
38
|
+
add('複製', () => duplicate(path, isDir), vendor);
|
|
39
|
+
if (!isDir) {
|
|
40
|
+
m.append(el('hr'));
|
|
41
|
+
add('保存した内容と比べる', () => ctx.showDiff(path), vendor);
|
|
42
|
+
add('整形(prettier)', () => ctx.run(() => ctx.formatFiles([path])), vendor);
|
|
43
|
+
}
|
|
44
|
+
add('パスをコピー', () => navigator.clipboard.writeText(path));
|
|
45
|
+
m.append(el('hr'));
|
|
46
|
+
if (vendor) {
|
|
47
|
+
const rest = path.slice(NODE_MODULES.length).split('/');
|
|
48
|
+
const pkg = rest[0] && (rest[0].startsWith('@') ? rest.slice(0, 2).join('/') : rest[0]);
|
|
49
|
+
add(pkg ? `npm uninstall ${pkg}` : 'npm uninstall', () => ctx.run(() => ctx.command(`npm uninstall ${pkg}`)), !pkg);
|
|
50
|
+
}
|
|
51
|
+
else add('削除', () => removePath(path, isDir));
|
|
52
|
+
|
|
53
|
+
ui.root.append(m);
|
|
54
|
+
const r = c.anchorRect;
|
|
55
|
+
const w = m.offsetWidth, h = m.offsetHeight;
|
|
56
|
+
m.style.left = `${Math.max(4, Math.min(r.left, innerWidth - w - 8))}px`;
|
|
57
|
+
m.style.top = `${r.bottom + h + 8 > innerHeight ? Math.max(4, r.top - h) : r.bottom}px`;
|
|
58
|
+
portal = m;
|
|
59
|
+
return el('span'); // ツリーに渡すのは空の印だけ
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const vendorGuard = (paths) => !paths.some((p) => isVendor(normalize(p)));
|
|
63
|
+
const tree = new FileTree({
|
|
64
|
+
paths: treePaths(),
|
|
65
|
+
initialExpansion: 'open',
|
|
66
|
+
flattenEmptyDirectories: false, // node_modules/dayjs のように1行にまとめない
|
|
67
|
+
search: true,
|
|
68
|
+
renaming: { canRename: ({ path }) => !isVendor(normalize(path)) },
|
|
69
|
+
dragAndDrop: {
|
|
70
|
+
canDrag: vendorGuard,
|
|
71
|
+
canDrop: ({ target }) => !JSON.stringify(target ?? '').includes(NODE_MODULES),
|
|
72
|
+
},
|
|
73
|
+
composition: {
|
|
74
|
+
contextMenu: { enabled: true, triggerMode: 'both', buttonVisibility: 'when-needed', render: menu, onClose: closePortal },
|
|
75
|
+
},
|
|
76
|
+
onSelectionChange: (paths) => { const p = paths[0] && normalize(paths[0]); if (p && files.has(p)) ctx.openFile(p); },
|
|
77
|
+
unsafeCSS: TREE_CSS,
|
|
78
|
+
});
|
|
79
|
+
// 名前の変更もドラッグ&ドロップでの移動も、ツリーの "move" を受けて反映する
|
|
80
|
+
tree.onMutation('move', ({ from, to }) => {
|
|
81
|
+
const f = normalize(from), t = normalize(to);
|
|
82
|
+
const isFolder = from.endsWith('/') || dirs.has(f) || (!files.has(f) && [...files.keys()].some((k) => k.startsWith(f + '/')));
|
|
83
|
+
movePath(f, t, isFolder);
|
|
84
|
+
});
|
|
85
|
+
tree.render({ containerWrapper: ui.treeEl });
|
|
86
|
+
const collapse = (p) => { try { (tree.getItem(p + '/') || tree.getItem(p))?.collapse(); } catch {} };
|
|
87
|
+
collapse('node_modules');
|
|
88
|
+
|
|
89
|
+
// ---- 変更の印(git status の見た目を借りる) ----
|
|
90
|
+
let lastStatus = [];
|
|
91
|
+
let syncTimer;
|
|
92
|
+
function refreshStatus() {
|
|
93
|
+
const set = [];
|
|
94
|
+
for (const p of files.keys()) {
|
|
95
|
+
if (isVendor(p)) continue;
|
|
96
|
+
if (!saved.has(p)) set.push({ path: p, status: 'added' });
|
|
97
|
+
else if (files.get(p) !== saved.get(p)) set.push({ path: p, status: 'modified' });
|
|
98
|
+
}
|
|
99
|
+
if ([...files.keys()].some(isVendor)) set.push({ path: NODE_MODULES, status: 'ignored' });
|
|
100
|
+
const remove = lastStatus.map((s) => s.path).filter((p) => !set.some((s) => s.path === p));
|
|
101
|
+
tree.applyGitStatusPatch({ set, remove });
|
|
102
|
+
lastStatus = set;
|
|
103
|
+
ctx.updateStatus();
|
|
104
|
+
ctx.saveDraft?.();
|
|
105
|
+
clearTimeout(syncTimer); syncTimer = setTimeout(() => workspaceFS.sync(), 400); // 言語サービスに知らせる
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// ---- 操作 ----
|
|
109
|
+
const has = (p) => files.has(p) || dirs.has(p) || [...files.keys()].some((k) => k.startsWith(p + '/'));
|
|
110
|
+
// 入力欄のチェック(空・node_modules・同じ名前)
|
|
111
|
+
const checkPath = (kind) => (v) => {
|
|
112
|
+
const p = normalize(v);
|
|
113
|
+
if (!p) return '名前を入れてください';
|
|
114
|
+
if (isVendor(p)) return 'node_modules の中には作れません(npm install を使ってください)';
|
|
115
|
+
if (/[\\:*?"<>|]/.test(p)) return '使えない文字が入っています';
|
|
116
|
+
if (kind === 'file' && v.endsWith('/')) return null; // 末尾が / ならフォルダとして作る
|
|
117
|
+
if (files.has(p)) return `${p} はもうあります`;
|
|
118
|
+
if (kind === 'folder' && has(p)) return `${p}/ はもうあります`;
|
|
119
|
+
return null;
|
|
120
|
+
};
|
|
121
|
+
// "src/" + "new-file.ts" の、ファイル名の部分だけを選んだ状態で出す
|
|
122
|
+
const nameRange = (prefix, name) => [prefix.length, prefix.length + name.replace(/\.[^.]*$/, '').length];
|
|
123
|
+
async function newFile(dir) {
|
|
124
|
+
const prefix = dir ? `${dir}/` : '';
|
|
125
|
+
const v = await ctx.ask({ title: '新しいファイル', message: 'フォルダは / で区切ります。末尾を / にするとフォルダを作ります', value: prefix + 'new-file.ts', select: nameRange(prefix, 'new-file.ts'), validate: checkPath('file'), ok: '作る' });
|
|
126
|
+
if (v == null) return;
|
|
127
|
+
if (v.endsWith('/')) { makeFolder(normalize(v)); return; }
|
|
128
|
+
const p = normalize(v);
|
|
129
|
+
addFiles([[p, '']]);
|
|
130
|
+
ctx.openFile(p);
|
|
131
|
+
}
|
|
132
|
+
async function newFolder(dir) {
|
|
133
|
+
const prefix = dir ? `${dir}/` : '';
|
|
134
|
+
const v = await ctx.ask({ title: '新しいフォルダ', value: prefix + 'new-folder', select: [prefix.length, prefix.length + 10], validate: checkPath('folder'), ok: '作る' });
|
|
135
|
+
if (v != null) makeFolder(normalize(v));
|
|
136
|
+
}
|
|
137
|
+
function makeFolder(p) {
|
|
138
|
+
if (!p || has(p)) return;
|
|
139
|
+
dirs.add(p);
|
|
140
|
+
tree.add(p + '/');
|
|
141
|
+
refreshStatus();
|
|
142
|
+
log.ok(`${p}/ を作りました`);
|
|
143
|
+
}
|
|
144
|
+
// 画像などをアップロード。1つならパスを、複数なら置き場所のフォルダを聞く
|
|
145
|
+
async function uploadFiles(dir, picked) {
|
|
146
|
+
const list = picked || await new Promise((resolve) => {
|
|
147
|
+
const input = el('input', { type: 'file', multiple: true });
|
|
148
|
+
input.onchange = () => resolve([...input.files]);
|
|
149
|
+
input.click();
|
|
150
|
+
});
|
|
151
|
+
if (!list.length) return;
|
|
152
|
+
const prefix = dir ? `${dir}/` : '';
|
|
153
|
+
let targets;
|
|
154
|
+
if (list.length === 1) {
|
|
155
|
+
const v = await ctx.ask({ title: `${list[0].name} をアップロード`, message: `置く場所(パス)を指定してください(${kb(list[0].size)})`, value: prefix + list[0].name, select: nameRange(prefix, list[0].name), validate: (x) => (normalize(x) ? (isVendor(normalize(x)) ? 'node_modules の中には置けません' : null) : 'パスを入れてください'), ok: 'アップロード' });
|
|
156
|
+
if (v == null) return;
|
|
157
|
+
targets = [[normalize(v), list[0]]];
|
|
158
|
+
} else {
|
|
159
|
+
const v = await ctx.ask({ title: `${list.length} 個のファイルをアップロード`, message: list.map((f) => f.name).join('\n'), value: dir || 'image', validate: (x) => (isVendor(normalize(x)) ? 'node_modules の中には置けません' : null), ok: 'アップロード' });
|
|
160
|
+
if (v == null) return;
|
|
161
|
+
targets = list.map((f) => [normalize(`${normalize(v)}/${f.name}`), f]);
|
|
162
|
+
}
|
|
163
|
+
const exists = targets.filter(([p]) => files.has(p)).map(([p]) => p);
|
|
164
|
+
if (exists.length && !(await ctx.confirm('上書きします', exists.join('\n'), { ok: '上書きする', danger: true }))) return;
|
|
165
|
+
const entries = [];
|
|
166
|
+
for (const [p, f] of targets) entries.push([p, fromBytes(new Uint8Array(await f.arrayBuffer()))]);
|
|
167
|
+
addFiles(entries);
|
|
168
|
+
for (const [p, f] of targets) log.ok(`${p} を追加しました (${kb(f.size)})`);
|
|
169
|
+
if (targets.length === 1) ctx.openFile(targets[0][0]);
|
|
170
|
+
}
|
|
171
|
+
function addFiles(entries, { collapseNew = false } = {}) {
|
|
172
|
+
const added = [];
|
|
173
|
+
for (const [p, text] of entries) {
|
|
174
|
+
if (!files.has(p)) added.push(p);
|
|
175
|
+
files.set(p, text);
|
|
176
|
+
const m = ctx.models?.get(p); if (m && m.getValue() !== text) m.setValue(text);
|
|
177
|
+
}
|
|
178
|
+
if (added.length) tree.batch(added.map((path) => ({ type: 'add', path })));
|
|
179
|
+
for (const d of [...dirs]) if (added.some((p) => p.startsWith(d + '/'))) dirs.delete(d);
|
|
180
|
+
if (collapseNew) collapse('node_modules');
|
|
181
|
+
refreshStatus();
|
|
182
|
+
}
|
|
183
|
+
// ファイルを消す(確認なし。node_modules の入れ替えなどに使う)
|
|
184
|
+
function dropFiles(paths) {
|
|
185
|
+
for (const f of paths) { files.delete(f); ctx.disposeModel?.(f); }
|
|
186
|
+
const gone = paths.filter((p) => !files.has(p));
|
|
187
|
+
if (gone.length) tree.batch(gone.map((path) => ({ type: 'remove', path })));
|
|
188
|
+
if (gone.includes(ctx.current)) ctx.closeEditor();
|
|
189
|
+
refreshStatus();
|
|
190
|
+
}
|
|
191
|
+
function duplicate(p, isDir) {
|
|
192
|
+
const targets = isDir ? [...files.keys()].filter((f) => f.startsWith(p + '/')) : [p];
|
|
193
|
+
const copyName = isDir ? `${p}-copy` : p.replace(/(\.[^./]+)?$/, '-copy$1');
|
|
194
|
+
if (isDir && !targets.length) { makeFolder(copyName); return; }
|
|
195
|
+
addFiles(targets.map((f) => [isDir ? copyName + f.slice(p.length) : copyName, files.get(f)]));
|
|
196
|
+
}
|
|
197
|
+
async function removePath(p, isFolder) {
|
|
198
|
+
const targets = isFolder ? [...files.keys()].filter((f) => f.startsWith(p + '/')) : [p];
|
|
199
|
+
const emptyDirs = isFolder ? [...dirs].filter((d) => d === p || d.startsWith(p + '/')) : [];
|
|
200
|
+
if (!targets.length && !emptyDirs.length) return;
|
|
201
|
+
if (targets.length && !(await ctx.confirm(`${targets.length} 個のファイルを削除します`, `保存するまでレコードには反映されません\n\n${targets.slice(0, 30).join('\n')}${targets.length > 30 ? '\n…' : ''}`, { ok: '削除', danger: true }))) return;
|
|
202
|
+
for (const f of targets) { files.delete(f); ctx.disposeModel(f); }
|
|
203
|
+
for (const d of emptyDirs) dirs.delete(d);
|
|
204
|
+
tree.remove(isFolder ? p + '/' : p, { recursive: isFolder });
|
|
205
|
+
if (targets.includes(ctx.current)) ctx.closeEditor();
|
|
206
|
+
refreshStatus();
|
|
207
|
+
}
|
|
208
|
+
function movePath(from, to, isFolder) {
|
|
209
|
+
if (isFolder) {
|
|
210
|
+
const moves = [...files.keys()].filter((f) => f.startsWith(from + '/')).map((f) => [f, to + f.slice(from.length)]);
|
|
211
|
+
for (const d of [...dirs]) if (d === from || d.startsWith(from + '/')) { dirs.delete(d); dirs.add(to + d.slice(from.length)); }
|
|
212
|
+
for (const [a, b] of moves) moveFile(a, b);
|
|
213
|
+
} else moveFile(from, to);
|
|
214
|
+
refreshStatus();
|
|
215
|
+
}
|
|
216
|
+
function moveFile(a, b) {
|
|
217
|
+
if (!files.has(a)) return;
|
|
218
|
+
files.set(b, files.get(a)); files.delete(a);
|
|
219
|
+
ctx.disposeModel(a);
|
|
220
|
+
if (ctx.current === a) { ctx.current = null; ctx.openFile(b); }
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
Object.assign(ctx, { tree, refreshStatus, newFile, newFolder, makeFolder, uploadFiles, addFiles, dropFiles, removePath, closeMenu: closePortal });
|
|
224
|
+
}
|