Ejemplo n.º 1
0
        protected override async Task OnAfterRenderAsync(bool firstRender)
        {
            await base.OnAfterRenderAsync(firstRender);

            if (firstRender)
            {
                // Create a timer that will determine how quickly the preview updates after changes
                _timer           = new Timer(750);
                _timer.Elapsed  += OnTimerElapsed;
                _timer.AutoReset = false;

                // Register our own language keywords
                List <string> keys    = new();
                var           context = new SimpleCircuit.Parser.ParsingContext();
                foreach (var factory in context.Factory.Factories)
                {
                    foreach (var metadata in factory.Metadata)
                    {
                        keys.AddRange(metadata.Keys);
                    }
                }
                await _js.InvokeVoidAsync("registerLanguage", new object[] { keys.ToArray() });

                var model = await _scriptEditor.GetModel();

                await MonacoEditor.SetModelLanguage(model, "simpleCircuit");

                await MonacoEditorBase.SetTheme("simpleCircuitTheme");

                // Try to find the last saved script
                bool hasScript = false;
                if (_localStore != null)
                {
                    string script = await _localStore.GetItemAsStringAsync("last_script");

                    string style = await _localStore.GetItemAsStringAsync("last_style");

                    if (!string.IsNullOrWhiteSpace(script))
                    {
                        if (!string.IsNullOrWhiteSpace(style))
                        {
                            await SetCurrentScript(script, style);
                        }
                        else
                        {
                            await SetCurrentScript(script, GraphicalCircuit.DefaultStyle);
                        }
                        hasScript = true;
                    }
                }

                // Give the user an initial demo
                if (!hasScript)
                {
                    await SetCurrentScript(Demo.Demos[0].Code, GraphicalCircuit.DefaultStyle);
                }
            }
        }
Ejemplo n.º 2
0
        private async Task <XmlDocument> ComputeXml(bool includeScript)
        {
            _errors   = null;
            _warnings = null;
            var         logger = new Logger();
            XmlDocument doc    = null;

            try
            {
                var code = await _scriptEditor.GetValue();

                var style = await _styleEditor.GetValue();

                var context = new SimpleCircuit.Parser.ParsingContext();
                context.Diagnostics = logger;

                // Store the script and style for next time
                await _localStore.SetItemAsStringAsync("last_script", code);

                await _localStore.SetItemAsStringAsync("last_style", style);

                await _js.InvokeVoidAsync("updateStyle", ModifyCSS(style));

                // Parse the script
                var lexer = SimpleCircuit.Parser.SimpleCircuitLexer.FromString(code);
                SimpleCircuit.Parser.Parser.Parse(lexer, context);
                var ckt = context.Circuit;

                // Include XML data
                ckt.Style = style;
                if (includeScript)
                {
                    ckt.Metadata.Add("script", code);
                }

                // We now need the last things to have executed
                if (ckt.Count > 0 && logger.ErrorCount == 0)
                {
                    doc = ckt.Render(logger, _jsTextFormatter);
                }
            }
            catch (Exception ex)
            {
                logger.Post(new SimpleCircuit.Diagnostics.DiagnosticMessage(SimpleCircuit.Diagnostics.SeverityLevel.Error,
                                                                            "Exception", ex.Message));
            }

            // Add our errors and warnings
            string errors = logger.Error.ToString();

            if (!string.IsNullOrWhiteSpace(errors))
            {
                _errors = (_errors == null) ? errors : _errors + Environment.NewLine + errors;
            }
            string warnings = logger.Warning.ToString();

            if (!string.IsNullOrWhiteSpace(warnings))
            {
                _warnings = (_warnings == null) ? warnings : _warnings + Environment.NewLine + warnings;
            }
            return(doc);
        }