Skip to content

Commit a42c2d1

Browse files
authored
feat: Add a BlockSvg.getStyle() method. (#8722)
* feat: Add a BlockSvg.getStyle() method. * refactor: Remove direct field access to BlockSvg.style.
1 parent 547ff18 commit a42c2d1

7 files changed

Lines changed: 26 additions & 17 deletions

File tree

core/block_svg.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ export class BlockSvg
243243
*
244244
* @returns #RRGGBB string.
245245
*/
246-
getColourSecondary(): string | undefined {
246+
getColourSecondary(): string {
247247
return this.style.colourSecondary;
248248
}
249249

@@ -252,7 +252,7 @@ export class BlockSvg
252252
*
253253
* @returns #RRGGBB string.
254254
*/
255-
getColourTertiary(): string | undefined {
255+
getColourTertiary(): string {
256256
return this.style.colourTertiary;
257257
}
258258

@@ -1172,6 +1172,15 @@ export class BlockSvg
11721172
}
11731173
}
11741174

1175+
/**
1176+
* Returns the BlockStyle object used to style this block.
1177+
*
1178+
* @returns This block's style object.
1179+
*/
1180+
getStyle(): BlockStyle {
1181+
return this.style;
1182+
}
1183+
11751184
/**
11761185
* Move this block to the front of the visible workspace.
11771186
* <g> tags do not respect z-index so SVG renders them in the

core/field_dropdown.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ export class FieldDropdown extends Field<string> {
291291

292292
if (this.getConstants()!.FIELD_DROPDOWN_COLOURED_DIV) {
293293
const primaryColour = block.getColour();
294-
const borderColour = (this.sourceBlock_ as BlockSvg).style.colourTertiary;
294+
const borderColour = (this.sourceBlock_ as BlockSvg).getColourTertiary();
295295
dropDownDiv.setColour(primaryColour, borderColour);
296296
}
297297

@@ -467,21 +467,21 @@ export class FieldDropdown extends Field<string> {
467467
* Updates the dropdown arrow to match the colour/style of the block.
468468
*/
469469
override applyColour() {
470-
const style = (this.sourceBlock_ as BlockSvg).style;
470+
const sourceBlock = this.sourceBlock_ as BlockSvg;
471471
if (this.borderRect_) {
472-
this.borderRect_.setAttribute('stroke', style.colourTertiary);
472+
this.borderRect_.setAttribute('stroke', sourceBlock.getColourTertiary());
473473
if (this.menu_) {
474-
this.borderRect_.setAttribute('fill', style.colourTertiary);
474+
this.borderRect_.setAttribute('fill', sourceBlock.getColourTertiary());
475475
} else {
476476
this.borderRect_.setAttribute('fill', 'transparent');
477477
}
478478
}
479479
// Update arrow's colour.
480-
if (this.sourceBlock_ && this.arrow) {
481-
if (this.sourceBlock_.isShadow()) {
482-
this.arrow.style.fill = style.colourSecondary;
480+
if (sourceBlock && this.arrow) {
481+
if (sourceBlock.isShadow()) {
482+
this.arrow.style.fill = sourceBlock.getColourSecondary();
483483
} else {
484-
this.arrow.style.fill = style.colourPrimary;
484+
this.arrow.style.fill = sourceBlock.getColour();
485485
}
486486
}
487487
}

core/field_input.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ export abstract class FieldInput<T extends InputTypes> extends Field<
226226

227227
if (!this.isFullBlockField() && this.borderRect_) {
228228
this.borderRect_!.style.display = 'block';
229-
this.borderRect_.setAttribute('stroke', block.style.colourTertiary);
229+
this.borderRect_.setAttribute('stroke', block.getColourTertiary());
230230
} else {
231231
this.borderRect_!.style.display = 'none';
232232
// In general, do *not* let fields control the color of blocks. Having the
@@ -429,8 +429,8 @@ export abstract class FieldInput<T extends InputTypes> extends Field<
429429
borderRadius = (bBox.bottom - bBox.top) / 2 + 'px';
430430
// Pull stroke colour from the existing shadow block
431431
const strokeColour = block.getParent()
432-
? (block.getParent() as BlockSvg).style.colourTertiary
433-
: (this.sourceBlock_ as BlockSvg).style.colourTertiary;
432+
? (block.getParent() as BlockSvg).getColourTertiary()
433+
: (this.sourceBlock_ as BlockSvg).getColourTertiary();
434434
htmlInput.style.border = 1 * scale + 'px solid ' + strokeColour;
435435
div!.style.borderRadius = borderRadius;
436436
div!.style.transition = 'box-shadow 0.25s ease 0s';

core/icons/comment_icon.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export class CommentIcon extends Icon implements IHasBubble, ISerializable {
126126

127127
override applyColour(): void {
128128
super.applyColour();
129-
const colour = (this.sourceBlock as BlockSvg).style.colourPrimary;
129+
const colour = (this.sourceBlock as BlockSvg).getColour();
130130
this.textInputBubble?.setColour(colour);
131131
}
132132

core/icons/mutator_icon.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ export class MutatorIcon extends Icon implements IHasBubble {
136136

137137
override applyColour(): void {
138138
super.applyColour();
139-
this.miniWorkspaceBubble?.setColour(this.sourceBlock.style.colourPrimary);
139+
this.miniWorkspaceBubble?.setColour(this.sourceBlock.getColour());
140140
this.miniWorkspaceBubble?.updateBlockStyles();
141141
}
142142

core/icons/warning_icon.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export class WarningIcon extends Icon implements IHasBubble {
108108

109109
override applyColour(): void {
110110
super.applyColour();
111-
this.textBubble?.setColour(this.sourceBlock.style.colourPrimary);
111+
this.textBubble?.setColour(this.sourceBlock.getColour());
112112
}
113113

114114
override updateCollapsed(): void {

core/renderers/zelos/path_object.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export class PathObject extends BasePathObject {
6868
// Set shadow stroke colour.
6969
const parent = block.getParent();
7070
if (block.isShadow() && parent) {
71-
this.svgPath.setAttribute('stroke', parent.style.colourTertiary);
71+
this.svgPath.setAttribute('stroke', parent.getColourTertiary());
7272
}
7373

7474
// Apply colour to outlines.

0 commit comments

Comments
 (0)