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

P2-674 Adds exports needed for the apply button #1127

Closed
wants to merge 14 commits into from
Closed
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
4 changes: 2 additions & 2 deletions packages/schema-blocks/src/blocks/BlockSuggestions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type BlockSuggestionDto = {
*
* @returns {ReactElement} The rendered block suggestion.
*/
function BlockSuggestion( { blockTitle, blockName, blockClientId }: BlockSuggestionDto ): ReactElement {
export function BlockSuggestion( { blockTitle, blockName, blockClientId }: BlockSuggestionDto ): ReactElement {
/**
* Onclick handler for the remove block.
*/
Expand Down Expand Up @@ -67,7 +67,7 @@ function BlockSuggestionAdded( { blockTitle }: BlockSuggestionAddedDto ): ReactE
*
* @returns {ReactElement} The rendered sidebar section with block suggestions.
*/
export default function RequiredBlocks( sidebarTitle: string, block: BlockInstance, suggestedBlocks: SuggestedBlockProperties[] ): ReactElement {
export function RequiredBlocks( sidebarTitle: string, block: BlockInstance, suggestedBlocks: SuggestedBlockProperties[] ): ReactElement {
const suggestedBlockNames = suggestedBlocks
.filter( suggestedBlock => typeof getBlockType( suggestedBlock.name ) !== "undefined" )
.map( suggestedBlock => suggestedBlock.name );
Expand Down
4 changes: 3 additions & 1 deletion packages/schema-blocks/src/blocks/warning-block/edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ export function edit( props: RenderEditProps ): JSX.Element {

const { removedBlock, warningText, isRequired } = props.attributes;

const className: string = [ "yoast-warning-block", isRequired ? "required" : "recommended" ].join( " " );

return createElement(
"div",
{
key: "warning-div",
className: [ "yoast-warning-block", isRequired ? "required" : "recommended" ].join( " " ),
className: className,
},
[
createElement(
Expand Down
6 changes: 3 additions & 3 deletions packages/schema-blocks/src/core/Definition.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { BlockValidation, BlockValidationResult } from "./validation";
import { BlockInstance } from "@wordpress/blocks";
import { isArray, mergeWith } from "lodash";
import Instruction from "./Instruction";
import Leaf from "./Leaf";
import { Instruction } from "./Instruction";
import { Leaf } from "./Leaf";
import logger from "../functions/logger";

export type DefinitionClass<T extends Definition> = {
Expand All @@ -14,7 +14,7 @@ export type DefinitionClass<T extends Definition> = {
/**
* Definition class.
*/
export default abstract class Definition {
export abstract class Definition {
public separator: string;
public template: string;
public instructions: Record<string, Instruction>;
Expand Down
5 changes: 3 additions & 2 deletions packages/schema-blocks/src/core/Instruction.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BlockInstance } from "@wordpress/blocks";
import logger from "../functions/logger";
import { BlockValidationResult, BlockValidation } from "./validation";
import logger from "../functions/logger";

export type InstructionPrimitive = string | number | boolean;
export type InstructionValue = InstructionPrimitive | InstructionObject | InstructionArray;
export type InstructionArray = readonly InstructionValue[];
Expand All @@ -15,7 +16,7 @@ export type InstructionOptions = InstructionObject & {
/**
* Abstract instruction class.
*/
export default abstract class Instruction {
export abstract class Instruction {
static registeredInstructions: Record<string, InstructionClass<Instruction>>;

public id: number;
Expand Down
2 changes: 1 addition & 1 deletion packages/schema-blocks/src/core/Leaf.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Leaf class
*/
export default abstract class Leaf {
export abstract class Leaf {
public parent: Leaf;
}
8 changes: 4 additions & 4 deletions packages/schema-blocks/src/core/blocks/BlockDefinition.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { createElement, Fragment, ReactElement } from "@wordpress/element";
import { registerBlockType, BlockConfiguration, BlockEditProps, BlockSaveProps } from "@wordpress/blocks";
import { InspectorControls } from "@wordpress/block-editor";
import BlockInstruction from "./BlockInstruction";
import Definition from "../Definition";
import BlockRootLeaf from "../../leaves/blocks/BlockRootLeaf";
import { BlockInstruction } from "./BlockInstruction";
import { Definition } from "../Definition";
import { BlockRootLeaf } from "../../leaves/blocks/BlockRootLeaf";
import parse from "../../functions/blocks/parse";
import { registerBlockDefinition } from "./BlockDefinitionRepository";
import { PanelBody } from "@wordpress/components";
Expand All @@ -26,7 +26,7 @@ export type MutableBlockConfiguration = {
/**
* BlockDefinition class.
*/
export default class BlockDefinition extends Definition {
export class BlockDefinition extends Definition {
public static separatorCharacters = [ "b", "c", "d", "f", "g", "h", "k", "m", "z" ];
public static parser = parse;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import BlockDefinition from "./BlockDefinition";
import { BlockDefinition } from "./BlockDefinition";

// Internal store of all known BlockDefinitions.
const registeredBlockDefinitions: Record<string, BlockDefinition> = {};
Expand Down
13 changes: 4 additions & 9 deletions packages/schema-blocks/src/core/blocks/BlockInstruction.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
import BlockLeaf from "./BlockLeaf";
import { BlockLeaf } from "./BlockLeaf";
import { RenderSaveProps, RenderEditProps } from "./BlockDefinition";
import { ReactElement } from "@wordpress/element";
import { BlockConfiguration, BlockInstance } from "@wordpress/blocks";
import { BlockValidationResult, BlockValidation } from "../validation";
import Instruction, { InstructionOptions } from "../Instruction";
import { Instruction } from "../Instruction";
import { attributeExists, attributeNotEmpty } from "../../functions/validators";
import validateMany from "../../functions/validators/validateMany";
import { validateMany } from "../../functions/validators/validateMany";
import logger from "../../functions/logger";

export type BlockInstructionClass = {
new( id: number, options: InstructionOptions ): BlockInstruction;
options: InstructionOptions;
};

/**
* BlockInstruction class.
*/
export default abstract class BlockInstruction extends Instruction {
export abstract class BlockInstruction extends Instruction {
/* eslint-disable @typescript-eslint/no-unused-vars */
/**
* Renders saving the element.
Expand Down
4 changes: 2 additions & 2 deletions packages/schema-blocks/src/core/blocks/BlockLeaf.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { RenderEditProps, RenderSaveProps } from "./BlockDefinition";
import Leaf from "../Leaf";
import { Leaf } from "../Leaf";

/**
* BlockLeaf class
*/
export default abstract class BlockLeaf extends Leaf {
export abstract class BlockLeaf extends Leaf {
public parent: BlockLeaf;

/**
Expand Down
4 changes: 4 additions & 0 deletions packages/schema-blocks/src/core/blocks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export { BlockDefinition, RenderEditProps, RenderSaveProps } from "./BlockDefinition";
export { getBlockDefinition, registerBlockDefinition } from "./BlockDefinitionRepository";
export { BlockInstruction } from "./BlockInstruction";
export { BlockLeaf } from "./BlockLeaf";
7 changes: 7 additions & 0 deletions packages/schema-blocks/src/core/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export * from "./blocks";
export * from "./schema";
export * from "./validation";

export { Leaf } from "./Leaf";
export { Instruction } from "./Instruction";
export { Definition } from "./Definition";
17 changes: 6 additions & 11 deletions packages/schema-blocks/src/core/schema/SchemaDefinition.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import SchemaInstruction from "./SchemaInstruction";
import SchemaLeaf from "./SchemaLeaf";
import Definition from "../Definition";
import parse from "../../functions/schema/parse";
import { SchemaInstruction } from "./SchemaInstruction";
import { SchemaLeaf } from "./SchemaLeaf";
import { Definition } from "../Definition";
import { parse } from "../../functions/schema/parse";
import { BlockInstance } from "@wordpress/blocks";
import { SchemaDefinitionConfiguration } from "./SchemaDefinitionConfiguration";

export type SchemaPrimitive = string | number | boolean;
export type SchemaValue = SchemaPrimitive | SchemaObject | SchemaArray;
Expand All @@ -11,16 +12,10 @@ export type SchemaArray = SchemaValue[];

export const schemaDefinitions: Record<string, SchemaDefinition> = {};

export type SchemaDefinitionConfiguration = {
name: string;
onlyNested?: boolean;
separateInGraph?: boolean;
};

/**
* Schema definition class.
*/
export default class SchemaDefinition extends Definition {
export class SchemaDefinition extends Definition {
public static separatorCharacters = [ "1", "2", "3", "4", "5", "6", "7", "8", "9" ];
public static parser = parse;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type SchemaDefinitionConfiguration = {
name: string;
onlyNested?: boolean;
separateInGraph?: boolean;
};
7 changes: 4 additions & 3 deletions packages/schema-blocks/src/core/schema/SchemaInstruction.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SchemaValue, SchemaDefinitionConfiguration } from "./SchemaDefinition";
import Instruction, { InstructionOptions } from "../Instruction";
import { SchemaValue } from "./SchemaDefinition";
import { SchemaDefinitionConfiguration } from "./SchemaDefinitionConfiguration";
import { Instruction, InstructionOptions } from "../Instruction";
import { BlockInstance } from "@wordpress/blocks";
import { BlockValidation, BlockValidationResult } from "../validation";

Expand All @@ -8,7 +9,7 @@ export type SchemaInstructionClass = { new( id: number, options: InstructionOpti
/**
* SchemaInstruction class.
*/
export default abstract class SchemaInstruction extends Instruction {
export abstract class SchemaInstruction extends Instruction {
/* eslint-disable @typescript-eslint/no-unused-vars */
/**
* Renders schema.
Expand Down
4 changes: 2 additions & 2 deletions packages/schema-blocks/src/core/schema/SchemaLeaf.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { SchemaValue } from "./SchemaDefinition";
import Leaf from "../Leaf";
import { Leaf } from "../Leaf";
import { BlockInstance } from "@wordpress/blocks";

/**
* Leaf class
*/
export default abstract class SchemaLeaf extends Leaf {
export abstract class SchemaLeaf extends Leaf {
parent: SchemaLeaf;

/**
Expand Down
4 changes: 4 additions & 0 deletions packages/schema-blocks/src/core/schema/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export { SchemaDefinition } from "./SchemaDefinition";
export { SchemaDefinitionConfiguration } from "./SchemaDefinitionConfiguration";
export { SchemaInstruction } from "./SchemaInstruction";
export { SchemaLeaf } from "./SchemaLeaf";
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BlockValidation } from ".";
import { BlockInstance } from "@wordpress/blocks";
import { sprintf, __ } from "@wordpress/i18n";

/**
* Contains the result of a block validation.
Expand All @@ -25,15 +26,22 @@ export class BlockValidationResult {
*/
public issues: BlockValidationResult[]

/**
* An optional message describing the result.
*/
public message: string;

/**
* @param clientId The clientId of the validated block.
* @param name The name of the validated block.
* @param result The validation result.
* @param message An optional message describing the result.
*/
constructor( clientId: string, name: string, result: BlockValidation ) {
constructor( clientId: string, name: string, result: BlockValidation, message?: string ) {
this.name = name;
this.clientId = clientId;
this.result = result;
this.message = message;
this.issues = [];
}

Expand All @@ -53,6 +61,22 @@ export class BlockValidationResult {
);
}

/**
* Named constructor for a 'missing block' validation result.
*
* @param name The name of the missing block.
*
* @constructor
*/
static MissingBlock( name: string ) {
return new BlockValidationResult(
null,
name,
BlockValidation.MissingBlock,
sprintf( __( "The '%s' block is required but missing.", "yoast-schema-blocks" ), name ),
hansjovis marked this conversation as resolved.
Show resolved Hide resolved
);
}

/**
* Named constructor for a 'valid' validation result.
*
Expand Down
14 changes: 6 additions & 8 deletions packages/schema-blocks/src/core/validation/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { BlockValidation } from "./BlockValidation";
import { BlockValidationResult } from "./BlockValidationResult";
import { RequiredBlockOption } from "./RequiredBlockOption";
import { RequiredBlock } from "./RequiredBlock";
import { RecommendedBlock } from "./RecommendedBlock";
import { SuggestedBlockProperties } from "./SuggestedBlockProperties";

export { BlockValidation, BlockValidationResult, RequiredBlockOption, RequiredBlock, RecommendedBlock, SuggestedBlockProperties };
export { BlockValidation } from "./BlockValidation";
export { BlockValidationResult } from "./BlockValidationResult";
export { RequiredBlockOption } from "./RequiredBlockOption";
export { RequiredBlock } from "./RequiredBlock";
export { RecommendedBlock } from "./RecommendedBlock";
export { SuggestedBlockProperties } from "./SuggestedBlockProperties";
23 changes: 22 additions & 1 deletion packages/schema-blocks/src/functions/BlockHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,25 @@ function getBlockType( blockName: string ): Block | undefined {
return select( "core/blocks" ).getBlockType( blockName );
}

export { getBlockByClientId, removeBlock, restoreBlock, getBlockType };
/**
* Retrieves a human readable block name.
*
* @param blockName The block name (e.g. the Gutenberg block id).
*
* @returns A human readable block title.
*/
function getHumanReadableBlockName( blockName: string ): string {
const blockType = getBlockType( blockName ) || null;
if ( blockType ) {
return blockType.title;
}

const lastSlash = blockName.lastIndexOf( "/" );
if ( lastSlash < 0 || lastSlash === blockName.length - 1 ) {
return blockName;
}

return blockName.substring( lastSlash + 1 );
}

export { getBlockByClientId, removeBlock, restoreBlock, getBlockType, getHumanReadableBlockName };
Empty file.
13 changes: 6 additions & 7 deletions packages/schema-blocks/src/functions/blocks/parse.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { flatMap } from "lodash";

import BlockLeaf from "../../core/blocks/BlockLeaf";
import BlockDefinition from "../../core/blocks/BlockDefinition";
import BlockInstructionLeaf from "../../leaves/blocks/BlockInstructionLeaf";
import BlockTextLeaf from "../../leaves/blocks/BlockTextLeaf";
import BlockElementLeaf from "../../leaves/blocks/BlockElementLeaf";
import { BlockLeaf } from "../../core/blocks/BlockLeaf";
import { BlockDefinition } from "../../core/blocks/BlockDefinition";
import { BlockInstructionLeaf } from "../../leaves/blocks/BlockInstructionLeaf";
import { BlockTextLeaf } from "../../leaves/blocks/BlockTextLeaf";
import { BlockElementLeaf } from "../../leaves/blocks/BlockElementLeaf";
import { AllHTMLAttributes } from "@wordpress/element";
import BlockRootLeaf from "../../leaves/blocks/BlockRootLeaf";
import { BlockRootLeaf } from "../../leaves/blocks/BlockRootLeaf";

/**
* Parses text into leaves.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import logger from "../logger";
* Updates the store with information about whether a block is valid or why it isn't.
* @param validations The blocks' validation results.
*/
export default function storeBlockValidation( validations: BlockValidationResult[] ) {
export function storeBlockValidation( validations: BlockValidationResult[] ) {
if ( validations.length < 1 ) {
return;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/schema-blocks/src/functions/gutenberg/watch.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { debounce, isEqual } from "lodash";
import { subscribe, select, dispatch } from "@wordpress/data";
import SchemaDefinition, { schemaDefinitions } from "../../core/schema/SchemaDefinition";
import { SchemaDefinition, schemaDefinitions } from "../../core/schema/SchemaDefinition";
import { BlockInstance } from "@wordpress/blocks";
import warningWatcher from "./watchers/warningWatcher";
import { warningWatcher } from "./watchers/warningWatcher";
import { getBlockDefinition } from "../../core/blocks/BlockDefinitionRepository";
import { BlockValidation, BlockValidationResult } from "../../core/validation";
import storeBlockValidation from "./storeBlockValidation";
import { storeBlockValidation } from "./storeBlockValidation";
import logger from "../logger";

let updatingSchema = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { dispatch } from "@wordpress/data";
import { BlockInstance, createBlock } from "@wordpress/blocks";
import { __, sprintf } from "@wordpress/i18n";

import { getBlockDefinition } from "../../../core/blocks/BlockDefinitionRepository";
import InnerBlocks from "../../../instructions/blocks/InnerBlocks";
import recurseOverBlocks from "../../blocks/recurseOverBlocks";
import { mapBlocksRecursively } from "../../innerBlocksHelper";
import BlockDefinition from "../../../core/blocks/BlockDefinition";
import { BlockDefinition } from "../../../core/blocks/BlockDefinition";
import { InstructionObject } from "../../../core/Instruction";
import { getBlockType } from "../../BlockHelper";
import { RecommendedBlock, RequiredBlock } from "../../../core/validation";
Expand Down Expand Up @@ -191,7 +190,7 @@ function addWarningsForRecommendedBlocks(
* @param blocks The current list of blocks.
* @param previousBlocks The previous list of blocks.
*/
export default function warningWatcher( blocks: BlockInstance[], previousBlocks: BlockInstance[] = [] ): void {
export function warningWatcher( blocks: BlockInstance[], previousBlocks: BlockInstance[] = [] ): void {
const currentBlockIds: string[] = mapBlocksRecursively( blocks, block => block.clientId );

recurseOverBlocks( previousBlocks, ( block: BlockInstance ) => {
Expand Down
Loading