Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add added the ability to customize the color of for each blocks #7435

Closed
Closed
Show file tree
Hide file tree
Changes from 2 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
9 changes: 9 additions & 0 deletions core/block_svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,15 @@ export class BlockSvg
return this.style.colourTertiary;
}

/**
* Get the field_label colour of a block.
*
* @returns #RRGGBB string.
*/
getColourFieldLabel(): string | undefined {
return this.style.colourFieldLabel;
}

/**
* Selects this block. Highlights the block visually and fires a select event
* if the block is not already selected.
Expand Down
13 changes: 13 additions & 0 deletions core/field_label.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import * as dom from './utils/dom.js';
import {Field, FieldConfig} from './field.js';
import * as fieldRegistry from './field_registry.js';
import * as parsing from './utils/parsing.js';
import {BlockSvg} from './block_svg.js';

/**
* Class for a non-editable, non-serializable text field.
Expand Down Expand Up @@ -124,6 +125,18 @@ export class FieldLabel extends Field<string> {
// the static fromJson method.
return new this(text, undefined, options);
}

/**
* Update field's textElement_ color.
*/
override applyColour() {
const style = (this.sourceBlock_ as BlockSvg).style;
if (this.textElement_) {
this.textElement_.style.fill = this.sourceBlock_?.isEnabled()
? style.colourFieldLabel
: '#ffffff';
}
}
}

fieldRegistry.register('field_label', FieldLabel);
Expand Down
3 changes: 3 additions & 0 deletions core/renderers/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,9 @@ export class ConstantProvider {
valid.colourTertiary = valid['colourTertiary']
? parsing.parseBlockColour(valid['colourTertiary']).hex
: this.generateTertiaryColour_(valid.colourPrimary);
valid.colourFieldLabel = valid['colourFieldLabel']
? parsing.parseBlockColour(valid['colourFieldLabel']).hex
: '#ffffff';

valid.hat = valid['hat'] || '';
return valid;
Expand Down
1 change: 1 addition & 0 deletions core/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ export namespace Theme {
colourPrimary: string;
colourSecondary: string;
colourTertiary: string;
colourFieldLabel: string;
hat: string;
}

Expand Down
14 changes: 14 additions & 0 deletions tests/mocha/theme_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ suite('Theme', function () {
'colourPrimary': '#aaaaaa',
'colourSecondary': '#bbbbbb',
'colourTertiary': '#cccccc',
'colourFieldLabel': '#dddddd',
'hat': 'cap',
},
};
Expand All @@ -61,12 +62,14 @@ suite('Theme', function () {
'colourPrimary': '#aaaaaa',
'colourSecondary': '#bbbbbb',
'colourTertiary': '#cccccc',
'colourFieldLabel': '#dddddd',
'hat': 'cap',
},
'styleTwo': {
'colourPrimary': '#000000',
'colourSecondary': '#999999',
'colourTertiary': '#4d4d4d',
'colourFieldLabel': '#0f0f0f',
'hat': '',
},
};
Expand Down Expand Up @@ -171,6 +174,7 @@ suite('Theme', function () {
'colourPrimary': '#000000',
'colourSecondary': '#999999',
'colourTertiary': '#4d4d4d',
'colourFieldLabel': '#ffffff',
'hat': '',
};
stringifyAndCompare(
Expand All @@ -185,6 +189,7 @@ suite('Theme', function () {
'colourPrimary': '#000000',
'colourSecondary': '#999999',
'colourTertiary': '#4d4d4d',
'colourFieldLabel': '#ffffff',
'hat': '',
};
stringifyAndCompare(
Expand All @@ -201,6 +206,7 @@ suite('Theme', function () {
'colourPrimary': '#012345',
'colourSecondary': '#99a7b5',
'colourTertiary': '#4d657d',
'colourFieldLabel': '#ffffff',
'hat': '',
};
stringifyAndCompare(
Expand All @@ -214,12 +220,14 @@ suite('Theme', function () {
'colourPrimary': '#aaaaaa',
'colourSecondary': '#bbbbbb',
'colourTertiary': '#cccccc',
'colourFieldLabel': '#dddddd',
'hat': 'cap',
};
const expectedOutput = {
'colourPrimary': '#aaaaaa',
'colourSecondary': '#bbbbbb',
'colourTertiary': '#cccccc',
'colourFieldLabel': '#dddddd',
'hat': 'cap',
};
stringifyAndCompare(
Expand All @@ -233,11 +241,13 @@ suite('Theme', function () {
'colourPrimary': '20',
'colourSecondary': '40',
'colourTertiary': '60',
'colourFieldLabel': '60',
};
const expectedOutput = {
'colourPrimary': '#a5745b',
'colourSecondary': '#a58c5b',
'colourTertiary': '#a5a55b',
'colourFieldLabel': '#a5a55b',
'hat': '',
};
stringifyAndCompare(
Expand All @@ -254,6 +264,7 @@ suite('Theme', function () {
'colourPrimary': '#a5745b',
'colourSecondary': '#dbc7bd',
'colourTertiary': '#c09e8c',
'colourFieldLabel': '#ffffff',
'hat': '',
};
stringifyAndCompare(
Expand All @@ -267,11 +278,13 @@ suite('Theme', function () {
'colourPrimary': 'red',
'colourSecondary': 'white',
'colourTertiary': 'blue',
'colourFieldLabel': 'black',
};
const expectedOutput = {
'colourPrimary': '#ff0000',
'colourSecondary': '#ffffff',
'colourTertiary': '#0000ff',
'colourFieldLabel': '#000000',
'hat': '',
};
stringifyAndCompare(
Expand All @@ -288,6 +301,7 @@ suite('Theme', function () {
'colourPrimary': '#000000',
'colourSecondary': '#999999',
'colourTertiary': '#4d4d4d',
'colourFieldLabel': '#ffffff',
'hat': '',
};
stringifyAndCompare(
Expand Down