Skip to content

Commit

Permalink
Address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ktbytechibong committed Sep 3, 2022
1 parent d1d7318 commit c72bb9a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
28 changes: 14 additions & 14 deletions core/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1371,7 +1371,7 @@ export class Block implements IASTNodeLocation, IDeletable {
* @returns Text of block.
*/
toString(opt_maxLength?: number, opt_emptyToken?: string): string {
const text = [];
const tokens = [];
const emptyFieldPlaceholder = opt_emptyToken || '?';

// Temporarily set flag to navigate to all fields.
Expand Down Expand Up @@ -1410,16 +1410,16 @@ export class Block implements IASTNodeLocation, IDeletable {
case ASTNode.types.INPUT: {
const connection = node.getLocation() as Connection;
if (!node.in()) {
text.push(emptyFieldPlaceholder);
tokens.push(emptyFieldPlaceholder);
} else if (shouldAddParentheses(connection)) {
text.push('(');
tokens.push('(');
}
break;
}
case ASTNode.types.FIELD: {
const field = node.getLocation() as Field;
if (field.name !== constants.COLLAPSED_FIELD_NAME) {
text.push(field.getText());
tokens.push(field.getText());
}
break;
}
Expand All @@ -1437,7 +1437,7 @@ export class Block implements IASTNodeLocation, IDeletable {
// If we hit an input on the way up, possibly close out parentheses.
if (node && node.getType() === ASTNode.types.INPUT &&
shouldAddParentheses(node.getLocation() as Connection)) {
text.push(')');
tokens.push(')');
}
}
if (node) {
Expand All @@ -1452,28 +1452,28 @@ export class Block implements IASTNodeLocation, IDeletable {
// Run through our text array and simplify expression to remove parentheses
// around single field blocks.
// E.g. ['repeat', '(', '10', ')', 'times', 'do', '?']
for (let i = 2; i < text.length; i++) {
if (text[i - 2] === '(' && text[i] === ')') {
text[i - 2] = text[i - 1];
text.splice(i - 1, 2);
for (let i = 2; i < tokens.length; i++) {
if (tokens[i - 2] === '(' && tokens[i] === ')') {
tokens[i - 2] = tokens[i - 1];
tokens.splice(i - 1, 2);
}
}

// Join the text array, removing spaces around added parentheses.
let combinedText: string = text.reduce(function(acc, value) {
let text: string = tokens.reduce(function(acc, value) {
return acc + (acc.substr(-1) === '(' || value === ')' ? '' : ' ') + value;
}, '');

combinedText = combinedText.trim() || '???';
text = text.trim() || '???';
if (opt_maxLength) {
// TODO: Improve truncation so that text from this block is given
// priority. E.g. "1+2+3+4+5+6+7+8+9=0" should be "...6+7+8+9=0", not
// "1+2+3+4+5...". E.g. "1+2+3+4+5=6+7+8+9+0" should be "...4+5=6+7...".
if (combinedText.length > opt_maxLength) {
combinedText = combinedText.substring(0, opt_maxLength - 3) + '...';
if (text.length > opt_maxLength) {
text = text.substring(0, opt_maxLength - 3) + '...';
}
}
return combinedText;
return text;
}

/**
Expand Down
10 changes: 5 additions & 5 deletions core/field_registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,19 @@ export function fromJson(options: AnyDuringMigration): Field|null {
* @param options
*/
function fromJsonInternal(options: AnyDuringMigration): Field|null {
const fieldObject =
registry.getObject(registry.Type.FIELD, options['type']) as unknown as
IRegistrableField |
null;
const fieldObject = registry.getObject(registry.Type.FIELD, options['type']);
if (!fieldObject) {
console.warn(
'Blockly could not create a field of type ' + options['type'] +
'. The field is probably not being registered. This could be because' +
' the file is not loaded, the field does not register itself (Issue' +
' #1584), or the registration is not being reached.');
return null;
} else if (typeof (fieldObject as any)['fromJson'] !== 'function') {
throw new TypeError('returned Field was not a IRegistrableField');
} else {
return (fieldObject as unknown as IRegistrableField).fromJson(options);
}
return fieldObject.fromJson(options);
}

export const TEST_ONLY = {
Expand Down

0 comments on commit c72bb9a

Please sign in to comment.