From ca3b9bd079316b6785873a0c51afcc4e9200ef29 Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Tue, 11 Oct 2022 15:40:56 -0700 Subject: [PATCH] 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 --- core/field_angle.ts | 4 ---- core/field_dropdown.ts | 12 ------------ core/field_textinput.ts | 4 ---- core/renderers/common/constants.ts | 7 +------ core/renderers/common/path_object.ts | 8 -------- core/renderers/zelos/path_object.ts | 12 ------------ core/theme.ts | 11 ++++++----- 7 files changed, 7 insertions(+), 51 deletions(-) diff --git a/core/field_angle.ts b/core/field_angle.ts index ecbfdb66f2c..33d81359372 100644 --- a/core/field_angle.ts +++ b/core/field_angle.ts @@ -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); diff --git a/core/field_dropdown.ts b/core/field_dropdown.ts index c58e7376d35..4385c037d8f 100644 --- a/core/field_dropdown.ts +++ b/core/field_dropdown.ts @@ -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_) { diff --git a/core/field_textinput.ts b/core/field_textinput.ts index dc4e29455c7..b639ffd2ffa 100644 --- a/core/field_textinput.ts +++ b/core/field_textinput.ts @@ -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( diff --git a/core/renderers/common/constants.ts b/core/renderers/common/constants.ts index 9017b3eaf94..82854abb498 100644 --- a/core/renderers/common/constants.ts +++ b/core/renderers/common/constants.ts @@ -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 { // Make a new object with all of the same properties. const valid = {} as BlockStyle; if (blockStyle) { diff --git a/core/renderers/common/path_object.ts b/core/renderers/common/path_object.ts index 712fce28b62..c28a629e47b 100644 --- a/core/renderers/common/path_object.ts +++ b/core/renderers/common/path_object.ts @@ -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); } diff --git a/core/renderers/zelos/path_object.ts b/core/renderers/zelos/path_object.ts index 05fba1f854a..fba99877a20 100644 --- a/core/renderers/zelos/path_object.ts +++ b/core/renderers/zelos/path_object.ts @@ -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); } diff --git a/core/theme.ts b/core/theme.ts index 2ac60b0de40..c34600233cd 100644 --- a/core/theme.ts +++ b/core/theme.ts @@ -17,7 +17,7 @@ import * as object from './utils/object.js'; export interface ITheme { - blockStyles?: {[key: string]: BlockStyle}; + blockStyles?: {[key: string]: Partial}; 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}, 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 {