|
1 | | -import * as Blockly from 'blockly'; |
2 | | -import {ContinuousFlyout} from '@blockly/continuous-toolbox'; |
| 1 | +import * as Blockly from "blockly"; |
| 2 | +import { ContinuousFlyout } from "@blockly/continuous-toolbox"; |
3 | 3 |
|
4 | 4 | export class CheckableContinuousFlyout extends ContinuousFlyout { |
5 | 5 | /** |
@@ -22,12 +22,18 @@ export class CheckableContinuousFlyout extends ContinuousFlyout { |
22 | 22 | * @const |
23 | 23 | */ |
24 | 24 | static CHECKMARK_PATH = |
25 | | - 'M' + CheckableContinuousFlyout.CHECKBOX_SIZE / 4 + |
26 | | - ' ' + CheckableContinuousFlyout.CHECKBOX_SIZE / 2 + |
27 | | - 'L' + 5 * CheckableContinuousFlyout.CHECKBOX_SIZE / 12 + |
28 | | - ' ' + 2 * CheckableContinuousFlyout.CHECKBOX_SIZE / 3 + |
29 | | - 'L' + 3 * CheckableContinuousFlyout.CHECKBOX_SIZE / 4 + |
30 | | - ' ' + CheckableContinuousFlyout.CHECKBOX_SIZE / 3; |
| 25 | + "M" + |
| 26 | + CheckableContinuousFlyout.CHECKBOX_SIZE / 4 + |
| 27 | + " " + |
| 28 | + CheckableContinuousFlyout.CHECKBOX_SIZE / 2 + |
| 29 | + "L" + |
| 30 | + (5 * CheckableContinuousFlyout.CHECKBOX_SIZE) / 12 + |
| 31 | + " " + |
| 32 | + (2 * CheckableContinuousFlyout.CHECKBOX_SIZE) / 3 + |
| 33 | + "L" + |
| 34 | + (3 * CheckableContinuousFlyout.CHECKBOX_SIZE) / 4 + |
| 35 | + " " + |
| 36 | + CheckableContinuousFlyout.CHECKBOX_SIZE / 3; |
31 | 37 |
|
32 | 38 | /** |
33 | 39 | * Size of the checkbox corner radius |
@@ -48,15 +54,14 @@ export class CheckableContinuousFlyout extends ContinuousFlyout { |
48 | 54 | * @const |
49 | 55 | */ |
50 | 56 | static CHECKBOX_SPACE_X = |
51 | | - CheckableContinuousFlyout.CHECKBOX_SIZE + |
52 | | - 2 * CheckableContinuousFlyout.CHECKBOX_MARGIN; |
53 | | - |
| 57 | + CheckableContinuousFlyout.CHECKBOX_SIZE + |
| 58 | + 2 * CheckableContinuousFlyout.CHECKBOX_MARGIN; |
54 | 59 |
|
55 | 60 | constructor(workspaceOptions) { |
56 | 61 | super(workspaceOptions); |
57 | | - this.tabWidth_ = 0; |
58 | | - this.MARGIN = 10; |
59 | | - this.GAP_Y = 8; |
| 62 | + this.tabWidth_ = -2; |
| 63 | + this.MARGIN = 12; |
| 64 | + this.GAP_Y = 12; |
60 | 65 | CheckableContinuousFlyout.CHECKBOX_MARGIN = this.MARGIN; |
61 | 66 |
|
62 | 67 | /** |
@@ -69,6 +74,13 @@ export class CheckableContinuousFlyout extends ContinuousFlyout { |
69 | 74 | this.checkboxes_ = new Map(); |
70 | 75 | } |
71 | 76 |
|
| 77 | + initFlyoutButton_(button, x, y) { |
| 78 | + if (button.isLabel()) { |
| 79 | + button.height = 40; |
| 80 | + } |
| 81 | + super.initFlyoutButton_(button, x, y); |
| 82 | + } |
| 83 | + |
72 | 84 | show(flyoutDef) { |
73 | 85 | this.clearOldCheckboxes(); |
74 | 86 | super.show(flyoutDef); |
@@ -95,16 +107,30 @@ export class CheckableContinuousFlyout extends ContinuousFlyout { |
95 | 107 | if (block.checkboxInFlyout) { |
96 | 108 | const coordinates = block.getRelativeToSurfaceXY(); |
97 | 109 | const checkbox = this.createCheckbox_( |
98 | | - block, coordinates.x, coordinates.y, block.getHeightWidth()); |
| 110 | + block, |
| 111 | + coordinates.x, |
| 112 | + coordinates.y, |
| 113 | + block.getHeightWidth() |
| 114 | + ); |
99 | 115 | let moveX = coordinates.x; |
100 | 116 | if (this.RTL) { |
101 | | - moveX -= (CheckableContinuousFlyout.CHECKBOX_SIZE + CheckableContinuousFlyout.CHECKBOX_MARGIN); |
| 117 | + moveX -= |
| 118 | + CheckableContinuousFlyout.CHECKBOX_SIZE + |
| 119 | + CheckableContinuousFlyout.CHECKBOX_MARGIN; |
102 | 120 | } else { |
103 | | - moveX += CheckableContinuousFlyout.CHECKBOX_SIZE + CheckableContinuousFlyout.CHECKBOX_MARGIN; |
| 121 | + moveX += |
| 122 | + CheckableContinuousFlyout.CHECKBOX_SIZE + |
| 123 | + CheckableContinuousFlyout.CHECKBOX_MARGIN; |
104 | 124 | } |
105 | 125 | block.moveBy(moveX, 0); |
106 | | - this.listeners.push(Blockly.browserEvents.bind(checkbox.svgRoot, |
107 | | - 'mousedown', null, this.checkboxClicked_(checkbox))); |
| 126 | + this.listeners.push( |
| 127 | + Blockly.browserEvents.bind( |
| 128 | + checkbox.svgRoot, |
| 129 | + "mousedown", |
| 130 | + null, |
| 131 | + this.checkboxClicked_(checkbox) |
| 132 | + ) |
| 133 | + ); |
108 | 134 | } |
109 | 135 | super.addBlockListeners_(root, block, rect); |
110 | 136 | } |
@@ -118,7 +144,7 @@ export class CheckableContinuousFlyout extends ContinuousFlyout { |
118 | 144 | * @private |
119 | 145 | */ |
120 | 146 | checkboxClicked_(checkboxObj) { |
121 | | - return function(e) { |
| 147 | + return function (e) { |
122 | 148 | this.setCheckboxState(checkboxObj.block.id, !checkboxObj.clicked); |
123 | 149 | // This event has been handled. No need to bubble up to the document. |
124 | 150 | e.stopPropagation(); |
@@ -138,40 +164,63 @@ export class CheckableContinuousFlyout extends ContinuousFlyout { |
138 | 164 | createCheckbox_(block, cursorX, cursorY, blockHW) { |
139 | 165 | var checkboxState = this.getCheckboxState(block.id); |
140 | 166 | var svgRoot = block.getSvgRoot(); |
141 | | - var extraSpace = CheckableContinuousFlyout.CHECKBOX_SIZE + CheckableContinuousFlyout.CHECKBOX_MARGIN; |
142 | | - var xOffset = this.RTL ? this.getWidth() / this.workspace_.scale - extraSpace : cursorX; |
143 | | - var yOffset = cursorY + blockHW.height / 2 - CheckableContinuousFlyout.CHECKBOX_SIZE / 2; |
| 167 | + var extraSpace = |
| 168 | + CheckableContinuousFlyout.CHECKBOX_SIZE + |
| 169 | + CheckableContinuousFlyout.CHECKBOX_MARGIN; |
| 170 | + var xOffset = this.RTL |
| 171 | + ? this.getWidth() / this.workspace_.scale - extraSpace |
| 172 | + : cursorX; |
| 173 | + var yOffset = |
| 174 | + cursorY + |
| 175 | + blockHW.height / 2 - |
| 176 | + CheckableContinuousFlyout.CHECKBOX_SIZE / 2; |
144 | 177 | var touchMargin = CheckableContinuousFlyout.CHECKBOX_TOUCH_PADDING; |
145 | | - var checkboxGroup = Blockly.utils.dom.createSvgElement('g', |
146 | | - { |
147 | | - 'transform': `translate(${xOffset}, ${yOffset})`, |
148 | | - 'fill': 'transparent', |
149 | | - }, null); |
150 | | - Blockly.utils.dom.createSvgElement('rect', |
151 | | - { |
152 | | - 'class': 'blocklyFlyoutCheckbox', |
153 | | - 'height': CheckableContinuousFlyout.CHECKBOX_SIZE, |
154 | | - 'width': CheckableContinuousFlyout.CHECKBOX_SIZE, |
155 | | - 'rx': CheckableContinuousFlyout.CHECKBOX_CORNER_RADIUS, |
156 | | - 'ry': CheckableContinuousFlyout.CHECKBOX_CORNER_RADIUS |
157 | | - }, checkboxGroup); |
158 | | - Blockly.utils.dom.createSvgElement('path', |
159 | | - { |
160 | | - 'class': 'blocklyFlyoutCheckboxPath', |
161 | | - 'd': CheckableContinuousFlyout.CHECKMARK_PATH |
162 | | - }, checkboxGroup); |
163 | | - Blockly.utils.dom.createSvgElement('rect', |
164 | | - { |
165 | | - 'class': 'blocklyTouchTargetBackground', |
166 | | - 'x': -touchMargin + 'px', |
167 | | - 'y': -touchMargin + 'px', |
168 | | - 'height': CheckableContinuousFlyout.CHECKBOX_SIZE + 2 * touchMargin, |
169 | | - 'width': CheckableContinuousFlyout.CHECKBOX_SIZE + 2 * touchMargin, |
170 | | - }, checkboxGroup); |
171 | | - var checkboxObj = {svgRoot: checkboxGroup, clicked: checkboxState, block: block}; |
| 178 | + var checkboxGroup = Blockly.utils.dom.createSvgElement( |
| 179 | + "g", |
| 180 | + { |
| 181 | + transform: `translate(${xOffset}, ${yOffset})`, |
| 182 | + fill: "transparent", |
| 183 | + }, |
| 184 | + null |
| 185 | + ); |
| 186 | + Blockly.utils.dom.createSvgElement( |
| 187 | + "rect", |
| 188 | + { |
| 189 | + class: "blocklyFlyoutCheckbox", |
| 190 | + height: CheckableContinuousFlyout.CHECKBOX_SIZE, |
| 191 | + width: CheckableContinuousFlyout.CHECKBOX_SIZE, |
| 192 | + rx: CheckableContinuousFlyout.CHECKBOX_CORNER_RADIUS, |
| 193 | + ry: CheckableContinuousFlyout.CHECKBOX_CORNER_RADIUS, |
| 194 | + }, |
| 195 | + checkboxGroup |
| 196 | + ); |
| 197 | + Blockly.utils.dom.createSvgElement( |
| 198 | + "path", |
| 199 | + { |
| 200 | + class: "blocklyFlyoutCheckboxPath", |
| 201 | + d: CheckableContinuousFlyout.CHECKMARK_PATH, |
| 202 | + }, |
| 203 | + checkboxGroup |
| 204 | + ); |
| 205 | + Blockly.utils.dom.createSvgElement( |
| 206 | + "rect", |
| 207 | + { |
| 208 | + class: "blocklyTouchTargetBackground", |
| 209 | + x: -touchMargin + "px", |
| 210 | + y: -touchMargin + "px", |
| 211 | + height: CheckableContinuousFlyout.CHECKBOX_SIZE + 2 * touchMargin, |
| 212 | + width: CheckableContinuousFlyout.CHECKBOX_SIZE + 2 * touchMargin, |
| 213 | + }, |
| 214 | + checkboxGroup |
| 215 | + ); |
| 216 | + var checkboxObj = { |
| 217 | + svgRoot: checkboxGroup, |
| 218 | + clicked: checkboxState, |
| 219 | + block: block, |
| 220 | + }; |
172 | 221 |
|
173 | 222 | if (checkboxState) { |
174 | | - Blockly.utils.dom.addClass((checkboxObj.svgRoot), 'checked'); |
| 223 | + Blockly.utils.dom.addClass(checkboxObj.svgRoot, "checked"); |
175 | 224 | } |
176 | 225 |
|
177 | 226 | this.workspace_.getCanvas().insertBefore(checkboxGroup, svgRoot); |
@@ -195,13 +244,20 @@ export class CheckableContinuousFlyout extends ContinuousFlyout { |
195 | 244 | checkboxObj.clicked = value; |
196 | 245 |
|
197 | 246 | if (checkboxObj.clicked) { |
198 | | - Blockly.utils.dom.addClass(checkboxObj.svgRoot, 'checked'); |
| 247 | + Blockly.utils.dom.addClass(checkboxObj.svgRoot, "checked"); |
199 | 248 | } else { |
200 | | - Blockly.utils.dom.removeClass(checkboxObj.svgRoot, 'checked'); |
| 249 | + Blockly.utils.dom.removeClass(checkboxObj.svgRoot, "checked"); |
201 | 250 | } |
202 | 251 |
|
203 | | - Blockly.Events.fire(new Blockly.Events.BlockChange( |
204 | | - checkboxObj.block, 'checkbox', null, oldValue, value)); |
| 252 | + Blockly.Events.fire( |
| 253 | + new Blockly.Events.BlockChange( |
| 254 | + checkboxObj.block, |
| 255 | + "checkbox", |
| 256 | + null, |
| 257 | + oldValue, |
| 258 | + value |
| 259 | + ) |
| 260 | + ); |
205 | 261 | } |
206 | 262 |
|
207 | 263 | /** |
|