Beispiel #1
0
        private async Task Initialize(string sourceText, string guid, Shortcut[] additionalShortcuts)
        {
            this.Guid = guid;

            EditorControl = this.FindControl <MarkdownSourceEditorControl>("EditorControl");

            await EditorControl.SetText(sourceText);

            OriginalText      = EditorControl.Text.ToString();
            OriginalDocument  = EditorControl.Document;
            LastSavedText     = OriginalText;
            OriginalTimeStamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();

            StatusBar = this.FindControl <StatusBar>("StatusBar");

            SaveHistoryContainer = this.FindControl <SaveHistoryContainer>("SaveHistoryContainer");
            PreviewPanel         = this.FindControl <MarkdownCanvasControl>("PreviewPanel");

            this.MarkdownRenderer = PreviewPanel.Renderer;

            this.MarkdownRenderer.ImageUriResolver = (a, b) => AsynchronousImageCache.ImageUriResolverAsynchronous(a, b);

            this.MarkdownRenderer.ImageMultiplier = 1.33;

            AsynchronousImageCache.CacheUpdated += this.CacheUpdated;

            this.DetachedFromVisualTree += (s, e) =>
            {
                AsynchronousImageCache.CacheUpdated -= this.CacheUpdated;
            };

            SettingsContainer = new SettingsContainer(additionalShortcuts)
            {
                Margin = new Thickness(10, 0, 0, 10), IsVisible = false
            };
            Grid.SetRow(SettingsContainer, 2);
            this.FindControl <Grid>("ContainerGrid").Children.Add(SettingsContainer);

            EditorControl.ClearUndoStack();

            this.CompilationErrorChecker = PreviewUpdater.Attach(this);

            string autosaveDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), Assembly.GetEntryAssembly().GetName().Name);

            Directory.CreateDirectory(Path.Combine(autosaveDirectory, Guid));
            AutoSaveFile   = Path.Combine(autosaveDirectory, Guid, "autosave_" + System.Guid.NewGuid().ToString("N") + ".cs");
            SaveDirectory  = Path.Combine(autosaveDirectory, Guid);
            this.AutoSaver = AutoSaver.Start(this, AutoSaveFile);

            InputHandler = new InputHandler(this, EditorControl);

            OpenSidePanel();
        }
Beispiel #2
0
        /// <summary>
        /// Create a new <see cref="Editor"/> instance.
        /// </summary>
        /// <param name="initialText">The initial text of the editor.</param>
        /// <param name="guid">A unique identifier for the document being edited. If this is <see langword="null"/>, a new <see cref="System.Guid"/> is generated. If the same identifier is used multiple times, the save history of the document will be available, even if the application has been closed between different sessions.</param>
        /// <param name="additionalShortcuts">Additional application-specific shortcuts (for display purposes only - you need to implement your own logic).</param>
        /// <returns>A fully initialised <see cref="Editor"/> instance.</returns>
        public static async Task <Editor> Create(string initialText = "", string guid = null, Shortcut[] additionalShortcuts = null)
        {
            if (string.IsNullOrEmpty(guid))
            {
                guid = System.Guid.NewGuid().ToString("N");
            }
            else
            {
                foreach (char c in Path.GetInvalidPathChars().Concat(Path.GetInvalidFileNameChars()))
                {
                    if (guid.Contains(c))
                    {
                        throw new ArgumentException("The provided Guid \"" + guid + "\" is not valid!\nThe Guid must be a valid identifier for a path or a file.", nameof(guid));
                    }
                }
            }

            AsynchronousImageCache.SetExitEventHandler();

            Editor tbr = new Editor(false);
            await tbr.Initialize(initialText, guid, additionalShortcuts ?? new Shortcut[0]);

            return(tbr);
        }