@kubohiroya/turbowarp-plus-editor-project-folder 0.7.0 → 0.9.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 +5 -1
- package/README.md +4 -0
- package/dist/THIRD_PARTY_NOTICES.md +716 -0
- package/dist/create-folder.d.ts +8 -0
- package/dist/explorer-files.d.ts +15 -0
- package/dist/explorer-menu.d.ts +49 -0
- package/dist/explorer.d.ts +46 -1
- package/dist/explorer.js +1911 -672
- package/dist/file-preview.d.ts +24 -0
- package/dist/folder-registry.d.ts +17 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +1028 -541
- package/dist/session.d.ts +10 -1
- package/dist/shared-JdaDvk2R.js +588 -0
- package/dist/shared-kI7qYraF.js +30130 -0
- package/dist/source.d.ts +4 -2
- package/dist/text-file-editor.d.ts +17 -0
- package/package.json +16 -1
- package/src/create-folder.ts +99 -0
- package/src/explorer-files.ts +167 -0
- package/src/explorer-menu.tsx +125 -0
- package/src/explorer.tsx +351 -16
- package/src/file-preview.ts +134 -0
- package/src/folder-registry.ts +50 -0
- package/src/index.ts +42 -2
- package/src/session.ts +105 -20
- package/src/source.ts +42 -38
- package/src/text-file-editor.ts +356 -0
- package/vite.config.ts +2 -0
- package/dist/shared-DLcytVOx.js +0 -445
package/dist/index.js
CHANGED
|
@@ -1,16 +1,23 @@
|
|
|
1
|
-
import { _ as
|
|
1
|
+
import { C as _asyncIterator, S as _objectSpread2, _ as readFolder, a as assetKey, b as unsupported, c as extensionsFile, d as manifestFile, f as parseProject, g as projectFile, h as projectChanges, i as assertFolderUnchanged, l as fileVersion, m as projectAssets, n as registerFolder, o as assetTypeFor, p as planExtensionSave, r as FolderError, s as equal, t as followProjectFolder, u as isObject, v as readFolderUpdate, x as _objectWithoutProperties, y as sameBytes } from "./shared-JdaDvk2R.js";
|
|
2
2
|
// src/block-reload.ts
|
|
3
|
-
var
|
|
3
|
+
var _excluded$1 = ["targets"];
|
|
4
|
+
var _excluded2$1 = ["targets"];
|
|
5
|
+
var _excluded3$1 = ["blocks"];
|
|
6
|
+
var _excluded4$1 = ["blocks"];
|
|
7
|
+
var _excluded5 = [
|
|
4
8
|
"inputs",
|
|
5
9
|
"fields",
|
|
6
10
|
"x",
|
|
7
11
|
"y"
|
|
8
|
-
]
|
|
12
|
+
];
|
|
13
|
+
var _excluded6 = [
|
|
9
14
|
"inputs",
|
|
10
15
|
"fields",
|
|
11
16
|
"x",
|
|
12
17
|
"y"
|
|
13
|
-
]
|
|
18
|
+
];
|
|
19
|
+
var isValue = (value) => typeof value === "string" || typeof value === "boolean" || typeof value === "number" && Number.isFinite(value);
|
|
20
|
+
var literals = {
|
|
14
21
|
4: ["math_number", "NUM"],
|
|
15
22
|
5: ["math_positive_number", "NUM"],
|
|
16
23
|
6: ["math_whole_number", "NUM"],
|
|
@@ -18,126 +25,151 @@ var b = ["targets"], x = ["targets"], S = ["blocks"], C = ["blocks"], w = [
|
|
|
18
25
|
8: ["math_angle", "NUM"],
|
|
19
26
|
9: ["colour_picker", "COLOUR"],
|
|
20
27
|
10: ["text", "TEXT"]
|
|
21
|
-
}, O = () => v("Only values and coordinates of existing blocks can be reloaded"), k = (e, n) => t(Object.keys(e), Object.keys(n)), A = (e, t) => typeof e != "number" || typeof t != "number" || !Number.isSafeInteger(e) || !Number.isSafeInteger(t) ? O() : {
|
|
22
|
-
x: e,
|
|
23
|
-
y: t
|
|
24
28
|
};
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
29
|
+
var reject$1 = () => unsupported("Only values and coordinates of existing blocks can be reloaded");
|
|
30
|
+
var keysMatch = (before, after) => equal(Object.keys(before), Object.keys(after));
|
|
31
|
+
var point = (x, y) => {
|
|
32
|
+
if (typeof x !== "number" || typeof y !== "number" || !Number.isSafeInteger(x) || !Number.isSafeInteger(y)) return reject$1();
|
|
33
|
+
return {
|
|
34
|
+
x,
|
|
35
|
+
y
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
function planBlockReload(before, after) {
|
|
39
|
+
const { targets: oldTargets } = before, oldProject = _objectWithoutProperties(before, _excluded$1);
|
|
40
|
+
const { targets: newTargets } = after, newProject = _objectWithoutProperties(after, _excluded2$1);
|
|
41
|
+
if (oldTargets.length !== newTargets.length || !equal(oldProject, newProject)) return reject$1();
|
|
42
|
+
const edits = [];
|
|
43
|
+
for (const [index, oldTarget] of oldTargets.entries()) {
|
|
44
|
+
const { blocks: oldBlocks } = oldTarget, oldRest = _objectWithoutProperties(oldTarget, _excluded3$1);
|
|
45
|
+
const _newTargets$index = newTargets[index], { blocks: newBlocks } = _newTargets$index, newRest = _objectWithoutProperties(_newTargets$index, _excluded4$1);
|
|
46
|
+
if (!equal(oldRest, newRest)) return reject$1();
|
|
47
|
+
if (!isObject(oldBlocks) || !isObject(newBlocks)) {
|
|
48
|
+
if (equal(oldBlocks, newBlocks)) continue;
|
|
49
|
+
return reject$1();
|
|
35
50
|
}
|
|
36
|
-
if (!
|
|
37
|
-
for (
|
|
38
|
-
|
|
39
|
-
if (
|
|
40
|
-
if (!
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
51
|
+
if (!keysMatch(oldBlocks, newBlocks)) return reject$1();
|
|
52
|
+
for (const [id, oldBlock] of Object.entries(oldBlocks)) {
|
|
53
|
+
const newBlock = newBlocks[id];
|
|
54
|
+
if (equal(oldBlock, newBlock)) continue;
|
|
55
|
+
if (!isObject(oldBlock) || !isObject(newBlock)) return reject$1();
|
|
56
|
+
const { inputs: oldInputs, fields: oldFields, x: oldX, y: oldY } = oldBlock, oldShape = _objectWithoutProperties(oldBlock, _excluded5);
|
|
57
|
+
const { inputs: newInputs, fields: newFields, x: newX, y: newY } = newBlock, newShape = _objectWithoutProperties(newBlock, _excluded6);
|
|
58
|
+
if (!equal(oldShape, newShape) || typeof oldShape.opcode !== "string") return reject$1();
|
|
59
|
+
const common = {
|
|
60
|
+
id,
|
|
61
|
+
target: oldTarget.name,
|
|
62
|
+
stage: oldTarget.isStage,
|
|
63
|
+
opcode: oldShape.opcode
|
|
48
64
|
};
|
|
49
|
-
if (
|
|
50
|
-
if (
|
|
51
|
-
|
|
65
|
+
if (oldX !== newX || oldY !== newY) {
|
|
66
|
+
if (oldShape.topLevel !== true) return reject$1();
|
|
67
|
+
edits.push(_objectSpread2(_objectSpread2({}, common), {}, {
|
|
52
68
|
kind: "position",
|
|
53
|
-
before:
|
|
54
|
-
after:
|
|
69
|
+
before: point(oldX, oldY),
|
|
70
|
+
after: point(newX, newY)
|
|
55
71
|
}));
|
|
56
72
|
}
|
|
57
|
-
if (!
|
|
58
|
-
if (!
|
|
59
|
-
for (
|
|
60
|
-
|
|
61
|
-
if (
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
}
|
|
73
|
+
if (!equal(oldFields, newFields)) {
|
|
74
|
+
if (!isObject(oldFields) || !isObject(newFields) || !keysMatch(oldFields, newFields)) return reject$1();
|
|
75
|
+
for (const [name, a] of Object.entries(oldFields)) {
|
|
76
|
+
const b = newFields[name];
|
|
77
|
+
if (equal(a, b)) continue;
|
|
78
|
+
if ([
|
|
79
|
+
"VARIABLE",
|
|
80
|
+
"LIST",
|
|
81
|
+
"BROADCAST_OPTION"
|
|
82
|
+
].includes(name) || oldShape.opcode === "sensing_of_object_menu" || !Array.isArray(a) || !Array.isArray(b) || a.length !== 1 || b.length !== 1 || !isValue(a[0]) || !isValue(b[0])) return reject$1();
|
|
83
|
+
edits.push(_objectSpread2(_objectSpread2({}, common), {}, {
|
|
84
|
+
kind: "field",
|
|
85
|
+
name,
|
|
86
|
+
before: a[0],
|
|
87
|
+
after: b[0]
|
|
88
|
+
}));
|
|
74
89
|
}
|
|
75
90
|
}
|
|
76
|
-
if (!
|
|
77
|
-
if (!
|
|
78
|
-
for (
|
|
79
|
-
|
|
80
|
-
if (
|
|
81
|
-
if (!Array.isArray(
|
|
82
|
-
|
|
83
|
-
if (
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
91
|
+
if (!equal(oldInputs, newInputs)) {
|
|
92
|
+
if (!isObject(oldInputs) || !isObject(newInputs) || !keysMatch(oldInputs, newInputs)) return reject$1();
|
|
93
|
+
for (const [name, a] of Object.entries(oldInputs)) {
|
|
94
|
+
const b = newInputs[name];
|
|
95
|
+
if (equal(a, b)) continue;
|
|
96
|
+
if (!Array.isArray(a) || !Array.isArray(b) || a[0] !== b[0]) return reject$1();
|
|
97
|
+
const slot = a[0] === 1 ? 1 : a[0] === 3 && typeof a[1] === "string" && a[1] === b[1] ? 2 : null;
|
|
98
|
+
if (slot === null || a.length !== slot + 1 || b.length !== slot + 1) return reject$1();
|
|
99
|
+
const from = a[slot];
|
|
100
|
+
const to = b[slot];
|
|
101
|
+
if (!Array.isArray(from) || !Array.isArray(to) || from.length !== 2 || to.length !== 2 || typeof from[0] !== "number" || !Object.hasOwn(literals, from[0]) || from[0] !== to[0] || !isValue(from[1]) || !isValue(to[1])) return reject$1();
|
|
102
|
+
const connected = slot === 2 ? a[1] : null;
|
|
103
|
+
edits.push(_objectSpread2(_objectSpread2({}, common), {}, {
|
|
88
104
|
kind: "input",
|
|
89
|
-
name
|
|
90
|
-
connected
|
|
91
|
-
tag:
|
|
92
|
-
before:
|
|
93
|
-
after:
|
|
105
|
+
name,
|
|
106
|
+
connected,
|
|
107
|
+
tag: from[0],
|
|
108
|
+
before: from[1],
|
|
109
|
+
after: to[1]
|
|
94
110
|
}));
|
|
95
111
|
}
|
|
96
112
|
}
|
|
97
113
|
}
|
|
98
114
|
}
|
|
99
|
-
return
|
|
115
|
+
return edits;
|
|
100
116
|
}
|
|
101
|
-
function
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
117
|
+
function applyBlockReload(targets, edits) {
|
|
118
|
+
const touched = /* @__PURE__ */ new Set();
|
|
119
|
+
const operations = edits.map((edit) => {
|
|
120
|
+
const matching = targets.filter((target) => target.isStage === edit.stage && (edit.stage || target.isOriginal && target.getName() === edit.target));
|
|
121
|
+
if (matching.length !== 1) return reject$1();
|
|
122
|
+
const target = matching[0];
|
|
123
|
+
const container = target.blocks;
|
|
124
|
+
const parent = container.getBlock(edit.id);
|
|
125
|
+
if (!parent || parent.opcode !== edit.opcode) return reject$1();
|
|
126
|
+
touched.add(target);
|
|
127
|
+
if (edit.kind === "position") {
|
|
128
|
+
var _parent$x, _parent$y;
|
|
129
|
+
if (!parent.topLevel || Math.round((_parent$x = parent.x) !== null && _parent$x !== void 0 ? _parent$x : 0) !== edit.before.x || Math.round((_parent$y = parent.y) !== null && _parent$y !== void 0 ? _parent$y : 0) !== edit.before.y) return reject$1();
|
|
130
|
+
return (direction) => container.moveBlock({
|
|
131
|
+
id: edit.id,
|
|
132
|
+
newCoordinate: edit[direction]
|
|
112
133
|
});
|
|
113
134
|
}
|
|
114
|
-
let
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
if (!
|
|
121
|
-
|
|
135
|
+
let block = parent;
|
|
136
|
+
let name = edit.name;
|
|
137
|
+
if (edit.kind === "input") {
|
|
138
|
+
var _edit$connected;
|
|
139
|
+
const input = parent.inputs[edit.name];
|
|
140
|
+
const shadow = input === null || input === void 0 ? void 0 : input.shadow;
|
|
141
|
+
if (!shadow || input.block !== ((_edit$connected = edit.connected) !== null && _edit$connected !== void 0 ? _edit$connected : shadow)) return reject$1();
|
|
142
|
+
const literal = container.getBlock(shadow);
|
|
143
|
+
const [opcode, field] = literals[edit.tag];
|
|
144
|
+
if (!(literal === null || literal === void 0 ? void 0 : literal.shadow) || literal.parent !== parent.id || literal.opcode !== opcode) return reject$1();
|
|
145
|
+
block = literal;
|
|
146
|
+
name = field;
|
|
122
147
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
148
|
+
const field = block.fields[name];
|
|
149
|
+
if (!field || field.id != null || field.value !== edit.before || block.isMonitored || parent.isMonitored) return reject$1();
|
|
150
|
+
return (direction) => container.changeBlock({
|
|
151
|
+
id: block.id,
|
|
126
152
|
element: "field",
|
|
127
|
-
name
|
|
128
|
-
value:
|
|
153
|
+
name,
|
|
154
|
+
value: edit[direction]
|
|
129
155
|
});
|
|
130
|
-
})
|
|
156
|
+
});
|
|
157
|
+
let index = 0;
|
|
131
158
|
try {
|
|
132
|
-
for (;
|
|
133
|
-
} catch (
|
|
134
|
-
for (;
|
|
135
|
-
throw
|
|
159
|
+
for (; index < operations.length; index++) operations[index]("after");
|
|
160
|
+
} catch (error) {
|
|
161
|
+
for (; index >= 0; index--) operations[index]("before");
|
|
162
|
+
throw error;
|
|
136
163
|
}
|
|
137
|
-
return [...
|
|
164
|
+
return [...touched];
|
|
138
165
|
}
|
|
139
166
|
// src/script-reload.ts
|
|
140
|
-
var
|
|
167
|
+
var _excluded = ["targets"];
|
|
168
|
+
var _excluded2 = ["targets"];
|
|
169
|
+
var _excluded3 = ["blocks"];
|
|
170
|
+
var _excluded4 = ["blocks"];
|
|
171
|
+
var reject = (detail = "Only changes to the scripts of existing targets can be reloaded in place") => unsupported(detail);
|
|
172
|
+
var corePrefixes = /* @__PURE__ */ new Set([
|
|
141
173
|
"motion",
|
|
142
174
|
"looks",
|
|
143
175
|
"sound",
|
|
@@ -151,441 +183,782 @@ var N = ["targets"], P = ["targets"], F = ["blocks"], I = ["blocks"], L = (e = "
|
|
|
151
183
|
"math",
|
|
152
184
|
"text",
|
|
153
185
|
"colour"
|
|
154
|
-
])
|
|
186
|
+
]);
|
|
187
|
+
var prefix = (opcode) => opcode.split("_", 1)[0];
|
|
188
|
+
var variableFields = /* @__PURE__ */ new Set([
|
|
155
189
|
"VARIABLE",
|
|
156
190
|
"LIST",
|
|
157
191
|
"BROADCAST_OPTION"
|
|
158
|
-
])
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
192
|
+
]);
|
|
193
|
+
var parentOf = (block) => isObject(block) && typeof block.parent === "string" ? block.parent : null;
|
|
194
|
+
function rootsOf(blocks) {
|
|
195
|
+
const roots = /* @__PURE__ */ new Map();
|
|
196
|
+
for (const id of Object.keys(blocks)) {
|
|
197
|
+
let current = id;
|
|
198
|
+
const seen = /* @__PURE__ */ new Set();
|
|
163
199
|
for (;;) {
|
|
164
|
-
if (
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
if (
|
|
168
|
-
if (!Object.hasOwn(
|
|
169
|
-
|
|
200
|
+
if (seen.has(current)) return reject("Blocks form a cycle");
|
|
201
|
+
seen.add(current);
|
|
202
|
+
const parent = parentOf(blocks[current]);
|
|
203
|
+
if (parent === null) break;
|
|
204
|
+
if (!Object.hasOwn(blocks, parent)) return reject("A block refers to a missing parent");
|
|
205
|
+
current = parent;
|
|
170
206
|
}
|
|
171
|
-
|
|
207
|
+
roots.set(id, current);
|
|
172
208
|
}
|
|
173
|
-
return
|
|
209
|
+
return roots;
|
|
174
210
|
}
|
|
175
|
-
function
|
|
176
|
-
for (
|
|
211
|
+
function opcodesOf(blocks, into) {
|
|
212
|
+
for (const block of Object.values(blocks)) if (isObject(block) && typeof block.opcode === "string") into.add(prefix(block.opcode));
|
|
177
213
|
}
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
214
|
+
/**
|
|
215
|
+
* Plans script replacements for changes that add, remove or reconnect blocks. Every other property of the
|
|
216
|
+
* project and its targets, including comments, must stay the same; otherwise the whole project is reloaded.
|
|
217
|
+
*/
|
|
218
|
+
function planScriptReload(before, after) {
|
|
219
|
+
const { targets: oldTargets } = before, oldProject = _objectWithoutProperties(before, _excluded);
|
|
220
|
+
const { targets: newTargets } = after, newProject = _objectWithoutProperties(after, _excluded2);
|
|
221
|
+
if (oldTargets.length !== newTargets.length || !equal(oldProject, newProject)) return reject();
|
|
222
|
+
const known = new Set(corePrefixes);
|
|
223
|
+
for (const target of oldTargets) if (isObject(target.blocks)) opcodesOf(target.blocks, known);
|
|
224
|
+
const edits = [];
|
|
225
|
+
for (const [index, oldTarget] of oldTargets.entries()) {
|
|
226
|
+
const { blocks: oldValue } = oldTarget, oldRest = _objectWithoutProperties(oldTarget, _excluded3);
|
|
227
|
+
const _newTargets$index = newTargets[index], { blocks: newValue } = _newTargets$index, newRest = _objectWithoutProperties(_newTargets$index, _excluded4);
|
|
228
|
+
if (!equal(oldRest, newRest)) return reject();
|
|
229
|
+
if (equal(oldValue, newValue)) continue;
|
|
230
|
+
const oldBlocks = isObject(oldValue) ? oldValue : {};
|
|
231
|
+
const newBlocks = isObject(newValue) ? newValue : {};
|
|
232
|
+
const oldRoots = rootsOf(oldBlocks);
|
|
233
|
+
const newRoots = rootsOf(newBlocks);
|
|
234
|
+
const removed = /* @__PURE__ */ new Set();
|
|
235
|
+
const added = /* @__PURE__ */ new Set();
|
|
236
|
+
const mark = (id) => {
|
|
237
|
+
const oldRoot = oldRoots.get(id);
|
|
238
|
+
const newRoot = newRoots.get(id);
|
|
239
|
+
let grew = false;
|
|
240
|
+
if (oldRoot !== void 0 && !removed.has(oldRoot)) {
|
|
241
|
+
removed.add(oldRoot);
|
|
242
|
+
grew = true;
|
|
243
|
+
}
|
|
244
|
+
if (newRoot !== void 0 && !added.has(newRoot)) {
|
|
245
|
+
added.add(newRoot);
|
|
246
|
+
grew = true;
|
|
247
|
+
}
|
|
248
|
+
return grew;
|
|
191
249
|
};
|
|
192
|
-
for (
|
|
193
|
-
for (let
|
|
194
|
-
|
|
195
|
-
for (
|
|
196
|
-
for (
|
|
250
|
+
for (const id of /* @__PURE__ */ new Set([...Object.keys(oldBlocks), ...Object.keys(newBlocks)])) if (!equal(oldBlocks[id], newBlocks[id])) mark(id);
|
|
251
|
+
for (let grew = true; grew;) {
|
|
252
|
+
grew = false;
|
|
253
|
+
for (const [id, root] of oldRoots) if (removed.has(root) && mark(id)) grew = true;
|
|
254
|
+
for (const [id, root] of newRoots) if (added.has(root) && mark(id)) grew = true;
|
|
197
255
|
}
|
|
198
|
-
|
|
199
|
-
for (
|
|
200
|
-
for (
|
|
201
|
-
if (
|
|
202
|
-
if (
|
|
256
|
+
const blocks = {};
|
|
257
|
+
for (const [id, root] of newRoots) if (added.has(root)) blocks[id] = newBlocks[id];
|
|
258
|
+
for (const block of Object.values(blocks)) {
|
|
259
|
+
if (!isObject(block)) continue;
|
|
260
|
+
if (typeof block.opcode !== "string") return reject("A block has no opcode");
|
|
261
|
+
if (!known.has(prefix(block.opcode))) return reject(`Blocks need an extension the project does not use: ${block.opcode}`);
|
|
203
262
|
}
|
|
204
|
-
|
|
205
|
-
target:
|
|
206
|
-
stage:
|
|
207
|
-
remove: [...
|
|
208
|
-
blocks
|
|
263
|
+
edits.push({
|
|
264
|
+
target: oldTarget.name,
|
|
265
|
+
stage: oldTarget.isStage,
|
|
266
|
+
remove: [...removed],
|
|
267
|
+
blocks
|
|
209
268
|
});
|
|
210
269
|
}
|
|
211
|
-
return
|
|
270
|
+
return edits;
|
|
212
271
|
}
|
|
213
|
-
var
|
|
214
|
-
|
|
215
|
-
return
|
|
272
|
+
var resolveTarget = (targets, edit) => {
|
|
273
|
+
const matching = targets.filter((target) => target.isStage === edit.stage && (edit.stage || target.isOriginal && target.getName() === edit.target));
|
|
274
|
+
return matching.length === 1 ? matching[0] : reject();
|
|
216
275
|
};
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
276
|
+
/**
|
|
277
|
+
* Applies planned script replacements. Every edit is checked against the VM before any change. If a change
|
|
278
|
+
* fails part way, every target is restored to its previous blocks.
|
|
279
|
+
*/
|
|
280
|
+
function applyScriptReload(targets, edits, deserialize) {
|
|
281
|
+
const prepared = edits.map((edit) => {
|
|
282
|
+
const target = resolveTarget(targets, edit);
|
|
283
|
+
const container = target.blocks;
|
|
284
|
+
for (const id of edit.remove) {
|
|
285
|
+
const block = container.getBlock(id);
|
|
286
|
+
if (!block || !block.topLevel || !container._scripts.includes(id)) return reject("The editor and the folder differ");
|
|
223
287
|
}
|
|
224
|
-
|
|
225
|
-
for (
|
|
226
|
-
var
|
|
227
|
-
if (typeof
|
|
228
|
-
if (
|
|
229
|
-
for (
|
|
230
|
-
var
|
|
231
|
-
if (
|
|
288
|
+
const created = Object.values(deserialize(JSON.parse(JSON.stringify(edit.blocks))));
|
|
289
|
+
for (const block of created) {
|
|
290
|
+
var _block$fields;
|
|
291
|
+
if (typeof block.id !== "string" || typeof block.opcode !== "string") return reject("A block could not be read");
|
|
292
|
+
if (block.comment && !(target.comments && Object.hasOwn(target.comments, block.comment))) return reject("A block refers to a missing comment");
|
|
293
|
+
for (const [name, field] of Object.entries((_block$fields = block.fields) !== null && _block$fields !== void 0 ? _block$fields : {})) {
|
|
294
|
+
var _target$lookupVariabl;
|
|
295
|
+
if (!variableFields.has(name) && !field.variableType) continue;
|
|
296
|
+
if (typeof field.id !== "string" || !((_target$lookupVariabl = target.lookupVariableById) === null || _target$lookupVariabl === void 0 ? void 0 : _target$lookupVariabl.call(target, field.id))) return reject("A block refers to a variable, list or message the project does not have");
|
|
232
297
|
}
|
|
233
298
|
}
|
|
234
299
|
return {
|
|
235
|
-
target
|
|
236
|
-
container
|
|
237
|
-
edit
|
|
238
|
-
created
|
|
300
|
+
target,
|
|
301
|
+
container,
|
|
302
|
+
edit,
|
|
303
|
+
created
|
|
239
304
|
};
|
|
240
|
-
})
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
305
|
+
});
|
|
306
|
+
const saved = prepared.map(({ container }) => ({
|
|
307
|
+
container,
|
|
308
|
+
blocks: _objectSpread2({}, container._blocks),
|
|
309
|
+
scripts: [...container._scripts],
|
|
310
|
+
tops: new Map(Object.entries(container._blocks).map(([id, block]) => [id, block.topLevel]))
|
|
245
311
|
}));
|
|
246
312
|
try {
|
|
247
|
-
for (
|
|
248
|
-
for (
|
|
249
|
-
for (
|
|
250
|
-
|
|
313
|
+
for (const { container, edit, created } of prepared) {
|
|
314
|
+
for (const id of edit.remove) container.deleteBlock(id);
|
|
315
|
+
for (const block of created) {
|
|
316
|
+
container.createBlock(block, true);
|
|
317
|
+
if (container._blocks[block.id] !== block) reject("A block id is already in use");
|
|
318
|
+
}
|
|
319
|
+
container.resetCache();
|
|
320
|
+
}
|
|
321
|
+
} catch (error) {
|
|
322
|
+
for (const { container, blocks, scripts, tops } of saved) {
|
|
323
|
+
container._blocks = blocks;
|
|
324
|
+
container._scripts = scripts;
|
|
325
|
+
for (const [id, top] of tops) blocks[id].topLevel = top;
|
|
326
|
+
container.resetCache();
|
|
251
327
|
}
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
328
|
+
throw error;
|
|
329
|
+
}
|
|
330
|
+
return prepared.map(({ target }) => target);
|
|
331
|
+
}
|
|
332
|
+
// src/create-folder.ts
|
|
333
|
+
var manifests = /* @__PURE__ */ new Set([
|
|
334
|
+
projectFile,
|
|
335
|
+
manifestFile,
|
|
336
|
+
extensionsFile
|
|
337
|
+
]);
|
|
338
|
+
var sourcePath = /^(?:assets\/[a-f0-9]{32}\.[A-Za-z0-9]+|extensions\/[A-Za-z0-9._-]+\.(?:js|manifest\.json))$/u;
|
|
339
|
+
function missing(error) {
|
|
340
|
+
return error instanceof DOMException && error.name === "NotFoundError";
|
|
341
|
+
}
|
|
342
|
+
async function assertEmpty(directory) {
|
|
343
|
+
var _iteratorAbruptCompletion = false;
|
|
344
|
+
var _didIteratorError = false;
|
|
345
|
+
var _iteratorError;
|
|
346
|
+
try {
|
|
347
|
+
for (var _iterator = _asyncIterator(directory.values()), _step; _iteratorAbruptCompletion = !(_step = await _iterator.next()).done; _iteratorAbruptCompletion = false) {
|
|
348
|
+
_step.value;
|
|
349
|
+
throw new FolderError("conflict", "Choose an empty project folder");
|
|
350
|
+
}
|
|
351
|
+
} catch (err) {
|
|
352
|
+
_didIteratorError = true;
|
|
353
|
+
_iteratorError = err;
|
|
354
|
+
} finally {
|
|
355
|
+
try {
|
|
356
|
+
if (_iteratorAbruptCompletion && _iterator.return != null) await _iterator.return();
|
|
357
|
+
} finally {
|
|
358
|
+
if (_didIteratorError) throw _iteratorError;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
/** Create and validate a project-folder snapshot in a directory chosen by the user. */
|
|
363
|
+
async function createProjectFolder({ directory, files, storage, current = () => true }) {
|
|
364
|
+
for (const filename of manifests) if (!files.has(filename)) return unsupported(`Missing ${filename}`);
|
|
365
|
+
for (const [filename, bytes] of files) if (!manifests.has(filename) && !sourcePath.test(filename) || !(bytes instanceof Uint8Array)) return unsupported(`Invalid project folder entry: ${filename}`);
|
|
366
|
+
const assertCurrent = () => {
|
|
367
|
+
if (!current()) throw new FolderError("conflict", "The project changed before the folder was saved");
|
|
368
|
+
};
|
|
369
|
+
await assertEmpty(directory);
|
|
370
|
+
const directories = /* @__PURE__ */ new Map();
|
|
371
|
+
const undo = [];
|
|
372
|
+
try {
|
|
373
|
+
const entries = [...files].sort(([a], [b]) => Number(manifests.has(a)) - Number(manifests.has(b)) || Number(a === "project.source.json") - Number(b === "project.source.json"));
|
|
374
|
+
for (const [filename, bytes] of entries) {
|
|
375
|
+
assertCurrent();
|
|
376
|
+
const [first, basename] = filename.split("/");
|
|
377
|
+
let parent = directory;
|
|
378
|
+
if (basename !== void 0) {
|
|
379
|
+
const existing = directories.get(first);
|
|
380
|
+
if (existing) parent = existing;
|
|
381
|
+
else {
|
|
382
|
+
if (await directory.getDirectoryHandle(first).catch((error) => {
|
|
383
|
+
if (missing(error)) return null;
|
|
384
|
+
throw error;
|
|
385
|
+
})) throw new FolderError("conflict");
|
|
386
|
+
parent = await directory.getDirectoryHandle(first, { create: true });
|
|
387
|
+
directories.set(first, parent);
|
|
388
|
+
undo.push(async () => {
|
|
389
|
+
if (await (await directory.getDirectoryHandle(first)).isSameEntry(parent)) await directory.removeEntry(first);
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
const name = basename !== null && basename !== void 0 ? basename : first;
|
|
394
|
+
if (await parent.getFileHandle(name).catch((error) => {
|
|
395
|
+
if (missing(error)) return null;
|
|
396
|
+
throw error;
|
|
397
|
+
})) throw new FolderError("conflict");
|
|
398
|
+
const file = await parent.getFileHandle(name, { create: true });
|
|
399
|
+
const initial = await file.getFile();
|
|
400
|
+
if (initial.size !== 0) throw new FolderError("conflict");
|
|
401
|
+
let expected = /* @__PURE__ */ new Uint8Array();
|
|
402
|
+
undo.push(async () => {
|
|
403
|
+
const entry = await parent.getFileHandle(name);
|
|
404
|
+
if (!await entry.isSameEntry(file)) return;
|
|
405
|
+
if (sameBytes(new Uint8Array(await (await entry.getFile()).arrayBuffer()), expected)) await parent.removeEntry(name);
|
|
406
|
+
});
|
|
407
|
+
const writable = await file.createWritable({ mode: "exclusive" });
|
|
408
|
+
try {
|
|
409
|
+
await writable.write(bytes);
|
|
410
|
+
assertCurrent();
|
|
411
|
+
if (fileVersion(await file.getFile()) !== fileVersion(initial)) throw new FolderError("conflict");
|
|
412
|
+
await writable.close();
|
|
413
|
+
expected = bytes;
|
|
414
|
+
} catch (error) {
|
|
415
|
+
await writable.abort().catch(() => {});
|
|
416
|
+
throw error;
|
|
417
|
+
}
|
|
257
418
|
}
|
|
258
|
-
|
|
419
|
+
const source = await readFolder(directory, storage);
|
|
420
|
+
assertCurrent();
|
|
421
|
+
return source;
|
|
422
|
+
} catch (error) {
|
|
423
|
+
for (const revert of undo.reverse()) await revert().catch(() => {});
|
|
424
|
+
throw error;
|
|
259
425
|
}
|
|
260
|
-
return i.map(({ target: e }) => e);
|
|
261
426
|
}
|
|
262
427
|
// src/session.ts
|
|
263
|
-
var
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
428
|
+
var storageReaders = /* @__PURE__ */ new WeakMap();
|
|
429
|
+
var readerFor = (storage) => {
|
|
430
|
+
const existing = storageReaders.get(storage);
|
|
431
|
+
if (existing) return existing;
|
|
432
|
+
const reader = { assets: null };
|
|
433
|
+
storage.addHelper({ load(type, id, format) {
|
|
434
|
+
var _reader$assets;
|
|
435
|
+
const asset = (_reader$assets = reader.assets) === null || _reader$assets === void 0 ? void 0 : _reader$assets.get(assetKey(type, id, format));
|
|
436
|
+
return asset ? Promise.resolve(asset) : null;
|
|
437
|
+
} }, 101);
|
|
438
|
+
storageReaders.set(storage, reader);
|
|
439
|
+
return reader;
|
|
272
440
|
};
|
|
273
|
-
function
|
|
274
|
-
let
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
441
|
+
function createFolderSession(vm, host) {
|
|
442
|
+
let current = null;
|
|
443
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
444
|
+
const notify = () => {
|
|
445
|
+
for (const listener of [...listeners]) listener();
|
|
446
|
+
};
|
|
447
|
+
const setCurrent = (session) => {
|
|
448
|
+
if (current === session) return;
|
|
449
|
+
current = session;
|
|
450
|
+
notify();
|
|
451
|
+
};
|
|
452
|
+
let busy = false;
|
|
453
|
+
let disposed = false;
|
|
454
|
+
let revision = 0;
|
|
455
|
+
let scanning = null;
|
|
456
|
+
const snapshot = () => parseProject(vm.toJSON(void 0, { allowOptimization: false }));
|
|
457
|
+
const runtimeTargets = () => {
|
|
458
|
+
var _vm$runtime$targets;
|
|
459
|
+
return (_vm$runtime$targets = vm.runtime.targets) !== null && _vm$runtime$targets !== void 0 ? _vm$runtime$targets : [];
|
|
460
|
+
};
|
|
461
|
+
const identify = (project) => {
|
|
462
|
+
const ids = /* @__PURE__ */ new Map();
|
|
463
|
+
for (const target of runtimeTargets()) if (target.isOriginal && target.id) ids.set(target.isStage ? "stage" : `sprite:${target.getName()}`, target.id);
|
|
464
|
+
const result = project.targets.map((target) => ids.get(target.isStage ? "stage" : `sprite:${target.name}`));
|
|
465
|
+
return result.every((id) => id !== void 0) ? result : void 0;
|
|
466
|
+
};
|
|
467
|
+
const identity = (session, edited) => {
|
|
468
|
+
const after = identify(edited);
|
|
469
|
+
return session.ids && after ? {
|
|
470
|
+
before: session.ids,
|
|
471
|
+
after
|
|
287
472
|
} : void 0;
|
|
288
|
-
}
|
|
289
|
-
|
|
473
|
+
};
|
|
474
|
+
const writeChecked = async (session, handle, data, existing) => {
|
|
475
|
+
const options = existing ? { mode: "exclusive" } : {};
|
|
476
|
+
const writable = await handle.createWritable(options);
|
|
290
477
|
try {
|
|
291
|
-
await
|
|
292
|
-
|
|
293
|
-
|
|
478
|
+
await writable.write(data);
|
|
479
|
+
if (existing) await assertFolderUnchanged(session.source);
|
|
480
|
+
active(session);
|
|
481
|
+
await writable.close();
|
|
482
|
+
} catch (error) {
|
|
483
|
+
await writable.abort().catch(() => {});
|
|
484
|
+
throw error;
|
|
294
485
|
}
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
486
|
+
};
|
|
487
|
+
const assetData = (name, kind) => {
|
|
488
|
+
if (kind === "fonts") {
|
|
489
|
+
var _vm$assets;
|
|
490
|
+
return (_vm$assets = vm.assets) === null || _vm$assets === void 0 || (_vm$assets = _vm$assets.find((asset) => asset && `${asset.assetId}.${asset.dataFormat}` === name)) === null || _vm$assets === void 0 ? void 0 : _vm$assets.data;
|
|
491
|
+
}
|
|
492
|
+
for (const target of runtimeTargets()) {
|
|
493
|
+
var _target$sprite$costum, _target$sprite, _target$sprite$sounds, _target$sprite2;
|
|
494
|
+
for (const entry of [...(_target$sprite$costum = (_target$sprite = target.sprite) === null || _target$sprite === void 0 ? void 0 : _target$sprite.costumes) !== null && _target$sprite$costum !== void 0 ? _target$sprite$costum : [], ...(_target$sprite$sounds = (_target$sprite2 = target.sprite) === null || _target$sprite2 === void 0 ? void 0 : _target$sprite2.sounds) !== null && _target$sprite$sounds !== void 0 ? _target$sprite$sounds : []]) {
|
|
495
|
+
const asset = entry.asset;
|
|
496
|
+
if (asset && `${asset.assetId}.${asset.dataFormat}` === name && asset.data) return asset.data;
|
|
301
497
|
}
|
|
302
498
|
}
|
|
303
499
|
return null;
|
|
304
|
-
}, k = () => {
|
|
305
|
-
x++;
|
|
306
|
-
}, A = () => {
|
|
307
|
-
m = null;
|
|
308
500
|
};
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
501
|
+
const editListeners = /* @__PURE__ */ new Set();
|
|
502
|
+
const notifyEdits = () => {
|
|
503
|
+
for (const listener of [...editListeners]) listener();
|
|
504
|
+
};
|
|
505
|
+
const changed = () => {
|
|
506
|
+
revision++;
|
|
507
|
+
notifyEdits();
|
|
508
|
+
};
|
|
509
|
+
const planSave = (session, edited) => {
|
|
510
|
+
const { source } = session;
|
|
511
|
+
const result = projectChanges(source.project, session.baseline, edited, identity(session, edited));
|
|
512
|
+
const extensionSave = planExtensionSave(source, result !== null && result !== void 0 ? result : source.project);
|
|
513
|
+
const needed = projectAssets(extensionSave.project);
|
|
514
|
+
return {
|
|
515
|
+
result,
|
|
516
|
+
extensionSave,
|
|
517
|
+
added: [...needed].filter(([name]) => !source.bytes.has(name)),
|
|
518
|
+
removed: [...source.bytes.keys()].filter((name) => !needed.has(name))
|
|
519
|
+
};
|
|
520
|
+
};
|
|
521
|
+
const loaded = () => setCurrent(null);
|
|
522
|
+
vm.runtime.on("PROJECT_LOADED", loaded);
|
|
523
|
+
vm.on("PROJECT_CHANGED", changed);
|
|
524
|
+
const active = (session) => {
|
|
525
|
+
if (disposed || current !== session || vm.runtime.getTargetForStage() !== session.stage) throw new FolderError("conflict");
|
|
526
|
+
};
|
|
527
|
+
const dispose = () => {
|
|
528
|
+
disposed = true;
|
|
529
|
+
setCurrent(null);
|
|
530
|
+
listeners.clear();
|
|
531
|
+
editListeners.clear();
|
|
532
|
+
vm.runtime.off("PROJECT_LOADED", loaded);
|
|
533
|
+
vm.off("PROJECT_CHANGED", changed);
|
|
534
|
+
};
|
|
535
|
+
const openFrom = async (getDirectory, confirmReplace, prepare = (directory) => readFolder(directory, vm.runtime.storage), stillCurrent = () => true) => {
|
|
536
|
+
if (disposed || busy) return "cancelled";
|
|
537
|
+
busy = true;
|
|
538
|
+
const priorStage = vm.runtime.getTargetForStage();
|
|
318
539
|
try {
|
|
319
|
-
var
|
|
320
|
-
|
|
321
|
-
if (!
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
540
|
+
var _vm$renderer;
|
|
541
|
+
const directory = await getDirectory();
|
|
542
|
+
if (!directory || disposed) return "cancelled";
|
|
543
|
+
const storage = vm.runtime.storage;
|
|
544
|
+
const source = await prepare(directory);
|
|
545
|
+
if (!source || disposed || !stillCurrent() || vm.runtime.getTargetForStage() !== priorStage) return "cancelled";
|
|
546
|
+
if (host.isProjectDirty() && !confirmReplace()) return "cancelled";
|
|
547
|
+
host.beforeLoad();
|
|
548
|
+
setCurrent(null);
|
|
549
|
+
const reader = readerFor(storage);
|
|
550
|
+
const assets = source.storageAssets;
|
|
551
|
+
reader.assets = assets;
|
|
552
|
+
let stage;
|
|
328
553
|
try {
|
|
329
|
-
|
|
554
|
+
vm.quit();
|
|
555
|
+
await vm.loadProject(source.loadText, { replaceLegacyExtensions: true });
|
|
556
|
+
stage = vm.runtime.getTargetForStage();
|
|
330
557
|
} finally {
|
|
331
|
-
|
|
558
|
+
if (reader.assets === assets) reader.assets = null;
|
|
332
559
|
}
|
|
333
|
-
if (
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
source
|
|
338
|
-
stage
|
|
339
|
-
baseline
|
|
340
|
-
ids:
|
|
560
|
+
if (disposed || !stage || vm.runtime.getTargetForStage() !== stage) return "cancelled";
|
|
561
|
+
host.onOpened(directory.name);
|
|
562
|
+
const baseline = snapshot();
|
|
563
|
+
setCurrent({
|
|
564
|
+
source,
|
|
565
|
+
stage,
|
|
566
|
+
baseline,
|
|
567
|
+
ids: identify(baseline),
|
|
341
568
|
pending: null
|
|
342
|
-
}
|
|
569
|
+
});
|
|
570
|
+
(_vm$renderer = vm.renderer) === null || _vm$renderer === void 0 || _vm$renderer.draw();
|
|
571
|
+
return "opened";
|
|
343
572
|
} finally {
|
|
344
|
-
|
|
573
|
+
busy = false;
|
|
345
574
|
}
|
|
346
|
-
}
|
|
347
|
-
|
|
575
|
+
};
|
|
576
|
+
const planEdits = (before, after) => {
|
|
577
|
+
var _host$blockReload;
|
|
348
578
|
try {
|
|
349
579
|
return {
|
|
350
580
|
kind: "values",
|
|
351
|
-
changes:
|
|
581
|
+
changes: planBlockReload(before, after)
|
|
352
582
|
};
|
|
353
|
-
} catch (
|
|
354
|
-
if (!(
|
|
583
|
+
} catch (error) {
|
|
584
|
+
if (!(error instanceof FolderError)) throw error;
|
|
355
585
|
}
|
|
356
|
-
if (!((
|
|
586
|
+
if (!((_host$blockReload = host.blockReload) === null || _host$blockReload === void 0 ? void 0 : _host$blockReload.deserializeBlocks)) return null;
|
|
357
587
|
try {
|
|
358
588
|
return {
|
|
359
589
|
kind: "scripts",
|
|
360
|
-
changes:
|
|
590
|
+
changes: planScriptReload(before, after)
|
|
361
591
|
};
|
|
362
|
-
} catch (
|
|
363
|
-
if (!(
|
|
592
|
+
} catch (error) {
|
|
593
|
+
if (!(error instanceof FolderError)) throw error;
|
|
364
594
|
return null;
|
|
365
595
|
}
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
596
|
+
};
|
|
597
|
+
const readUpdate = async (session) => {
|
|
598
|
+
var _previous$update;
|
|
599
|
+
const previous = typeof session.pending === "object" ? session.pending : null;
|
|
600
|
+
const update = await readFolderUpdate(session.source, (_previous$update = previous === null || previous === void 0 ? void 0 : previous.update) !== null && _previous$update !== void 0 ? _previous$update : null);
|
|
601
|
+
if (!update || typeof update === "string") return update;
|
|
602
|
+
if ((previous === null || previous === void 0 ? void 0 : previous.update) === update) return _objectSpread2(_objectSpread2({}, previous), {}, { stable: true });
|
|
603
|
+
return {
|
|
604
|
+
update,
|
|
605
|
+
edits: planEdits(session.source.project, update.project),
|
|
606
|
+
stable: false
|
|
373
607
|
};
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
608
|
+
};
|
|
609
|
+
const reloadKind = () => {
|
|
610
|
+
const pending = current === null || current === void 0 ? void 0 : current.pending;
|
|
611
|
+
if (!pending || pending === "waiting") return null;
|
|
612
|
+
return pending === "structure" || pending.edits === null ? "full" : "partial";
|
|
613
|
+
};
|
|
614
|
+
const reloadState = () => {
|
|
615
|
+
var _host$blockReload2;
|
|
616
|
+
const pending = current === null || current === void 0 ? void 0 : current.pending;
|
|
617
|
+
if (!pending) return null;
|
|
618
|
+
if (busy) return "busy";
|
|
619
|
+
if (pending === "waiting") return "waiting";
|
|
620
|
+
if (host.isProjectDirty()) return "dirty";
|
|
621
|
+
if ((_host$blockReload2 = host.blockReload) === null || _host$blockReload2 === void 0 ? void 0 : _host$blockReload2.isRunning()) return "running";
|
|
622
|
+
return reloadKind() === "full" ? "full" : "ready";
|
|
623
|
+
};
|
|
624
|
+
const canApply = () => {
|
|
625
|
+
var _host$blockReload3;
|
|
626
|
+
return !host.isProjectDirty() && !((_host$blockReload3 = host.blockReload) === null || _host$blockReload3 === void 0 ? void 0 : _host$blockReload3.isRunning());
|
|
384
627
|
};
|
|
385
628
|
return {
|
|
386
629
|
detach() {
|
|
387
|
-
|
|
630
|
+
setCurrent(null);
|
|
631
|
+
},
|
|
632
|
+
/** Calls `listener` when a folder opens or closes, and after a save. Returns the function that stops it. */
|
|
633
|
+
subscribe(listener) {
|
|
634
|
+
listeners.add(listener);
|
|
635
|
+
return () => {
|
|
636
|
+
listeners.delete(listener);
|
|
637
|
+
};
|
|
638
|
+
},
|
|
639
|
+
subscribeEdits(listener) {
|
|
640
|
+
editListeners.add(listener);
|
|
641
|
+
return () => {
|
|
642
|
+
editListeners.delete(listener);
|
|
643
|
+
};
|
|
644
|
+
},
|
|
645
|
+
/** Files affected by the pending save. A save in progress still owns its baseline. */
|
|
646
|
+
unsavedPaths() {
|
|
647
|
+
if (busy) return null;
|
|
648
|
+
const paths = /* @__PURE__ */ new Set();
|
|
649
|
+
if (!current) return paths;
|
|
650
|
+
try {
|
|
651
|
+
const { result, extensionSave, added, removed } = planSave(current, snapshot());
|
|
652
|
+
if (result) paths.add(projectFile);
|
|
653
|
+
if (extensionSave.manifestChanged) paths.add(extensionsFile);
|
|
654
|
+
if (added.length || removed.length) paths.add(manifestFile);
|
|
655
|
+
for (const name of [...added.map(([name]) => name), ...removed]) paths.add(`assets/${name}`);
|
|
656
|
+
for (const name of [...extensionSave.writes.keys(), ...extensionSave.removed]) paths.add(`extensions/${name}`);
|
|
657
|
+
} catch (error) {
|
|
658
|
+
if (!(error instanceof FolderError)) throw error;
|
|
659
|
+
if (host.isProjectDirty()) paths.add(projectFile);
|
|
660
|
+
}
|
|
661
|
+
return paths;
|
|
388
662
|
},
|
|
389
663
|
get canSave() {
|
|
390
|
-
return
|
|
664
|
+
return current !== null;
|
|
391
665
|
},
|
|
392
666
|
get busy() {
|
|
393
|
-
return
|
|
667
|
+
return busy;
|
|
394
668
|
},
|
|
395
669
|
get reloadState() {
|
|
396
|
-
return
|
|
670
|
+
return reloadState();
|
|
397
671
|
},
|
|
398
672
|
get reloadKind() {
|
|
399
|
-
return
|
|
673
|
+
return reloadKind();
|
|
400
674
|
},
|
|
675
|
+
/** A partial change that has stayed the same across two scans and may be applied without asking. */
|
|
401
676
|
get canAutoReload() {
|
|
402
|
-
|
|
403
|
-
return
|
|
677
|
+
const pending = current === null || current === void 0 ? void 0 : current.pending;
|
|
678
|
+
return reloadState() === "ready" && typeof pending === "object" && pending !== null && pending.stable;
|
|
404
679
|
},
|
|
680
|
+
/** True while a change is new or looks incomplete, so a host can look again sooner than its interval. */
|
|
405
681
|
get settling() {
|
|
406
|
-
|
|
407
|
-
return
|
|
682
|
+
const pending = current === null || current === void 0 ? void 0 : current.pending;
|
|
683
|
+
return pending === "waiting" || typeof pending === "object" && pending !== null && !pending.stable;
|
|
408
684
|
},
|
|
685
|
+
/** Identifies the pending project file version, so a host can attempt an automatic apply only once. */
|
|
409
686
|
get pendingVersion() {
|
|
410
|
-
|
|
411
|
-
return typeof
|
|
687
|
+
const pending = current === null || current === void 0 ? void 0 : current.pending;
|
|
688
|
+
return typeof pending === "object" && pending !== null ? pending.update.version : null;
|
|
412
689
|
},
|
|
413
690
|
get directory() {
|
|
414
|
-
var
|
|
415
|
-
return (
|
|
691
|
+
var _current$source$direc;
|
|
692
|
+
return (_current$source$direc = current === null || current === void 0 ? void 0 : current.source.directory) !== null && _current$source$direc !== void 0 ? _current$source$direc : null;
|
|
416
693
|
},
|
|
694
|
+
async mutate(directory, operation) {
|
|
695
|
+
const session = current;
|
|
696
|
+
if (!session || busy || session.source.directory !== directory) throw new FolderError("conflict");
|
|
697
|
+
const guard = () => active(session);
|
|
698
|
+
guard();
|
|
699
|
+
busy = true;
|
|
700
|
+
try {
|
|
701
|
+
await operation(guard);
|
|
702
|
+
} finally {
|
|
703
|
+
busy = false;
|
|
704
|
+
if (current === session) notify();
|
|
705
|
+
}
|
|
706
|
+
},
|
|
707
|
+
/**
|
|
708
|
+
* Reads what to compare before unsaved edits are replaced. `editor` is `null` when the edits cannot be
|
|
709
|
+
* written to the folder, and `disk` when the folder's project file cannot be read.
|
|
710
|
+
*/
|
|
417
711
|
async comparison() {
|
|
418
|
-
|
|
419
|
-
if (!
|
|
420
|
-
|
|
712
|
+
const session = current;
|
|
713
|
+
if (!session) return null;
|
|
714
|
+
const decoder = new TextDecoder();
|
|
715
|
+
const base = decoder.decode(session.source.files.get(projectFile));
|
|
716
|
+
let editor;
|
|
421
717
|
try {
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
718
|
+
const edited = snapshot();
|
|
719
|
+
const result = projectChanges(session.source.project, session.baseline, edited, identity(session, edited));
|
|
720
|
+
editor = result ? `${JSON.stringify(planExtensionSave(session.source, result).project, null, 2)}\n` : base;
|
|
721
|
+
} catch (error) {
|
|
722
|
+
if (!(error instanceof FolderError)) throw error;
|
|
723
|
+
editor = null;
|
|
427
724
|
}
|
|
428
|
-
let
|
|
725
|
+
let disk;
|
|
429
726
|
try {
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
} catch (
|
|
433
|
-
|
|
727
|
+
const file = await (await session.source.directory.getFileHandle(projectFile)).getFile();
|
|
728
|
+
disk = decoder.decode(new Uint8Array(await file.arrayBuffer()));
|
|
729
|
+
} catch (_unused) {
|
|
730
|
+
disk = null;
|
|
434
731
|
}
|
|
435
|
-
return
|
|
436
|
-
base
|
|
437
|
-
editor
|
|
438
|
-
disk
|
|
732
|
+
return current === session ? {
|
|
733
|
+
base,
|
|
734
|
+
editor,
|
|
735
|
+
disk
|
|
439
736
|
} : null;
|
|
440
737
|
},
|
|
441
738
|
refresh() {
|
|
442
|
-
if (
|
|
443
|
-
if (
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
739
|
+
if (disposed || busy || !current || !host.blockReload) return Promise.resolve();
|
|
740
|
+
if (scanning) return scanning;
|
|
741
|
+
const session = current;
|
|
742
|
+
const baseline = session.baseline;
|
|
743
|
+
scanning = readUpdate(session).then((pending) => {
|
|
744
|
+
if (current === session && session.baseline === baseline && !busy) session.pending = pending;
|
|
447
745
|
}).finally(() => {
|
|
448
|
-
|
|
449
|
-
})
|
|
746
|
+
scanning = null;
|
|
747
|
+
});
|
|
748
|
+
return scanning;
|
|
450
749
|
},
|
|
451
750
|
async reload() {
|
|
452
|
-
if (!
|
|
453
|
-
|
|
454
|
-
|
|
751
|
+
if (!current || reloadState() !== "ready" || !host.blockReload) return false;
|
|
752
|
+
const session = current;
|
|
753
|
+
active(session);
|
|
754
|
+
busy = true;
|
|
455
755
|
try {
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
756
|
+
const editRevision = revision;
|
|
757
|
+
const pending = await readUpdate(session);
|
|
758
|
+
if (current !== session || disposed || vm.runtime.getTargetForStage() !== session.stage) return false;
|
|
759
|
+
session.pending = pending;
|
|
760
|
+
if (!pending || typeof pending === "string" || pending.edits === null || editRevision !== revision || !canApply()) return false;
|
|
761
|
+
let targets;
|
|
459
762
|
try {
|
|
460
|
-
var
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
} catch (
|
|
464
|
-
|
|
763
|
+
var _vm$runtime$targets2, _vm$runtime$targets3;
|
|
764
|
+
const { edits } = pending;
|
|
765
|
+
targets = edits.kind === "values" ? applyBlockReload((_vm$runtime$targets2 = vm.runtime.targets) !== null && _vm$runtime$targets2 !== void 0 ? _vm$runtime$targets2 : [], edits.changes) : applyScriptReload((_vm$runtime$targets3 = vm.runtime.targets) !== null && _vm$runtime$targets3 !== void 0 ? _vm$runtime$targets3 : [], edits.changes, host.blockReload.deserializeBlocks);
|
|
766
|
+
} catch (error) {
|
|
767
|
+
session.pending = _objectSpread2(_objectSpread2({}, pending), {}, { edits: null });
|
|
768
|
+
throw error;
|
|
465
769
|
}
|
|
466
|
-
|
|
770
|
+
session.baseline = snapshot();
|
|
771
|
+
session.ids = identify(session.baseline);
|
|
772
|
+
session.source.project = pending.update.project;
|
|
773
|
+
session.source.files.set(projectFile, pending.update.bytes);
|
|
774
|
+
session.source.versions.set(projectFile, pending.update.version);
|
|
775
|
+
session.pending = null;
|
|
776
|
+
host.blockReload.onApplied(targets);
|
|
777
|
+
notify();
|
|
778
|
+
return true;
|
|
467
779
|
} finally {
|
|
468
|
-
|
|
780
|
+
busy = false;
|
|
781
|
+
notifyEdits();
|
|
469
782
|
}
|
|
470
783
|
},
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
784
|
+
/**
|
|
785
|
+
* Reads the open folder again and replaces the project, for changes a partial reload cannot apply. Unsaved
|
|
786
|
+
* edits are discarded only when `confirmReplace` agrees. The VM is left untouched while a project runs.
|
|
787
|
+
*/
|
|
788
|
+
reloadAll(confirmReplace) {
|
|
789
|
+
var _host$blockReload4;
|
|
790
|
+
const session = current;
|
|
791
|
+
if (!session || !session.pending || session.pending === "waiting" || ((_host$blockReload4 = host.blockReload) === null || _host$blockReload4 === void 0 ? void 0 : _host$blockReload4.isRunning())) return Promise.resolve("cancelled");
|
|
792
|
+
return openFrom(() => Promise.resolve(current === session ? session.source.directory : null), confirmReplace);
|
|
475
793
|
},
|
|
476
|
-
open(
|
|
477
|
-
return
|
|
794
|
+
open(pick, confirmReplace) {
|
|
795
|
+
return openFrom(async () => {
|
|
478
796
|
try {
|
|
479
|
-
return await
|
|
797
|
+
return await pick({
|
|
480
798
|
id: "turbowarp-plus-project-folder",
|
|
481
799
|
mode: "readwrite"
|
|
482
800
|
});
|
|
483
|
-
} catch (
|
|
484
|
-
if (
|
|
485
|
-
throw
|
|
801
|
+
} catch (error) {
|
|
802
|
+
if (error instanceof DOMException && error.name === "AbortError") return null;
|
|
803
|
+
throw error;
|
|
486
804
|
}
|
|
487
|
-
},
|
|
805
|
+
}, confirmReplace);
|
|
806
|
+
},
|
|
807
|
+
openDirectory(directory, confirmReplace) {
|
|
808
|
+
return openFrom(() => Promise.resolve(directory), confirmReplace);
|
|
488
809
|
},
|
|
489
|
-
|
|
490
|
-
|
|
810
|
+
createFromSnapshot(getDirectory, prepareFiles, isCurrent) {
|
|
811
|
+
const stage = vm.runtime.getTargetForStage();
|
|
812
|
+
let savedRevision;
|
|
813
|
+
const currentProject = () => !disposed && vm.runtime.getTargetForStage() === stage && (savedRevision === void 0 || revision === savedRevision) && isCurrent();
|
|
814
|
+
return openFrom(getDirectory, currentProject, async (directory) => {
|
|
815
|
+
if (!currentProject()) return null;
|
|
816
|
+
savedRevision = revision;
|
|
817
|
+
const files = await prepareFiles();
|
|
818
|
+
if (!currentProject()) return null;
|
|
819
|
+
return createProjectFolder({
|
|
820
|
+
directory,
|
|
821
|
+
files,
|
|
822
|
+
storage: vm.runtime.storage,
|
|
823
|
+
current: currentProject
|
|
824
|
+
});
|
|
825
|
+
}, currentProject);
|
|
491
826
|
},
|
|
492
827
|
async save() {
|
|
493
|
-
if (!
|
|
494
|
-
|
|
495
|
-
|
|
828
|
+
if (!current || busy) throw new FolderError("conflict");
|
|
829
|
+
const session = current;
|
|
830
|
+
active(session);
|
|
831
|
+
busy = true;
|
|
496
832
|
try {
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
833
|
+
const saveRevision = revision;
|
|
834
|
+
const edited = snapshot();
|
|
835
|
+
const ids = identify(edited);
|
|
836
|
+
const { result, extensionSave, added: newAssets, removed } = planSave(session, edited);
|
|
837
|
+
await assertFolderUnchanged(session.source);
|
|
838
|
+
active(session);
|
|
839
|
+
const { source } = session;
|
|
840
|
+
const added = /* @__PURE__ */ new Map();
|
|
841
|
+
for (const [name, kind] of newAssets) {
|
|
842
|
+
const data = assetData(name, kind);
|
|
843
|
+
if (!data) return unsupported(`The data of ${name} is not in the Editor`);
|
|
844
|
+
const [id, format] = name.split(".");
|
|
845
|
+
if (vm.runtime.storage.createAsset(assetTypeFor(vm.runtime.storage, kind, format), format, data.slice(), null, true).assetId !== id) return unsupported(`Asset hash mismatch: ${name}`);
|
|
846
|
+
added.set(name, data);
|
|
507
847
|
}
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
848
|
+
if (!result && !removed.length && !extensionSave.removed.length) {
|
|
849
|
+
if (revision === saveRevision) host.onSaved();
|
|
850
|
+
notify();
|
|
851
|
+
return {
|
|
852
|
+
status: "unchanged",
|
|
853
|
+
sourceRemoved: []
|
|
854
|
+
};
|
|
855
|
+
}
|
|
856
|
+
const encoder = new TextEncoder();
|
|
857
|
+
let manifest = null;
|
|
858
|
+
if (added.size || removed.length) {
|
|
859
|
+
const value = JSON.parse(new TextDecoder().decode(source.files.get(manifestFile)));
|
|
860
|
+
value.archiveEntries = [...value.archiveEntries.filter((name) => !removed.includes(name)), ...added.keys()];
|
|
861
|
+
manifest = encoder.encode(`${JSON.stringify(value, null, 2)}\n`);
|
|
517
862
|
}
|
|
518
|
-
|
|
519
|
-
directory:
|
|
520
|
-
files:
|
|
863
|
+
const root = {
|
|
864
|
+
directory: source.directory,
|
|
865
|
+
files: source.files,
|
|
521
866
|
prefix: ""
|
|
522
|
-
}
|
|
523
|
-
|
|
524
|
-
|
|
867
|
+
};
|
|
868
|
+
const assets = {
|
|
869
|
+
directory: await source.directory.getDirectoryHandle("assets", { create: true }),
|
|
870
|
+
files: source.bytes,
|
|
525
871
|
prefix: "assets/"
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
|
|
872
|
+
};
|
|
873
|
+
const extensions = {
|
|
874
|
+
directory: await source.directory.getDirectoryHandle("extensions", { create: true }),
|
|
875
|
+
files: source.extensionBytes,
|
|
529
876
|
prefix: "extensions/"
|
|
530
|
-
}
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
877
|
+
};
|
|
878
|
+
const undo = [];
|
|
879
|
+
const write = async ({ directory, files, prefix }, name, data) => {
|
|
880
|
+
const previous = files.get(name);
|
|
881
|
+
if (!previous) {
|
|
882
|
+
if (await directory.getFileHandle(name).catch((error) => {
|
|
883
|
+
if (error instanceof DOMException && error.name === "NotFoundError") return null;
|
|
884
|
+
throw error;
|
|
885
|
+
})) throw new FolderError("conflict");
|
|
886
|
+
}
|
|
887
|
+
const handle = await directory.getFileHandle(name, previous ? void 0 : { create: true });
|
|
888
|
+
const key = prefix + name;
|
|
889
|
+
const previousVersion = source.versions.get(key);
|
|
890
|
+
let committed = false;
|
|
891
|
+
undo.push(async () => {
|
|
892
|
+
let version = previousVersion;
|
|
539
893
|
try {
|
|
540
|
-
if (!
|
|
541
|
-
|
|
542
|
-
if (!
|
|
543
|
-
if (
|
|
544
|
-
|
|
894
|
+
if (!committed && previous) return;
|
|
895
|
+
const bytes = new Uint8Array(await (await handle.getFile()).arrayBuffer());
|
|
896
|
+
if (!sameBytes(bytes, committed ? data : /* @__PURE__ */ new Uint8Array())) return;
|
|
897
|
+
if (previous) {
|
|
898
|
+
const writable = await handle.createWritable();
|
|
545
899
|
try {
|
|
546
|
-
await
|
|
547
|
-
|
|
548
|
-
|
|
900
|
+
await writable.write(previous);
|
|
901
|
+
await writable.close();
|
|
902
|
+
} catch (error) {
|
|
903
|
+
await writable.abort().catch(() => {});
|
|
904
|
+
throw error;
|
|
549
905
|
}
|
|
550
|
-
|
|
551
|
-
} else await
|
|
906
|
+
version = fileVersion(await handle.getFile());
|
|
907
|
+
} else await directory.removeEntry(name);
|
|
552
908
|
} finally {
|
|
553
|
-
|
|
909
|
+
if (previous) files.set(name, previous);
|
|
910
|
+
else files.delete(name);
|
|
911
|
+
if (version === void 0) source.versions.delete(key);
|
|
912
|
+
else source.versions.set(key, version);
|
|
554
913
|
}
|
|
555
|
-
})
|
|
914
|
+
});
|
|
915
|
+
await writeChecked(session, handle, data, previous !== void 0);
|
|
916
|
+
committed = true;
|
|
917
|
+
files.set(name, data);
|
|
918
|
+
source.versions.set(key, fileVersion(await handle.getFile()));
|
|
556
919
|
};
|
|
557
920
|
try {
|
|
558
|
-
for (
|
|
559
|
-
for (
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
921
|
+
for (const [name, data] of added) await write(assets, name, data);
|
|
922
|
+
for (const [name, data] of extensionSave.writes) await write(extensions, name, data);
|
|
923
|
+
if (manifest) await write(root, manifestFile, manifest);
|
|
924
|
+
if (extensionSave.manifestChanged) await write(root, extensionsFile, encoder.encode(`${JSON.stringify(extensionSave.embedded, null, 2)}\n`));
|
|
925
|
+
if (result) await write(root, projectFile, encoder.encode(`${JSON.stringify(extensionSave.project, null, 2)}\n`));
|
|
926
|
+
} catch (error) {
|
|
927
|
+
for (const restore of undo.reverse()) await restore().catch(() => {});
|
|
928
|
+
throw error;
|
|
564
929
|
}
|
|
565
|
-
|
|
566
|
-
|
|
930
|
+
source.project = extensionSave.project;
|
|
931
|
+
source.embedded = extensionSave.embedded;
|
|
932
|
+
for (const [{ directory, files, prefix }, names] of [[assets, removed], [extensions, extensionSave.removed]]) for (const name of names) {
|
|
567
933
|
try {
|
|
568
|
-
|
|
569
|
-
if (!
|
|
570
|
-
await
|
|
571
|
-
} catch (
|
|
572
|
-
if (!(
|
|
934
|
+
const file = await (await directory.getFileHandle(name)).getFile();
|
|
935
|
+
if (!sameBytes(new Uint8Array(await file.arrayBuffer()), files.get(name))) continue;
|
|
936
|
+
await directory.removeEntry(name);
|
|
937
|
+
} catch (error) {
|
|
938
|
+
if (!(error instanceof DOMException && error.name === "NotFoundError")) continue;
|
|
573
939
|
}
|
|
574
|
-
|
|
940
|
+
files.delete(name);
|
|
941
|
+
source.versions.delete(prefix + name);
|
|
575
942
|
}
|
|
576
|
-
|
|
943
|
+
session.baseline = edited;
|
|
944
|
+
session.ids = ids;
|
|
945
|
+
session.pending = null;
|
|
946
|
+
if (current === session && revision === saveRevision) host.onSaved();
|
|
947
|
+
notify();
|
|
948
|
+
return {
|
|
577
949
|
status: "saved",
|
|
578
|
-
sourceRemoved:
|
|
950
|
+
sourceRemoved: extensionSave.sourceRemoved
|
|
579
951
|
};
|
|
580
952
|
} finally {
|
|
581
|
-
|
|
953
|
+
busy = false;
|
|
954
|
+
notifyEdits();
|
|
582
955
|
}
|
|
583
956
|
},
|
|
584
|
-
dispose
|
|
957
|
+
dispose
|
|
585
958
|
};
|
|
586
959
|
}
|
|
587
960
|
// src/index.ts
|
|
588
|
-
var
|
|
961
|
+
var messages = {
|
|
589
962
|
en: {
|
|
590
963
|
open: "Open Project Folder…",
|
|
591
964
|
save: "Save to Project Folder",
|
|
@@ -593,7 +966,7 @@ var X = {
|
|
|
593
966
|
opened: "Project folder opened.",
|
|
594
967
|
saved: "Project folder saved.",
|
|
595
968
|
unchanged: "No changes to save.",
|
|
596
|
-
sourceRemoved: (
|
|
969
|
+
sourceRemoved: (ids) => `Removed source metadata for replaced extensions: ${ids.join(", ")}. Use sb3-toolchain to fetch the managed sources again if you need to keep their provenance.`,
|
|
597
970
|
conflict: "The project folder changed outside the editor or is no longer open. Your edits are still in the editor.",
|
|
598
971
|
unsupported: "This project or edit cannot be saved to a project folder yet.",
|
|
599
972
|
failed: "Could not access the project folder.",
|
|
@@ -626,7 +999,7 @@ var X = {
|
|
|
626
999
|
opened: "プロジェクトフォルダを開きました。",
|
|
627
1000
|
saved: "プロジェクトフォルダに保存しました。",
|
|
628
1001
|
unchanged: "保存する変更はありません。",
|
|
629
|
-
sourceRemoved: (
|
|
1002
|
+
sourceRemoved: (ids) => `差し替えた拡張機能(${ids.join("、")})の出どころ情報を削除しました。出どころを保つには、sb3-toolchain で拡張機能を取得し直してください。`,
|
|
630
1003
|
conflict: "外部でフォルダが変更されたか、作品が切り替わりました。編集中の変更はエディターに残っています。",
|
|
631
1004
|
unsupported: "この作品または編集は、まだプロジェクトフォルダに保存できません。",
|
|
632
1005
|
failed: "プロジェクトフォルダにアクセスできませんでした。",
|
|
@@ -652,13 +1025,23 @@ var X = {
|
|
|
652
1025
|
waiting: "外部の書き込みが終わるのを待っています。",
|
|
653
1026
|
full: "この変更は作品全体の再読み込みが必要です。"
|
|
654
1027
|
}
|
|
655
|
-
}
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
1028
|
+
};
|
|
1029
|
+
var menus = /* @__PURE__ */ new WeakMap();
|
|
1030
|
+
/**
|
|
1031
|
+
* `autoReload` (default true) applies value and position changes without a click when the Editor has no unsaved
|
|
1032
|
+
* edits and the project is stopped. Other changes always wait for the reload command.
|
|
1033
|
+
*/
|
|
1034
|
+
function mountProjectFolder({ vm, document, host, locale = "en", autoReload = true }) {
|
|
1035
|
+
var _vm$runtime;
|
|
1036
|
+
const view = document.defaultView;
|
|
1037
|
+
const pickDirectory = view === null || view === void 0 ? void 0 : view.showDirectoryPicker;
|
|
1038
|
+
if (!pickDirectory || !((_vm$runtime = vm.runtime) === null || _vm$runtime === void 0 ? void 0 : _vm$runtime.storage)) return null;
|
|
1039
|
+
const session = createFolderSession(vm, host);
|
|
1040
|
+
const status = document.createElement("output");
|
|
1041
|
+
status.dataset.projectFolderStatus = "";
|
|
1042
|
+
status.setAttribute("role", "status");
|
|
1043
|
+
status.hidden = true;
|
|
1044
|
+
Object.assign(status.style, {
|
|
662
1045
|
position: "fixed",
|
|
663
1046
|
insetInlineEnd: "1rem",
|
|
664
1047
|
bottom: "1rem",
|
|
@@ -669,19 +1052,33 @@ function Q({ vm: e, document: t, host: n, locale: r = "en", autoReload: i = !0 }
|
|
|
669
1052
|
color: "CanvasText",
|
|
670
1053
|
border: "1px solid currentColor",
|
|
671
1054
|
borderRadius: "0.4rem"
|
|
672
|
-
})
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
1055
|
+
});
|
|
1056
|
+
document.body.append(status);
|
|
1057
|
+
let timer;
|
|
1058
|
+
const report = (text) => {
|
|
1059
|
+
if (!status.isConnected) return;
|
|
1060
|
+
status.hidden = false;
|
|
1061
|
+
status.textContent = text;
|
|
1062
|
+
view.clearTimeout(timer);
|
|
1063
|
+
timer = view.setTimeout(() => {
|
|
1064
|
+
status.hidden = true;
|
|
1065
|
+
}, 7500);
|
|
1066
|
+
};
|
|
1067
|
+
const messageFor = (locale) => locale.startsWith("ja") ? messages.ja : messages.en;
|
|
1068
|
+
const fail = (locale, error) => {
|
|
1069
|
+
const message = messageFor(locale);
|
|
1070
|
+
const detail = error instanceof FolderError && error.message !== error.code ? ` (${error.message})` : "";
|
|
1071
|
+
report((error instanceof FolderError ? message[error.code] : message.failed) + detail);
|
|
1072
|
+
};
|
|
1073
|
+
let stopWatching;
|
|
1074
|
+
let renderReload = () => {};
|
|
1075
|
+
if (host.blockReload) {
|
|
1076
|
+
var _host$blockReload$com;
|
|
1077
|
+
const message = messageFor(locale);
|
|
1078
|
+
const notice = document.createElement("aside");
|
|
1079
|
+
notice.dataset.projectFolderExternal = "";
|
|
1080
|
+
notice.hidden = true;
|
|
1081
|
+
Object.assign(notice.style, {
|
|
685
1082
|
position: "fixed",
|
|
686
1083
|
insetInlineEnd: "1rem",
|
|
687
1084
|
bottom: "4.5rem",
|
|
@@ -693,140 +1090,230 @@ function Q({ vm: e, document: t, host: n, locale: r = "en", autoReload: i = !0 }
|
|
|
693
1090
|
border: "1px solid currentColor",
|
|
694
1091
|
borderRadius: "0.4rem"
|
|
695
1092
|
});
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
1093
|
+
const text = document.createElement("div");
|
|
1094
|
+
text.setAttribute("role", "status");
|
|
1095
|
+
const button = document.createElement("button");
|
|
1096
|
+
button.type = "button";
|
|
1097
|
+
button.textContent = message.reload;
|
|
1098
|
+
notice.append(text, button);
|
|
1099
|
+
document.body.append(notice);
|
|
1100
|
+
const compareChanges = (_host$blockReload$com = host.blockReload.compareChanges) === null || _host$blockReload$com === void 0 ? void 0 : _host$blockReload$com.bind(host.blockReload);
|
|
1101
|
+
let comparing = false;
|
|
1102
|
+
const compare = async (show) => {
|
|
1103
|
+
if (comparing) return;
|
|
1104
|
+
comparing = true;
|
|
1105
|
+
try {
|
|
1106
|
+
const texts = await session.comparison();
|
|
1107
|
+
if (!texts) return;
|
|
1108
|
+
const sections = [{
|
|
1109
|
+
title: message.diskChanges,
|
|
1110
|
+
before: texts.base,
|
|
1111
|
+
after: texts.disk
|
|
1112
|
+
}];
|
|
1113
|
+
if (texts.editor !== null) sections.unshift({
|
|
1114
|
+
title: message.yourEdits,
|
|
1115
|
+
before: texts.base,
|
|
1116
|
+
after: texts.editor
|
|
1117
|
+
});
|
|
1118
|
+
if (await show({
|
|
1119
|
+
title: message.compareTitle,
|
|
1120
|
+
description: message.compareDescription,
|
|
1121
|
+
notes: texts.editor === null ? [message.compareUnsaved] : [],
|
|
1122
|
+
sections,
|
|
1123
|
+
actions: [{
|
|
1124
|
+
id: "keep",
|
|
1125
|
+
label: message.keep
|
|
1126
|
+
}, {
|
|
1127
|
+
id: "discard",
|
|
1128
|
+
label: message.discardLoad
|
|
1129
|
+
}],
|
|
1130
|
+
closeLabel: message.close
|
|
1131
|
+
}) === "discard" && session.reloadState === "dirty") apply(() => session.reloadAll(() => true));
|
|
1132
|
+
} catch (error) {
|
|
1133
|
+
fail(locale, error);
|
|
1134
|
+
} finally {
|
|
1135
|
+
comparing = false;
|
|
1136
|
+
renderReload();
|
|
734
1137
|
}
|
|
735
|
-
}, y, b = null, x = null, S = null, C = (t) => {
|
|
736
|
-
let n = t();
|
|
737
|
-
h(), n.then((t) => {
|
|
738
|
-
(t === !0 || t === "opened") && d(e.reloaded);
|
|
739
|
-
}, (e) => p(r, e)).finally(h);
|
|
740
1138
|
};
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
1139
|
+
let settle;
|
|
1140
|
+
let autoTried = null;
|
|
1141
|
+
let observed = null;
|
|
1142
|
+
let observer = null;
|
|
1143
|
+
const apply = (run) => {
|
|
1144
|
+
const done = run();
|
|
1145
|
+
renderReload();
|
|
1146
|
+
done.then((result) => {
|
|
1147
|
+
if (result === true || result === "opened") report(message.reloaded);
|
|
1148
|
+
}, (error) => fail(locale, error)).finally(renderReload);
|
|
1149
|
+
};
|
|
1150
|
+
renderReload = () => {
|
|
1151
|
+
const state = session.reloadState;
|
|
1152
|
+
const kind = session.reloadKind;
|
|
1153
|
+
if (autoReload && session.canAutoReload && session.pendingVersion !== autoTried) {
|
|
1154
|
+
autoTried = session.pendingVersion;
|
|
1155
|
+
apply(() => session.reload());
|
|
745
1156
|
return;
|
|
746
1157
|
}
|
|
747
|
-
|
|
1158
|
+
notice.hidden = !state;
|
|
1159
|
+
button.hidden = !kind;
|
|
1160
|
+
button.disabled = ![
|
|
748
1161
|
"ready",
|
|
749
1162
|
"full",
|
|
750
1163
|
"dirty"
|
|
751
|
-
].includes(
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
1164
|
+
].includes(state !== null && state !== void 0 ? state : "");
|
|
1165
|
+
const action = state === "dirty" ? compareChanges ? message.compare : message.discard : kind === "full" ? message.reloadAll : message.reload;
|
|
1166
|
+
if (button.textContent !== action) button.textContent = action;
|
|
1167
|
+
const detail = !state || state === "ready" ? "" : state === "running" && kind === "partial" && autoReload ? message.runningAuto : message[state];
|
|
1168
|
+
const label = detail ? `${message.external} ${detail}` : message.external;
|
|
1169
|
+
if (text.textContent !== label) text.textContent = label;
|
|
1170
|
+
const directory = session.directory;
|
|
1171
|
+
if (observer && observed !== directory) {
|
|
1172
|
+
observer.disconnect();
|
|
1173
|
+
observed = directory;
|
|
1174
|
+
if (directory) observer.observe(directory, { recursive: true }).catch(() => {});
|
|
1175
|
+
}
|
|
1176
|
+
};
|
|
1177
|
+
const refresh = () => {
|
|
1178
|
+
if (document.visibilityState !== "visible") return;
|
|
1179
|
+
session.refresh().then(() => {
|
|
1180
|
+
renderReload();
|
|
1181
|
+
if (session.settling) schedule(500);
|
|
1182
|
+
}, (error) => fail(locale, error));
|
|
758
1183
|
};
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
}, (e) => p(r, e));
|
|
763
|
-
}, T = (e) => {
|
|
764
|
-
o.clearTimeout(y), y = o.setTimeout(w, e);
|
|
1184
|
+
const schedule = (delay) => {
|
|
1185
|
+
view.clearTimeout(settle);
|
|
1186
|
+
settle = view.setTimeout(refresh, delay);
|
|
765
1187
|
};
|
|
766
|
-
if (
|
|
767
|
-
|
|
768
|
-
} catch (
|
|
769
|
-
|
|
1188
|
+
if (view.FileSystemObserver) try {
|
|
1189
|
+
observer = new view.FileSystemObserver(() => schedule(300));
|
|
1190
|
+
} catch (_unused) {
|
|
1191
|
+
observer = null;
|
|
770
1192
|
}
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
1193
|
+
button.addEventListener("click", () => {
|
|
1194
|
+
const state = session.reloadState;
|
|
1195
|
+
if (state === "ready") apply(() => session.reload());
|
|
1196
|
+
else if (state === "full") apply(() => session.reloadAll(() => true));
|
|
1197
|
+
else if (state === "dirty" && compareChanges) compare(compareChanges);
|
|
1198
|
+
else if (state === "dirty") apply(() => session.reloadAll(() => view.confirm(message.discardConfirm)));
|
|
774
1199
|
});
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
1200
|
+
const unsubscribe = host.blockReload.subscribe(renderReload);
|
|
1201
|
+
const interval = view.setInterval(refresh, 2e3);
|
|
1202
|
+
view.addEventListener("focus", refresh);
|
|
1203
|
+
document.addEventListener("visibilitychange", refresh);
|
|
1204
|
+
stopWatching = () => {
|
|
1205
|
+
unsubscribe();
|
|
1206
|
+
view.clearInterval(interval);
|
|
1207
|
+
view.clearTimeout(settle);
|
|
1208
|
+
observer === null || observer === void 0 || observer.disconnect();
|
|
1209
|
+
view.removeEventListener("focus", refresh);
|
|
1210
|
+
document.removeEventListener("visibilitychange", refresh);
|
|
1211
|
+
notice.remove();
|
|
778
1212
|
};
|
|
779
1213
|
}
|
|
780
|
-
|
|
781
|
-
session
|
|
782
|
-
open(
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
}, (
|
|
1214
|
+
const menu = {
|
|
1215
|
+
session,
|
|
1216
|
+
open(locale) {
|
|
1217
|
+
const message = messageFor(locale);
|
|
1218
|
+
session.open((options) => pickDirectory.call(view, options), () => view.confirm(message.replace)).then((result) => {
|
|
1219
|
+
if (result === "opened") report(message.opened);
|
|
1220
|
+
}, (error) => fail(locale, error)).finally(renderReload);
|
|
1221
|
+
renderReload();
|
|
787
1222
|
},
|
|
788
|
-
save(
|
|
789
|
-
if (!
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
1223
|
+
save(locale) {
|
|
1224
|
+
if (!session.busy) {
|
|
1225
|
+
var _host$saveWithLock, _host$saveWithLock2;
|
|
1226
|
+
const message = messageFor(locale);
|
|
1227
|
+
const directory = session.directory;
|
|
1228
|
+
const save = () => {
|
|
1229
|
+
if (session.directory !== directory) throw new FolderError("conflict");
|
|
1230
|
+
return session.save();
|
|
1231
|
+
};
|
|
1232
|
+
((_host$saveWithLock = (_host$saveWithLock2 = host.saveWithLock) === null || _host$saveWithLock2 === void 0 ? void 0 : _host$saveWithLock2.call(host, save)) !== null && _host$saveWithLock !== void 0 ? _host$saveWithLock : save()).then(({ status, sourceRemoved }) => {
|
|
1233
|
+
const detail = sourceRemoved.length ? ` ${message.sourceRemoved(sourceRemoved)}` : "";
|
|
1234
|
+
report(message[status] + detail);
|
|
1235
|
+
}, (error) => fail(locale, error)).finally(renderReload);
|
|
1236
|
+
renderReload();
|
|
795
1237
|
}
|
|
796
1238
|
}
|
|
797
|
-
}
|
|
798
|
-
|
|
1239
|
+
};
|
|
1240
|
+
menus.set(vm, menu);
|
|
1241
|
+
const unregister = registerFolder(vm, {
|
|
1242
|
+
get directory() {
|
|
1243
|
+
return session.directory;
|
|
1244
|
+
},
|
|
1245
|
+
subscribe: (listener) => session.subscribe(listener),
|
|
1246
|
+
subscribeEdits: (listener) => session.subscribeEdits(listener),
|
|
1247
|
+
unsavedPaths: () => session.unsavedPaths(),
|
|
1248
|
+
open: (locale) => menu.open(locale),
|
|
1249
|
+
mutate: (directory, operation) => session.mutate(directory, operation).finally(renderReload)
|
|
1250
|
+
});
|
|
1251
|
+
return {
|
|
1252
|
+
openDirectory: (...args) => session.openDirectory(...args).finally(renderReload),
|
|
1253
|
+
save: () => session.save().finally(renderReload),
|
|
1254
|
+
createFromSnapshot({ prepareFiles, directory, current = () => true }) {
|
|
1255
|
+
return session.createFromSnapshot(async () => {
|
|
1256
|
+
if (directory) return directory;
|
|
1257
|
+
try {
|
|
1258
|
+
return await pickDirectory.call(view, {
|
|
1259
|
+
id: "turbowarp-plus-project-folder",
|
|
1260
|
+
mode: "readwrite"
|
|
1261
|
+
});
|
|
1262
|
+
} catch (error) {
|
|
1263
|
+
if (error instanceof DOMException && error.name === "AbortError") return null;
|
|
1264
|
+
throw error;
|
|
1265
|
+
}
|
|
1266
|
+
}, prepareFiles, current).finally(renderReload);
|
|
1267
|
+
},
|
|
1268
|
+
/**
|
|
1269
|
+
* The folder the loaded project came from, or `null` once another project loaded or the folder was
|
|
1270
|
+
* detached. A host can tell projects apart by it, for example to keep the layout of each one.
|
|
1271
|
+
*/
|
|
1272
|
+
get directory() {
|
|
1273
|
+
return session.directory;
|
|
1274
|
+
},
|
|
799
1275
|
dispose() {
|
|
800
|
-
|
|
1276
|
+
menus.delete(vm);
|
|
1277
|
+
unregister();
|
|
1278
|
+
session.dispose();
|
|
1279
|
+
stopWatching === null || stopWatching === void 0 || stopWatching();
|
|
1280
|
+
view.clearTimeout(timer);
|
|
1281
|
+
status.remove();
|
|
801
1282
|
}
|
|
802
1283
|
};
|
|
803
1284
|
}
|
|
804
|
-
function
|
|
805
|
-
|
|
806
|
-
if (
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
1285
|
+
function projectFolderActions(vm, locale = "en") {
|
|
1286
|
+
const entry = menus.get(vm);
|
|
1287
|
+
if (!entry) return void 0;
|
|
1288
|
+
return {
|
|
1289
|
+
open: () => entry.open(locale),
|
|
1290
|
+
save: entry.session.canSave ? () => entry.save(locale) : void 0,
|
|
1291
|
+
detach: () => entry.session.detach()
|
|
810
1292
|
};
|
|
811
1293
|
}
|
|
812
|
-
function
|
|
813
|
-
|
|
814
|
-
if (!
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
1294
|
+
function renderProjectFolderMenu(createElement, { vm, locale = "en", MenuItem, MenuSection, onRequestClose }) {
|
|
1295
|
+
const entry = menus.get(vm);
|
|
1296
|
+
if (!entry) return null;
|
|
1297
|
+
const labels = locale.startsWith("ja") ? messages.ja : messages.en;
|
|
1298
|
+
const item = (action) => {
|
|
1299
|
+
const run = () => {
|
|
1300
|
+
onRequestClose();
|
|
1301
|
+
entry[action](locale);
|
|
818
1302
|
};
|
|
819
|
-
return
|
|
820
|
-
command: `project-folder-${
|
|
821
|
-
onClick:
|
|
822
|
-
},
|
|
1303
|
+
return createElement(MenuItem, {
|
|
1304
|
+
command: `project-folder-${action}`,
|
|
1305
|
+
onClick: run
|
|
1306
|
+
}, createElement("span", {
|
|
823
1307
|
role: "button",
|
|
824
1308
|
tabIndex: 0,
|
|
825
|
-
onKeyDown(
|
|
826
|
-
(
|
|
1309
|
+
onKeyDown(event) {
|
|
1310
|
+
if (event.key !== "Enter" && event.key !== " ") return;
|
|
1311
|
+
event.preventDefault();
|
|
1312
|
+
event.stopPropagation();
|
|
1313
|
+
run();
|
|
827
1314
|
}
|
|
828
|
-
},
|
|
1315
|
+
}, labels[action]));
|
|
829
1316
|
};
|
|
830
|
-
return
|
|
1317
|
+
return createElement(MenuSection, null, item("open"), entry.session.canSave ? item("save") : null);
|
|
831
1318
|
}
|
|
832
|
-
export {
|
|
1319
|
+
export { createProjectFolder, followProjectFolder, mountProjectFolder, projectFolderActions, renderProjectFolderMenu };
|