Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "scratch-gui",
"version": "3.8.0",
"version": "3.8.1",
"private": true,
"description": "MistWarp editor, player, and community site",
"author": "MistWarp contributors",
Expand Down
2 changes: 1 addition & 1 deletion src/community/commit-changes.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const scriptWasMoved = script => script.change === 'moved';
export const hasMarkedLines = diff => String(diff || '').split('\n').some(line => /^[+-]\s/.test(line));

export const plainScriptTitle = title => String(title || '')
.replace(/\s*::\s*[a-z]+/gi, '')
.replace(/\s*::\s*[#\w-]+/g, '')
.replace(/\[([^\]]*?)(?: v)?\]/g, '$1')
.trim();

Expand Down
3 changes: 2 additions & 1 deletion src/community/components/DiffView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import BlocksCompare from './BlocksCompare.jsx';
import EmptyState from './ui/EmptyState.jsx';
import StatusMessage from './ui/StatusMessage.jsx';
import {groupFilesBySprite, spriteLabel, spriteOfPath} from './SpriteList.jsx';
import {plainScriptTitle} from '../commit-changes.js';
import styles from './DiffView.module.css';

const DIFF_HEADER = /^diff --(?:mwp|git) a\/(.+?) b\/(.+)$/;
Expand Down Expand Up @@ -559,7 +560,7 @@ const DiffView = ({diff, spriteFilter = '', onOpenFile = null, loadAsset = null,
>
{blockScripts[group.name].slice(0, MAX_BLOCK_SCRIPTS).map(script => (
<div className={styles.blockScript} key={script.key}>
<span className={styles.blockDesc}>{script.desc}</span>
<span className={styles.blockDesc}>{plainScriptTitle(script.desc)}</span>
<BlocksCompare source={script.diff} />
</div>
))}
Expand Down
42 changes: 29 additions & 13 deletions src/community/scratchblocks-translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ const cleanMenuValue = value => {
return text.replace(/^_+|_+$/g, '');
};

const obscuredValue = node => (node.active && node.active.type !== 'null' ? node.active : node.shadow);

const unwrapCall = value => {
let node = value;
while (node && node.type === 'call' && node.value && node.value !== node) node = node.value;
while (node && ((node.type === 'call' && node.value && node.value !== node) || node.type === 'obscured')) {
node = node.type === 'obscured' ? obscuredValue(node) : node.value;
}
return node;
};

Expand All @@ -46,9 +50,18 @@ const opcodeOf = node => {

const isMenuCall = node => {
const name = calleeName(node);
return MENU_SHADOWS.has(name) || name.endsWith('menu') || name.endsWith('menus') || name.startsWith('menu_');
if (MENU_SHADOWS.has(name) || name.endsWith('menu') || name.endsWith('menus')) return true;
if (name.startsWith('menu_')) return true;
const args = node?.args || [];
return name.includes('_menu_') && args.length === 1 && args[0].sep === 'field';
};

const wordsOf = name => String(name)
.replace(/_/g, ' ')
.replace(/([a-z0-9])([A-Z])/g, '$1 $2')
.replace(/([A-Z]+)([A-Z][a-z])/g, '$1 $2')
.toLowerCase();

const translateMenuValue = value => {
if (typeof value === 'string') return cleanMenuValue(value);
const node = unwrapCall(value);
Expand Down Expand Up @@ -134,7 +147,11 @@ const inputOf = (node, key, fallback = '0') => {
const booleanInputOf = (node, key) => {
const value = unwrapCall(argValue(node, key));
if (!value || value.type === 'null') return '<>';
return translateValue(value);
const inner = value.type === 'call' && value.callee ? value : null;
if (!inner || isMenuCall(inner)) return translateValue(value);
// eslint-disable-next-line no-use-before-define
const text = translateCall(inner);
return text.startsWith('<') && text.endsWith('>') ? text : `<${text}>`;
};

const extensionColor = (text, color) => `${text} :: ${color}`;
Expand Down Expand Up @@ -163,7 +180,7 @@ export const translateCall = node => {
const extension = translateKnownExtension(node);
if (extension) return extension;
if (node.callee.type === 'identOrMethod') {
const name = String(node.callee.method || node.callee.ident || 'extension block').replace(/_/g, ' ');
const name = wordsOf(node.callee.method || node.callee.ident || 'extension block');
const inputs = (node.args || [])
.filter(arg => arg.value !== null && typeof arg.value !== 'undefined')
.map(arg => translateValue(arg.value))
Expand Down Expand Up @@ -275,8 +292,8 @@ export const translateCall = node => {
case 'operator_lt': return `<${inputOf(node, 'OPERAND1')} < ${inputOf(node, 'OPERAND2')}>`;
case 'operator_equals': return `<${inputOf(node, 'OPERAND1')} = ${inputOf(node, 'OPERAND2')}>`;
case 'operator_gt': return `<${inputOf(node, 'OPERAND1')} > ${inputOf(node, 'OPERAND2')}>`;
case 'operator_and': return `<${inputOf(node, 'OPERAND1')} and ${inputOf(node, 'OPERAND2')}>`;
case 'operator_or': return `<${inputOf(node, 'OPERAND1')} or ${inputOf(node, 'OPERAND2')}>`;
case 'operator_and': return `<${booleanInputOf(node, 'OPERAND1')} and ${booleanInputOf(node, 'OPERAND2')}>`;
case 'operator_or': return `<${booleanInputOf(node, 'OPERAND1')} or ${booleanInputOf(node, 'OPERAND2')}>`;
case 'operator_not': return `<not ${booleanInputOf(node, 'OPERAND')}>`;
case 'operator_join': return `(join ${inputOf(node, 'STRING1', 'apple')} ${inputOf(node, 'STRING2', 'banana')})`;
case 'operator_letter_of': return `(letter ${inputOf(node, 'LETTER', '1')} of ${inputOf(node, 'STRING', 'apple')})`;
Expand Down Expand Up @@ -325,14 +342,16 @@ export const translateCall = node => {
case 'pen_setPenHueToNumber': return `set pen color to ${inputOf(node, 'HUE', '50')}`;
case 'pen_changePenHueBy': return `change pen color by ${inputOf(node, 'HUE', '10')}`;
default: {
const pretty = opcode.includes('_') ? opcode.slice(opcode.indexOf('_') + 1).replace(/_/g, ' ') : opcode;
const prefix = opcode.split('_')[0];
const core = CORE_OPCODE_PREFIXES.has(prefix);
const rest = opcode.includes('_') ? opcode.slice(opcode.indexOf('_') + 1) : opcode;
const pretty = core ? rest.replace(/_/g, ' ') : wordsOf(rest);
const inputs = (node.args || [])
.filter(arg => arg.key && arg.value !== null && typeof arg.value !== 'undefined')
.map(arg => translateValue(arg.value))
.join(' ');
const text = inputs ? `${pretty} ${inputs}` : pretty;
const prefix = opcode.split('_')[0];
return !CORE_OPCODE_PREFIXES.has(prefix) ? `${text} :: extension` : text;
return core ? text : `${text} :: extension`;
}
}
};
Expand Down Expand Up @@ -419,10 +438,7 @@ const translateStatements = (calls, indent, marks) => {
};

const conditionOf = (node, key) => {
const rawValue = argValue(node, key);
const value = unwrapCall(rawValue);
if (!value || value.type === 'null') return '<>';
const text = translateValue(rawValue);
const text = booleanInputOf(node, key);
return text.startsWith('<') && text.endsWith('>') ? text : `<${text}>`;
};

Expand Down
1 change: 1 addition & 0 deletions test/unit/community-commit-changes.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ describe('commit changes helpers', () => {
expect(plainScriptTitle('when [any v] key pressed')).toBe('when any key pressed');
expect(plainScriptTitle('isVisible [iframe1] :: extension')).toBe('isVisible iframe1');
expect(plainScriptTitle('Define "Typing system %s"')).toBe('Define "Typing system %s"');
expect(plainScriptTitle('set saved (key) to (1) :: #277a59')).toBe('set saved (key) to (1)');
});

test('rounds long decimals in saved values', () => {
Expand Down
38 changes: 38 additions & 0 deletions test/unit/community-scratchblocks-translate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,44 @@ const translateSource = source => {
};

describe('scratchblocks translation', () => {
test('draws extension booleans, menus and hidden defaults in the right shapes', () => {
const parsed = parseFractch([
'sprite "A";',
'when broadcast "X" at 0,0 {',
' if touchingMouse() && lmsLooksPlus.spriteVisible(' +
'TARGET: shadow lmsLooksPlus.menu_spriteMenu(field spriteMenu: "Field to type in")) {',
' append(lists["MOUSE HOVERING"], 0);',
' }',
' if strings.identical(OPERAND1: vars["WHAT WAS TYPED"], OPERAND2: (PASSWORD ?? "a")) {',
' say 1;',
' }',
' if !lmsclonesplus.isClone() {',
' say 2;',
' }',
'}',
''
].join('\n'));
const text = translateScript(parsed.scripts[0]);
expect(text).toContain(
'if <<touching [mouse-pointer v]?> and <sprite visible [Field to type in v] :: extension>> then'
);
expect(text).toContain('if <identical (WHAT WAS TYPED) (PASSWORD) :: extension> then');
expect(text).toContain('if <not <is clone :: extension>> then');
expect(text).not.toContain('<(');
expect(text).not.toContain('menu');
});

test('keeps extension reporters with menu in their name as reporters', () => {
const parsed = parseFractch([
'sprite "A";',
'when flag at 0,0 {',
' say lmsFoo.thing_menu_x(VALUE: 3);',
'}',
''
].join('\n'));
expect(translateScript(parsed.scripts[0])).toContain('say (thing menu x (3) :: extension)');
});

test('translates hats to scratch language', () => {
expect(translateSource('when flag {\n}\n')).toContain('when green flag clicked');
expect(translateSource('when broadcast go {\n}\n')).toContain('when I receive [go v]');
Expand Down