Exemple #1
0
        /// <summary>
        /// Start monitoring the workspace.  If a change is made that changes the XML,
        /// clear the key from the URL.  Stop monitoring the workspace once such a
        /// change is detected.
        /// </summary>
        /// <param name="workspace">Workspace.</param>
        private static void monitorChanges_(WorkspaceSvg workspace)
        {
            var startXmlDom  = Blockly.Xml.workspaceToDom(workspace);
            var startXmlText = Blockly.Xml.domToText(startXmlDom);
            Action <Events.Abstract> bindData = null;
            var change = new Action <Events.Abstract>((e) => {
                var xmlDom  = Blockly.Xml.workspaceToDom(workspace);
                var xmlText = Blockly.Xml.domToText(xmlDom);
                if (startXmlText != xmlText)
                {
                    Window.Location.Hash = "";
                    workspace.removeChangeListener(bindData);
                }
            });

            bindData = workspace.addChangeListener(change);
        }
Exemple #2
0
        /// <summary>
        /// Create a main workspace and add it to the SVG.
        /// </summary>
        /// <param name="svg">SVG element with pattern defined.</param>
        /// <param name="options">Dictionary of options.</param>
        /// <returns>Newly created main workspace.</returns>
        private static WorkspaceSvg createMainWorkspace_(SVGElement svg, Options options)
        {
            options.parentWorkspace = null;
            var mainWorkspace = new WorkspaceSvg(options);

            mainWorkspace.scale = options.zoomOptions.startScale;
            svg.AppendChild(mainWorkspace.createDom("blocklyMainBackground"));
            // A null translation will also apply the correct initial scale.
            mainWorkspace.translate(0, 0);
            mainWorkspace.markFocused(null);

            if (!options.readOnly && !options.hasScrollbars)
            {
                var workspaceChanged = new Action <Events.Abstract>((e) => {
                    if (Core.dragMode_ == Core.DRAG_NONE)
                    {
                        var metrics  = mainWorkspace.getMetrics();
                        var edgeLeft = metrics.viewLeft + metrics.absoluteLeft;
                        var edgeTop  = metrics.viewTop + metrics.absoluteTop;
                        if (metrics.contentTop < edgeTop ||
                            metrics.contentTop + metrics.contentHeight >
                            metrics.viewHeight + edgeTop ||
                            metrics.contentLeft <
                            (options.RTL ? metrics.viewLeft : edgeLeft) ||
                            metrics.contentLeft + metrics.contentWidth > (options.RTL ?
                                                                          metrics.viewWidth : metrics.viewWidth + edgeLeft))
                        {
                            // One or more blocks may be out of bounds.  Bump them back in.
                            var MARGIN = 25;
                            var blocks = mainWorkspace.getTopBlocks(false);
                            foreach (var block in blocks)
                            {
                                var blockXY = block.getRelativeToSurfaceXY();
                                var blockHW = ((BlockSvg)block).getHeightWidth();
                                // Bump any block that's above the top back inside.
                                var overflowTop = edgeTop + MARGIN - blockHW.height - blockXY.y;
                                if (overflowTop > 0)
                                {
                                    block.moveBy(0, overflowTop);
                                }
                                // Bump any block that's below the bottom back inside.
                                var overflowBottom =
                                    edgeTop + metrics.viewHeight - MARGIN - blockXY.y;
                                if (overflowBottom < 0)
                                {
                                    block.moveBy(0, overflowBottom);
                                }
                                // Bump any block that's off the left back inside.
                                var overflowLeft = MARGIN + edgeLeft -
                                                   blockXY.x - (options.RTL ? 0 : blockHW.width);
                                if (overflowLeft > 0)
                                {
                                    block.moveBy(overflowLeft, 0);
                                }
                                // Bump any block that's off the right back inside.
                                var overflowRight = edgeLeft + metrics.viewWidth - MARGIN -
                                                    blockXY.x + (options.RTL ? blockHW.width : 0);
                                if (overflowRight < 0)
                                {
                                    block.moveBy(overflowRight, 0);
                                }
                            }
                        }
                    }
                });
                mainWorkspace.addChangeListener(workspaceChanged);
            }
            // The SVG is now fully assembled.
            Core.svgResize(mainWorkspace);
            WidgetDiv.createDom();
            Tooltip.createDom();
            return(mainWorkspace);
        }
Exemple #3
0
        public Workspace Init(string id, Union <string, Element> toolbox)
        {
            this.toolbox = toolbox.Is <string>() ? toolbox.As <string>() : toolbox.As <Element>().InnerHTML;
            var tab = Document.GetElementById(id);
            // <div dir="LTR" id="blockly-div"></div>
            var div = (HTMLDivElement)goog.dom.createDom("div");

            div.SetAttribute("dir", "LTR");
            div.SetAttribute("class", "blockly-div");
            _IdNo = No++;
            _WorkspaceElementId = "blockly-div" + _IdNo;
            div.SetAttribute("id", _WorkspaceElementId);
            div.SetAttribute("style", "z-index: " + _IdNo);
            div.Style.Left     = "0";
            div.Style.Top      = "0";
            div.Style.Width    = "100%";
            div.Style.Height   = "100%";
            div.Style.Position = Position.Absolute;
            tab.AppendChild(div);

            Core.HSV_SATURATION = 1.0;
            Core.HSV_VALUE      = 0.8;
            _Workspace          = Core.inject(_WorkspaceElementId, new Dictionary <string, object>()
            {
                { "toolbox", toolbox.Value ?? Document.GetElementById("toolbox") },
                { "collapse", true },
                { "comments", true },
                { "disable", true },
                { "maxBlocks", Int32.MaxValue },
                { "trashcan", true },
                { "horizontalLayout", false },
                { "toolboxPosition", "start" },
                { "css", true },
                { "rtl", false },
                { "scrollbars", true },
                { "sounds", false },
                { "oneBasedIndex", false },
                { "zoom", new Dictionary <string, object>()
                  {
                      { "controls", true },
                      { "wheel", true },
                      { "startScale", 0.8 },
                      { "maxcale", 3 },
                      { "minScale", 0.3 }
                  } }
            });

            if (No != 2)
            {
                Hide();
            }
            else
            {
                Show();
            }

            _Workspace.toolbox_.flyout_.flyoutCategory =
                new Func <Union <string, JsArray <Node>, NodeList>, Workspace, JsArray <Node> >(FlyoutCategory);

            _Workspace.addChangeListener(Workspace_Changed);

            return(_Workspace);
        }