@kubohiroya/turbowarp-plus-editor-code-views 0.0.0-stage → 0.2.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/LICENSE +373 -0
- package/README.ja.md +42 -0
- package/README.md +41 -2
- package/dist/asset-edit-session.d.ts +24 -0
- package/dist/asset-edit-session.js +58 -0
- package/dist/block-argument-text.d.ts +39 -0
- package/dist/block-argument-text.js +170 -0
- package/dist/block-drag.d.ts +17 -0
- package/dist/block-drag.js +184 -0
- package/dist/code-extensions.d.ts +38 -0
- package/dist/code-extensions.js +418 -0
- package/dist/code-pane.d.ts +80 -0
- package/dist/code-pane.js +398 -0
- package/dist/custom-extension-fields.d.ts +45 -0
- package/dist/custom-extension-fields.js +144 -0
- package/dist/detached-block-palette-view.d.ts +10 -0
- package/dist/detached-block-palette-view.js +62 -0
- package/dist/detached-block-palette.d.ts +41 -0
- package/dist/detached-block-palette.js +197 -0
- package/dist/dockview-unsaved.d.ts +45 -0
- package/dist/dockview-unsaved.js +123 -0
- package/dist/history.d.ts +57 -0
- package/dist/history.js +85 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +729 -0
- package/dist/native-extension-field.d.ts +26 -0
- package/dist/native-extension-field.js +204 -0
- package/dist/native-field-svg.d.ts +20 -0
- package/dist/native-field-svg.js +156 -0
- package/dist/palette-category-reorder.d.ts +4 -0
- package/dist/palette-category-reorder.js +167 -0
- package/dist/palette-category-search.d.ts +26 -0
- package/dist/palette-category-search.js +74 -0
- package/dist/palette-dropdown-resize.d.ts +19 -0
- package/dist/palette-dropdown-resize.js +135 -0
- package/dist/parent-field-layout.d.ts +2 -0
- package/dist/parent-field-layout.js +90 -0
- package/dist/parent-field-mirror.d.ts +3 -0
- package/dist/parent-field-mirror.js +290 -0
- package/dist/parent-field-resources.d.ts +9 -0
- package/dist/parent-field-resources.js +199 -0
- package/dist/preview-chrome.d.ts +9 -0
- package/dist/preview-chrome.js +78 -0
- package/dist/procedure-editor.d.ts +9 -0
- package/dist/procedure-editor.js +122 -0
- package/dist/procedure-edits.d.ts +16 -0
- package/dist/procedure-edits.js +47 -0
- package/dist/programmatic-extension-field.d.ts +20 -0
- package/dist/programmatic-extension-field.js +69 -0
- package/dist/protocol.d.ts +15 -0
- package/dist/protocol.js +36 -0
- package/dist/script-layout-dialog.d.ts +10 -0
- package/dist/script-layout-dialog.js +93 -0
- package/dist/script-layout-view.d.ts +68 -0
- package/dist/script-layout-view.js +164 -0
- package/dist/script-layout-workspace.d.ts +45 -0
- package/dist/script-layout-workspace.js +55 -0
- package/dist/script-layout.d.ts +17 -0
- package/dist/script-layout.js +25 -0
- package/dist/script-view-menu.d.ts +46 -0
- package/dist/script-view-menu.js +161 -0
- package/dist/sprite-code-windows.d.ts +32 -0
- package/dist/sprite-code-windows.js +180 -0
- package/dist/stage-code-prewarm.d.ts +52 -0
- package/dist/stage-code-prewarm.js +216 -0
- package/dist/target-combobox.d.ts +10 -0
- package/dist/target-combobox.js +92 -0
- package/dist/variable-references.d.ts +1 -0
- package/dist/variable-references.js +87 -0
- package/dist/workspace.d.ts +104 -0
- package/dist/workspace.js +1102 -0
- package/package.json +100 -4
- package/src/asset-edit-session.ts +72 -0
- package/src/block-argument-text.ts +172 -0
- package/src/block-drag.ts +190 -0
- package/src/code-extensions.ts +394 -0
- package/src/code-pane.ts +393 -0
- package/src/custom-extension-fields.ts +154 -0
- package/src/detached-block-palette-view.tsx +97 -0
- package/src/detached-block-palette.ts +218 -0
- package/src/dockview-unsaved.ts +134 -0
- package/src/history.ts +114 -0
- package/src/index.ts +680 -0
- package/src/native-extension-field.ts +201 -0
- package/src/native-field-svg.ts +140 -0
- package/src/palette-category-reorder.ts +155 -0
- package/src/palette-category-search.ts +82 -0
- package/src/palette-dropdown-resize.ts +132 -0
- package/src/parent-field-layout.ts +88 -0
- package/src/parent-field-mirror.ts +260 -0
- package/src/parent-field-resources.ts +179 -0
- package/src/preview-chrome.ts +82 -0
- package/src/procedure-editor.ts +123 -0
- package/src/procedure-edits.ts +46 -0
- package/src/programmatic-extension-field.ts +76 -0
- package/src/protocol.ts +47 -0
- package/src/script-layout-dialog.ts +104 -0
- package/src/script-layout-view.ts +198 -0
- package/src/script-layout-workspace.ts +95 -0
- package/src/script-layout.ts +40 -0
- package/src/script-view-menu.ts +213 -0
- package/src/sprite-code-windows.tsx +183 -0
- package/src/stage-code-prewarm.ts +242 -0
- package/src/styles.css +595 -0
- package/src/target-combobox.tsx +106 -0
- package/src/variable-references.ts +85 -0
- package/src/workspace.ts +1125 -0
- package/tsconfig.json +23 -0
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { isSafeSvgDrawingAttribute, svgDrawingElements } from "./native-field-svg.js";
|
|
2
|
+
const maxBytes = 256 * 1024;
|
|
3
|
+
const maxSide = 512;
|
|
4
|
+
const idPattern = /^[\w-]{1,48}$/;
|
|
5
|
+
const pngHeader = [137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82];
|
|
6
|
+
const svgNamespace = 'http://www.w3.org/2000/svg';
|
|
7
|
+
function readManifest(manifest) {
|
|
8
|
+
const assets = new Map();
|
|
9
|
+
if (!manifest || typeof manifest !== 'object' || Array.isArray(manifest))
|
|
10
|
+
return assets;
|
|
11
|
+
for (const [id, value] of Object.entries(manifest).slice(0, 8)) {
|
|
12
|
+
if (!idPattern.test(id) || !value || typeof value !== 'object')
|
|
13
|
+
continue;
|
|
14
|
+
const { kind, url } = value;
|
|
15
|
+
if ((kind !== 'image' && kind !== 'font') || typeof url !== 'string' || url.length > 2048)
|
|
16
|
+
continue;
|
|
17
|
+
const parsed = URL.parse(url);
|
|
18
|
+
if (parsed?.protocol !== 'https:' || parsed.username || parsed.password || parsed.hash)
|
|
19
|
+
continue;
|
|
20
|
+
assets.set(id, { kind, url: parsed.href });
|
|
21
|
+
}
|
|
22
|
+
return assets;
|
|
23
|
+
}
|
|
24
|
+
async function readAsset(parent, asset, signal) {
|
|
25
|
+
const response = await parent.fetch(asset.url, {
|
|
26
|
+
signal, mode: 'cors', credentials: 'omit', redirect: 'error', referrerPolicy: 'no-referrer'
|
|
27
|
+
});
|
|
28
|
+
const type = response.headers.get('content-type')?.split(';')[0].trim().toLowerCase();
|
|
29
|
+
const acceptable = asset.kind === 'font' ? type === 'font/woff2' : type === 'image/png' || type === 'image/svg+xml';
|
|
30
|
+
if (!response.ok || !acceptable || !response.body || Number(response.headers.get('content-length')) > maxBytes) {
|
|
31
|
+
await response.body?.cancel();
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
const reader = response.body.getReader();
|
|
35
|
+
const buffer = new Uint8Array(maxBytes);
|
|
36
|
+
let count = 0;
|
|
37
|
+
try {
|
|
38
|
+
for (;;) {
|
|
39
|
+
const { done, value } = await reader.read();
|
|
40
|
+
if (done)
|
|
41
|
+
return { type, bytes: buffer.slice(0, count) };
|
|
42
|
+
if (count + value.byteLength > maxBytes) {
|
|
43
|
+
await reader.cancel();
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
buffer.set(value, count);
|
|
47
|
+
count += value.byteLength;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
finally {
|
|
51
|
+
reader.releaseLock();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
function staticSvg(document, bytes) {
|
|
55
|
+
const view = document.defaultView;
|
|
56
|
+
const markup = new TextDecoder().decode(bytes);
|
|
57
|
+
if (/<!doctype/i.test(markup))
|
|
58
|
+
return;
|
|
59
|
+
const svg = new view.DOMParser().parseFromString(markup, 'image/svg+xml').documentElement;
|
|
60
|
+
if (svg.localName !== 'svg' || svg.namespaceURI !== svgNamespace)
|
|
61
|
+
return;
|
|
62
|
+
const output = document.createElementNS(svgNamespace, 'svg');
|
|
63
|
+
for (const name of ['width', 'height']) {
|
|
64
|
+
const size = svg.getAttribute(name) ?? '';
|
|
65
|
+
if (!/^\d+(?:\.\d+)?(?:px)?$/.test(size) || Number.parseFloat(size) <= 0 || Number.parseFloat(size) > maxSide)
|
|
66
|
+
return;
|
|
67
|
+
output.setAttribute(name, size);
|
|
68
|
+
}
|
|
69
|
+
const viewBox = svg.getAttribute('viewBox')?.trim().split(/[\s,]+/);
|
|
70
|
+
if (viewBox?.length === 4 && viewBox.every(value => /^-?\d+(?:\.\d+)?$/.test(value) && Number.isFinite(Number(value))) &&
|
|
71
|
+
Number(viewBox[2]) > 0 && Number(viewBox[3]) > 0)
|
|
72
|
+
output.setAttribute('viewBox', viewBox.join(' '));
|
|
73
|
+
let remaining = 128;
|
|
74
|
+
function copy(original, target, depth) {
|
|
75
|
+
for (const { name, value } of original.attributes) {
|
|
76
|
+
if ((depth || (name !== 'width' && name !== 'height')) &&
|
|
77
|
+
isSafeSvgDrawingAttribute(name, value))
|
|
78
|
+
target.setAttribute(name, value);
|
|
79
|
+
}
|
|
80
|
+
for (const child of original.childNodes) {
|
|
81
|
+
if (remaining <= 0)
|
|
82
|
+
break;
|
|
83
|
+
if (child.nodeType === 3 && (original.localName === 'text' || original.localName === 'tspan')) {
|
|
84
|
+
remaining--;
|
|
85
|
+
target.append(child.textContent.slice(0, 1024));
|
|
86
|
+
}
|
|
87
|
+
else if (child.nodeType === 1 && depth < 8) {
|
|
88
|
+
const element = child;
|
|
89
|
+
if (element.namespaceURI !== svgNamespace || !svgDrawingElements.has(element.localName))
|
|
90
|
+
continue;
|
|
91
|
+
remaining--;
|
|
92
|
+
const next = document.createElementNS(svgNamespace, element.localName);
|
|
93
|
+
copy(element, next, depth + 1);
|
|
94
|
+
target.append(next);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
copy(svg, output, 0);
|
|
99
|
+
return new view.XMLSerializer().serializeToString(output);
|
|
100
|
+
}
|
|
101
|
+
async function rasterize(document, type, bytes, signal) {
|
|
102
|
+
if (signal.aborted)
|
|
103
|
+
return;
|
|
104
|
+
let data = bytes;
|
|
105
|
+
if (type === 'image/png') {
|
|
106
|
+
if (bytes.length < 24 || !pngHeader.every((byte, index) => bytes[index] === byte))
|
|
107
|
+
return;
|
|
108
|
+
const header = new DataView(bytes.buffer);
|
|
109
|
+
if (![header.getUint32(16), header.getUint32(20)].every(side => side > 0 && side <= maxSide))
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
data = staticSvg(document, bytes);
|
|
114
|
+
if (!data)
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
const view = document.defaultView;
|
|
118
|
+
const image = document.createElement('img');
|
|
119
|
+
const url = view.URL.createObjectURL(new view.Blob([data], { type }));
|
|
120
|
+
const revoke = () => view.URL.revokeObjectURL(url);
|
|
121
|
+
signal.addEventListener('abort', revoke, { once: true });
|
|
122
|
+
try {
|
|
123
|
+
// Decode SVG only in image mode, then send pixels to the popup.
|
|
124
|
+
image.src = url;
|
|
125
|
+
await image.decode();
|
|
126
|
+
const { naturalWidth: width, naturalHeight: height } = image;
|
|
127
|
+
if (signal.aborted || !width || !height || width > maxSide || height > maxSide)
|
|
128
|
+
return;
|
|
129
|
+
const canvas = document.createElement('canvas');
|
|
130
|
+
canvas.width = width;
|
|
131
|
+
canvas.height = height;
|
|
132
|
+
const context = canvas.getContext('2d');
|
|
133
|
+
if (!context)
|
|
134
|
+
return;
|
|
135
|
+
context.drawImage(image, 0, 0);
|
|
136
|
+
return canvas.toDataURL('image/png');
|
|
137
|
+
}
|
|
138
|
+
finally {
|
|
139
|
+
signal.removeEventListener('abort', revoke);
|
|
140
|
+
revoke();
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
export function createFieldResources(source, options, valid) {
|
|
144
|
+
const assets = readManifest(options?.manifest);
|
|
145
|
+
const pending = new Map();
|
|
146
|
+
const fonts = [];
|
|
147
|
+
const controller = new AbortController();
|
|
148
|
+
const parent = source.ownerDocument.defaultView;
|
|
149
|
+
async function load(asset, child) {
|
|
150
|
+
const signal = AbortSignal.any([controller.signal, AbortSignal.timeout(5000)]);
|
|
151
|
+
if (!await options.canFetch(asset.url) || signal.aborted || !valid())
|
|
152
|
+
return;
|
|
153
|
+
const resource = await readAsset(parent, asset, signal);
|
|
154
|
+
if (!resource || signal.aborted || !valid())
|
|
155
|
+
return;
|
|
156
|
+
const { type, bytes } = resource;
|
|
157
|
+
if (asset.kind === 'image')
|
|
158
|
+
return rasterize(source.ownerDocument, type, bytes, signal);
|
|
159
|
+
if (String.fromCharCode(...bytes.subarray(0, 4)) !== 'wOF2')
|
|
160
|
+
return;
|
|
161
|
+
const family = `tw-field-font-${parent.crypto.randomUUID()}`;
|
|
162
|
+
const face = new child.defaultView.FontFace(family, bytes);
|
|
163
|
+
await face.load();
|
|
164
|
+
if (signal.aborted || !valid())
|
|
165
|
+
return;
|
|
166
|
+
child.fonts.add(face);
|
|
167
|
+
fonts.push({ document: child, face });
|
|
168
|
+
return family;
|
|
169
|
+
}
|
|
170
|
+
function assetFor(node, kind) {
|
|
171
|
+
const asset = assets.get(node.getAttribute(`data-plus-field-${kind}`) ?? '');
|
|
172
|
+
return asset?.kind === kind ? asset : undefined;
|
|
173
|
+
}
|
|
174
|
+
return {
|
|
175
|
+
hasImage(node) {
|
|
176
|
+
return Boolean(assetFor(node, 'image'));
|
|
177
|
+
},
|
|
178
|
+
attach(original, proxy) {
|
|
179
|
+
const image = proxy.localName === 'img';
|
|
180
|
+
const asset = assetFor(original, image ? 'image' : 'font');
|
|
181
|
+
if (!asset)
|
|
182
|
+
return;
|
|
183
|
+
if (!pending.has(asset))
|
|
184
|
+
pending.set(asset, load(asset, proxy.ownerDocument).catch(() => undefined));
|
|
185
|
+
return pending.get(asset).then(value => {
|
|
186
|
+
if (!value || controller.signal.aborted || !valid())
|
|
187
|
+
return;
|
|
188
|
+
if (image)
|
|
189
|
+
proxy.src = value;
|
|
190
|
+
return image ? undefined : value;
|
|
191
|
+
});
|
|
192
|
+
},
|
|
193
|
+
dispose() {
|
|
194
|
+
controller.abort();
|
|
195
|
+
for (const { document, face } of fonts)
|
|
196
|
+
document.fonts.delete(face);
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export type PresentationElements = (injection: HTMLElement) => {
|
|
2
|
+
wrapper?: Element | null;
|
|
3
|
+
extensionButton?: HTMLElement | null;
|
|
4
|
+
watermark?: HTMLElement | null;
|
|
5
|
+
};
|
|
6
|
+
export declare function captureCodePreviewChrome(workspace: any, target: any, presentationElements: PresentationElements | null, includeNavigation: boolean): {
|
|
7
|
+
mount(container: any): void;
|
|
8
|
+
dispose(): void;
|
|
9
|
+
};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// These are display-only copies of the controls that belong to the shared editable workspace.
|
|
2
|
+
export function captureCodePreviewChrome(workspace, target, presentationElements, includeNavigation) {
|
|
3
|
+
const injection = workspace.getInjectionDiv();
|
|
4
|
+
const svg = workspace.getParentSvg();
|
|
5
|
+
const { wrapper, extensionButton, watermark } = presentationElements?.(injection) ?? {};
|
|
6
|
+
if (!wrapper)
|
|
7
|
+
return null;
|
|
8
|
+
const chrome = injection.ownerDocument.createElement('div');
|
|
9
|
+
chrome.setAttribute('inert', '');
|
|
10
|
+
chrome.dataset.codePreviewChrome = '';
|
|
11
|
+
const origin = wrapper.getBoundingClientRect();
|
|
12
|
+
function copy(node, { right = false, bottom = false, stretch = false, deep = true } = {}) {
|
|
13
|
+
if (!node)
|
|
14
|
+
return;
|
|
15
|
+
const rect = node.getBoundingClientRect();
|
|
16
|
+
if (!rect.width || !rect.height)
|
|
17
|
+
return;
|
|
18
|
+
const clone = node.cloneNode(deep);
|
|
19
|
+
clone.removeAttribute('id');
|
|
20
|
+
for (const child of clone.querySelectorAll('[id]'))
|
|
21
|
+
child.removeAttribute('id');
|
|
22
|
+
const style = clone.style;
|
|
23
|
+
// Override the native positioning rules while retaining the control's theme and contents.
|
|
24
|
+
for (const [key, value] of Object.entries({
|
|
25
|
+
position: 'absolute',
|
|
26
|
+
left: right && !stretch ? 'auto' : `${rect.left - origin.left}px`,
|
|
27
|
+
right: right || stretch ? `${origin.right - rect.right}px` : 'auto',
|
|
28
|
+
top: bottom ? 'auto' : `${rect.top - origin.top}px`,
|
|
29
|
+
bottom: bottom ? `${origin.bottom - rect.bottom}px` : 'auto',
|
|
30
|
+
width: stretch ? 'auto' : `${rect.width}px`,
|
|
31
|
+
height: `${rect.height}px`
|
|
32
|
+
}))
|
|
33
|
+
style.setProperty(key, value, 'important');
|
|
34
|
+
chrome.appendChild(clone);
|
|
35
|
+
return clone;
|
|
36
|
+
}
|
|
37
|
+
const palette = injection.querySelector(':scope > .blocklyToolboxDiv');
|
|
38
|
+
const paletteCopy = copy(palette, { stretch: palette?.hasAttribute('data-horizontal-block-palette') });
|
|
39
|
+
const paletteScroll = palette?.scrollLeft ?? 0;
|
|
40
|
+
copy(injection.querySelector(':scope > .blocklyFlyout'));
|
|
41
|
+
copy(injection.querySelector(':scope > .blocklyFlyoutScrollbar'));
|
|
42
|
+
copy(extensionButton);
|
|
43
|
+
const zoom = includeNavigation && svg.querySelector('.blocklyZoom');
|
|
44
|
+
if (zoom) {
|
|
45
|
+
const shell = copy(svg, { right: true, bottom: true, deep: false });
|
|
46
|
+
if (shell) {
|
|
47
|
+
shell.classList.remove('blocklySvg');
|
|
48
|
+
shell.appendChild(zoom.cloneNode(true));
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
copy(injection.querySelector(':scope > .blocklyScrollbarVertical.blocklyMainWorkspaceScrollbar'), { right: true });
|
|
52
|
+
copy(injection.querySelector(':scope > .blocklyScrollbarHorizontal.blocklyMainWorkspaceScrollbar'), { bottom: true });
|
|
53
|
+
if (includeNavigation) {
|
|
54
|
+
const minimap = copy(injection.querySelector(':scope > [data-workspace-minimap]'), { right: true, bottom: true });
|
|
55
|
+
const image = copy(watermark, { right: true, bottom: true });
|
|
56
|
+
// The GUI throttles costume changes, so the last frame may still show the previous target.
|
|
57
|
+
const asset = target.getCostumes()[target.currentCostume]?.asset;
|
|
58
|
+
if (minimap && watermark && asset) {
|
|
59
|
+
const mark = image ?? watermark.cloneNode(false);
|
|
60
|
+
const img = mark.ownerDocument.createElement('img');
|
|
61
|
+
img.src = asset.encodeDataURI();
|
|
62
|
+
mark.replaceChildren(img);
|
|
63
|
+
mark.style.cssText = watermark.style.cssText;
|
|
64
|
+
mark.style.position = 'absolute';
|
|
65
|
+
chrome.appendChild(mark);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return {
|
|
69
|
+
mount(container) {
|
|
70
|
+
container.appendChild(chrome);
|
|
71
|
+
if (paletteCopy)
|
|
72
|
+
paletteCopy.scrollLeft = paletteScroll;
|
|
73
|
+
},
|
|
74
|
+
dispose() {
|
|
75
|
+
chrome.remove();
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
export function installProcedureEditor({ blocks, workspace, getLocale, canEdit, commit }) {
|
|
2
|
+
const document = workspace.getInjectionDiv().ownerDocument;
|
|
3
|
+
let cancel = () => { };
|
|
4
|
+
let creating = false;
|
|
5
|
+
const createDefinition = blocks.Procedures.createProcedureDefCallback_;
|
|
6
|
+
blocks.Procedures.createProcedureDefCallback_ = function (source) {
|
|
7
|
+
creating = true;
|
|
8
|
+
try {
|
|
9
|
+
createDefinition.call(this, source);
|
|
10
|
+
}
|
|
11
|
+
finally {
|
|
12
|
+
creating = false;
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
blocks.Procedures.externalProcedureDefCallback = (mutation, apply) => {
|
|
16
|
+
if (!canEdit())
|
|
17
|
+
return;
|
|
18
|
+
cancel();
|
|
19
|
+
const originalName = creating ? null : mutation.getAttribute('proccode');
|
|
20
|
+
const ja = /^ja(?:-|$)/i.test(getLocale());
|
|
21
|
+
const text = ja ? {
|
|
22
|
+
title: 'ブロック定義', input: '引数を追加(数値または文字列)', boolean: '真偽値の引数を追加',
|
|
23
|
+
label: 'ラベルを追加', warp: '画面を再描画せずに実行する', cancel: 'キャンセル', save: 'OK',
|
|
24
|
+
duplicate: 'このブロック定義はすでにあります。'
|
|
25
|
+
} : {
|
|
26
|
+
title: 'Block definition', input: 'Add number or text input', boolean: 'Add boolean input',
|
|
27
|
+
label: 'Add label', warp: 'Run without screen refresh', cancel: 'Cancel', save: 'OK',
|
|
28
|
+
duplicate: 'This block definition already exists.'
|
|
29
|
+
};
|
|
30
|
+
const dialog = document.createElement('dialog');
|
|
31
|
+
dialog.className = 'twPlusProcedureDialog';
|
|
32
|
+
dialog.setAttribute('aria-label', text.title);
|
|
33
|
+
dialog.innerHTML = `<form>
|
|
34
|
+
<h2></h2>
|
|
35
|
+
<div data-procedure-workspace></div>
|
|
36
|
+
<div data-procedure-buttons></div>
|
|
37
|
+
<label><input type="checkbox"><span></span></label>
|
|
38
|
+
<p role="alert" hidden></p>
|
|
39
|
+
<footer class="tw-plus-dialog-actions"></footer>
|
|
40
|
+
</form>`;
|
|
41
|
+
dialog.querySelector('h2').textContent = text.title;
|
|
42
|
+
dialog.querySelector('label span').textContent = text.warp;
|
|
43
|
+
const error = dialog.querySelector('p');
|
|
44
|
+
const form = dialog.querySelector('form');
|
|
45
|
+
const addButton = (container, label, onClick) => {
|
|
46
|
+
const button = document.createElement('button');
|
|
47
|
+
button.type = onClick ? 'button' : 'submit';
|
|
48
|
+
button.textContent = label;
|
|
49
|
+
if (onClick)
|
|
50
|
+
button.addEventListener('click', onClick);
|
|
51
|
+
dialog.querySelector(container).appendChild(button);
|
|
52
|
+
return button;
|
|
53
|
+
};
|
|
54
|
+
const area = dialog.querySelector('[data-procedure-workspace]');
|
|
55
|
+
document.body.appendChild(dialog);
|
|
56
|
+
dialog.showModal();
|
|
57
|
+
// ScratchBlocks positions its text editor in document coordinates.
|
|
58
|
+
// The full-viewport dialog keeps that widget inside the modal focus scope.
|
|
59
|
+
const widget = blocks.WidgetDiv.DIV;
|
|
60
|
+
dialog.appendChild(widget);
|
|
61
|
+
const draft = blocks.inject(area, {
|
|
62
|
+
toolbox: '<xml></xml>', media: workspace.options.pathToMedia, scrollbars: true,
|
|
63
|
+
rtl: workspace.RTL, zoom: { startScale: 0.9 }
|
|
64
|
+
});
|
|
65
|
+
const declaration = draft.newBlock('procedures_declaration');
|
|
66
|
+
declaration.setMovable(false);
|
|
67
|
+
declaration.setDeletable(false);
|
|
68
|
+
declaration.contextMenu = false;
|
|
69
|
+
declaration.domToMutation(mutation);
|
|
70
|
+
declaration.initSvg();
|
|
71
|
+
declaration.render();
|
|
72
|
+
declaration.moveBy(16, 20);
|
|
73
|
+
const warp = dialog.querySelector('input');
|
|
74
|
+
warp.checked = declaration.getWarp();
|
|
75
|
+
cancel = () => {
|
|
76
|
+
cancel = () => { };
|
|
77
|
+
blocks.WidgetDiv.hide(true);
|
|
78
|
+
document.body.appendChild(widget);
|
|
79
|
+
draft.dispose();
|
|
80
|
+
workspace.markFocused();
|
|
81
|
+
dialog.close();
|
|
82
|
+
dialog.remove();
|
|
83
|
+
};
|
|
84
|
+
dialog.addEventListener('cancel', event => {
|
|
85
|
+
event.preventDefault();
|
|
86
|
+
cancel();
|
|
87
|
+
});
|
|
88
|
+
const addPart = (label, add) => addButton('[data-procedure-buttons]', label, () => {
|
|
89
|
+
blocks.WidgetDiv.hide(true);
|
|
90
|
+
declaration.onChangeFn();
|
|
91
|
+
add.call(declaration);
|
|
92
|
+
});
|
|
93
|
+
addPart(text.input, declaration.addStringNumberExternal);
|
|
94
|
+
addPart(text.boolean, declaration.addBooleanExternal);
|
|
95
|
+
addPart(text.label, declaration.addLabelExternal);
|
|
96
|
+
addButton('footer', text.cancel, () => cancel()).dataset.dialogAction = 'cancel';
|
|
97
|
+
addButton('footer', text.save).dataset.dialogAction = 'confirm';
|
|
98
|
+
form.addEventListener('submit', event => {
|
|
99
|
+
event.preventDefault();
|
|
100
|
+
if (!canEdit()) {
|
|
101
|
+
cancel();
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
blocks.WidgetDiv.hide(true);
|
|
105
|
+
declaration.onChangeFn();
|
|
106
|
+
declaration.setWarp(warp.checked);
|
|
107
|
+
const result = declaration.mutationToDom(true);
|
|
108
|
+
const name = result.getAttribute('proccode');
|
|
109
|
+
if (name !== originalName && blocks.Procedures.getDefineBlock(name, workspace)) {
|
|
110
|
+
error.textContent = text.duplicate;
|
|
111
|
+
error.hidden = false;
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
cancel();
|
|
115
|
+
commit(() => {
|
|
116
|
+
apply(result);
|
|
117
|
+
workspace.refreshToolboxSelection_();
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
};
|
|
121
|
+
return { cancel: () => cancel() };
|
|
122
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export declare function removeObsoleteProcedureInputs(blocks: any, blockIds: Set<string>): void;
|
|
2
|
+
/**
|
|
3
|
+
* Whether a script editor shows only the hat of a script. The flag is kept in the hat's mutation, so it is
|
|
4
|
+
* saved in the project and travels with the script. ScratchBlocks ignores the attribute on hats that read
|
|
5
|
+
* no mutation, such as a function definition's.
|
|
6
|
+
*/
|
|
7
|
+
export declare const foldedDefinitionAttribute = "tw-plus-folded";
|
|
8
|
+
type MutableBlock = {
|
|
9
|
+
mutation?: Record<string, unknown> & {
|
|
10
|
+
tagName?: string;
|
|
11
|
+
children?: unknown[];
|
|
12
|
+
};
|
|
13
|
+
} | undefined;
|
|
14
|
+
export declare function isFoldedDefinition(block: MutableBlock): boolean;
|
|
15
|
+
export declare function setFoldedDefinition(block: NonNullable<MutableBlock>, folded: boolean): void;
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// ScratchBlocks disconnects removed arguments before emitting the mutation.
|
|
2
|
+
// The VM keeps the old input slots and ignores delete events for their shadows.
|
|
3
|
+
export function removeObsoleteProcedureInputs(blocks, blockIds) {
|
|
4
|
+
for (const id of blockIds) {
|
|
5
|
+
const block = blocks.getBlock(id);
|
|
6
|
+
if (block?.opcode !== 'procedures_call' && block?.opcode !== 'procedures_prototype')
|
|
7
|
+
continue;
|
|
8
|
+
const argumentIds = new Set(JSON.parse(block.mutation.argumentids));
|
|
9
|
+
for (const name of Object.keys(block.inputs)) {
|
|
10
|
+
if (argumentIds.has(name))
|
|
11
|
+
continue;
|
|
12
|
+
const { block: childId, shadow: shadowId } = block.inputs[name];
|
|
13
|
+
if (childId && childId !== shadowId && blocks.getBlock(childId)?.parent === id) {
|
|
14
|
+
throw new Error('Procedure edit left a removed argument connected');
|
|
15
|
+
}
|
|
16
|
+
delete block.inputs[name];
|
|
17
|
+
const shadow = shadowId && blocks.getBlock(shadowId);
|
|
18
|
+
if (shadow?.shadow && (shadow.parent === id || shadow.parent == null))
|
|
19
|
+
blocks.deleteBlock(shadowId);
|
|
20
|
+
}
|
|
21
|
+
blocks.resetCache();
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Whether a script editor shows only the hat of a script. The flag is kept in the hat's mutation, so it is
|
|
26
|
+
* saved in the project and travels with the script. ScratchBlocks ignores the attribute on hats that read
|
|
27
|
+
* no mutation, such as a function definition's.
|
|
28
|
+
*/
|
|
29
|
+
export const foldedDefinitionAttribute = 'tw-plus-folded';
|
|
30
|
+
export function isFoldedDefinition(block) {
|
|
31
|
+
return block?.mutation?.[foldedDefinitionAttribute] === 'true';
|
|
32
|
+
}
|
|
33
|
+
export function setFoldedDefinition(block, folded) {
|
|
34
|
+
if (folded) {
|
|
35
|
+
block.mutation = { tagName: 'mutation', children: [], ...block.mutation, [foldedDefinitionAttribute]: 'true' };
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
if (!block.mutation)
|
|
39
|
+
return;
|
|
40
|
+
const { [foldedDefinitionAttribute]: _, ...rest } = block.mutation;
|
|
41
|
+
// Drop the mutation this flag made, so an unfolded hat saves as it did before.
|
|
42
|
+
const empty = Object.keys(rest).every(key => key === 'tagName' || key === 'children') && !rest.children?.length;
|
|
43
|
+
if (empty)
|
|
44
|
+
delete block.mutation;
|
|
45
|
+
else
|
|
46
|
+
block.mutation = rest;
|
|
47
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export type FieldAction = {
|
|
2
|
+
id: string;
|
|
3
|
+
label: string;
|
|
4
|
+
};
|
|
5
|
+
export type FieldActionPanel = {
|
|
6
|
+
title: string;
|
|
7
|
+
actions: readonly FieldAction[];
|
|
8
|
+
};
|
|
9
|
+
/** A field editor supplied by a trusted extension. Callbacks run in the parent editor. */
|
|
10
|
+
export interface ProgrammaticExtensionField {
|
|
11
|
+
type: 'turbowarp_plus_actions';
|
|
12
|
+
getEditor(value: string): FieldActionPanel;
|
|
13
|
+
applyAction(value: string, actionId: string): string | undefined | Promise<string | undefined>;
|
|
14
|
+
}
|
|
15
|
+
export declare function runFieldAction(editor: ProgrammaticExtensionField, value: string, actionId: string, complete: (next: string | undefined) => void, onError: (error: unknown) => void): void;
|
|
16
|
+
export declare function readFieldActionPanel(editor: ProgrammaticExtensionField, value: string): FieldActionPanel | undefined;
|
|
17
|
+
export declare function createFieldActionDialog(document: Document): {
|
|
18
|
+
close: () => void;
|
|
19
|
+
show(panel: FieldActionPanel, select: (id: string) => void, locale?: string): void;
|
|
20
|
+
};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
export function runFieldAction(editor, value, actionId, complete, onError) {
|
|
2
|
+
try {
|
|
3
|
+
const next = editor.applyAction(value, actionId);
|
|
4
|
+
if (typeof next === 'string')
|
|
5
|
+
complete(next);
|
|
6
|
+
else if (next === undefined)
|
|
7
|
+
complete(undefined);
|
|
8
|
+
else
|
|
9
|
+
void Promise.resolve(next).then(complete).catch(onError);
|
|
10
|
+
}
|
|
11
|
+
catch (error) {
|
|
12
|
+
onError(error);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
export function readFieldActionPanel(editor, value) {
|
|
16
|
+
const panel = editor.getEditor(value);
|
|
17
|
+
if (typeof panel?.title !== 'string' || !Array.isArray(panel.actions) ||
|
|
18
|
+
!panel.actions.length || panel.actions.length > 32)
|
|
19
|
+
return;
|
|
20
|
+
const actions = [];
|
|
21
|
+
const ids = new Set();
|
|
22
|
+
for (const action of panel.actions) {
|
|
23
|
+
if (typeof action?.id !== 'string' || !action.id || typeof action.label !== 'string' || ids.has(action.id))
|
|
24
|
+
return;
|
|
25
|
+
ids.add(action.id);
|
|
26
|
+
actions.push({ id: action.id, label: action.label });
|
|
27
|
+
}
|
|
28
|
+
return { title: panel.title, actions };
|
|
29
|
+
}
|
|
30
|
+
export function createFieldActionDialog(document) {
|
|
31
|
+
let current;
|
|
32
|
+
const close = () => {
|
|
33
|
+
current?.close();
|
|
34
|
+
current?.remove();
|
|
35
|
+
current = undefined;
|
|
36
|
+
};
|
|
37
|
+
return { close, show(panel, select, locale) {
|
|
38
|
+
close();
|
|
39
|
+
const dialog = document.createElement('dialog');
|
|
40
|
+
const title = document.createElement('h2');
|
|
41
|
+
title.textContent = panel.title;
|
|
42
|
+
dialog.setAttribute('aria-label', panel.title);
|
|
43
|
+
dialog.append(title);
|
|
44
|
+
for (const { id, label } of panel.actions) {
|
|
45
|
+
const button = document.createElement('button');
|
|
46
|
+
button.textContent = label;
|
|
47
|
+
button.addEventListener('click', () => {
|
|
48
|
+
close();
|
|
49
|
+
select(id);
|
|
50
|
+
});
|
|
51
|
+
dialog.append(button);
|
|
52
|
+
}
|
|
53
|
+
const cancel = document.createElement('button');
|
|
54
|
+
cancel.textContent = /^ja(?:-|$)/i.test(locale) ? 'キャンセル' : 'Cancel';
|
|
55
|
+
cancel.addEventListener('click', close);
|
|
56
|
+
cancel.dataset.dialogAction = 'cancel';
|
|
57
|
+
const footer = document.createElement('div');
|
|
58
|
+
footer.className = 'tw-plus-dialog-actions';
|
|
59
|
+
footer.append(cancel);
|
|
60
|
+
dialog.append(footer);
|
|
61
|
+
dialog.addEventListener('cancel', event => {
|
|
62
|
+
event.preventDefault();
|
|
63
|
+
close();
|
|
64
|
+
});
|
|
65
|
+
document.body.append(dialog);
|
|
66
|
+
current = dialog;
|
|
67
|
+
dialog.showModal();
|
|
68
|
+
} };
|
|
69
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
interface Edit {
|
|
2
|
+
type: string;
|
|
3
|
+
commentId?: string;
|
|
4
|
+
blockId?: string;
|
|
5
|
+
element?: string;
|
|
6
|
+
xml?: string | {
|
|
7
|
+
outerHTML: string;
|
|
8
|
+
};
|
|
9
|
+
[field: string]: unknown;
|
|
10
|
+
}
|
|
11
|
+
export declare function isCommentEdit(event: Edit): boolean;
|
|
12
|
+
export declare const isCodeEdit: (event: Edit) => boolean;
|
|
13
|
+
export declare function encodeEdit(event: Edit): Edit;
|
|
14
|
+
export declare function decodeEdit(event: Edit): Edit;
|
|
15
|
+
export {};
|
package/dist/protocol.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
const blockFields = {
|
|
2
|
+
create: ['blockId', 'xml'],
|
|
3
|
+
change: ['blockId', 'element', 'name', 'newValue'],
|
|
4
|
+
move: ['blockId', 'oldParentId', 'oldInputName', 'newParentId', 'newInputName', 'newCoordinate'],
|
|
5
|
+
delete: ['blockId']
|
|
6
|
+
};
|
|
7
|
+
const commentFields = {
|
|
8
|
+
comment_create: ['commentId', 'blockId', 'text', 'xy', 'width', 'height', 'minimized'],
|
|
9
|
+
comment_change: ['commentId', 'newContents_'],
|
|
10
|
+
comment_move: ['commentId', 'newCoordinate_'],
|
|
11
|
+
comment_delete: ['commentId', 'blockId']
|
|
12
|
+
};
|
|
13
|
+
export function isCommentEdit(event) {
|
|
14
|
+
return event && Object.hasOwn(commentFields, event.type) && typeof event.commentId === 'string';
|
|
15
|
+
}
|
|
16
|
+
function isBlockEdit(event) {
|
|
17
|
+
return event && Object.hasOwn(blockFields, event.type) && typeof event.blockId === 'string' &&
|
|
18
|
+
(event.type !== 'change' || ['field', 'mutation'].includes(event.element));
|
|
19
|
+
}
|
|
20
|
+
export const isCodeEdit = (event) => isBlockEdit(event) || isCommentEdit(event);
|
|
21
|
+
export function encodeEdit(event) {
|
|
22
|
+
if (!isCodeEdit(event))
|
|
23
|
+
return null;
|
|
24
|
+
const fields = isCommentEdit(event) ? commentFields : blockFields;
|
|
25
|
+
const data = { type: event.type };
|
|
26
|
+
for (const key of fields[event.type])
|
|
27
|
+
data[key] = key === 'xml' ? event.xml.outerHTML : event[key];
|
|
28
|
+
return data;
|
|
29
|
+
}
|
|
30
|
+
export function decodeEdit(event) {
|
|
31
|
+
if (!isCodeEdit(event))
|
|
32
|
+
return null;
|
|
33
|
+
if (event.type === 'create')
|
|
34
|
+
return typeof event.xml === 'string' ? { ...event, xml: { outerHTML: event.xml } } : null;
|
|
35
|
+
return event;
|
|
36
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type LayoutEvents, type LayoutWorkspace } from './script-layout-workspace.ts';
|
|
2
|
+
export interface RearrangementContext {
|
|
3
|
+
workspace: LayoutWorkspace;
|
|
4
|
+
events: LayoutEvents;
|
|
5
|
+
canArrange(): boolean;
|
|
6
|
+
target(): unknown;
|
|
7
|
+
}
|
|
8
|
+
export declare function openRearrangement({ workspace, events, canArrange, target }: RearrangementContext, locale: string): {
|
|
9
|
+
close: () => void;
|
|
10
|
+
};
|