Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: When using the toolbox-search, auto select the found dropdown item #2222

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

constantinIliescu
Copy link

@constantinIliescu constantinIliescu commented Feb 26, 2024

Fixes: #1940

Uses the Block State as suggested here:
#2004 (comment)

Fixes: bug that didn't indexed the last trigram from a word

The basics

The details

Resolves

Fixes #1940

Proposed Changes

Use block state for efficient indexing

Reason for Changes

Have the found drop down item automatically selected in the drop down

Test Coverage

Unit test have been added to cover the false positive, false negative and duplicate cases

Documentation

Additional Information

Fixes: google#1940

Uses the Block State as suggested here:
google#2004 (comment)
Fixes: bug that didn't indexed the last trigram from a word
@constantinIliescu constantinIliescu requested a review from a team as a code owner February 26, 2024 09:53
@constantinIliescu constantinIliescu requested review from maribethb and removed request for a team February 26, 2024 09:53
@BeksOmega BeksOmega assigned BeksOmega and unassigned maribethb Mar 20, 2024
@BeksOmega BeksOmega requested review from BeksOmega and removed request for maribethb March 20, 2024 22:47
Copy link
Contributor

@BeksOmega BeksOmega left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally LGTM! I'll give it a more thorough review after the first round of changes have been fixed.

blockState.id = undefined;
blockState.extraState = undefined;
blockState.data = undefined;
const stateString = JSON.stringify(blockState);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason you need to stringify this? I think you should be able to add it to a Set<Blockly.serialization.blocks.State>. If the stringification is for deduplication or something, could you add a comment inline?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the reason was duplication, without stringify we would have ended with a lot of duplication. I will put a comment in the code

plugins/toolbox-search/src/block_searcher.ts Show resolved Hide resolved
Comment on lines +177 to +187
if (blockState.fields) {
return {
kind: 'block',
type: blockState.type,
fields: blockState.fields,
};
} else
return {
kind: 'block',
type: blockState.type,
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (blockState.fields) {
return {
kind: 'block',
type: blockState.type,
fields: blockState.fields,
};
} else
return {
kind: 'block',
type: blockState.type,
};
return {kind: 'block', ...blockState};

Comment on lines +31 to +39
if (blockState != null) {
this.indexBlockText(blockType.replaceAll('_', ' '), blockState);
block.inputList.forEach((input) => {
input.fieldRow.forEach((field) => {
this.indexDropdownOption(field, blockState);
this.indexBlockText(field.getText(), blockState);
});
});
});
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of wrapping this could you use an early return?

if (!blockState) return;
this.indexBlockText // etc...

This function already has a lot of nesting and I'd prefer to avoid adding more if we can :P

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, I will fix it

Comment on lines +58 to +60
if (state.fields == undefined) {
state.fields = {};
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this case should be impossible since you assing to state.fields above. Was the typescript compiler giving you warnings about this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it was the compiler complaining. I don't exactly remember

state.fields[field.name] = option[1];
}
this.indexBlockText(option[0], state);
this.indexBlockText(option[1], state);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should be indexing the language-neutral text.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I added this on some blocks the language-neutral text was more informative than the block text. I don't remember now a clear example, but I take a look. BTW, sorry for replaying so late, but lately I am little busy with an internal project. I would try to find some time to fix your comments.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No rush! It took us three weeks to get back to you so we can't expect you to be more responsive :P

Copy link
Contributor

@BeksOmega BeksOmega left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some more comments for when you have time to take a look!

@@ -3,6 +3,12 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [1.2.7](https://github.com/google/blockly-samples/compare/@blockly/toolbox-search@1.2.6...@blockly/toolbox-search@1.2.7) (2024-02-23)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you revert this? The CHANGELOG doesn't need to be edited manually (it gets updated automatically based on submitted PRs)

if (typeof option[0] === 'string') {
this.indexBlockText(option[0], blockType);
if (state.fields == undefined) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throughout, can you replace == with === and != with !==?

private indexDropdownOption(
field: Blockly.Field,
blockState: Blockly.serialization.blocks.State,
) {
if (field instanceof Blockly.FieldDropdown) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you switch this to use an early return as well? (not your code but I think it would make it more readable).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a jsdoc comment to the trigramsToBlocks map that specifies that the Set<string> contains stringified JSON?

Comment on lines 89 to +91
];
const resultState = result.map((item) => JSON.parse(item));
return resultState;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to set this up to just return from the top. But I can't put a suggestion over this whole function b/c github :P

Suggested change
];
const resultState = result.map((item) => JSON.parse(item));
return resultState;
].map(JSON.parse);

Comment on lines +112 to +113
blockState.extraState = undefined;
blockState.data = undefined;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these being removed? It seems like they should probably be kept.

*/
blockTypesMatching(query: string): string[] {
return [
blockTypesMatching(query: string): Blockly.serialization.blocks.State[] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this is public, changing the return type of this method is actually a breaking change. Could you create another method blockStatesMatching that has the current behavior here? And then modify this to only return the block /types/ not the full state. You'll need to update other places to use your new blockStatesMatching method.

@maribethb maribethb changed the title Fix: When using the toolbox-search, auto select the found dropdown item fix: When using the toolbox-search, auto select the found dropdown item Mar 26, 2024
@BeksOmega
Copy link
Contributor

@constantinIliescu are you still interested in working on this? If not I'll close this on Friday!

@constantinIliescu
Copy link
Author

Hi Beka,
Yes, I intend to finish this , but I still need some more time. I been caught in more urgent things lately.

@BeksOmega
Copy link
Contributor

Hi Beka, Yes, I intend to finish this , but I still need some more time. I been caught in more urgent things lately.

All good no rush! Just wanted to check in in case you wanted to hand it over to someone else =)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update toolbox-search to search dropdown field options
3 participants