/**
* @license
* Copyright 2011 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Functions for injecting Blockly into a web page.
*/
'use strict';
/**
* Functions for injecting Blockly into a web page.
* @namespace Blockly.inject
*/
goog.module('Blockly.inject');
const Css = goog.require('Blockly.Css');
const Msg = goog.require('Blockly.Msg');
const Tooltip = goog.require('Blockly.Tooltip');
const Touch = goog.require('Blockly.Touch');
const WidgetDiv = goog.require('Blockly.WidgetDiv');
const aria = goog.require('Blockly.utils.aria');
const browserEvents = goog.require('Blockly.browserEvents');
const bumpObjects = goog.require('Blockly.bumpObjects');
const common = goog.require('Blockly.common');
const dom = goog.require('Blockly.utils.dom');
const userAgent = goog.require('Blockly.utils.userAgent');
const {BlockDragSurfaceSvg} = goog.require('Blockly.BlockDragSurfaceSvg');
/* eslint-disable-next-line no-unused-vars */
const {BlocklyOptions} = goog.requireType('Blockly.BlocklyOptions');
const {DropDownDiv} = goog.require('Blockly.DropDownDiv');
const {Grid} = goog.require('Blockly.Grid');
const {Options} = goog.require('Blockly.Options');
const {ScrollbarPair} = goog.require('Blockly.ScrollbarPair');
const {ShortcutRegistry} = goog.require('Blockly.ShortcutRegistry');
const {Svg} = goog.require('Blockly.utils.Svg');
const {WorkspaceDragSurfaceSvg} = goog.require('Blockly.WorkspaceDragSurfaceSvg');
const {WorkspaceSvg} = goog.require('Blockly.WorkspaceSvg');
const {Workspace} = goog.require('Blockly.Workspace');
/**
* Inject a Blockly editor into the specified container element (usually a div).
* @param {Element|string} container Containing element, or its ID,
* or a CSS selector.
* @param {BlocklyOptions=} opt_options Optional dictionary of options.
* @return {!WorkspaceSvg} Newly created main workspace.
* @alias Blockly.inject
*/
const inject = function(container, opt_options) {
if (typeof container === 'string') {
container =
document.getElementById(container) || document.querySelector(container);
}
// Verify that the container is in document.
if (!container || !dom.containsNode(document, container)) {
throw Error('Error: container is not in current document.');
}
const options =
new Options(opt_options || (/** @type {!BlocklyOptions} */ ({})));
const subContainer = document.createElement('div');
subContainer.className = 'injectionDiv';
subContainer.tabIndex = 0;
aria.setState(subContainer, aria.State.LABEL, Msg['WORKSPACE_ARIA_LABEL']);
container.appendChild(subContainer);
const svg = createDom(subContainer, options);
// Create surfaces for dragging things. These are optimizations
// so that the browser does not repaint during the drag.
const blockDragSurface = new BlockDragSurfaceSvg(subContainer);
const workspaceDragSurface = new WorkspaceDragSurfaceSvg(subContainer);
const workspace =
createMainWorkspace(svg, options, blockDragSurface, workspaceDragSurface);
init(workspace);
// Keep focus on the first workspace so entering keyboard navigation looks
// correct.
common.setMainWorkspace(workspace);
common.svgResize(workspace);
subContainer.addEventListener('focusin', function() {
common.setMainWorkspace(workspace);
});
return workspace;
};
/**
* Create the SVG image.
* @param {!Element} container Containing element.
* @param {!Options} options Dictionary of options.
* @return {!Element} Newly created SVG image.
*/
const createDom = function(container, options) {
// Sadly browsers (Chrome vs Firefox) are currently inconsistent in laying
// out content in RTL mode. Therefore Blockly forces the use of LTR,
// then manually positions content in RTL as needed.
container.setAttribute('dir', 'LTR');
// Load CSS.
Css.inject(options.hasCss, options.pathToMedia);
// Build the SVG DOM.
/*
*/
const svg = dom.createSvgElement(
Svg.SVG, {
'xmlns': dom.SVG_NS,
'xmlns:html': dom.HTML_NS,
'xmlns:xlink': dom.XLINK_NS,
'version': '1.1',
'class': 'blocklySvg',
'tabindex': '0',
},
container);
/*
... filters go here ...
*/
const defs = dom.createSvgElement(Svg.DEFS, {}, svg);
// Each filter/pattern needs a unique ID for the case of multiple Blockly
// instances on a page. Browser behaviour becomes undefined otherwise.
// https://neil.fraser.name/news/2015/11/01/
const rnd = String(Math.random()).substring(2);
options.gridPattern = Grid.createDom(rnd, options.gridOptions, defs);
return svg;
};
/**
* Create a main workspace and add it to the SVG.
* @param {!Element} svg SVG element with pattern defined.
* @param {!Options} options Dictionary of options.
* @param {!BlockDragSurfaceSvg} blockDragSurface Drag surface SVG
* for the blocks.
* @param {!WorkspaceDragSurfaceSvg} workspaceDragSurface Drag surface
* SVG for the workspace.
* @return {!WorkspaceSvg} Newly created main workspace.
*/
const createMainWorkspace = function(
svg, options, blockDragSurface, workspaceDragSurface) {
options.parentWorkspace = null;
const mainWorkspace =
new WorkspaceSvg(options, blockDragSurface, workspaceDragSurface);
const wsOptions = mainWorkspace.options;
mainWorkspace.scale = wsOptions.zoomOptions.startScale;
svg.appendChild(mainWorkspace.createDom('blocklyMainBackground'));
// Set the theme name and renderer name onto the injection div.
dom.addClass(
mainWorkspace.getInjectionDiv(),
mainWorkspace.getRenderer().getClassName());
dom.addClass(
mainWorkspace.getInjectionDiv(), mainWorkspace.getTheme().getClassName());
if (!wsOptions.hasCategories && wsOptions.languageTree) {
// Add flyout as an