Skip to content

Commit

Permalink
merge-upstream: make scratch-blocks lazy again
Browse files Browse the repository at this point in the history
  • Loading branch information
GarboMuffin committed Dec 26, 2023
1 parent c89ac37 commit 1170792
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/containers/blocks.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
SOUNDS_TAB_INDEX
} from '../reducers/editor-tab';
import AddonHooks from '../addons/hooks.js';
import LoadScratchBlocksHOC from '../lib/tw-load-scratch-blocks-hoc.jsx';

// TW: Strings we add to scratch-blocks are localized here
const messages = defineMessages({
Expand Down Expand Up @@ -807,5 +808,5 @@ export default injectIntl(errorBoundaryHOC('Blocks')(
connect(
mapStateToProps,
mapDispatchToProps
)(Blocks)
)(LoadScratchBlocksHOC(Blocks))
));
3 changes: 2 additions & 1 deletion src/containers/custom-procedures.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import defaultsDeep from 'lodash.defaultsdeep';
import PropTypes from 'prop-types';
import React from 'react';
import CustomProceduresComponent from '../components/custom-procedures/custom-procedures.jsx';
import ScratchBlocks from 'scratch-blocks';
import LazyScratchBlocks from '../lib/tw-lazy-scratch-blocks';
import {connect} from 'react-redux';

class CustomProcedures extends React.Component {
Expand Down Expand Up @@ -37,6 +37,7 @@ class CustomProcedures extends React.Component {
{rtl: this.props.isRtl}
);

const ScratchBlocks = LazyScratchBlocks.get();
// @todo This is a hack to make there be no toolbox.
const oldDefaultToolbox = ScratchBlocks.Blocks.defaultToolbox;
ScratchBlocks.Blocks.defaultToolbox = null;
Expand Down
3 changes: 2 additions & 1 deletion src/lib/backpack/block-to-image.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import computedStyleToInlineStyle from 'computed-style-to-inline-style';
import ScratchBlocks from 'scratch-blocks';
import LazyScratchBlocks from '../tw-lazy-scratch-blocks';

/**
* Given a blockId, return a data-uri image that can be used to create a thumbnail.
Expand All @@ -8,6 +8,7 @@ import ScratchBlocks from 'scratch-blocks';
*/
export default function (blockId) {
// Not sure any better way to access the scratch-blocks workspace than this...
const ScratchBlocks = LazyScratchBlocks.get();
const block = ScratchBlocks.getMainWorkspace().getBlockById(blockId);
const blockSvg = block.getSvgRoot().cloneNode(true /* deep */);

Expand Down
4 changes: 3 additions & 1 deletion src/lib/blocks.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import LazyScratchBlocks from './tw-lazy-scratch-blocks';

/**
* Connect scratch blocks with the vm
* @param {VirtualMachine} vm - The scratch vm
* @return {ScratchBlocks} ScratchBlocks connected with the vm
*/
export default function (vm) {
const ScratchBlocks = require('scratch-blocks');
const ScratchBlocks = LazyScratchBlocks.get();
const jsonForMenuBlock = function (name, menuOptionsFn, colors, start) {
return {
message0: '%1',
Expand Down
23 changes: 15 additions & 8 deletions src/lib/make-toolbox-xml.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import ScratchBlocks from 'scratch-blocks';
import LazyScratchBlocks from './tw-lazy-scratch-blocks';
import {defaultBlockColors} from './themes';

const categorySeparator = '<sep gap="36"/>';

const blockSeparator = '<sep gap="36"/>'; // At default scale, about 28px

const translate = (id, english) => {
if (LazyScratchBlocks.isLoaded()) {
return LazyScratchBlocks.get().ScratchMsgs.translate(id, english);
}
return english;
};

/* eslint-disable no-unused-vars */
const motion = function (isInitialSetup, isStage, targetId, colors) {
const stageSelected = ScratchBlocks.ScratchMsgs.translate(
const stageSelected = translate(
'MOTION_STAGE_SELECTED',
'Stage selected: no motion blocks'
);
Expand Down Expand Up @@ -154,8 +161,8 @@ const xmlEscape = function (unsafe) {
};

const looks = function (isInitialSetup, isStage, targetId, costumeName, backdropName, colors) {
const hello = ScratchBlocks.ScratchMsgs.translate('LOOKS_HELLO', 'Hello!');
const hmm = ScratchBlocks.ScratchMsgs.translate('LOOKS_HMM', 'Hmm...');
const hello = translate('LOOKS_HELLO', 'Hello!');
const hmm = translate('LOOKS_HMM', 'Hmm...');
// Note: the category's secondaryColour matches up with the blocks' tertiary color, both used for border color.
return `
<category name="%{BKY_CATEGORY_LOOKS}" id="looks" colour="${colors.primary}" secondaryColour="${colors.tertiary}">
Expand Down Expand Up @@ -441,7 +448,7 @@ const control = function (isInitialSetup, isStage, targetId, colors) {
};

const sensing = function (isInitialSetup, isStage, targetId, colors) {
const name = ScratchBlocks.ScratchMsgs.translate('SENSING_ASK_TEXT', 'What\'s your name?');
const name = translate('SENSING_ASK_TEXT', 'What\'s your name?');
// Note: the category's secondaryColour matches up with the blocks' tertiary color, both used for border color.
return `
<category
Expand Down Expand Up @@ -521,9 +528,9 @@ const sensing = function (isInitialSetup, isStage, targetId, colors) {
};

const operators = function (isInitialSetup, isStage, targetId, colors) {
const apple = ScratchBlocks.ScratchMsgs.translate('OPERATORS_JOIN_APPLE', 'apple');
const banana = ScratchBlocks.ScratchMsgs.translate('OPERATORS_JOIN_BANANA', 'banana');
const letter = ScratchBlocks.ScratchMsgs.translate('OPERATORS_LETTEROF_APPLE', 'a');
const apple = translate('OPERATORS_JOIN_APPLE', 'apple');
const banana = translate('OPERATORS_JOIN_BANANA', 'banana');
const letter = translate('OPERATORS_LETTEROF_APPLE', 'a');
// Note: the category's secondaryColour matches up with the blocks' tertiary color, both used for border color.
return `
<category
Expand Down

0 comments on commit 1170792

Please sign in to comment.