From 3d06e7eb33f36b741d172542f7180d297a1c4ec7 Mon Sep 17 00:00:00 2001 From: kozbial Date: Tue, 17 Sep 2019 11:21:16 -0700 Subject: [PATCH 1/3] Creating cursordrawer in renderer. --- core/renderers/common/renderer.js | 17 +++++++++++++++-- core/workspace_svg.js | 22 +++++++++++++--------- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/core/renderers/common/renderer.js b/core/renderers/common/renderer.js index 1582d0824..db3856c62 100644 --- a/core/renderers/common/renderer.js +++ b/core/renderers/common/renderer.js @@ -32,6 +32,7 @@ goog.require('Blockly.blockRendering.Drawer'); goog.require('Blockly.blockRendering.IPathObject'); goog.require('Blockly.blockRendering.PathObject'); goog.require('Blockly.blockRendering.RenderInfo'); +goog.require('Blockly.CursorSvg'); /** @@ -50,7 +51,7 @@ Blockly.blockRendering.Renderer = function() { }; /** - * Initialize the renderer + * Initialize the renderer. * @package */ Blockly.blockRendering.Renderer.prototype.init = function() { @@ -98,6 +99,19 @@ Blockly.blockRendering.Renderer.prototype.makeDebugger_ = function() { return new Blockly.blockRendering.Debug(); }; +/** + * Create a new instance of the renderer's cursor drawer + * @param {!Blockly.WorkspaceSvg} workspace The workspace the cursor belongs to. + * @param {boolean=} opt_marker True if the cursor is a marker. A marker is used + * to save a location and is an immovable cursor. False or undefined if the + * cursor is not a marker. + * @return {!Blockly.CursorSvg} The cursor drawer. + * @package + */ +Blockly.blockRendering.Renderer.prototype.makeCursorDrawer = function(workspace, opt_marker) { + return new Blockly.CursorSvg(workspace, opt_marker); +}; + /** * Create a new instance of a renderer path object. * @param {!SVGElement} root The root SVG element. @@ -133,4 +147,3 @@ Blockly.blockRendering.Renderer.prototype.render = function(block) { info.measure(); this.makeDrawer_(block, info).draw(); }; - diff --git a/core/workspace_svg.js b/core/workspace_svg.js index 9a8a5b944..630039bd1 100644 --- a/core/workspace_svg.js +++ b/core/workspace_svg.js @@ -139,16 +139,16 @@ Blockly.WorkspaceSvg = function(options, this.registerToolboxCategoryCallback(Blockly.PROCEDURE_CATEGORY_NAME, Blockly.Procedures.flyoutCategory); } - - /** - * The block renderer used for rendering blocks on this workspace. - * @type {!Blockly.blockRendering.Renderer} - * @private - */ - this.renderer_ = Blockly.blockRendering.init(this.options.renderer || 'geras'); }; Blockly.utils.object.inherits(Blockly.WorkspaceSvg, Blockly.Workspace); +/** + * The block renderer used for rendering blocks on this workspace. + * @type {!Blockly.blockRendering.Renderer} + * @private + */ +Blockly.WorkspaceSvg.prototype.renderer_ = undefined; + /** * A wrapper function called when a resize event occurs. * You can pass the result to `unbindEvent_`. @@ -419,6 +419,10 @@ Blockly.WorkspaceSvg.prototype.inverseScreenCTMDirty_ = true; * @return {!Blockly.blockRendering.Renderer} The renderer attached to this workspace. */ Blockly.WorkspaceSvg.prototype.getRenderer = function() { + if (!this.renderer_) { + this.renderer_ = + Blockly.blockRendering.init(this.options.renderer || 'geras'); + } return this.renderer_; }; @@ -433,7 +437,7 @@ Blockly.WorkspaceSvg.prototype.setCursor = function(cursor) { } this.cursor_ = cursor; if (this.cursor_) { - this.cursor_.setDrawer(new Blockly.CursorSvg(this, false)); + this.cursor_.setDrawer(this.getRenderer().makeCursorDrawer(this, false)); this.setCursorSvg(this.cursor_.getDrawer().createDom()); } }; @@ -450,7 +454,7 @@ Blockly.WorkspaceSvg.prototype.setMarker = function(marker) { } this.marker_ = marker; if (this.marker_) { - this.marker_.setDrawer(new Blockly.CursorSvg(this, true)); + this.marker_.setDrawer(this.getRenderer().makeCursorDrawer(this, true)); this.setMarkerSvg(this.marker_.getDrawer().createDom()); } }; From c533a0d943cee1fbc64a76dd7ddc9dd1e7752145 Mon Sep 17 00:00:00 2001 From: kozbial Date: Wed, 18 Sep 2019 11:55:01 -0700 Subject: [PATCH 2/3] Setting cursor and marker directly in Workspace and adding check to setCursor/setDrawer. --- core/keyboard_nav/cursor_svg.js | 3 +-- core/workspace.js | 8 ++------ core/workspace_svg.js | 25 ++++++++++++------------- 3 files changed, 15 insertions(+), 21 deletions(-) diff --git a/core/keyboard_nav/cursor_svg.js b/core/keyboard_nav/cursor_svg.js index e0731da4a..1bf28459c 100644 --- a/core/keyboard_nav/cursor_svg.js +++ b/core/keyboard_nav/cursor_svg.js @@ -68,8 +68,7 @@ Blockly.CursorSvg = function(workspace, opt_marker) { * @type {Blockly.blockRendering.ConstantProvider} * @private */ - this.constants_ = new Blockly.blockRendering.ConstantProvider(); - this.constants_.init(); + this.constants_ = workspace.getRenderer().getConstants(); }; /** diff --git a/core/workspace.js b/core/workspace.js index dcc56b787..a858a11d7 100644 --- a/core/workspace.js +++ b/core/workspace.js @@ -122,24 +122,20 @@ Blockly.Workspace = function(opt_options) { * @type {Blockly.Cursor} * @protected */ - this.cursor_ = null; + this.cursor_ = new Blockly.Cursor(); /** * The marker used to mark a location for keyboard navigation. * @type {Blockly.MarkerCursor} * @protected */ - this.marker_ = null; + this.marker_ = new Blockly.MarkerCursor(); // Set the default theme. This is for headless workspaces. This will get // overwritten by the theme passed into the inject call for rendered workspaces. if (!Blockly.getTheme()) { Blockly.setTheme(Blockly.Themes.Classic); } - - this.setCursor(new Blockly.Cursor()); - - this.setMarker(new Blockly.MarkerCursor()); }; /** diff --git a/core/workspace_svg.js b/core/workspace_svg.js index 630039bd1..b8648cb2f 100644 --- a/core/workspace_svg.js +++ b/core/workspace_svg.js @@ -139,16 +139,16 @@ Blockly.WorkspaceSvg = function(options, this.registerToolboxCategoryCallback(Blockly.PROCEDURE_CATEGORY_NAME, Blockly.Procedures.flyoutCategory); } + + /** + * The block renderer used for rendering blocks on this workspace. + * @type {!Blockly.blockRendering.Renderer} + * @private + */ + this.renderer_ = Blockly.blockRendering.init(this.options.renderer || 'geras'); }; Blockly.utils.object.inherits(Blockly.WorkspaceSvg, Blockly.Workspace); -/** - * The block renderer used for rendering blocks on this workspace. - * @type {!Blockly.blockRendering.Renderer} - * @private - */ -Blockly.WorkspaceSvg.prototype.renderer_ = undefined; - /** * A wrapper function called when a resize event occurs. * You can pass the result to `unbindEvent_`. @@ -419,20 +419,17 @@ Blockly.WorkspaceSvg.prototype.inverseScreenCTMDirty_ = true; * @return {!Blockly.blockRendering.Renderer} The renderer attached to this workspace. */ Blockly.WorkspaceSvg.prototype.getRenderer = function() { - if (!this.renderer_) { - this.renderer_ = - Blockly.blockRendering.init(this.options.renderer || 'geras'); - } return this.renderer_; }; /** * Sets the cursor for use with keyboard navigation. + * * @param {Blockly.Cursor} cursor The cursor used to move around this workspace. * @override */ Blockly.WorkspaceSvg.prototype.setCursor = function(cursor) { - if (this.cursor_) { + if (this.cursor_ && this.cursor_.getDrawer()) { this.cursor_.getDrawer().dispose(); } this.cursor_ = cursor; @@ -449,7 +446,7 @@ Blockly.WorkspaceSvg.prototype.setCursor = function(cursor) { * @override */ Blockly.WorkspaceSvg.prototype.setMarker = function(marker) { - if (this.marker_) { + if (this.marker_ && this.marker_.getDrawer()) { this.marker_.getDrawer().dispose(); } this.marker_ = marker; @@ -666,9 +663,11 @@ Blockly.WorkspaceSvg.prototype.createDom = function(opt_backgroundClass) { } this.recordDeleteAreas(); + this.cursor_.setDrawer(this.getRenderer().makeCursorDrawer(this, false)); var svgCursor = this.cursor_.getDrawer().createDom(); this.svgGroup_.appendChild(svgCursor); + this.marker_.setDrawer(this.getRenderer().makeCursorDrawer(this, true)); var svgMarker = this.marker_.getDrawer().createDom(); this.svgGroup_.appendChild(svgMarker); From 60e6b8c6df381ca2c34cfb8afe1cea550f6786e1 Mon Sep 17 00:00:00 2001 From: kozbial Date: Thu, 19 Sep 2019 10:37:40 -0700 Subject: [PATCH 3/3] style and doc updates. --- core/renderers/common/renderer.js | 5 +++-- core/workspace.js | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/core/renderers/common/renderer.js b/core/renderers/common/renderer.js index db3856c62..b232c1f01 100644 --- a/core/renderers/common/renderer.js +++ b/core/renderers/common/renderer.js @@ -100,7 +100,7 @@ Blockly.blockRendering.Renderer.prototype.makeDebugger_ = function() { }; /** - * Create a new instance of the renderer's cursor drawer + * Create a new instance of the renderer's cursor drawer. * @param {!Blockly.WorkspaceSvg} workspace The workspace the cursor belongs to. * @param {boolean=} opt_marker True if the cursor is a marker. A marker is used * to save a location and is an immovable cursor. False or undefined if the @@ -108,7 +108,8 @@ Blockly.blockRendering.Renderer.prototype.makeDebugger_ = function() { * @return {!Blockly.CursorSvg} The cursor drawer. * @package */ -Blockly.blockRendering.Renderer.prototype.makeCursorDrawer = function(workspace, opt_marker) { +Blockly.blockRendering.Renderer.prototype.makeCursorDrawer = function( + workspace, opt_marker) { return new Blockly.CursorSvg(workspace, opt_marker); }; diff --git a/core/workspace.js b/core/workspace.js index a858a11d7..9810de537 100644 --- a/core/workspace.js +++ b/core/workspace.js @@ -119,14 +119,14 @@ Blockly.Workspace = function(opt_options) { /** * The cursor used to navigate around the AST for keyboard navigation. - * @type {Blockly.Cursor} + * @type {!Blockly.Cursor} * @protected */ this.cursor_ = new Blockly.Cursor(); /** * The marker used to mark a location for keyboard navigation. - * @type {Blockly.MarkerCursor} + * @type {!Blockly.MarkerCursor} * @protected */ this.marker_ = new Blockly.MarkerCursor();