Skip to content
This repository has been archived by the owner on Oct 4, 2022. It is now read-only.

P2 786 only render ok when block is valid #1146

Merged
merged 41 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
4f15ce7
Added a method that checks if the blockValidationResult is considered…
Apr 7, 2021
d976c74
Adds a check to see if the added suggestion is valid
Apr 7, 2021
0e3a803
Updates the tests for the blocksuggestions
Apr 7, 2021
f772213
Improval of the test by mocking the functions directly
Apr 7, 2021
bc2a734
Merge branch 'develop' of https://github.com/Yoast/javascript into P2…
Apr 8, 2021
9eb9930
Process CR
Apr 8, 2021
c467972
Remove typehints in jsdoc
Apr 8, 2021
54dfb3a
Merge branch 'develop' of https://github.com/Yoast/javascript into P2…
Apr 8, 2021
cd2ea53
Check if the block validation is OK
Apr 12, 2021
c67c29a
Merge branch 'develop' of https://github.com/Yoast/javascript into P2…
Apr 12, 2021
ebfa7e5
Merge branch 'develop' of https://github.com/Yoast/javascript into P2…
Apr 14, 2021
8e2debd
Refactored the BlockSuggestionsPresenter a little bit
Apr 15, 2021
7404075
Changes the working of the validation a little bit.
Apr 15, 2021
51d120d
make the SidebarWarningPresenter retrieve validation for a client Id
increddibelly Apr 15, 2021
74d36e4
fix unit tests
increddibelly Apr 16, 2021
c89071d
extract the BlockSuggestionsPresenter logic from the component
increddibelly Apr 16, 2021
e95a228
BlockSuggestionsPresenter allows more detailed control now; validatio…
increddibelly Apr 19, 2021
565f3fe
Merge branch 'develop' into P2-786-only-render-ok-when-block-is-valid
increddibelly Apr 19, 2021
0426ce0
added additional valid results and validation
increddibelly Apr 20, 2021
9c10dcf
Add validation to RichtTextBase
increddibelly Apr 20, 2021
e251ab9
remove unused code
increddibelly Apr 20, 2021
25356ce
added ValidationBlockInstruction to allow blocks to opt-in validation
increddibelly Apr 23, 2021
9872f78
fixing unit tests
increddibelly Apr 23, 2021
cd31dfe
fixed unit tests
increddibelly Apr 29, 2021
94a719f
Bump @wordpress/data to the latest version
johannadevos Apr 29, 2021
87a09ca
prevent an error for missing instructions
increddibelly Apr 29, 2021
f9f5aaf
Merge remote-tracking branch 'origin/release/16.3' into P2-786-only-r…
increddibelly Apr 29, 2021
cad6799
replaced ValidatingBlockInstruction base class with single function
increddibelly Apr 30, 2021
9f8404e
Merge remote-tracking branch 'origin/release/16.3' into P2-786-only-r…
increddibelly Apr 30, 2021
1d0e61d
fine tune variationpicker validation
increddibelly May 3, 2021
a41779b
fix Date validation
increddibelly May 3, 2021
3c14f53
typo
increddibelly May 3, 2021
e28dcb0
Merge remote-tracking branch 'origin/develop' into P2-786-only-render…
increddibelly May 3, 2021
8db91db
fix Date tests.
increddibelly May 4, 2021
318b5a3
Update packages/schema-blocks/src/functions/presenters/BlockSuggestio…
increddibelly May 6, 2021
0f4d7b1
Update packages/schema-blocks/src/functions/presenters/BlockSuggestio…
increddibelly May 6, 2021
94fa03a
processed CR comments
increddibelly May 6, 2021
c93634a
Update packages/schema-blocks/src/core/validation/BlockValidationResu…
increddibelly May 6, 2021
3cfa7f1
fix CS
increddibelly May 6, 2021
a92916b
make method of reading current block content consistent with other Yo…
increddibelly May 6, 2021
0053d33
Removed superfluous isValid check from BlockSuggestionsPresenter.
hansjovis May 6, 2021
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
Prev Previous commit
Next Next commit
fine tune variationpicker validation
  • Loading branch information
increddibelly committed May 3, 2021
commit 1d0e61db546682ed6a79540f3415fca7e5f7a83e
12 changes: 6 additions & 6 deletions packages/schema-blocks/src/core/Instruction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ export default abstract class Instruction {
* @returns {void}
*/
static register<I extends typeof Instruction>( this: I, name: string, instruction: InstructionClass<I["prototype"]> ): void {
if ( typeof Instruction.registeredInstructions === "undefined" ) {
Instruction.registeredInstructions = {};
if ( typeof this.registeredInstructions === "undefined" ) {
this.registeredInstructions = {};
}

Instruction.registeredInstructions[ name ] = instruction;
this.registeredInstructions[ name ] = instruction;
}

/**
Expand All @@ -93,11 +93,11 @@ export default abstract class Instruction {
* @returns The instruction instance.
*/
static create<I extends typeof Instruction>( this: I, name: string, id: number, options: InstructionOptions ): I["prototype"] {
if ( typeof Instruction.registeredInstructions === "undefined" ) {
Instruction.registeredInstructions = {};
if ( typeof this.registeredInstructions === "undefined" ) {
this.registeredInstructions = {};
}

const klass = Instruction.registeredInstructions[ name ];
const klass = this.registeredInstructions[ name ];

if ( ! klass ) {
logger.error( "Invalid instruction: ", name );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export class BlockValidationResult {
constructor( clientId: string, name: string, result: BlockValidation, blockPresence: BlockPresence, message?: string ) {
this.name = name;
this.clientId = clientId;
this.name = name;
this.result = result;
this.blockPresence = blockPresence;
this.issues = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ export function InnerBlocksSidebar( props: InnerBlocksSidebarProps ): ReactEleme
return <Fragment key={ "innerblocks-sidebar-" + props.currentBlock.clientId }>
<SidebarHeader />
<WarningList warnings={ warnings } />
henk
<BlockSuggestions
heading={ __( "Required Blocks", "yoast-schema-blocks" ) }
parentClientId={ props.currentBlock.clientId }
Expand Down Expand Up @@ -108,7 +107,7 @@ function WarningList( props: WarningListProps ): ReactElement {
<div className="yoast-block-sidebar-warnings">
<div className="yoast-block-sidebar-title">{ __( "Analysis", "yoast-schema-blocks" ) }</div>
<ul className="yoast-block-sidebar-warnings">
{ ...props.warnings.map( warning => <Warning warning={ warning } key={ warning.text } /> ) }
{...props.warnings.map( warning => <Warning warning={ warning } key={ warning.text } /> )}
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
{...props.warnings.map( warning => <Warning warning={ warning } key={ warning.text } /> )}
{ ...props.warnings.map( warning => <Warning warning={ warning } key={ warning.text } /> ) }

</ul>
</div>
</Fragment>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { __, sprintf } from "@wordpress/i18n";
import { BlockValidation, BlockValidationResult, BlockPresence } from "../../core/validation";
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice that you consolidated these imports 👍

import { getHumanReadableBlockName } from "../BlockHelper";
import { getAllDescendantIssues } from "../validators";
import logger from "../logger";

/**
* A warning message for in the sidebar schema analysis.
Expand Down Expand Up @@ -90,8 +89,6 @@ export function createAnalysisMessages( validation: BlockValidationResult ): Sid
}

const issues = getAllDescendantIssues( validation );
logger.debug( "Issues:", issues );

const messages = [];

messages.push( ...getErrorMessages( issues ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ export function isValidResult( source: BlockValidation ): boolean {
export function isMissingResult( source: BlockValidation ): boolean {
return [
BlockValidation.MissingRecommendedBlock,
BlockValidation.MissingRecommendedVariation,

BlockValidation.MissingRequiredBlock,
BlockValidation.MissingRequiredVariation,
].includes( source );
}

Expand All @@ -39,6 +36,8 @@ export function isEmptyResult( source: BlockValidation ): boolean {
return [
BlockValidation.MissingRecommendedAttribute,
BlockValidation.MissingRequiredAttribute,
BlockValidation.MissingRecommendedVariation,
BlockValidation.MissingRequiredVariation,
].includes( source );
}

Expand Down
20 changes: 16 additions & 4 deletions packages/schema-blocks/src/instructions/blocks/Heading.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Fragment } from "react";
import { BlockControls, RichText as WordPressRichText } from "@wordpress/block-editor";
import { BlockConfiguration } from "@wordpress/blocks";
import { BlockConfiguration, BlockInstance } from "@wordpress/blocks";
import { createElement } from "@wordpress/element";
import { Fragment } from "react";

import BlockInstruction from "../../core/blocks/BlockInstruction";
import { BlockInstruction, BlockLeaf } from "../../core/blocks";
import { RenderEditProps, RenderSaveProps } from "../../core/blocks/BlockDefinition";
import { BlockValidationResult } from "../../core/validation";
import RichTextBase, { RichTextEditProps, RichTextSaveProps } from "./abstract/RichTextBase";
import BlockLeaf from "../../core/blocks/BlockLeaf";
import HeadingLevelDropdown from "../../functions/presenters/HeadingLevelDropdown";
import { defaultValidate } from "../../functions/validators/defaultValidate";


/**
Expand Down Expand Up @@ -121,6 +122,17 @@ export class Heading extends RichTextBase {
key: i,
};
}

/**
* Checks if the instruction block is valid.
*
* @param blockInstance The attributes from the block.
*
* @returns {BlockValidationResult} The validation result.
*/
validate( blockInstance: BlockInstance ): BlockValidationResult {
return defaultValidate( blockInstance, this );
}
}

BlockInstruction.register( "heading", Heading );
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { BlockConfiguration, BlockInstance } from "@wordpress/blocks";
import { BlockConfiguration, BlockInstance, getBlockContent } from "@wordpress/blocks";
import { RichText as WordPressRichText } from "@wordpress/block-editor";
import { createElement } from "@wordpress/element";
import { BlockLeaf, BlockInstruction } from "../../../core/blocks";
import { RenderSaveProps, RenderEditProps } from "../../../core/blocks/BlockDefinition";
import { BlockPresence, BlockValidationResult } from "../../../core/validation";
import { BlockPresence, BlockValidation, BlockValidationResult } from "../../../core/validation";

export interface RichTextSaveProps extends WordPressRichText.ContentProps<keyof HTMLElementTagNameMap> {
"data-id": string;
Expand Down Expand Up @@ -86,25 +86,26 @@ export default abstract class RichTextBase extends BlockInstruction {
*/
validate( blockInstance: BlockInstance ): BlockValidationResult {
let presence = BlockPresence.Unknown;
let validation = BlockValidation.Unknown;
if ( this.options.required === true ) {
presence = BlockPresence.Required;
validation = BlockValidation.MissingRequiredAttribute;
} else {
if ( this.options.required === false ) {
presence = BlockPresence.Recommended;
}
presence = BlockPresence.Recommended;
validation = BlockValidation.MissingRecommendedAttribute;
}

// Does this block have any HTML content?
if ( blockInstance.originalContent ) {
// Get the current editor content of this block from the store.
const content: string = getBlockContent( blockInstance ) || "";
increddibelly marked this conversation as resolved.
Show resolved Hide resolved
if ( content ) {
// Remove all characters from < up to and including > (i.e. strip the tags).
const innerText = blockInstance.originalContent.replace( /(<([^>]+)>)/ig, "" );

if ( innerText.length > 0 ) {
return BlockValidationResult.Valid( blockInstance, this.constructor.name, presence );
const innerText = content.replace( /(<([^>]+)>)/ig, "" );
if ( innerText.trim().length > 0 ) {
return BlockValidationResult.Valid( blockInstance, this.options.name, presence );
}
}

return BlockValidationResult.MissingBlock( this.constructor.name, presence );
return new BlockValidationResult( blockInstance.clientId, this.options.name, validation, presence );
}

/**
Expand Down