|
22 | 22 | * @fileoverview Vertical separator field. Draws a vertical line. |
23 | 23 | |
24 | 24 | */ |
25 | | -'use strict'; |
26 | | - |
27 | | -goog.provide('Blockly.FieldVerticalSeparator'); |
28 | | - |
29 | | -goog.require('Blockly.Field'); |
30 | | -goog.require('goog.dom'); |
31 | | -goog.require('goog.math.Size'); |
32 | | - |
| 25 | +import * as Blockly from 'blockly/core'; |
33 | 26 |
|
34 | 27 | /** |
35 | 28 | * Class for a vertical separator line. |
36 | 29 | * @extends {Blockly.Field} |
37 | 30 | * @constructor |
38 | 31 | */ |
39 | | -Blockly.FieldVerticalSeparator = function() { |
40 | | - this.sourceBlock_ = null; |
41 | | - this.width_ = 1; |
42 | | - this.height_ = Blockly.BlockSvg.ICON_SEPARATOR_HEIGHT; |
43 | | - this.size_ = new goog.math.Size(this.width_, this.height_); |
44 | | -}; |
45 | | -goog.inherits(Blockly.FieldVerticalSeparator, Blockly.Field); |
46 | | - |
47 | | -/** |
48 | | - * Construct a FieldVerticalSeparator from a JSON arg object. |
49 | | - * @param {!Object} _element A JSON object with options (unused, but passed in |
50 | | - * by Field.fromJson). |
51 | | - * @returns {!Blockly.FieldVerticalSeparator} The new field instance. |
52 | | - * @package |
53 | | - * @nocollapse |
54 | | - */ |
55 | | -Blockly.FieldVerticalSeparator.fromJson = function( |
56 | | - /* eslint-disable no-unused-vars */ _element |
57 | | - /* eslint-enable no-unused-vars */) { |
58 | | - return new Blockly.FieldVerticalSeparator(); |
59 | | -}; |
60 | | -/** |
61 | | - * Editable fields are saved by the XML renderer, non-editable fields are not. |
62 | | - */ |
63 | | -Blockly.FieldVerticalSeparator.prototype.EDITABLE = false; |
64 | | - |
65 | | -/** |
66 | | - * Install this field on a block. |
67 | | - */ |
68 | | -Blockly.FieldVerticalSeparator.prototype.init = function() { |
69 | | - if (this.fieldGroup_) { |
70 | | - // Image has already been initialized once. |
71 | | - return; |
| 32 | +class FieldVerticalSeparator extends Blockly.Field { |
| 33 | + constructor() { |
| 34 | + super(Blockly.Field.SKIP_SETUP); |
| 35 | + /** |
| 36 | + * Editable fields are saved by the XML renderer, non-editable fields are not. |
| 37 | + */ |
| 38 | + this.EDITABLE = false; |
72 | 39 | } |
73 | | - // Build the DOM. |
74 | | - /** @type {SVGElement} */ |
75 | | - this.fieldGroup_ = Blockly.utils.createSvgElement('g', {}, null); |
76 | | - if (!this.visible_) { |
77 | | - this.fieldGroup_.style.display = 'none'; |
78 | | - } |
79 | | - /** @type {SVGElement} */ |
80 | | - this.lineElement_ = Blockly.utils.createSvgElement('line', |
81 | | - { |
82 | | - 'stroke': this.sourceBlock_.getColourSecondary(), |
83 | | - 'stroke-linecap': 'round', |
84 | | - 'x1': 0, |
85 | | - 'y1': 0, |
86 | | - 'x2': 0, |
87 | | - 'y2': this.height_ |
88 | | - }, this.fieldGroup_); |
89 | | - |
90 | | - this.sourceBlock_.getSvgRoot().appendChild(this.fieldGroup_); |
91 | | -}; |
92 | 40 |
|
93 | | -/** |
94 | | - * Set the height of the line element, without adjusting the field's height. |
95 | | - * This allows the line's height to be changed without causing it to be |
96 | | - * centered with the new height (needed for correct rendering of hat blocks). |
97 | | - * @param {number} newHeight the new height for the line. |
98 | | - * @package |
99 | | - */ |
100 | | -Blockly.FieldVerticalSeparator.prototype.setLineHeight = function(newHeight) { |
101 | | - this.lineElement_.setAttribute('y2', newHeight); |
102 | | -}; |
| 41 | + /** |
| 42 | + * Construct a FieldVerticalSeparator from a JSON arg object. |
| 43 | + * @param {!Object} _element A JSON object with options (unused, but passed in |
| 44 | + * by Field.fromJson). |
| 45 | + * @returns {!Blockly.FieldVerticalSeparator} The new field instance. |
| 46 | + * @package |
| 47 | + * @nocollapse |
| 48 | + */ |
| 49 | + static fromJson = function( |
| 50 | + /* eslint-disable no-unused-vars */ _element |
| 51 | + /* eslint-enable no-unused-vars */) { |
| 52 | + return new FieldVerticalSeparator(); |
| 53 | + } |
103 | 54 |
|
104 | | -/** |
105 | | - * Dispose of all DOM objects belonging to this text. |
106 | | - */ |
107 | | -Blockly.FieldVerticalSeparator.prototype.dispose = function() { |
108 | | - goog.dom.removeNode(this.fieldGroup_); |
109 | | - this.fieldGroup_ = null; |
110 | | - this.lineElement_ = null; |
111 | | -}; |
| 55 | + /** |
| 56 | + * Install this field on a block. |
| 57 | + */ |
| 58 | + initView() { |
| 59 | + const height = 10 * this.getConstants().GRID_UNIT; |
| 60 | + this.size_ = new Blockly.utils.Size(1, height); |
| 61 | + |
| 62 | + /** @type {SVGElement} */ |
| 63 | + this.lineElement_ = Blockly.utils.dom.createSvgElement('line', |
| 64 | + { |
| 65 | + 'stroke': this.sourceBlock_.getColourSecondary(), |
| 66 | + 'stroke-linecap': 'round', |
| 67 | + 'x1': 0, |
| 68 | + 'y1': 0, |
| 69 | + 'x2': 0, |
| 70 | + 'y2': height |
| 71 | + }, this.fieldGroup_); |
| 72 | + } |
112 | 73 |
|
113 | | -/** |
114 | | - * Get the value of this field. A no-op in this case. |
115 | | - * @return {string} null. |
116 | | - * @override |
117 | | - */ |
118 | | -Blockly.FieldVerticalSeparator.prototype.getValue = function() { |
119 | | - return null; |
120 | | -}; |
| 74 | + /** |
| 75 | + * Set the height of the line element, without adjusting the field's height. |
| 76 | + * This allows the line's height to be changed without causing it to be |
| 77 | + * centered with the new height (needed for correct rendering of hat blocks). |
| 78 | + * @param {number} newHeight the new height for the line. |
| 79 | + * @package |
| 80 | + */ |
| 81 | + setLineHeight(newHeight) { |
| 82 | + this.lineElement_.setAttribute('y2', newHeight); |
| 83 | + }; |
| 84 | + |
| 85 | + /** |
| 86 | + * Get the value of this field. A no-op in this case. |
| 87 | + * @return {string} null. |
| 88 | + * @override |
| 89 | + */ |
| 90 | + getValue() { |
| 91 | + return null; |
| 92 | + } |
121 | 93 |
|
122 | | -/** |
123 | | - * Set the value of this field. A no-op in this case. |
124 | | - * @param {?string} src New value. |
125 | | - * @override |
126 | | - */ |
127 | | -Blockly.FieldVerticalSeparator.prototype.setValue = function( |
128 | | - /* eslint-disable no-unused-vars */ src |
129 | | - /* eslint-enable no-unused-vars */) { |
130 | | - return; |
131 | | -}; |
| 94 | + getText() { |
| 95 | + return ''; |
| 96 | + } |
132 | 97 |
|
133 | | -/** |
134 | | - * Set the text of this field. A no-op in this case. |
135 | | - * @param {?string} alt New text. |
136 | | - * @override |
137 | | - */ |
138 | | -Blockly.FieldVerticalSeparator.prototype.setText = function( |
139 | | - /* eslint-disable no-unused-vars */ alt |
140 | | - /* eslint-enable no-unused-vars */) { |
141 | | - return; |
142 | | -}; |
| 98 | + /** |
| 99 | + * Set the value of this field. A no-op in this case. |
| 100 | + * @param {?string} src New value. |
| 101 | + * @override |
| 102 | + */ |
| 103 | + setValue( |
| 104 | + /* eslint-disable no-unused-vars */ src |
| 105 | + /* eslint-enable no-unused-vars */) { |
| 106 | + return; |
| 107 | + } |
143 | 108 |
|
144 | | -/** |
145 | | - * Separator lines are fixed width, no need to render. |
146 | | - * @private |
147 | | - */ |
148 | | -Blockly.FieldVerticalSeparator.prototype.render_ = function() { |
149 | | - // NOP |
150 | | -}; |
| 109 | + /** |
| 110 | + * Separator lines are fixed width, no need to render. |
| 111 | + * @private |
| 112 | + */ |
| 113 | + render_() { |
| 114 | + // NOP |
| 115 | + } |
151 | 116 |
|
152 | | -/** |
153 | | - * Separator lines are fixed width, no need to update. |
154 | | - * @private |
155 | | - */ |
156 | | -Blockly.FieldVerticalSeparator.prototype.updateWidth = function() { |
157 | | - // NOP |
158 | | -}; |
| 117 | + /** |
| 118 | + * Separator lines are fixed width, no need to update. |
| 119 | + * @private |
| 120 | + */ |
| 121 | + updateWidth() { |
| 122 | + // NOP |
| 123 | + } |
| 124 | +} |
159 | 125 |
|
160 | | -Blockly.Field.register( |
161 | | - 'field_vertical_separator', Blockly.FieldVerticalSeparator); |
| 126 | +Blockly.fieldRegistry.register('field_vertical_separator', FieldVerticalSeparator); |