@kubohiroya/turbowarp-plus-editor-project-folder 0.8.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/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 +33 -3
- package/dist/explorer.js +1882 -694
- package/dist/file-preview.d.ts +24 -0
- package/dist/folder-registry.d.ts +4 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +1019 -555
- package/dist/session.d.ts +6 -0
- package/dist/shared-JdaDvk2R.js +588 -0
- package/dist/shared-kI7qYraF.js +30130 -0
- 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 +295 -21
- package/src/file-preview.ts +134 -0
- package/src/folder-registry.ts +5 -0
- package/src/index.ts +27 -2
- package/src/session.ts +77 -12
- package/src/source.ts +16 -8
- package/src/text-file-editor.ts +356 -0
- package/vite.config.ts +2 -0
- package/dist/shared-DlmcdO1_.js +0 -458
package/dist/index.js
CHANGED
|
@@ -1,16 +1,23 @@
|
|
|
1
|
-
import { S 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 S = ["targets"], C = ["targets"], w = ["blocks"], T = ["blocks"], E = [
|
|
|
18
25
|
8: ["math_angle", "NUM"],
|
|
19
26
|
9: ["colour_picker", "COLOUR"],
|
|
20
27
|
10: ["text", "TEXT"]
|
|
21
|
-
}, A = () => r("Only values and coordinates of existing blocks can be reloaded"), j = (e, t) => g(Object.keys(e), Object.keys(t)), M = (e, t) => typeof e != "number" || typeof t != "number" || !Number.isSafeInteger(e) || !Number.isSafeInteger(t) ? A() : {
|
|
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,452 +183,782 @@ var F = ["targets"], I = ["targets"], L = ["blocks"], R = ["blocks"], z = (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();
|
|
327
|
+
}
|
|
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");
|
|
251
350
|
}
|
|
252
|
-
} catch (
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
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
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
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
|
|
291
472
|
} : void 0;
|
|
292
|
-
}
|
|
293
|
-
|
|
473
|
+
};
|
|
474
|
+
const writeChecked = async (session, handle, data, existing) => {
|
|
475
|
+
const options = existing ? { mode: "exclusive" } : {};
|
|
476
|
+
const writable = await handle.createWritable(options);
|
|
294
477
|
try {
|
|
295
|
-
await
|
|
296
|
-
|
|
297
|
-
|
|
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;
|
|
298
485
|
}
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
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;
|
|
303
491
|
}
|
|
304
|
-
for (
|
|
305
|
-
var
|
|
306
|
-
for (
|
|
307
|
-
|
|
308
|
-
if (
|
|
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;
|
|
309
497
|
}
|
|
310
498
|
}
|
|
311
499
|
return null;
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
500
|
+
};
|
|
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();
|
|
324
539
|
try {
|
|
325
|
-
var
|
|
326
|
-
|
|
327
|
-
if (!
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
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;
|
|
334
553
|
try {
|
|
335
|
-
|
|
554
|
+
vm.quit();
|
|
555
|
+
await vm.loadProject(source.loadText, { replaceLegacyExtensions: true });
|
|
556
|
+
stage = vm.runtime.getTargetForStage();
|
|
336
557
|
} finally {
|
|
337
|
-
|
|
558
|
+
if (reader.assets === assets) reader.assets = null;
|
|
338
559
|
}
|
|
339
|
-
if (
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
source
|
|
344
|
-
stage
|
|
345
|
-
baseline
|
|
346
|
-
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),
|
|
347
568
|
pending: null
|
|
348
|
-
})
|
|
569
|
+
});
|
|
570
|
+
(_vm$renderer = vm.renderer) === null || _vm$renderer === void 0 || _vm$renderer.draw();
|
|
571
|
+
return "opened";
|
|
349
572
|
} finally {
|
|
350
|
-
|
|
573
|
+
busy = false;
|
|
351
574
|
}
|
|
352
|
-
}
|
|
353
|
-
|
|
575
|
+
};
|
|
576
|
+
const planEdits = (before, after) => {
|
|
577
|
+
var _host$blockReload;
|
|
354
578
|
try {
|
|
355
579
|
return {
|
|
356
580
|
kind: "values",
|
|
357
|
-
changes:
|
|
581
|
+
changes: planBlockReload(before, after)
|
|
358
582
|
};
|
|
359
|
-
} catch (
|
|
360
|
-
if (!(
|
|
583
|
+
} catch (error) {
|
|
584
|
+
if (!(error instanceof FolderError)) throw error;
|
|
361
585
|
}
|
|
362
|
-
if (!((
|
|
586
|
+
if (!((_host$blockReload = host.blockReload) === null || _host$blockReload === void 0 ? void 0 : _host$blockReload.deserializeBlocks)) return null;
|
|
363
587
|
try {
|
|
364
588
|
return {
|
|
365
589
|
kind: "scripts",
|
|
366
|
-
changes:
|
|
590
|
+
changes: planScriptReload(before, after)
|
|
367
591
|
};
|
|
368
|
-
} catch (
|
|
369
|
-
if (!(
|
|
592
|
+
} catch (error) {
|
|
593
|
+
if (!(error instanceof FolderError)) throw error;
|
|
370
594
|
return null;
|
|
371
595
|
}
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
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
|
|
379
607
|
};
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
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());
|
|
390
627
|
};
|
|
391
628
|
return {
|
|
392
629
|
detach() {
|
|
393
|
-
|
|
630
|
+
setCurrent(null);
|
|
394
631
|
},
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
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);
|
|
398
637
|
};
|
|
399
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;
|
|
662
|
+
},
|
|
400
663
|
get canSave() {
|
|
401
|
-
return
|
|
664
|
+
return current !== null;
|
|
402
665
|
},
|
|
403
666
|
get busy() {
|
|
404
|
-
return
|
|
667
|
+
return busy;
|
|
405
668
|
},
|
|
406
669
|
get reloadState() {
|
|
407
|
-
return
|
|
670
|
+
return reloadState();
|
|
408
671
|
},
|
|
409
672
|
get reloadKind() {
|
|
410
|
-
return
|
|
673
|
+
return reloadKind();
|
|
411
674
|
},
|
|
675
|
+
/** A partial change that has stayed the same across two scans and may be applied without asking. */
|
|
412
676
|
get canAutoReload() {
|
|
413
|
-
|
|
414
|
-
return
|
|
677
|
+
const pending = current === null || current === void 0 ? void 0 : current.pending;
|
|
678
|
+
return reloadState() === "ready" && typeof pending === "object" && pending !== null && pending.stable;
|
|
415
679
|
},
|
|
680
|
+
/** True while a change is new or looks incomplete, so a host can look again sooner than its interval. */
|
|
416
681
|
get settling() {
|
|
417
|
-
|
|
418
|
-
return
|
|
682
|
+
const pending = current === null || current === void 0 ? void 0 : current.pending;
|
|
683
|
+
return pending === "waiting" || typeof pending === "object" && pending !== null && !pending.stable;
|
|
419
684
|
},
|
|
685
|
+
/** Identifies the pending project file version, so a host can attempt an automatic apply only once. */
|
|
420
686
|
get pendingVersion() {
|
|
421
|
-
|
|
422
|
-
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;
|
|
423
689
|
},
|
|
424
690
|
get directory() {
|
|
425
|
-
var
|
|
426
|
-
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;
|
|
427
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
|
+
*/
|
|
428
711
|
async comparison() {
|
|
429
|
-
|
|
430
|
-
if (!
|
|
431
|
-
|
|
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;
|
|
432
717
|
try {
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
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;
|
|
438
724
|
}
|
|
439
|
-
let
|
|
725
|
+
let disk;
|
|
440
726
|
try {
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
} catch (
|
|
444
|
-
|
|
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;
|
|
445
731
|
}
|
|
446
|
-
return
|
|
447
|
-
base
|
|
448
|
-
editor
|
|
449
|
-
disk
|
|
732
|
+
return current === session ? {
|
|
733
|
+
base,
|
|
734
|
+
editor,
|
|
735
|
+
disk
|
|
450
736
|
} : null;
|
|
451
737
|
},
|
|
452
738
|
refresh() {
|
|
453
|
-
if (
|
|
454
|
-
if (
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
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;
|
|
458
745
|
}).finally(() => {
|
|
459
|
-
|
|
460
|
-
})
|
|
746
|
+
scanning = null;
|
|
747
|
+
});
|
|
748
|
+
return scanning;
|
|
461
749
|
},
|
|
462
750
|
async reload() {
|
|
463
|
-
if (!
|
|
464
|
-
|
|
465
|
-
|
|
751
|
+
if (!current || reloadState() !== "ready" || !host.blockReload) return false;
|
|
752
|
+
const session = current;
|
|
753
|
+
active(session);
|
|
754
|
+
busy = true;
|
|
466
755
|
try {
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
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;
|
|
470
762
|
try {
|
|
471
|
-
var
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
} catch (
|
|
475
|
-
|
|
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;
|
|
476
769
|
}
|
|
477
|
-
|
|
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;
|
|
478
779
|
} finally {
|
|
479
|
-
|
|
780
|
+
busy = false;
|
|
781
|
+
notifyEdits();
|
|
480
782
|
}
|
|
481
783
|
},
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
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);
|
|
486
793
|
},
|
|
487
|
-
open(
|
|
488
|
-
return
|
|
794
|
+
open(pick, confirmReplace) {
|
|
795
|
+
return openFrom(async () => {
|
|
489
796
|
try {
|
|
490
|
-
return await
|
|
797
|
+
return await pick({
|
|
491
798
|
id: "turbowarp-plus-project-folder",
|
|
492
799
|
mode: "readwrite"
|
|
493
800
|
});
|
|
494
|
-
} catch (
|
|
495
|
-
if (
|
|
496
|
-
throw
|
|
801
|
+
} catch (error) {
|
|
802
|
+
if (error instanceof DOMException && error.name === "AbortError") return null;
|
|
803
|
+
throw error;
|
|
497
804
|
}
|
|
498
|
-
},
|
|
805
|
+
}, confirmReplace);
|
|
806
|
+
},
|
|
807
|
+
openDirectory(directory, confirmReplace) {
|
|
808
|
+
return openFrom(() => Promise.resolve(directory), confirmReplace);
|
|
499
809
|
},
|
|
500
|
-
|
|
501
|
-
|
|
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);
|
|
502
826
|
},
|
|
503
827
|
async save() {
|
|
504
|
-
if (!
|
|
505
|
-
|
|
506
|
-
|
|
828
|
+
if (!current || busy) throw new FolderError("conflict");
|
|
829
|
+
const session = current;
|
|
830
|
+
active(session);
|
|
831
|
+
busy = true;
|
|
507
832
|
try {
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
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);
|
|
518
847
|
}
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
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`);
|
|
528
862
|
}
|
|
529
|
-
|
|
530
|
-
directory:
|
|
531
|
-
files:
|
|
863
|
+
const root = {
|
|
864
|
+
directory: source.directory,
|
|
865
|
+
files: source.files,
|
|
532
866
|
prefix: ""
|
|
533
|
-
}
|
|
534
|
-
|
|
535
|
-
|
|
867
|
+
};
|
|
868
|
+
const assets = {
|
|
869
|
+
directory: await source.directory.getDirectoryHandle("assets", { create: true }),
|
|
870
|
+
files: source.bytes,
|
|
536
871
|
prefix: "assets/"
|
|
537
|
-
}
|
|
538
|
-
|
|
539
|
-
|
|
872
|
+
};
|
|
873
|
+
const extensions = {
|
|
874
|
+
directory: await source.directory.getDirectoryHandle("extensions", { create: true }),
|
|
875
|
+
files: source.extensionBytes,
|
|
540
876
|
prefix: "extensions/"
|
|
541
|
-
}
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
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;
|
|
550
893
|
try {
|
|
551
|
-
if (!
|
|
552
|
-
|
|
553
|
-
if (!
|
|
554
|
-
if (
|
|
555
|
-
|
|
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();
|
|
556
899
|
try {
|
|
557
|
-
await
|
|
558
|
-
|
|
559
|
-
|
|
900
|
+
await writable.write(previous);
|
|
901
|
+
await writable.close();
|
|
902
|
+
} catch (error) {
|
|
903
|
+
await writable.abort().catch(() => {});
|
|
904
|
+
throw error;
|
|
560
905
|
}
|
|
561
|
-
|
|
562
|
-
} else await
|
|
906
|
+
version = fileVersion(await handle.getFile());
|
|
907
|
+
} else await directory.removeEntry(name);
|
|
563
908
|
} finally {
|
|
564
|
-
|
|
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);
|
|
565
913
|
}
|
|
566
|
-
})
|
|
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()));
|
|
567
919
|
};
|
|
568
920
|
try {
|
|
569
|
-
for (
|
|
570
|
-
for (
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
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;
|
|
575
929
|
}
|
|
576
|
-
|
|
577
|
-
|
|
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) {
|
|
578
933
|
try {
|
|
579
|
-
|
|
580
|
-
if (!
|
|
581
|
-
await
|
|
582
|
-
} catch (
|
|
583
|
-
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;
|
|
584
939
|
}
|
|
585
|
-
|
|
940
|
+
files.delete(name);
|
|
941
|
+
source.versions.delete(prefix + name);
|
|
586
942
|
}
|
|
587
|
-
|
|
943
|
+
session.baseline = edited;
|
|
944
|
+
session.ids = ids;
|
|
945
|
+
session.pending = null;
|
|
946
|
+
if (current === session && revision === saveRevision) host.onSaved();
|
|
947
|
+
notify();
|
|
948
|
+
return {
|
|
588
949
|
status: "saved",
|
|
589
|
-
sourceRemoved:
|
|
950
|
+
sourceRemoved: extensionSave.sourceRemoved
|
|
590
951
|
};
|
|
591
952
|
} finally {
|
|
592
|
-
|
|
953
|
+
busy = false;
|
|
954
|
+
notifyEdits();
|
|
593
955
|
}
|
|
594
956
|
},
|
|
595
|
-
dispose
|
|
957
|
+
dispose
|
|
596
958
|
};
|
|
597
959
|
}
|
|
598
960
|
// src/index.ts
|
|
599
|
-
var
|
|
961
|
+
var messages = {
|
|
600
962
|
en: {
|
|
601
963
|
open: "Open Project Folder…",
|
|
602
964
|
save: "Save to Project Folder",
|
|
@@ -604,7 +966,7 @@ var Q = {
|
|
|
604
966
|
opened: "Project folder opened.",
|
|
605
967
|
saved: "Project folder saved.",
|
|
606
968
|
unchanged: "No changes to save.",
|
|
607
|
-
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.`,
|
|
608
970
|
conflict: "The project folder changed outside the editor or is no longer open. Your edits are still in the editor.",
|
|
609
971
|
unsupported: "This project or edit cannot be saved to a project folder yet.",
|
|
610
972
|
failed: "Could not access the project folder.",
|
|
@@ -637,7 +999,7 @@ var Q = {
|
|
|
637
999
|
opened: "プロジェクトフォルダを開きました。",
|
|
638
1000
|
saved: "プロジェクトフォルダに保存しました。",
|
|
639
1001
|
unchanged: "保存する変更はありません。",
|
|
640
|
-
sourceRemoved: (
|
|
1002
|
+
sourceRemoved: (ids) => `差し替えた拡張機能(${ids.join("、")})の出どころ情報を削除しました。出どころを保つには、sb3-toolchain で拡張機能を取得し直してください。`,
|
|
641
1003
|
conflict: "外部でフォルダが変更されたか、作品が切り替わりました。編集中の変更はエディターに残っています。",
|
|
642
1004
|
unsupported: "この作品または編集は、まだプロジェクトフォルダに保存できません。",
|
|
643
1005
|
failed: "プロジェクトフォルダにアクセスできませんでした。",
|
|
@@ -663,13 +1025,23 @@ var Q = {
|
|
|
663
1025
|
waiting: "外部の書き込みが終わるのを待っています。",
|
|
664
1026
|
full: "この変更は作品全体の再読み込みが必要です。"
|
|
665
1027
|
}
|
|
666
|
-
}
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
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, {
|
|
673
1045
|
position: "fixed",
|
|
674
1046
|
insetInlineEnd: "1rem",
|
|
675
1047
|
bottom: "1rem",
|
|
@@ -680,19 +1052,33 @@ function ee({ vm: e, document: t, host: n, locale: r = "en", autoReload: i = !0
|
|
|
680
1052
|
color: "CanvasText",
|
|
681
1053
|
border: "1px solid currentColor",
|
|
682
1054
|
borderRadius: "0.4rem"
|
|
683
|
-
})
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
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, {
|
|
696
1082
|
position: "fixed",
|
|
697
1083
|
insetInlineEnd: "1rem",
|
|
698
1084
|
bottom: "4.5rem",
|
|
@@ -704,152 +1090,230 @@ function ee({ vm: e, document: t, host: n, locale: r = "en", autoReload: i = !0
|
|
|
704
1090
|
border: "1px solid currentColor",
|
|
705
1091
|
borderRadius: "0.4rem"
|
|
706
1092
|
});
|
|
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
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
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();
|
|
745
1137
|
}
|
|
746
|
-
}, y, b = null, x = null, S = null, C = (t) => {
|
|
747
|
-
let n = t();
|
|
748
|
-
_(), n.then((t) => {
|
|
749
|
-
(t === !0 || t === "opened") && d(e.reloaded);
|
|
750
|
-
}, (e) => m(r, e)).finally(_);
|
|
751
1138
|
};
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
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());
|
|
756
1156
|
return;
|
|
757
1157
|
}
|
|
758
|
-
|
|
1158
|
+
notice.hidden = !state;
|
|
1159
|
+
button.hidden = !kind;
|
|
1160
|
+
button.disabled = ![
|
|
759
1161
|
"ready",
|
|
760
1162
|
"full",
|
|
761
1163
|
"dirty"
|
|
762
|
-
].includes(
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
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));
|
|
769
1183
|
};
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
}, (e) => m(r, e));
|
|
774
|
-
}, T = (e) => {
|
|
775
|
-
o.clearTimeout(y), y = o.setTimeout(w, e);
|
|
1184
|
+
const schedule = (delay) => {
|
|
1185
|
+
view.clearTimeout(settle);
|
|
1186
|
+
settle = view.setTimeout(refresh, delay);
|
|
776
1187
|
};
|
|
777
|
-
if (
|
|
778
|
-
|
|
779
|
-
} catch (
|
|
780
|
-
|
|
1188
|
+
if (view.FileSystemObserver) try {
|
|
1189
|
+
observer = new view.FileSystemObserver(() => schedule(300));
|
|
1190
|
+
} catch (_unused) {
|
|
1191
|
+
observer = null;
|
|
781
1192
|
}
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
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)));
|
|
785
1199
|
});
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
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();
|
|
789
1212
|
};
|
|
790
1213
|
}
|
|
791
|
-
|
|
792
|
-
session
|
|
793
|
-
open(
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
}, (
|
|
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();
|
|
798
1222
|
},
|
|
799
|
-
save(
|
|
800
|
-
if (!
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
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();
|
|
806
1237
|
}
|
|
807
1238
|
}
|
|
808
1239
|
};
|
|
809
|
-
|
|
810
|
-
|
|
1240
|
+
menus.set(vm, menu);
|
|
1241
|
+
const unregister = registerFolder(vm, {
|
|
811
1242
|
get directory() {
|
|
812
|
-
return
|
|
1243
|
+
return session.directory;
|
|
813
1244
|
},
|
|
814
|
-
subscribe: (
|
|
815
|
-
|
|
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)
|
|
816
1250
|
});
|
|
817
1251
|
return {
|
|
818
|
-
openDirectory: (...
|
|
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
|
+
*/
|
|
819
1272
|
get directory() {
|
|
820
|
-
return
|
|
1273
|
+
return session.directory;
|
|
821
1274
|
},
|
|
822
1275
|
dispose() {
|
|
823
|
-
|
|
1276
|
+
menus.delete(vm);
|
|
1277
|
+
unregister();
|
|
1278
|
+
session.dispose();
|
|
1279
|
+
stopWatching === null || stopWatching === void 0 || stopWatching();
|
|
1280
|
+
view.clearTimeout(timer);
|
|
1281
|
+
status.remove();
|
|
824
1282
|
}
|
|
825
1283
|
};
|
|
826
1284
|
}
|
|
827
|
-
function
|
|
828
|
-
|
|
829
|
-
if (
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
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()
|
|
833
1292
|
};
|
|
834
1293
|
}
|
|
835
|
-
function
|
|
836
|
-
|
|
837
|
-
if (!
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
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);
|
|
841
1302
|
};
|
|
842
|
-
return
|
|
843
|
-
command: `project-folder-${
|
|
844
|
-
onClick:
|
|
845
|
-
},
|
|
1303
|
+
return createElement(MenuItem, {
|
|
1304
|
+
command: `project-folder-${action}`,
|
|
1305
|
+
onClick: run
|
|
1306
|
+
}, createElement("span", {
|
|
846
1307
|
role: "button",
|
|
847
1308
|
tabIndex: 0,
|
|
848
|
-
onKeyDown(
|
|
849
|
-
(
|
|
1309
|
+
onKeyDown(event) {
|
|
1310
|
+
if (event.key !== "Enter" && event.key !== " ") return;
|
|
1311
|
+
event.preventDefault();
|
|
1312
|
+
event.stopPropagation();
|
|
1313
|
+
run();
|
|
850
1314
|
}
|
|
851
|
-
},
|
|
1315
|
+
}, labels[action]));
|
|
852
1316
|
};
|
|
853
|
-
return
|
|
1317
|
+
return createElement(MenuSection, null, item("open"), entry.session.canSave ? item("save") : null);
|
|
854
1318
|
}
|
|
855
|
-
export {
|
|
1319
|
+
export { createProjectFolder, followProjectFolder, mountProjectFolder, projectFolderActions, renderProjectFolderMenu };
|