Files
blockly/core/warning.ts
T
Christopher Allen b0475b0c68 chore: Fix whitespace (#6243)
* fix: Remove spurious blank lines

  Remove extraneous blank lines introduced by deletion of
  'use strict'; pragmas.

  Also fix the location of the goog.declareModuleId call in
  core/utils/array.ts.

* fix: Add missing double-blank-line before body of modules

  Our convention is to have two blank lines between the imports (or
  module ID, if there are no imports) and the beginning of the body
  of the module.  Enforce this.

* fix: one addition format error for PR #6243
2022-06-24 19:33:39 +01:00

164 lines
4.3 KiB
TypeScript

/**
* @license
* Copyright 2012 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Object representing a warning.
*/
/**
* Object representing a warning.
* @class
*/
import * as goog from '../closure/goog/goog.js';
goog.declareModuleId('Blockly.Warning');
// Unused import preserved for side-effects. Remove if unneeded.
import './events/events_bubble_open';
/* eslint-disable-next-line no-unused-vars */
import {BlockSvg} from './block_svg.js';
import {Bubble} from './bubble.js';
import * as eventUtils from './events/utils.js';
import {Icon} from './icon.js';
/* eslint-disable-next-line no-unused-vars */
import {Coordinate} from './utils/coordinate.js';
import * as dom from './utils/dom.js';
import {Svg} from './utils/svg.js';
/**
* Class for a warning.
* @alias Blockly.Warning
*/
export class Warning extends Icon {
text_: AnyDuringMigration;
/** The top-level node of the warning text, or null if not created. */
private paragraphElement_: SVGTextElement|null = null;
/** Does this icon get hidden when the block is collapsed? */
override collapseHidden = false;
override bubble_: AnyDuringMigration;
/** @param block The block associated with this warning. */
constructor(block: BlockSvg) {
super(block);
this.createIcon();
// The text_ object can contain multiple warnings.
this.text_ = Object.create(null);
}
/**
* Draw the warning icon.
* @param group The icon group.
*/
protected override drawIcon_(group: Element) {
// Triangle with rounded corners.
dom.createSvgElement(
Svg.PATH, {
'class': 'blocklyIconShape',
'd': 'M2,15Q-1,15 0.5,12L6.5,1.7Q8,-1 9.5,1.7L15.5,12Q17,15 14,15z',
},
group);
// Can't use a real '!' text character since different browsers and
// operating systems render it differently. Body of exclamation point.
dom.createSvgElement(
Svg.PATH, {
'class': 'blocklyIconSymbol',
'd': 'm7,4.8v3.16l0.27,2.27h1.46l0.27,-2.27v-3.16z',
},
group);
// Dot of exclamation point.
dom.createSvgElement(
Svg.RECT, {
'class': 'blocklyIconSymbol',
'x': '7',
'y': '11',
'height': '2',
'width': '2',
},
group);
}
/**
* Show or hide the warning bubble.
* @param visible True if the bubble should be visible.
*/
override setVisible(visible: boolean) {
if (visible === this.isVisible()) {
return;
}
// AnyDuringMigration because: Property 'block_' does not exist on type
// 'Warning'.
eventUtils.fire(new (eventUtils.get(eventUtils.BUBBLE_OPEN))!
((this as AnyDuringMigration).block_, visible, 'warning'));
if (visible) {
this.createBubble_();
} else {
this.disposeBubble_();
}
}
/** Show the bubble. */
private createBubble_() {
this.paragraphElement_ = Bubble.textToDom(this.getText());
// AnyDuringMigration because: Property 'block_' does not exist on type
// 'Warning'.
this.bubble_ = Bubble.createNonEditableBubble(
this.paragraphElement_, (this as AnyDuringMigration).block_ as BlockSvg,
this.iconXY_ as Coordinate);
this.applyColour();
}
/** Dispose of the bubble and references to it. */
private disposeBubble_() {
this.bubble_.dispose();
this.bubble_ = null;
this.paragraphElement_ = null;
}
/**
* Set this warning's text.
* @param text Warning text (or '' to delete). This supports linebreaks.
* @param id An ID for this text entry to be able to maintain multiple
* warnings.
*/
setText(text: string, id: string) {
if (this.text_[id] === text) {
return;
}
if (text) {
this.text_[id] = text;
} else {
delete this.text_[id];
}
if (this.isVisible()) {
this.setVisible(false);
this.setVisible(true);
}
}
/**
* Get this warning's texts.
* @return All texts concatenated into one string.
*/
getText(): string {
const allWarnings = [];
for (const id in this.text_) {
allWarnings.push(this.text_[id]);
}
return allWarnings.join('\n');
}
/** Dispose of this warning. */
override dispose() {
// AnyDuringMigration because: Property 'block_' does not exist on type
// 'Warning'.
(this as AnyDuringMigration).block_.warning = null;
super.dispose();
}
}