mirror of
https://github.com/google/blockly.git
synced 2026-01-04 15:40:08 +01:00
* fix(build): Restore erroneously-deleted filter function This was deleted in PR #7406 as it was mainly being used to filter core/ vs. test/mocha/ deps into separate deps files - but it turns out also to be used for filtering error messages too. Oops. * refactor(tests): Migrate advanced compilation test to ES Modules * refactor(build): Migrate main.js to TypeScript This turns out to be pretty straight forward, even if it would cause crashing if one actually tried to import this module instead of just feeding it to Closure Compiler. * chore(build): Remove goog.declareModuleId calls Replace goog.declareModuleId calls with a comment recording the former module ID for posterity (or at least until we decide how to reformat the renamings file. * chore(tests): Delete closure/goog/* For the moment we still need something to serve as base.js for the benefit of closure-make-deps, so we keep a vestigial base.js around, containing only the @provideGoog declaration. * refactor(build): Remove vestigial base.js By changing slightly the command line arguments to closure-make-deps and closure-calculate-chunks the need to have any base.js is eliminated. * chore: Typo fix for PR #7415
193 lines
5.2 KiB
TypeScript
193 lines
5.2 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright 2019 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
/**
|
|
* Object in charge of storing and updating a workspace theme
|
|
* and UI components.
|
|
*
|
|
* @class
|
|
*/
|
|
// Former goog.module ID: Blockly.ThemeManager
|
|
|
|
import type {Theme} from './theme.js';
|
|
import * as arrayUtils from './utils/array.js';
|
|
import * as dom from './utils/dom.js';
|
|
import type {Workspace} from './workspace.js';
|
|
import type {WorkspaceSvg} from './workspace_svg.js';
|
|
|
|
/**
|
|
* Class for storing and updating a workspace's theme and UI components.
|
|
*/
|
|
export class ThemeManager {
|
|
/** A list of workspaces that are subscribed to this theme. */
|
|
private subscribedWorkspaces_: Workspace[] = [];
|
|
private componentDB = new Map<string, Component[]>();
|
|
|
|
/**
|
|
* @param workspace The main workspace.
|
|
* @param theme The workspace theme.
|
|
* @internal
|
|
*/
|
|
constructor(
|
|
private readonly workspace: WorkspaceSvg,
|
|
private theme: Theme,
|
|
) {}
|
|
|
|
/**
|
|
* Get the workspace theme.
|
|
*
|
|
* @returns The workspace theme.
|
|
* @internal
|
|
*/
|
|
getTheme(): Theme {
|
|
return this.theme;
|
|
}
|
|
|
|
/**
|
|
* Set the workspace theme, and refresh the workspace and all components.
|
|
*
|
|
* @param theme The workspace theme.
|
|
* @internal
|
|
*/
|
|
setTheme(theme: Theme) {
|
|
const prevTheme = this.theme;
|
|
this.theme = theme;
|
|
// Set the theme name onto the injection div.
|
|
const injectionDiv = this.workspace.getInjectionDiv();
|
|
if (injectionDiv) {
|
|
if (prevTheme) {
|
|
const oldClassName = prevTheme.getClassName();
|
|
if (oldClassName) {
|
|
dom.removeClass(injectionDiv, oldClassName);
|
|
}
|
|
}
|
|
const newClassName = this.theme.getClassName();
|
|
if (newClassName) {
|
|
dom.addClass(injectionDiv, newClassName);
|
|
}
|
|
}
|
|
|
|
// Refresh all subscribed workspaces.
|
|
for (
|
|
let i = 0, workspace;
|
|
(workspace = this.subscribedWorkspaces_[i]);
|
|
i++
|
|
) {
|
|
(workspace as WorkspaceSvg).refreshTheme();
|
|
}
|
|
|
|
// Refresh all registered Blockly UI components.
|
|
for (const [key, componentList] of this.componentDB) {
|
|
for (const component of componentList) {
|
|
const element = component.element;
|
|
const propertyName = component.propertyName;
|
|
const style = this.theme && this.theme.getComponentStyle(key);
|
|
element.style.setProperty(propertyName, style || '');
|
|
}
|
|
}
|
|
|
|
for (const workspace of this.subscribedWorkspaces_) {
|
|
(workspace as WorkspaceSvg).hideChaff();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Subscribe a workspace to changes to the selected theme. If a new theme is
|
|
* set, the workspace is called to refresh its blocks.
|
|
*
|
|
* @param workspace The workspace to subscribe.
|
|
* @internal
|
|
*/
|
|
subscribeWorkspace(workspace: Workspace) {
|
|
this.subscribedWorkspaces_.push(workspace);
|
|
}
|
|
|
|
/**
|
|
* Unsubscribe a workspace to changes to the selected theme.
|
|
*
|
|
* @param workspace The workspace to unsubscribe.
|
|
* @internal
|
|
*/
|
|
unsubscribeWorkspace(workspace: Workspace) {
|
|
if (!arrayUtils.removeElem(this.subscribedWorkspaces_, workspace)) {
|
|
throw Error(
|
|
"Cannot unsubscribe a workspace that hasn't been subscribed.",
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Subscribe an element to changes to the selected theme. If a new theme is
|
|
* selected, the element's style is refreshed with the new theme's style.
|
|
*
|
|
* @param element The element to subscribe.
|
|
* @param componentName The name used to identify the component. This must be
|
|
* the same name used to configure the style in the Theme object.
|
|
* @param propertyName The inline style property name to update.
|
|
* @internal
|
|
*/
|
|
subscribe(
|
|
element: HTMLElement | SVGElement,
|
|
componentName: string,
|
|
propertyName: string,
|
|
) {
|
|
if (!this.componentDB.has(componentName)) {
|
|
this.componentDB.set(componentName, []);
|
|
}
|
|
|
|
// Add the element to our component map.
|
|
this.componentDB.get(componentName)!.push({element, propertyName});
|
|
|
|
// Initialize the element with its corresponding theme style.
|
|
const style = this.theme && this.theme.getComponentStyle(componentName);
|
|
element.style.setProperty(propertyName, style || '');
|
|
}
|
|
|
|
/**
|
|
* Unsubscribe an element to changes to the selected theme.
|
|
*
|
|
* @param element The element to unsubscribe.
|
|
* @internal
|
|
*/
|
|
unsubscribe(element: HTMLElement | SVGElement) {
|
|
if (!element) {
|
|
return;
|
|
}
|
|
// Go through all component, and remove any references to this element.
|
|
for (const [componentName, elements] of this.componentDB) {
|
|
for (let i = elements.length - 1; i >= 0; i--) {
|
|
if (elements[i].element === element) {
|
|
elements.splice(i, 1);
|
|
}
|
|
}
|
|
// Clean up the component map entry if the list is empty.
|
|
if (!elements.length) {
|
|
this.componentDB.delete(componentName);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Dispose of this theme manager.
|
|
*
|
|
* @internal
|
|
*/
|
|
dispose() {
|
|
this.subscribedWorkspaces_.length = 0;
|
|
this.componentDB.clear();
|
|
}
|
|
}
|
|
|
|
export namespace ThemeManager {
|
|
/** The type for a Blockly UI Component. */
|
|
export interface Component {
|
|
element: HTMLElement | SVGElement;
|
|
propertyName: string;
|
|
}
|
|
}
|
|
|
|
export type Component = ThemeManager.Component;
|