|
|
|
|
@@ -16,131 +16,168 @@
|
|
|
|
|
* @name Blockly.WidgetDiv
|
|
|
|
|
* @namespace
|
|
|
|
|
*/
|
|
|
|
|
goog.provide('Blockly.WidgetDiv');
|
|
|
|
|
goog.module('Blockly.WidgetDiv');
|
|
|
|
|
goog.module.declareLegacyNamespace();
|
|
|
|
|
|
|
|
|
|
goog.require('Blockly.common');
|
|
|
|
|
goog.require('Blockly.utils.dom');
|
|
|
|
|
|
|
|
|
|
goog.requireType('Blockly.utils.Rect');
|
|
|
|
|
goog.requireType('Blockly.utils.Size');
|
|
|
|
|
goog.requireType('Blockly.WorkspaceSvg');
|
|
|
|
|
/* eslint-disable-next-line no-unused-vars */
|
|
|
|
|
const Rect = goog.requireType('Blockly.utils.Rect');
|
|
|
|
|
/* eslint-disable-next-line no-unused-vars */
|
|
|
|
|
const Size = goog.requireType('Blockly.utils.Size');
|
|
|
|
|
/* eslint-disable-next-line no-unused-vars */
|
|
|
|
|
const WorkspaceSvg = goog.requireType('Blockly.WorkspaceSvg');
|
|
|
|
|
const common = goog.require('Blockly.common');
|
|
|
|
|
const deprecation = goog.require('Blockly.utils.deprecation');
|
|
|
|
|
const dom = goog.require('Blockly.utils.dom');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The object currently using this container.
|
|
|
|
|
* @type {Object}
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
Blockly.WidgetDiv.owner_ = null;
|
|
|
|
|
let owner = null;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Optional cleanup function set by whichever object uses the widget.
|
|
|
|
|
* @type {Function}
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
Blockly.WidgetDiv.dispose_ = null;
|
|
|
|
|
let dispose = null;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A class name representing the current owner's workspace renderer.
|
|
|
|
|
* @type {string}
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
Blockly.WidgetDiv.rendererClassName_ = '';
|
|
|
|
|
let rendererClassName = '';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A class name representing the current owner's workspace theme.
|
|
|
|
|
* @type {string}
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
Blockly.WidgetDiv.themeClassName_ = '';
|
|
|
|
|
let themeClassName = '';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The HTML container for popup overlays (e.g. editor widgets).
|
|
|
|
|
* @type {?Element}
|
|
|
|
|
*/
|
|
|
|
|
let DIV;
|
|
|
|
|
/** @deprecated September 2021 */
|
|
|
|
|
exports.DIV = DIV;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the HTML container for editor widgets.
|
|
|
|
|
* @return {?Element} The editor widget container.
|
|
|
|
|
*/
|
|
|
|
|
const getDiv = function() {
|
|
|
|
|
return DIV;
|
|
|
|
|
};
|
|
|
|
|
exports.getDiv = getDiv;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Allows unit tests to reset the div.
|
|
|
|
|
* @param {?Element} newDiv The new value for the DIV field.
|
|
|
|
|
*/
|
|
|
|
|
const testOnly_setDiv = function(newDiv) {
|
|
|
|
|
DIV = newDiv;
|
|
|
|
|
};
|
|
|
|
|
exports.testOnly_setDiv = testOnly_setDiv;
|
|
|
|
|
|
|
|
|
|
Object.defineProperties(exports, {
|
|
|
|
|
DIV: {
|
|
|
|
|
get: function() {
|
|
|
|
|
deprecation.warn(
|
|
|
|
|
'Blockly.WidgetDiv.DIV', 'September 2021', 'September 2022',
|
|
|
|
|
'Blockly.WidgetDiv.getDiv()');
|
|
|
|
|
return getDiv();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create the widget div and inject it onto the page.
|
|
|
|
|
*/
|
|
|
|
|
Blockly.WidgetDiv.createDom = function() {
|
|
|
|
|
if (Blockly.WidgetDiv.DIV) {
|
|
|
|
|
const createDom = function() {
|
|
|
|
|
if (DIV) {
|
|
|
|
|
return; // Already created.
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* The HTML container for popup overlays (e.g. editor widgets).
|
|
|
|
|
* @type {!Element}
|
|
|
|
|
*/
|
|
|
|
|
Blockly.WidgetDiv.DIV = document.createElement('div');
|
|
|
|
|
Blockly.WidgetDiv.DIV.className = 'blocklyWidgetDiv';
|
|
|
|
|
var container = Blockly.common.getParentContainer() || document.body;
|
|
|
|
|
container.appendChild(Blockly.WidgetDiv.DIV);
|
|
|
|
|
|
|
|
|
|
DIV = document.createElement('div');
|
|
|
|
|
DIV.className = 'blocklyWidgetDiv';
|
|
|
|
|
const container = common.getParentContainer() || document.body;
|
|
|
|
|
container.appendChild(DIV);
|
|
|
|
|
};
|
|
|
|
|
exports.createDom = createDom;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialize and display the widget div. Close the old one if needed.
|
|
|
|
|
* @param {!Object} newOwner The object that will be using this container.
|
|
|
|
|
* @param {boolean} rtl Right-to-left (true) or left-to-right (false).
|
|
|
|
|
* @param {Function} dispose Optional cleanup function to be run when the
|
|
|
|
|
* @param {Function} newDispose Optional cleanup function to be run when the
|
|
|
|
|
* widget is closed.
|
|
|
|
|
*/
|
|
|
|
|
Blockly.WidgetDiv.show = function(newOwner, rtl, dispose) {
|
|
|
|
|
Blockly.WidgetDiv.hide();
|
|
|
|
|
Blockly.WidgetDiv.owner_ = newOwner;
|
|
|
|
|
Blockly.WidgetDiv.dispose_ = dispose;
|
|
|
|
|
var div = Blockly.WidgetDiv.DIV;
|
|
|
|
|
const show = function(newOwner, rtl, newDispose) {
|
|
|
|
|
hide();
|
|
|
|
|
owner = newOwner;
|
|
|
|
|
dispose = newDispose;
|
|
|
|
|
const div = DIV;
|
|
|
|
|
div.style.direction = rtl ? 'rtl' : 'ltr';
|
|
|
|
|
div.style.display = 'block';
|
|
|
|
|
var mainWorkspace =
|
|
|
|
|
/** @type {!Blockly.WorkspaceSvg} */ (Blockly.common.getMainWorkspace());
|
|
|
|
|
Blockly.WidgetDiv.rendererClassName_ =
|
|
|
|
|
mainWorkspace.getRenderer().getClassName();
|
|
|
|
|
Blockly.WidgetDiv.themeClassName_ = mainWorkspace.getTheme().getClassName();
|
|
|
|
|
Blockly.utils.dom.addClass(div, Blockly.WidgetDiv.rendererClassName_);
|
|
|
|
|
Blockly.utils.dom.addClass(div, Blockly.WidgetDiv.themeClassName_);
|
|
|
|
|
const mainWorkspace =
|
|
|
|
|
/** @type {!WorkspaceSvg} */ (common.getMainWorkspace());
|
|
|
|
|
rendererClassName = mainWorkspace.getRenderer().getClassName();
|
|
|
|
|
themeClassName = mainWorkspace.getTheme().getClassName();
|
|
|
|
|
dom.addClass(div, rendererClassName);
|
|
|
|
|
dom.addClass(div, themeClassName);
|
|
|
|
|
};
|
|
|
|
|
exports.show = show;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Destroy the widget and hide the div.
|
|
|
|
|
*/
|
|
|
|
|
Blockly.WidgetDiv.hide = function() {
|
|
|
|
|
if (!Blockly.WidgetDiv.isVisible()) {
|
|
|
|
|
const hide = function() {
|
|
|
|
|
if (!isVisible()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Blockly.WidgetDiv.owner_ = null;
|
|
|
|
|
owner = null;
|
|
|
|
|
|
|
|
|
|
var div = Blockly.WidgetDiv.DIV;
|
|
|
|
|
const div = DIV;
|
|
|
|
|
div.style.display = 'none';
|
|
|
|
|
div.style.left = '';
|
|
|
|
|
div.style.top = '';
|
|
|
|
|
Blockly.WidgetDiv.dispose_ && Blockly.WidgetDiv.dispose_();
|
|
|
|
|
Blockly.WidgetDiv.dispose_ = null;
|
|
|
|
|
dispose && dispose();
|
|
|
|
|
dispose = null;
|
|
|
|
|
div.textContent = '';
|
|
|
|
|
|
|
|
|
|
if (Blockly.WidgetDiv.rendererClassName_) {
|
|
|
|
|
Blockly.utils.dom.removeClass(div, Blockly.WidgetDiv.rendererClassName_);
|
|
|
|
|
Blockly.WidgetDiv.rendererClassName_ = '';
|
|
|
|
|
if (rendererClassName) {
|
|
|
|
|
dom.removeClass(div, rendererClassName);
|
|
|
|
|
rendererClassName = '';
|
|
|
|
|
}
|
|
|
|
|
if (Blockly.WidgetDiv.themeClassName_) {
|
|
|
|
|
Blockly.utils.dom.removeClass(div, Blockly.WidgetDiv.themeClassName_);
|
|
|
|
|
Blockly.WidgetDiv.themeClassName_ = '';
|
|
|
|
|
if (themeClassName) {
|
|
|
|
|
dom.removeClass(div, themeClassName);
|
|
|
|
|
themeClassName = '';
|
|
|
|
|
}
|
|
|
|
|
(/** @type {!Blockly.WorkspaceSvg} */ (
|
|
|
|
|
Blockly.common.getMainWorkspace())).markFocused();
|
|
|
|
|
(/** @type {!WorkspaceSvg} */ (common.getMainWorkspace())).markFocused();
|
|
|
|
|
};
|
|
|
|
|
exports.hide = hide;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Is the container visible?
|
|
|
|
|
* @return {boolean} True if visible.
|
|
|
|
|
*/
|
|
|
|
|
Blockly.WidgetDiv.isVisible = function() {
|
|
|
|
|
return !!Blockly.WidgetDiv.owner_;
|
|
|
|
|
const isVisible = function() {
|
|
|
|
|
return !!owner;
|
|
|
|
|
};
|
|
|
|
|
exports.isVisible = isVisible;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Destroy the widget and hide the div if it is being used by the specified
|
|
|
|
|
* object.
|
|
|
|
|
* @param {!Object} oldOwner The object that was using this container.
|
|
|
|
|
*/
|
|
|
|
|
Blockly.WidgetDiv.hideIfOwner = function(oldOwner) {
|
|
|
|
|
if (Blockly.WidgetDiv.owner_ == oldOwner) {
|
|
|
|
|
Blockly.WidgetDiv.hide();
|
|
|
|
|
const hideIfOwner = function(oldOwner) {
|
|
|
|
|
if (owner == oldOwner) {
|
|
|
|
|
hide();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
exports.hideIfOwner = hideIfOwner;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the widget div's position and height. This function does nothing clever:
|
|
|
|
|
@@ -148,12 +185,11 @@ Blockly.WidgetDiv.hideIfOwner = function(oldOwner) {
|
|
|
|
|
* @param {number} x Horizontal location (window coordinates, not body).
|
|
|
|
|
* @param {number} y Vertical location (window coordinates, not body).
|
|
|
|
|
* @param {number} height The height of the widget div (pixels).
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
Blockly.WidgetDiv.positionInternal_ = function(x, y, height) {
|
|
|
|
|
Blockly.WidgetDiv.DIV.style.left = x + 'px';
|
|
|
|
|
Blockly.WidgetDiv.DIV.style.top = y + 'px';
|
|
|
|
|
Blockly.WidgetDiv.DIV.style.height = height + 'px';
|
|
|
|
|
const positionInternal = function(x, y, height) {
|
|
|
|
|
DIV.style.left = x + 'px';
|
|
|
|
|
DIV.style.top = y + 'px';
|
|
|
|
|
DIV.style.height = height + 'px';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@@ -161,56 +197,53 @@ Blockly.WidgetDiv.positionInternal_ = function(x, y, height) {
|
|
|
|
|
* The widget should be placed adjacent to but not overlapping the anchor
|
|
|
|
|
* rectangle. The preferred position is directly below and aligned to the left
|
|
|
|
|
* (LTR) or right (RTL) side of the anchor.
|
|
|
|
|
* @param {!Blockly.utils.Rect} viewportBBox The bounding rectangle of the
|
|
|
|
|
* @param {!Rect} viewportBBox The bounding rectangle of the
|
|
|
|
|
* current viewport, in window coordinates.
|
|
|
|
|
* @param {!Blockly.utils.Rect} anchorBBox The bounding rectangle of the anchor,
|
|
|
|
|
* @param {!Rect} anchorBBox The bounding rectangle of the anchor,
|
|
|
|
|
* in window coordinates.
|
|
|
|
|
* @param {!Blockly.utils.Size} widgetSize The size of the widget that is inside
|
|
|
|
|
* @param {!Size} widgetSize The size of the widget that is inside
|
|
|
|
|
* the widget div, in window coordinates.
|
|
|
|
|
* @param {boolean} rtl Whether the workspace is in RTL mode. This determines
|
|
|
|
|
* horizontal alignment.
|
|
|
|
|
* @package
|
|
|
|
|
*/
|
|
|
|
|
Blockly.WidgetDiv.positionWithAnchor = function(viewportBBox, anchorBBox,
|
|
|
|
|
widgetSize, rtl) {
|
|
|
|
|
var y = Blockly.WidgetDiv.calculateY_(viewportBBox, anchorBBox, widgetSize);
|
|
|
|
|
var x = Blockly.WidgetDiv.calculateX_(viewportBBox, anchorBBox, widgetSize,
|
|
|
|
|
rtl);
|
|
|
|
|
const positionWithAnchor = function(viewportBBox, anchorBBox, widgetSize, rtl) {
|
|
|
|
|
const y = calculateY(viewportBBox, anchorBBox, widgetSize);
|
|
|
|
|
const x = calculateX(viewportBBox, anchorBBox, widgetSize, rtl);
|
|
|
|
|
|
|
|
|
|
if (y < 0) {
|
|
|
|
|
Blockly.WidgetDiv.positionInternal_(x, 0, widgetSize.height + y);
|
|
|
|
|
positionInternal(x, 0, widgetSize.height + y);
|
|
|
|
|
} else {
|
|
|
|
|
Blockly.WidgetDiv.positionInternal_(x, y, widgetSize.height);
|
|
|
|
|
positionInternal(x, y, widgetSize.height);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
/** @package */
|
|
|
|
|
exports.positionWithAnchor = positionWithAnchor;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Calculate an x position (in window coordinates) such that the widget will not
|
|
|
|
|
* be offscreen on the right or left.
|
|
|
|
|
* @param {!Blockly.utils.Rect} viewportBBox The bounding rectangle of the
|
|
|
|
|
* @param {!Rect} viewportBBox The bounding rectangle of the
|
|
|
|
|
* current viewport, in window coordinates.
|
|
|
|
|
* @param {!Blockly.utils.Rect} anchorBBox The bounding rectangle of the anchor,
|
|
|
|
|
* @param {!Rect} anchorBBox The bounding rectangle of the anchor,
|
|
|
|
|
* in window coordinates.
|
|
|
|
|
* @param {!Blockly.utils.Size} widgetSize The dimensions of the widget inside
|
|
|
|
|
* @param {!Size} widgetSize The dimensions of the widget inside
|
|
|
|
|
* the widget div.
|
|
|
|
|
* @param {boolean} rtl Whether the Blockly workspace is in RTL mode.
|
|
|
|
|
* @return {number} A valid x-coordinate for the top left corner of the widget
|
|
|
|
|
* div, in window coordinates.
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
Blockly.WidgetDiv.calculateX_ = function(viewportBBox, anchorBBox, widgetSize,
|
|
|
|
|
rtl) {
|
|
|
|
|
const calculateX = function(viewportBBox, anchorBBox, widgetSize, rtl) {
|
|
|
|
|
if (rtl) {
|
|
|
|
|
// Try to align the right side of the field and the right side of widget.
|
|
|
|
|
var widgetLeft = anchorBBox.right - widgetSize.width;
|
|
|
|
|
const widgetLeft = anchorBBox.right - widgetSize.width;
|
|
|
|
|
// Don't go offscreen left.
|
|
|
|
|
var x = Math.max(widgetLeft, viewportBBox.left);
|
|
|
|
|
const x = Math.max(widgetLeft, viewportBBox.left);
|
|
|
|
|
// But really don't go offscreen right:
|
|
|
|
|
return Math.min(x, viewportBBox.right - widgetSize.width);
|
|
|
|
|
} else {
|
|
|
|
|
// Try to align the left side of the field and the left side of widget.
|
|
|
|
|
// Don't go offscreen right.
|
|
|
|
|
var x = Math.min(anchorBBox.left, viewportBBox.right - widgetSize.width);
|
|
|
|
|
const x = Math.min(anchorBBox.left, viewportBBox.right - widgetSize.width);
|
|
|
|
|
// But left is more important, because that's where the text is.
|
|
|
|
|
return Math.max(x, viewportBBox.left);
|
|
|
|
|
}
|
|
|
|
|
@@ -219,17 +252,16 @@ Blockly.WidgetDiv.calculateX_ = function(viewportBBox, anchorBBox, widgetSize,
|
|
|
|
|
/**
|
|
|
|
|
* Calculate a y position (in window coordinates) such that the widget will not
|
|
|
|
|
* be offscreen on the top or bottom.
|
|
|
|
|
* @param {!Blockly.utils.Rect} viewportBBox The bounding rectangle of the
|
|
|
|
|
* @param {!Rect} viewportBBox The bounding rectangle of the
|
|
|
|
|
* current viewport, in window coordinates.
|
|
|
|
|
* @param {!Blockly.utils.Rect} anchorBBox The bounding rectangle of the anchor,
|
|
|
|
|
* @param {!Rect} anchorBBox The bounding rectangle of the anchor,
|
|
|
|
|
* in window coordinates.
|
|
|
|
|
* @param {!Blockly.utils.Size} widgetSize The dimensions of the widget inside
|
|
|
|
|
* @param {!Size} widgetSize The dimensions of the widget inside
|
|
|
|
|
* the widget div.
|
|
|
|
|
* @return {number} A valid y-coordinate for the top left corner of the widget
|
|
|
|
|
* div, in window coordinates.
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
Blockly.WidgetDiv.calculateY_ = function(viewportBBox, anchorBBox, widgetSize) {
|
|
|
|
|
const calculateY = function(viewportBBox, anchorBBox, widgetSize) {
|
|
|
|
|
// Flip the widget vertically if off the bottom.
|
|
|
|
|
if (anchorBBox.bottom + widgetSize.height >= viewportBBox.bottom) {
|
|
|
|
|
// The bottom of the widget is at the top of the field.
|
|
|
|
|
|