Skip to content
This repository was archived by the owner on Oct 4, 2022. It is now read-only.
Merged
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
56 changes: 56 additions & 0 deletions packages/schema-blocks/src/functions/initialize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { registerBlockType } from "@wordpress/blocks";

import "../instructions";
import logger, { LogLevel } from "./logger";
import { WarningBlock } from "../blocks/warning-block/configuration";
import { processBlock, processSchema } from "./process";
import filter from "./gutenberg/filter";
import watch from "./gutenberg/watch";

/**
* Removes all whitespace including line breaks from a string.
*
* @param text The text to remove the whitespace from.
*
* @returns {string} The converted string.
*/
function removeWhitespace( text: string ): string {
return text.split( "\n" ).map( ( s: string ) => s.trim() ).join( "" );
}

/**
* Initializes the Schema templates and block templates.
*
* @param logLevel The required minimum severity for a log message to appear in logs.
*/
export function initialize( logLevel: LogLevel = LogLevel.ERROR ) {
logger.setLogLevel( logLevel );

registerBlockType( "yoast/warning-block", WarningBlock );

jQuery( 'script[type="text/schema-template"]' ).each( function() {
try {
const template = removeWhitespace( this.innerHTML );
const definition = processSchema( template );
definition.register();
} catch ( e ) {
logger.error( "Failed to parse schema-template", e, this );
}
} );

// Filter in our schema definitions with Gutenberg.
filter();

jQuery( 'script[type="text/block-template"]' ).each( function() {
try {
const template = removeWhitespace( this.innerHTML );
const definition = processBlock( template );
definition.register();
} catch ( e ) {
logger.error( "Failed to parse gutenberg-template", e, this );
}
} );

// Watch Gutenberg for block changes that require schema updates.
watch();
}
61 changes: 6 additions & 55 deletions packages/schema-blocks/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,8 @@
import "./instructions";
import { processSchema, processBlock } from "./functions/process";
import watch from "./functions/gutenberg/watch";
import filter from "./functions/gutenberg/filter";
import { registerBlockType } from "@wordpress/blocks";
import { WarningBlock } from "./blocks/warning-block/configuration";
import logger, { LogLevel } from "./functions/logger";
import { initialize } from "./functions/initialize";
import BlockInstruction from "./core/blocks/BlockInstruction";
import { RenderEditProps, RenderSaveProps } from "./core/blocks/BlockDefinition";
import { LogLevel } from "./functions/logger";

export { LogLevel };
export { BlockInstruction, RenderSaveProps, RenderEditProps, LogLevel };

/**
* Removes all whitespace including line breaks from a string.
*
* @param text The text to remove the whitespace from.
*
* @returns {string} The converted string.
*/
function removeWhitespace( text: string ): string {
return text.split( "\n" ).map( ( s: string ) => s.trim() ).join( "" );
}

/**
* Initializes the Schema templates and block templates.
*
* @param logLevel The required minimum severity for a log message to appear in logs.
*/
export default function initialize( logLevel: LogLevel = LogLevel.ERROR ) {
logger.setLogLevel( logLevel );

registerBlockType( "yoast/warning-block", WarningBlock );

jQuery( 'script[type="text/schema-template"]' ).each( function() {
try {
const template = removeWhitespace( this.innerHTML );
const definition = processSchema( template );
definition.register();
} catch ( e ) {
logger.error( "Failed to parse schema-template", e, this );
}
} );

// Filter in our schema definitions with Gutenberg.
filter();

jQuery( 'script[type="text/block-template"]' ).each( function() {
try {
const template = removeWhitespace( this.innerHTML );
const definition = processBlock( template );
definition.register();
} catch ( e ) {
logger.error( "Failed to parse gutenberg-template", e, this );
}
} );

// Watch Gutenberg for block changes that require schema updates.
watch();
}
export default initialize;