@kubohiroya/turbowarp-plus-editor-project-folder 0.13.0 → 0.15.0
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/README.ja.md +31 -2
- package/README.md +17 -2
- package/dist/browser-project-dialog.d.ts +20 -0
- package/dist/browser-projects.d.ts +47 -0
- package/dist/explorer.js +1 -1
- package/dist/external-file-editor.d.ts +7 -0
- package/dist/index.d.ts +25 -2
- package/dist/index.js +704 -19
- package/dist/{shared-BYk-uDpM.js → shared-BlgxbepC.js} +495 -33
- package/dist/text-minimap.d.ts +2 -0
- package/dist/zip.d.ts +9 -0
- package/package.json +1 -1
- package/src/browser-project-dialog.ts +257 -0
- package/src/browser-projects.ts +211 -0
- package/src/external-file-editor.ts +59 -0
- package/src/index.ts +75 -16
- package/src/text-file-editor.ts +282 -35
- package/src/text-minimap.ts +124 -0
- package/src/zip.ts +103 -0
package/src/zip.ts
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Writes a folder as a ZIP archive without compression, so a project folder kept in the browser can be saved as one
|
|
3
|
+
* file. Entries use UTF-8 names. The archive holds at most 65535 entries and 4 GiB, which a project folder stays under.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
type IterableDirectory = FileSystemDirectoryHandle & {values(): AsyncIterable<FileSystemHandle>};
|
|
7
|
+
|
|
8
|
+
const crcTable = (() => {
|
|
9
|
+
const table = new Uint32Array(256);
|
|
10
|
+
for (let n = 0; n < 256; n++) {
|
|
11
|
+
let c = n;
|
|
12
|
+
for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
|
|
13
|
+
table[n] = c >>> 0;
|
|
14
|
+
}
|
|
15
|
+
return table;
|
|
16
|
+
})();
|
|
17
|
+
|
|
18
|
+
export function crc32(bytes: Uint8Array) {
|
|
19
|
+
let crc = 0xffffffff;
|
|
20
|
+
for (let i = 0; i < bytes.length; i++) crc = crcTable[(crc ^ bytes[i]) & 0xff] ^ (crc >>> 8);
|
|
21
|
+
return (crc ^ 0xffffffff) >>> 0;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function dosTime(time: number) {
|
|
25
|
+
const date = new Date(Math.max(time, Date.UTC(1980, 0, 1)));
|
|
26
|
+
return {
|
|
27
|
+
time: (date.getHours() << 11) | (date.getMinutes() << 5) | Math.floor(date.getSeconds() / 2),
|
|
28
|
+
date: ((date.getFullYear() - 1980) << 9) | ((date.getMonth() + 1) << 5) | date.getDate()
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
type Entry = {name: string; data: Uint8Array; modified: number; directory: boolean};
|
|
33
|
+
|
|
34
|
+
async function collect(directory: FileSystemDirectoryHandle, prefix: string, exclude: (path: string) => boolean, out: Entry[]) {
|
|
35
|
+
const children: FileSystemHandle[] = [];
|
|
36
|
+
for await (const child of (directory as IterableDirectory).values()) children.push(child);
|
|
37
|
+
children.sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
|
|
38
|
+
if (prefix && !children.length) out.push({name: prefix, data: new Uint8Array(), modified: Date.now(), directory: true});
|
|
39
|
+
for (const child of children) {
|
|
40
|
+
const path = prefix + child.name;
|
|
41
|
+
if (exclude(path)) continue;
|
|
42
|
+
if (child.kind === 'directory') {
|
|
43
|
+
await collect(child as FileSystemDirectoryHandle, `${path}/`, exclude, out);
|
|
44
|
+
} else {
|
|
45
|
+
const file = await (child as FileSystemFileHandle).getFile();
|
|
46
|
+
out.push({name: path, data: new Uint8Array(await file.arrayBuffer()), modified: file.lastModified, directory: false});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** The files under `directory` as a ZIP. `exclude` receives paths relative to `directory`, such as `.git`. */
|
|
52
|
+
export async function zipDirectory(directory: FileSystemDirectoryHandle, {exclude = () => false}: {
|
|
53
|
+
exclude?: (path: string) => boolean;
|
|
54
|
+
} = {}) {
|
|
55
|
+
const entries: Entry[] = [];
|
|
56
|
+
await collect(directory, '', exclude, entries);
|
|
57
|
+
if (entries.length > 0xffff) throw new RangeError('Too many files for a ZIP archive');
|
|
58
|
+
const encoder = new TextEncoder();
|
|
59
|
+
const parts: Uint8Array[] = [];
|
|
60
|
+
const central: Uint8Array[] = [];
|
|
61
|
+
let offset = 0;
|
|
62
|
+
for (const entry of entries) {
|
|
63
|
+
const name = encoder.encode(entry.name);
|
|
64
|
+
const crc = crc32(entry.data);
|
|
65
|
+
const {time, date} = dosTime(entry.modified);
|
|
66
|
+
const local = new DataView(new ArrayBuffer(30));
|
|
67
|
+
local.setUint32(0, 0x04034b50, true);
|
|
68
|
+
local.setUint16(4, 20, true);
|
|
69
|
+
local.setUint16(6, 0x0800, true); // UTF-8 names
|
|
70
|
+
local.setUint16(8, 0, true); // stored
|
|
71
|
+
local.setUint16(10, time, true);
|
|
72
|
+
local.setUint16(12, date, true);
|
|
73
|
+
local.setUint32(14, crc, true);
|
|
74
|
+
local.setUint32(18, entry.data.length, true);
|
|
75
|
+
local.setUint32(22, entry.data.length, true);
|
|
76
|
+
local.setUint16(26, name.length, true);
|
|
77
|
+
const header = new DataView(new ArrayBuffer(46));
|
|
78
|
+
header.setUint32(0, 0x02014b50, true);
|
|
79
|
+
header.setUint16(4, 20, true);
|
|
80
|
+
header.setUint16(6, 20, true);
|
|
81
|
+
header.setUint16(8, 0x0800, true);
|
|
82
|
+
header.setUint16(12, time, true);
|
|
83
|
+
header.setUint16(14, date, true);
|
|
84
|
+
header.setUint32(16, crc, true);
|
|
85
|
+
header.setUint32(20, entry.data.length, true);
|
|
86
|
+
header.setUint32(24, entry.data.length, true);
|
|
87
|
+
header.setUint16(28, name.length, true);
|
|
88
|
+
header.setUint32(38, entry.directory ? 0x10 : 0, true);
|
|
89
|
+
header.setUint32(42, offset, true);
|
|
90
|
+
parts.push(new Uint8Array(local.buffer), name, entry.data);
|
|
91
|
+
central.push(new Uint8Array(header.buffer), name);
|
|
92
|
+
offset += 30 + name.length + entry.data.length;
|
|
93
|
+
if (offset > 0xffffffff) throw new RangeError('The folder is too large for a ZIP archive');
|
|
94
|
+
}
|
|
95
|
+
const size = central.reduce((total, part) => total + part.length, 0);
|
|
96
|
+
const end = new DataView(new ArrayBuffer(22));
|
|
97
|
+
end.setUint32(0, 0x06054b50, true);
|
|
98
|
+
end.setUint16(8, entries.length, true);
|
|
99
|
+
end.setUint16(10, entries.length, true);
|
|
100
|
+
end.setUint32(12, size, true);
|
|
101
|
+
end.setUint32(16, offset, true);
|
|
102
|
+
return new Blob([...parts, ...central, new Uint8Array(end.buffer)] as BlobPart[], {type: 'application/zip'});
|
|
103
|
+
}
|