fix: move Theme to use a Partial type for BlockStyle (#6532)

* fix: move theme to use Partial type

* chore: remove useless error throwing

* chore: format

* chore: update validatedBlockStyle_ to use Partial
This commit is contained in:
Beka Westberg
2022-10-11 15:40:56 -07:00
committed by GitHub
parent 6456ab90f0
commit ca3b9bd079
7 changed files with 7 additions and 51 deletions

View File

@@ -228,10 +228,6 @@ export class FieldAngle extends FieldTextInput {
dropDownDiv.getContentDiv().appendChild(this.editor_ as AnyDuringMigration);
if (this.sourceBlock_ instanceof BlockSvg) {
if (!this.sourceBlock_.style.colourTertiary) {
throw new Error(
'The renderer did not properly initialize the block style');
}
dropDownDiv.setColour(
this.sourceBlock_.style.colourPrimary,
this.sourceBlock_.style.colourTertiary);

View File

@@ -285,10 +285,6 @@ export class FieldDropdown extends Field {
const borderColour = this.getSourceBlock().isShadow() ?
(this.getSourceBlock().getParent() as BlockSvg).style.colourTertiary :
(this.sourceBlock_ as BlockSvg).style.colourTertiary;
if (!borderColour) {
throw new Error(
'The renderer did not properly initialize the block style');
}
dropDownDiv.setColour(primaryColour, borderColour);
}
@@ -503,14 +499,6 @@ export class FieldDropdown extends Field {
*/
override applyColour() {
const style = (this.sourceBlock_ as BlockSvg).style;
if (!style.colourSecondary) {
throw new Error(
'The renderer did not properly initialize the block style');
}
if (!style.colourTertiary) {
throw new Error(
'The renderer did not properly initialize the block style');
}
if (this.borderRect_) {
this.borderRect_.setAttribute('stroke', style.colourTertiary);
if (this.menu_) {

View File

@@ -222,10 +222,6 @@ export class FieldTextInput extends Field {
const source = this.sourceBlock_ as BlockSvg;
if (this.borderRect_) {
if (!source.style.colourTertiary) {
throw new Error(
'The renderer did not properly initialize the block style');
}
this.borderRect_.setAttribute('stroke', source.style.colourTertiary);
} else {
source.pathObject.svgPath.setAttribute(

View File

@@ -658,12 +658,7 @@ export class ConstantProvider {
* @param blockStyle A full or partial block style object.
* @returns A full block style object, with all required properties populated.
*/
protected validatedBlockStyle_(blockStyle: {
colourPrimary: string,
colourSecondary?: string,
colourTertiary?: string,
hat?: string
}): BlockStyle {
protected validatedBlockStyle_(blockStyle: Partial<BlockStyle>): BlockStyle {
// Make a new object with all of the same properties.
const valid = {} as BlockStyle;
if (blockStyle) {

View File

@@ -137,10 +137,6 @@ export class PathObject implements IPathObject {
* @internal
*/
applyColour(block: BlockSvg) {
if (!this.style.colourTertiary) {
throw new Error(
'The renderer did not properly initialize the block style');
}
this.svgPath.setAttribute('stroke', this.style.colourTertiary);
this.svgPath.setAttribute('fill', this.style.colourPrimary);
@@ -199,10 +195,6 @@ export class PathObject implements IPathObject {
*/
protected updateShadow_(shadow: boolean) {
if (shadow) {
if (!this.style.colourSecondary) {
throw new Error(
'The renderer did not properly initialize the block style');
}
this.svgPath.setAttribute('stroke', 'none');
this.svgPath.setAttribute('fill', this.style.colourSecondary);
}

View File

@@ -77,19 +77,11 @@ export class PathObject extends BasePathObject {
// Set shadow stroke colour.
const parent = block.getParent();
if (block.isShadow() && parent) {
if (!parent.style.colourTertiary) {
throw new Error(
'The renderer did not properly initialize the block style');
}
this.svgPath.setAttribute('stroke', parent.style.colourTertiary);
}
// Apply colour to outlines.
for (const outline of this.outlines.values()) {
if (!this.style.colourTertiary) {
throw new Error(
'The renderer did not properly initialize the block style');
}
outline.setAttribute('fill', this.style.colourTertiary);
}
}
@@ -183,10 +175,6 @@ export class PathObject extends BasePathObject {
setOutlinePath(name: string, pathString: string) {
const outline = this.getOutlinePath_(name);
outline.setAttribute('d', pathString);
if (!this.style.colourTertiary) {
throw new Error(
'The renderer did not properly initialize the block style');
}
outline.setAttribute('fill', this.style.colourTertiary);
}

View File

@@ -17,7 +17,7 @@ import * as object from './utils/object.js';
export interface ITheme {
blockStyles?: {[key: string]: BlockStyle};
blockStyles?: {[key: string]: Partial<BlockStyle>};
categoryStyles?: {[key: string]: CategoryStyle};
componentStyles?: ComponentStyle;
fontStyle?: FontStyle;
@@ -58,7 +58,8 @@ export class Theme implements ITheme {
* @param opt_componentStyles A map of Blockly component names to style value.
*/
constructor(
public name: string, opt_blockStyles?: {[key: string]: BlockStyle},
public name: string,
opt_blockStyles?: {[key: string]: Partial<BlockStyle>},
opt_categoryStyles?: {[key: string]: CategoryStyle},
opt_componentStyles?: ComponentStyle) {
/** The block styles map. */
@@ -187,9 +188,9 @@ export class Theme implements ITheme {
export namespace Theme {
export interface BlockStyle {
colourPrimary: string;
colourSecondary?: string;
colourTertiary?: string;
hat?: string;
colourSecondary: string;
colourTertiary: string;
hat: string;
}
export interface CategoryStyle {