示例#1
0
        // Gets the edit text result. May throw.
        static string EditTextResult(IEditor editor, EditTextArgs args)
        {
            try
            {
                if (args.IsLocked)
                {
                    return(null);
                }

                if (File.Exists(editor.FileName))
                {
                    // read and return
                    return(File.ReadAllText(editor.FileName, Encoding.Unicode));
                }
                else
                {
                    // no file, e.g. the text was empty and user exits without saving; case 080502
                    return(string.Empty);
                }
            }
            finally
            {
                try
                {
                    File.Delete(editor.FileName);
                }
                catch (IOException e)
                {
                    Log.TraceException(e);
                }
            }
        }
示例#2
0
        // Creates an editor. Async safe. Unlikely throws.
        static IEditor EditTextCreate(EditTextArgs args)
        {
            //! FarNet API, safe for async
            var editor = Far.Api.CreateEditor();

            //! avoid native API, do not use TempName
            editor.FileName = Kit.TempFileName(args.Extension);

            editor.CodePage       = 1200;
            editor.DisableHistory = true;
            if (!string.IsNullOrEmpty(args.Title))
            {
                editor.Title = args.Title;
            }
            if (args.IsLocked)
            {
                editor.IsLocked = true;
            }
            if (args.EditorOpened != null)
            {
                editor.Opened += args.EditorOpened;
            }

            //? unlikely throws
            if (!string.IsNullOrEmpty(args.Text))
            {
                File.WriteAllText(editor.FileName, args.Text, Encoding.Unicode);
            }

            return(editor);
        }
示例#3
0
        public static string EditText(EditTextArgs args)
        {
            if (args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }

            var editor = EditTextCreate(args);

            editor.Open(OpenMode.Modal);
            return(EditTextResult(editor, args));
        }
示例#4
0
        public static async Task <string> EditTextAsync(EditTextArgs args)
        {
            if (args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }

            var editor = EditTextCreate(args);
            await Tasks.Editor(editor);

            return(EditTextResult(editor, args));
        }
示例#5
0
        public static string EditText(EditTextArgs args)
        {
            if (args == null) throw new ArgumentNullException("args");

            var file = Far.Api.TempName();
            if (args.Extension != null)
                file += "." + args.Extension;

            try
            {
                if (!string.IsNullOrEmpty(args.Text))
                    File.WriteAllText(file, args.Text, Encoding.Unicode);

                var editor = Far.Api.CreateEditor();
                editor.FileName = file;
                editor.CodePage = 1200;
                editor.DisableHistory = true;
                if (!string.IsNullOrEmpty(args.Title))
                    editor.Title = args.Title;
                if (args.IsLocked)
                    editor.IsLocked = true;

                editor.Open(OpenMode.Modal);
                if (args.IsLocked)
                    return null;

                if (File.Exists(file))
                {
                    // read and return
                    return File.ReadAllText(file, Encoding.Unicode);
                }
                else
                {
                    // no file, e.g. the text was empty and user exits without saving; case 080502
                    return string.Empty;
                }
            }
            finally
            {
                try
                {
                    File.Delete(file);
                }
                catch (IOException e)
                {
                    Log.TraceException(e);
                }
            }
        }
示例#6
0
        public static string EditText(EditTextArgs args)
        {
            if (args == null)
            {
                throw new ArgumentNullException("args");
            }

            var file = Far.Api.TempName();

            if (!string.IsNullOrEmpty(args.Extension))
            {
                file += args.Extension[0] == '.' ? args.Extension : "." + args.Extension;
            }

            try
            {
                if (!string.IsNullOrEmpty(args.Text))
                {
                    File.WriteAllText(file, args.Text, Encoding.Unicode);
                }

                var editor = Far.Api.CreateEditor();
                editor.FileName       = file;
                editor.CodePage       = 1200;
                editor.DisableHistory = true;
                if (!string.IsNullOrEmpty(args.Title))
                {
                    editor.Title = args.Title;
                }
                if (args.IsLocked)
                {
                    editor.IsLocked = true;
                }
                if (args.EditorOpened != null)
                {
                    editor.Opened += args.EditorOpened;
                }

                editor.Open(OpenMode.Modal);
                if (args.IsLocked)
                {
                    return(null);
                }

                if (File.Exists(file))
                {
                    // read and return
                    return(File.ReadAllText(file, Encoding.Unicode));
                }
                else
                {
                    // no file, e.g. the text was empty and user exits without saving; case 080502
                    return(string.Empty);
                }
            }
            finally
            {
                try
                {
                    File.Delete(file);
                }
                catch (IOException e)
                {
                    Log.TraceException(e);
                }
            }
        }