Fix circular dependency.

This commit is contained in:
Neil Fraser
2018-10-12 05:47:27 -07:00
committed by Neil Fraser
parent e62bb1af73
commit dddb94aedd
17 changed files with 105 additions and 69 deletions

View File

@@ -36,8 +36,7 @@ goog.provide('Blockly.Events.Move'); // Deprecated.
goog.require('Blockly.Events');
goog.require('Blockly.Events.Abstract');
// TODO Fix circular dependencies
//goog.require('Blockly.Xml');
goog.require('Blockly.Xml.utils');
goog.require('goog.math.Coordinate');
@@ -274,7 +273,7 @@ Blockly.Events.Create.prototype.fromJson = function(json) {
Blockly.Events.Create.prototype.run = function(forward) {
var workspace = this.getEventWorkspace_();
if (forward) {
var xml = Blockly.Xml.createElement('xml');
var xml = Blockly.Xml.utils.createElement('xml');
xml.appendChild(this.xml);
Blockly.Xml.domToWorkspace(xml, workspace);
} else {
@@ -364,7 +363,7 @@ Blockly.Events.Delete.prototype.run = function(forward) {
}
}
} else {
var xml = Blockly.Xml.createElement('xml');
var xml = Blockly.Xml.utils.createElement('xml');
xml.appendChild(this.oldXml);
Blockly.Xml.domToWorkspace(xml, workspace);
}

View File

@@ -27,7 +27,7 @@
/**
* The top level namespace used to access the Blockly library.
* @namespace Blockly
**/
*/
goog.provide('Blockly');
goog.require('Blockly.BlockSvg.render');

View File

@@ -30,7 +30,7 @@
/**
* @name Blockly.Extensions
* @namespace
**/
*/
goog.provide('Blockly.Extensions');
goog.require('Blockly.Mutator');

View File

@@ -379,7 +379,7 @@ Blockly.Field.prototype.render_ = function() {
* Updates thw width of the field. This calls getCachedWidth which won't cache
* the approximated width on IE/Edge when `getComputedTextLength` fails. Once
* it eventually does succeed, the result will be cached.
**/
*/
Blockly.Field.prototype.updateWidth = function() {
var width = Blockly.Field.getCachedWidth(this.textElement_);
if (this.borderRect_) {

View File

@@ -34,6 +34,7 @@ goog.require('Blockly.Icon');
goog.require('Blockly.utils');
goog.require('Blockly.WorkspaceSvg');
goog.require('Blockly.Xml');
goog.require('Blockly.Xml.utils');
/**
@@ -130,9 +131,9 @@ Blockly.Mutator.prototype.createEditor_ = function() {
null);
// Convert the list of names into a list of XML objects for the flyout.
if (this.quarkNames_.length) {
var quarkXml = Blockly.Xml.createElement('xml');
var quarkXml = Blockly.Xml.utils.createElement('xml');
for (var i = 0, quarkName; quarkName = this.quarkNames_[i]; i++) {
var element = Blockly.Xml.createElement('block');
var element = Blockly.Xml.utils.createElement('block');
element.setAttribute('type', quarkName);
quarkXml.appendChild(element);
}

View File

@@ -133,7 +133,7 @@ Blockly.Options = function(options) {
/**
* The parent of the current workspace, or null if there is no parent workspace.
* @type {Blockly.Workspace}
**/
*/
Blockly.Options.prototype.parentWorkspace = null;
/**

View File

@@ -27,7 +27,7 @@
/**
* @name Blockly.Procedures
* @namespace
**/
*/
goog.provide('Blockly.Procedures');
goog.require('Blockly.Blocks');
@@ -37,6 +37,7 @@ goog.require('Blockly.Field');
goog.require('Blockly.Names');
goog.require('Blockly.Workspace');
goog.require('Blockly.Xml');
goog.require('Blockly.Xml.utils');
/**
@@ -185,12 +186,12 @@ Blockly.Procedures.flyoutCategory = function(workspace) {
// <block type="procedures_defnoreturn" gap="16">
// <field name="NAME">do something</field>
// </block>
var block = Blockly.Xml.createElement('block');
var block = Blockly.Xml.utils.createElement('block');
block.setAttribute('type', 'procedures_defnoreturn');
block.setAttribute('gap', 16);
var nameField = Blockly.Xml.createElement('field');
var nameField = Blockly.Xml.utils.createElement('field');
nameField.setAttribute('name', 'NAME');
nameField.appendChild(Blockly.Xml.createTextNode(
nameField.appendChild(Blockly.Xml.utils.createTextNode(
Blockly.Msg['PROCEDURES_DEFNORETURN_PROCEDURE']));
block.appendChild(nameField);
xmlList.push(block);
@@ -199,19 +200,19 @@ Blockly.Procedures.flyoutCategory = function(workspace) {
// <block type="procedures_defreturn" gap="16">
// <field name="NAME">do something</field>
// </block>
var block = Blockly.Xml.createElement('block');
var block = Blockly.Xml.utils.createElement('block');
block.setAttribute('type', 'procedures_defreturn');
block.setAttribute('gap', 16);
var nameField = Blockly.Xml.createElement('field');
var nameField = Blockly.Xml.utils.createElement('field');
nameField.setAttribute('name', 'NAME');
nameField.appendChild(Blockly.Xml.createTextNode(
nameField.appendChild(Blockly.Xml.utils.createTextNode(
Blockly.Msg['PROCEDURES_DEFRETURN_PROCEDURE']));
block.appendChild(nameField);
xmlList.push(block);
}
if (Blockly.Blocks['procedures_ifreturn']) {
// <block type="procedures_ifreturn" gap="16"></block>
var block = Blockly.Xml.createElement('block');
var block = Blockly.Xml.utils.createElement('block');
block.setAttribute('type', 'procedures_ifreturn');
block.setAttribute('gap', 16);
xmlList.push(block);
@@ -230,14 +231,14 @@ Blockly.Procedures.flyoutCategory = function(workspace) {
// <arg name="x"></arg>
// </mutation>
// </block>
var block = Blockly.Xml.createElement('block');
var block = Blockly.Xml.utils.createElement('block');
block.setAttribute('type', templateName);
block.setAttribute('gap', 16);
var mutation = Blockly.Xml.createElement('mutation');
var mutation = Blockly.Xml.utils.createElement('mutation');
mutation.setAttribute('name', name);
block.appendChild(mutation);
for (var j = 0; j < args.length; j++) {
var arg = Blockly.Xml.createElement('arg');
var arg = Blockly.Xml.utils.createElement('arg');
arg.setAttribute('name', args[j]);
mutation.appendChild(arg);
}

View File

@@ -32,7 +32,7 @@
/**
* @name Blockly.Tooltip
* @namespace
**/
*/
goog.provide('Blockly.Tooltip');
goog.require('Blockly.utils');

View File

@@ -27,7 +27,7 @@
/**
* @name Blockly.Touch
* @namespace
**/
*/
goog.provide('Blockly.Touch');
goog.require('Blockly.utils');

View File

@@ -27,7 +27,7 @@
/**
* @name Blockly.utils.uiMenu
* @namespace
**/
*/
goog.provide('Blockly.utils.uiMenu');

View File

@@ -29,7 +29,7 @@
/**
* @name Blockly.utils
* @namespace
**/
*/
goog.provide('Blockly.utils');
goog.require('goog.dom');

View File

@@ -27,7 +27,7 @@
/**
* @name Blockly.Variables
* @namespace
**/
*/
goog.provide('Blockly.Variables');
goog.require('Blockly.Blocks');

View File

@@ -29,7 +29,7 @@
/**
* @name Blockly.WidgetDiv
* @namespace
**/
*/
goog.provide('Blockly.WidgetDiv');
goog.require('Blockly.Css');

View File

@@ -31,7 +31,7 @@ goog.require('Blockly.Events.CommentCreate');
goog.require('Blockly.Events.CommentDelete');
goog.require('Blockly.Events.CommentMove');
goog.require('Blockly.utils');
goog.require('Blockly.Xml');
goog.require('Blockly.Xml.utils');
goog.require('goog.math.Coordinate');
@@ -278,7 +278,7 @@ Blockly.WorkspaceComment.prototype.toXmlWithXY = function(opt_noId) {
* @package
*/
Blockly.WorkspaceComment.prototype.toXml = function(opt_noId) {
var commentElement = Blockly.Xml.createElement('comment');
var commentElement = Blockly.Xml.utils.createElement('comment');
if (!opt_noId) {
commentElement.setAttribute('id', this.id);
}

View File

@@ -33,6 +33,7 @@ goog.provide('Blockly.Events.CommentMove');
goog.require('Blockly.Events');
goog.require('Blockly.Events.Abstract');
goog.require('Blockly.Xml');
goog.require('Blockly.Xml.utils');
goog.require('goog.math.Coordinate');
@@ -227,7 +228,7 @@ Blockly.Events.CommentCreate.prototype.run = function(forward) {
Blockly.Events.CommentCreateDeleteHelper = function(event, create) {
var workspace = event.getEventWorkspace_();
if (create) {
var xml = Blockly.Xml.createElement('xml');
var xml = Blockly.Xml.utils.createElement('xml');
xml.appendChild(event.xml);
Blockly.Xml.domToWorkspace(xml, workspace);
} else {

View File

@@ -27,11 +27,12 @@
/**
* @name Blockly.Xml
* @namespace
**/
*/
goog.provide('Blockly.Xml');
goog.require('Blockly.Events.BlockCreate');
goog.require('Blockly.Events.VarCreate');
goog.require('Blockly.Xml.utils');
/**
@@ -41,7 +42,7 @@ goog.require('Blockly.Events.VarCreate');
* @return {!Element} XML document.
*/
Blockly.Xml.workspaceToDom = function(workspace, opt_noId) {
var xml = Blockly.Xml.createElement('xml');
var xml = Blockly.Xml.utils.createElement('xml');
var variablesElement = Blockly.Xml.variablesToDom(
Blockly.Variables.allUsedVarModels(workspace));
if (variablesElement.hasChildNodes()) {
@@ -65,10 +66,10 @@ Blockly.Xml.workspaceToDom = function(workspace, opt_noId) {
* @return {!Element} List of XML elements.
*/
Blockly.Xml.variablesToDom = function(variableList) {
var variables = Blockly.Xml.createElement('variables');
var variables = Blockly.Xml.utils.createElement('variables');
for (var i = 0, variable; variable = variableList[i]; i++) {
var element = Blockly.Xml.createElement('variable');
element.appendChild(Blockly.Xml.createTextNode(variable.name));
var element = Blockly.Xml.utils.createElement('variable');
element.appendChild(Blockly.Xml.utils.createTextNode(variable.name));
element.setAttribute('type', variable.type);
element.setAttribute('id', variable.getId());
variables.appendChild(element);
@@ -122,8 +123,8 @@ Blockly.Xml.fieldToDomVariable_ = function(field) {
if (!variable) {
throw Error('Tried to serialize a variable field with no variable.');
}
var container = Blockly.Xml.createElement('field');
container.appendChild(Blockly.Xml.createTextNode(variable.name));
var container = Blockly.Xml.utils.createElement('field');
container.appendChild(Blockly.Xml.utils.createTextNode(variable.name));
container.setAttribute('name', field.name);
container.setAttribute('id', variable.getId());
container.setAttribute('variabletype', variable.type);
@@ -142,8 +143,8 @@ Blockly.Xml.fieldToDom_ = function(field) {
if (field.referencesVariables()) {
return Blockly.Xml.fieldToDomVariable_(field);
} else {
var container = Blockly.Xml.createElement('field');
container.appendChild(Blockly.Xml.createTextNode(field.getValue()));
var container = Blockly.Xml.utils.createElement('field');
container.appendChild(Blockly.Xml.utils.createTextNode(field.getValue()));
container.setAttribute('name', field.name);
return container;
}
@@ -177,7 +178,7 @@ Blockly.Xml.allFieldsToDom_ = function(block, element) {
* @return {!Element} Tree of XML elements.
*/
Blockly.Xml.blockToDom = function(block, opt_noId) {
var element = Blockly.Xml.createElement(block.isShadow() ? 'shadow' : 'block');
var element = Blockly.Xml.utils.createElement(block.isShadow() ? 'shadow' : 'block');
element.setAttribute('type', block.type);
if (!opt_noId) {
element.setAttribute('id', block.id);
@@ -194,8 +195,8 @@ Blockly.Xml.blockToDom = function(block, opt_noId) {
var commentText = block.getCommentText();
if (commentText) {
var commentElement = Blockly.Xml.createElement('comment');
containerElement.appendChild(Blockly.Xml.createTextNode(commentText));
var commentElement = Blockly.Xml.utils.createElement('comment');
containerElement.appendChild(Blockly.Xml.utils.createTextNode(commentText));
if (typeof block.comment == 'object') {
commentElement.setAttribute('pinned', block.comment.isVisible());
var hw = block.comment.getBubbleSize();
@@ -206,8 +207,8 @@ Blockly.Xml.blockToDom = function(block, opt_noId) {
}
if (block.data) {
var dataElement = Blockly.Xml.createElement('data');
dataElement.appendChild(Blockly.Xml.createTextNode(block.data));
var dataElement = Blockly.Xml.utils.createElement('data');
dataElement.appendChild(Blockly.Xml.utils.createTextNode(block.data));
element.appendChild(dataElement);
}
@@ -219,9 +220,9 @@ Blockly.Xml.blockToDom = function(block, opt_noId) {
} else {
var childBlock = input.connection.targetBlock();
if (input.type == Blockly.INPUT_VALUE) {
container = Blockly.Xml.createElement('value');
container = Blockly.Xml.utils.createElement('value');
} else if (input.type == Blockly.NEXT_STATEMENT) {
container = Blockly.Xml.createElement('statement');
container = Blockly.Xml.utils.createElement('statement');
}
var shadow = input.connection.getShadowDom();
if (shadow && (!childBlock || !childBlock.isShadow())) {
@@ -258,8 +259,8 @@ Blockly.Xml.blockToDom = function(block, opt_noId) {
var nextBlock = block.getNextBlock();
if (nextBlock) {
var container = Blockly.Xml.createElement('next');
container.appendChild(Blockly.Xml.createTextNode(
var container = Blockly.Xml.utils.createElement('next');
container.appendChild(Blockly.Xml.utils.createTextNode(
Blockly.Xml.blockToDom(nextBlock, opt_noId)));
element.appendChild(container);
}
@@ -866,27 +867,6 @@ Blockly.Xml.deleteNext = function(xmlBlock) {
}
};
/**
* Create DOM element for XML.
* @param {tagName} name Name of DOM element.
* @return {!Element} New DOM element.
*/
Blockly.Xml.createElement = function(tagName) {
// TODO: Namespace this element.
// TODO: Support node.js.
return document.createElement(tagName);
};
/**
* Create text element for XML.
* @param {text} text Text content.
* @return {!Node} New DOM node.
*/
Blockly.Xml.createTextNode = function(text) {
// TODO: Support node.js.
return document.createTextNode(text);
};
// Export symbols that would otherwise be renamed by Closure compiler.
if (!goog.global['Blockly']) {
goog.global['Blockly'] = {};

54
core/xml_utils.js Normal file
View File

@@ -0,0 +1,54 @@
/**
* @license
* Visual Blocks Editor
*
* Copyright 2018 Google Inc.
* https://developers.google.com/blockly/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview XML element manipulation.
* @author fraser@google.com (Neil Fraser)
*/
'use strict';
/**
* @name Blockly.Xml.utils
* @namespace
*/
goog.provide('Blockly.Xml.utils');
/**
* Create DOM element for XML.
* @param {tagName} name Name of DOM element.
* @return {!Element} New DOM element.
*/
Blockly.Xml.utils.createElement = function(tagName) {
// TODO: Namespace this element.
// TODO: Support node.js.
return document.createElement(tagName);
};
/**
* Create text element for XML.
* @param {text} text Text content.
* @return {!Node} New DOM node.
*/
Blockly.Xml.utils.createTextNode = function(text) {
// TODO: Support node.js.
return document.createTextNode(text);
};