Skip to content

A Swiss Army knife for developers.

Added support of custom end of line to text editor.

Etienne Baudouxb705728594e8
diff --git a/src/app/dev/DevToys.Api/Tool/GUI/Components/UITextEndOfLinePreference.cs b/src/app/dev/DevToys.Api/Tool/GUI/Components/UITextEndOfLinePreference.cs
new file mode 100644
index 000000000..44f3c5ed8
--- /dev/null
+++ b/src/app/dev/DevToys.Api/Tool/GUI/Components/UITextEndOfLinePreference.cs
@@ -0,0 +1,22 @@
+namespace DevToys.Api;
+
+/// <summary>
+/// End of line character preference.
+/// </summary>
+public enum UITextEndOfLinePreference
+{
+ /// <summary>
+ /// Use the end of line character identified in the text buffer.
+ /// </summary>
+ TextDefault = 0,
+
+ /// <summary>
+ /// Use line feed (\n) as the end of line character.
+ /// </summary>
+ LF = 1,
+
+ /// <summary>
+ /// Use carriage return and line feed (\r\n) as the end of line character.
+ /// </summary>
+ CRLF = 2
+}
diff --git a/src/app/dev/DevToys.Blazor/BuiltInTools/Settings/Settings.Designer.cs b/src/app/dev/DevToys.Blazor/BuiltInTools/Settings/Settings.Designer.cs
index 48abf6763..0b91f33e0 100644
--- a/src/app/dev/DevToys.Blazor/BuiltInTools/Settings/Settings.Designer.cs
+++ b/src/app/dev/DevToys.Blazor/BuiltInTools/Settings/Settings.Designer.cs
@@ -168,6 +168,42 @@ namespace DevToys.Blazor.BuiltInTools.Settings {
}
}
+ /// <summary>
+ /// Looks up a localized string similar to End Of Line character.
+ /// </summary>
+ internal static string EndOfLinePreference {
+ get {
+ return ResourceManager.GetString("EndOfLinePreference", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to Same end of line as input (automatic).
+ /// </summary>
+ internal static string EndOfLinePreferenceAutomatic {
+ get {
+ return ResourceManager.GetString("EndOfLinePreferenceAutomatic", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to Carriage Return and Line Feed (\r\n) (Windows).
+ /// </summary>
+ internal static string EndOfLinePreferenceCRLF {
+ get {
+ return ResourceManager.GetString("EndOfLinePreferenceCRLF", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to Line Feed (\n) (Unix).
+ /// </summary>
+ internal static string EndOfLinePreferenceLF {
+ get {
+ return ResourceManager.GetString("EndOfLinePreferenceLF", resourceCulture);
+ }
+ }
+
/// <summary>
/// Looks up a localized string similar to Font.
/// </summary>
diff --git a/src/app/dev/DevToys.Blazor/BuiltInTools/Settings/Settings.resx b/src/app/dev/DevToys.Blazor/BuiltInTools/Settings/Settings.resx
index 6d6b28ead..a40fd4736 100644
--- a/src/app/dev/DevToys.Blazor/BuiltInTools/Settings/Settings.resx
+++ b/src/app/dev/DevToys.Blazor/BuiltInTools/Settings/Settings.resx
@@ -153,6 +153,18 @@
<data name="Editing" xml:space="preserve">
<value>Editing</value>
</data>
+ <data name="EndOfLinePreference" xml:space="preserve">
+ <value>End Of Line character</value>
+ </data>
+ <data name="EndOfLinePreferenceAutomatic" xml:space="preserve">
+ <value>Same end of line as input (automatic)</value>
+ </data>
+ <data name="EndOfLinePreferenceCRLF" xml:space="preserve">
+ <value>Carriage Return and Line Feed (\r\n) (Windows)</value>
+ </data>
+ <data name="EndOfLinePreferenceLF" xml:space="preserve">
+ <value>Line Feed (\n) (Unix)</value>
+ </data>
<data name="Font" xml:space="preserve">
<value>Font</value>
</data>
diff --git a/src/app/dev/DevToys.Blazor/BuiltInTools/Settings/SettingsGuiTool.cs b/src/app/dev/DevToys.Blazor/BuiltInTools/Settings/SettingsGuiTool.cs
index 2e4d876a7..e7bf70315 100644
--- a/src/app/dev/DevToys.Blazor/BuiltInTools/Settings/SettingsGuiTool.cs
+++ b/src/app/dev/DevToys.Blazor/BuiltInTools/Settings/SettingsGuiTool.cs
@@ -30,6 +30,7 @@ internal sealed class SettingsGuiTool : IGuiTool
private readonly IUIDropDownListItem[] _availableFonts;
private readonly IUISetting _smartDetectionAutomaticallyPasteSetting = Setting("smart-detection-automatically-paste-setting");
private readonly IUISetting _textEditorFontSetting = Setting("text-editor-font-setting");
+ private readonly IUISetting _textEditorEndOfLineSetting = Setting("text-editor-eol-setting");
private readonly string _previewJsonText;
[ImportingConstructor]
@@ -49,6 +50,8 @@ internal sealed class SettingsGuiTool : IGuiTool
// Load sample JSON.
_previewJsonText = LoadPreviewJsonText();
+
+ OnTextEditorEndOfLinePreferenceChanged(_settingsProvider.GetSetting(PredefinedSettings.TextEditorEndOfLinePreference));
}
public UIToolView View
@@ -175,6 +178,16 @@ internal sealed class SettingsGuiTool : IGuiTool
stateDescriptionWhenOn: Settings.RenderWhitespaceStateDescriptionWhenOn,
stateDescriptionWhenOff: null),
+ _textEditorEndOfLineSetting
+ .Title(Settings.EndOfLinePreference)
+ .Handle(
+ _settingsProvider,
+ PredefinedSettings.TextEditorEndOfLinePreference,
+ OnTextEditorEndOfLinePreferenceChanged,
+ Item(Settings.EndOfLinePreferenceAutomatic, UITextEndOfLinePreference.TextDefault),
+ Item(Settings.EndOfLinePreferenceLF, UITextEndOfLinePreference.LF),
+ Item(Settings.EndOfLinePreferenceCRLF, UITextEndOfLinePreference.CRLF)),
+
Setting("text-editor-clear-text-on-paste-settings")
.Title(Settings.PasteClearsText)
.Description(Settings.PasteClearsTextDescription)
@@ -244,6 +257,25 @@ internal sealed class SettingsGuiTool : IGuiTool
_textEditorFontSetting.StateDescription(fontName);
}
+ private void OnTextEditorEndOfLinePreferenceChanged(UITextEndOfLinePreference endOfLinePreference)
+ {
+ switch (endOfLinePreference)
+ {
+ case UITextEndOfLinePreference.TextDefault:
+ _textEditorEndOfLineSetting.StateDescription(Settings.EndOfLinePreferenceAutomatic);
+ break;
+ case UITextEndOfLinePreference.LF:
+ _textEditorEndOfLineSetting.StateDescription(Settings.EndOfLinePreferenceLF);
+ break;
+ case UITextEndOfLinePreference.CRLF:
+ _textEditorEndOfLineSetting.StateDescription(Settings.EndOfLinePreferenceCRLF);
+ break;
+ default:
+ ThrowHelper.ThrowNotSupportedException();
+ break;
+ }
+ }
+
private IUIDropDownListItem[] LoadAvailableFonts()
{
string[] systemFontFamilies = _fontProvider.GetFontFamilies();
diff --git a/src/app/dev/DevToys.Blazor/Components/Text/MonacoEditor/Monaco/TextModel.cs b/src/app/dev/DevToys.Blazor/Components/Text/MonacoEditor/Monaco/TextModel.cs
index a84f748ef..559d68bcc 100644
--- a/src/app/dev/DevToys.Blazor/Components/Text/MonacoEditor/Monaco/TextModel.cs
+++ b/src/app/dev/DevToys.Blazor/Components/Text/MonacoEditor/Monaco/TextModel.cs
@@ -21,7 +21,7 @@ public class TextModel
/// Get the resolved options for this model.
/// </summary>
public ValueTask<TextModelResolvedOptions> GetOptionsAsync(IJSRuntime runtime)
- => runtime.InvokeAsync<TextModelResolvedOptions>("devtoys.MonacoEditor.model.getOptions", Uri);
+ => runtime.InvokeAsync<TextModelResolvedOptions>("devtoys.MonacoEditor.TextModel.getOptions", Uri);
/// <summary>
/// Get the current version id of the model.
@@ -29,7 +29,7 @@ public class TextModel
/// the version id is incremented.
/// </summary>
public ValueTask<int> GetVersionIdAsync(IJSRuntime runtime)
- => runtime.InvokeAsync<int>("devtoys.MonacoEditor.model.getVersionId", Uri);
+ => runtime.InvokeAsync<int>("devtoys.MonacoEditor.TextModel.getVersionId", Uri);
/// <summary>
/// Get the alternative version id of the model.
@@ -37,13 +37,13 @@ public class TextModel
/// it will return the same values in the case of undo-redo.
/// </summary>
public ValueTask<int> GetAlternativeVersionIdAsync(IJSRuntime runtime)
- => runtime.InvokeAsync<int>("devtoys.MonacoEditor.model.getAlternativeVersionId", Uri);
+ => runtime.InvokeAsync<int>("devtoys.MonacoEditor.TextModel.getAlternativeVersionId", Uri);
/// <summary>
/// Replace the entire text buffer value contained in this model.
/// </summary>
public ValueTask<bool> SetValueAsync(IJSRuntime runtime, string newValue)
- => runtime.InvokeVoidWithErrorHandlingAsync("devtoys.MonacoEditor.model.setValue", Uri, newValue);
+ => runtime.InvokeVoidWithErrorHandlingAsync("devtoys.MonacoEditor.TextModel.setValue", Uri, newValue);
/// <summary>
/// Get the text stored in this model.
@@ -52,13 +52,13 @@ public class TextModel
/// @return The text.
/// </summary>
public ValueTask<string> GetValueAsync(IJSRuntime runtime, EndOfLinePreference? eol, bool? preserveBOM)
- => runtime.InvokeAsync<string>("devtoys.MonacoEditor.model.getValue", Uri, eol, preserveBOM);
+ => runtime.InvokeAsync<string>("devtoys.MonacoEditor.TextModel.getValue", Uri, eol, preserveBOM);
/// <summary>
/// Get the length of the text stored in this model.
/// </summary>
public ValueTask<int> GetValueLengthAsync(IJSRuntime runtime, EndOfLinePreference? eol, bool? preserveBOM)
- => runtime.InvokeAsync<int>("devtoys.MonacoEditor.model.getValueLength", Uri, eol, preserveBOM);
+ => runtime.InvokeAsync<int>("devtoys.MonacoEditor.TextModel.getValueLength", Uri, eol, preserveBOM);
/// <summary>
/// Get the text in a certain range.
@@ -67,7 +67,7 @@ public class TextModel
/// @return The text.
/// </summary>
public ValueTask<string> GetValueInRangeAsync(IJSRuntime runtime, Range range, EndOfLinePreference? eol)
- => runtime.InvokeAsync<string>("devtoys.MonacoEditor.model.getValueInRange", Uri, range, eol);
+ => runtime.InvokeAsync<string>("devtoys.MonacoEditor.TextModel.getValueInRange", Uri, range, eol);
/// <summary>
/// Get the length of text in a certain range.
@@ -75,83 +75,83 @@ public class TextModel
/// @return The text length.
/// </summary>
public ValueTask<int> GetValueLengthInRangeAsync(IJSRuntime runtime, Range range)
- => runtime.InvokeAsync<int>("devtoys.MonacoEditor.model.getValueLengthInRange", Uri, range);
+ => runtime.InvokeAsync<int>("devtoys.MonacoEditor.TextModel.getValueLengthInRange", Uri, range);
/// <summary>
/// Get the character count of text in a certain range.
/// @param range The range describing what text length to get.
/// </summary>
public ValueTask<int> GetCharacterCountInRangeAsync(IJSRuntime runtime, Range range)
- => runtime.InvokeAsync<int>("devtoys.MonacoEditor.model.getCharacterCountInRange", Uri, range);
+ => runtime.InvokeAsync<int>("devtoys.MonacoEditor.TextModel.getCharacterCountInRange", Uri, range);
/// <summary>
/// Get the number of lines in the model.
/// </summary>
public ValueTask<int> GetLineCountAsync(IJSRuntime runtime)
- => runtime.InvokeAsync<int>("devtoys.MonacoEditor.model.getLineCount", Uri);
+ => runtime.InvokeAsync<int>("devtoys.MonacoEditor.TextModel.getLineCount", Uri);
/// <summary>
/// Get the text for a certain line.
/// </summary>
public ValueTask<string> GetLineContentAsync(IJSRuntime runtime, int lineNumber)
- => runtime.InvokeAsync<string>("devtoys.MonacoEditor.model.getLineContent", Uri, lineNumber);
+ => runtime.InvokeAsync<string>("devtoys.MonacoEditor.TextModel.getLineContent", Uri, lineNumber);
/// <summary>
/// Get the text length for a certain line.
/// </summary>
public ValueTask<int> GetLineLengthAsync(IJSRuntime runtime, int lineNumber)
- => runtime.InvokeAsync<int>("devtoys.MonacoEditor.model.getLineLength", Uri, lineNumber);
+ => runtime.InvokeAsync<int>("devtoys.MonacoEditor.TextModel.getLineLength", Uri, lineNumber);
/// <summary>
/// Get the text for all lines.
/// </summary>
public ValueTask<List<string>> GetLinesContentAsync(IJSRuntime runtime)
- => runtime.InvokeAsync<List<string>>("devtoys.MonacoEditor.model.getLinesContent", Uri);
+ => runtime.InvokeAsync<List<string>>("devtoys.MonacoEditor.TextModel.getLinesContent", Uri);
/// <summary>
/// Get the end of line sequence predominantly used in the text buffer.
/// @return EOL char sequence (e.g.: '\n' or '\r\n').
/// </summary>
public ValueTask<string> GetEOLAsync(IJSRuntime runtime)
- => runtime.InvokeAsync<string>("devtoys.MonacoEditor.model.getEOL", Uri);
+ => runtime.InvokeAsync<string>("devtoys.MonacoEditor.TextModel.getEOL", Uri);
/// <summary>
/// Get the end of line sequence predominantly used in the text buffer.
/// </summary>
public ValueTask<EndOfLineSequence> GetEndOfLineSequenceAsync(IJSRuntime runtime)
- => runtime.InvokeAsync<EndOfLineSequence>("devtoys.MonacoEditor.model.getEndOfLineSequence", Uri);
+ => runtime.InvokeAsync<EndOfLineSequence>("devtoys.MonacoEditor.TextModel.getEndOfLineSequence", Uri);
/// <summary>
/// Get the minimum legal column for line at `lineNumber`
/// </summary>
public ValueTask<int> GetLineMinColumnAsync(IJSRuntime runtime, int lineNumber)
- => runtime.InvokeAsync<int>("devtoys.MonacoEditor.model.getLineMinColumn", Uri, lineNumber);
+ => runtime.InvokeAsync<int>("devtoys.MonacoEditor.TextModel.getLineMinColumn", Uri, lineNumber);
/// <summary>
/// Get the maximum legal column for line at `lineNumber`
/// </summary>
public ValueTask<int> GetLineMaxColumnAsync(IJSRuntime runtime, int lineNumber)
- => runtime.InvokeAsync<int>("devtoys.MonacoEditor.model.getLineMaxColumn", Uri, lineNumber);
+ => runtime.InvokeAsync<int>("devtoys.MonacoEditor.TextModel.getLineMaxColumn", Uri, lineNumber);
/// <summary>
/// Returns the column before the first non whitespace character for line at `lineNumber`.
/// Returns 0 if line is empty or contains only whitespace.
/// </summary>
public ValueTask<int> GetLineFirstNonWhitespaceColumnAsync(IJSRuntime runtime, int lineNumber)
- => runtime.InvokeAsync<int>("devtoys.MonacoEditor.model.getLineFirstNonWhitespaceColumn", Uri, lineNumber);
+ => runtime.InvokeAsync<int>("devtoys.MonacoEditor.TextModel.getLineFirstNonWhitespaceColumn", Uri, lineNumber);
/// <summary>
/// Returns the column after the last non whitespace character for line at `lineNumber`.
/// Returns 0 if line is empty or contains only whitespace.
/// </summary>
public ValueTask<int> GetLineLastNonWhitespaceColumnAsync(IJSRuntime runtime, int lineNumber)
- => runtime.InvokeAsync<int>("devtoys.MonacoEditor.model.getLineLastNonWhitespaceColumn", Uri, lineNumber);
+ => runtime.InvokeAsync<int>("devtoys.MonacoEditor.TextModel.getLineLastNonWhitespaceColumn", Uri, lineNumber);
/// <summary>
/// Create a valid position.
/// </summary>
public ValueTask<Position> ValidatePositionAsync(IJSRuntime runtime, Position position)
- => runtime.InvokeAsync<Position>("devtoys.MonacoEditor.model.validatePosition", Uri, position);
+ => runtime.InvokeAsync<Position>("devtoys.MonacoEditor.TextModel.validatePosition", Uri, position);
/// <summary>
/// Advances the given position by the given offset (negative offsets are also accepted)
@@ -164,13 +164,13 @@ public class TextModel
/// line terminator, throws an exception.
/// </summary>
public ValueTask<Position> ModifyPositionAsync(IJSRuntime runtime, Position position, int offset)
- => runtime.InvokeAsync<Position>("devtoys.MonacoEditor.model.modifyPosition", Uri, position, offset);
+ => runtime.InvokeAsync<Position>("devtoys.MonacoEditor.TextModel.modifyPosition", Uri, position, offset);
/// <summary>
/// Create a valid range.
/// </summary>
public ValueTask<Range> ValidateRangeAsync(IJSRuntime runtime, Range range)
- => runtime.InvokeAsync<Range>("devtoys.MonacoEditor.model.validateRange", Uri, range);
+ => runtime.InvokeAsync<Range>("devtoys.MonacoEditor.TextModel.validateRange", Uri, range);
/// <summary>
/// Converts the position to a zero-based offset.
@@ -181,7 +181,7 @@ public class TextModel
/// @return A valid zero-based offset.
/// </summary>
public ValueTask<int> GetOffsetAtAsync(IJSRuntime runtime, Position position)
- => runtime.InvokeAsync<int>("devtoys.MonacoEditor.model.getOffsetAt", Uri, position);
+ => runtime.InvokeAsync<int>("devtoys.MonacoEditor.TextModel.getOffsetAt", Uri, position);
/// <summary>
/// Converts a zero-based offset to a position.
@@ -190,19 +190,19 @@ public class TextModel
/// @return A valid [position](#Position).
/// </summary>
public ValueTask<Position> GetPositionAtAsync(IJSRuntime runtime, int offset)
- => runtime.InvokeAsync<Position>("devtoys.MonacoEditor.model.getPositionAt", Uri, offset);
+ => runtime.InvokeAsync<Position>("devtoys.MonacoEditor.TextModel.getPositionAt", Uri, offset);
/// <summary>
/// Get a range covering the entire model.
/// </summary>
public ValueTask<Range> GetFullModelRangeAsync(IJSRuntime runtime)
- => runtime.InvokeAsync<Range>("devtoys.MonacoEditor.model.getFullModelRange", Uri);
+ => runtime.InvokeAsync<Range>("devtoys.MonacoEditor.TextModel.getFullModelRange", Uri);
/// <summary>
/// Returns if the model was disposed or not.
/// </summary>
public ValueTask<bool> IsDisposedAsync(IJSRuntime runtime)
- => runtime.InvokeAsync<bool>("devtoys.MonacoEditor.model.isDisposed", Uri);
+ => runtime.InvokeAsync<bool>("devtoys.MonacoEditor.TextModel.isDisposed", Uri);
/// <summary>
/// Search the model.
@@ -216,7 +216,7 @@ public class TextModel
/// @return The ranges where the matches are. It is empty if not matches have been found.
/// </summary>
public ValueTask<List<FindMatch>> FindMatchesAsync(IJSRuntime runtime, string searchString, bool searchOnlyEditableRange, bool isRegex, bool matchCase, string wordSeparators, bool captureMatches, int? limitResultCount)
- => runtime.InvokeAsync<List<FindMatch>>("devtoys.MonacoEditor.model.findMatches", Uri, searchString, searchOnlyEditableRange, isRegex, matchCase, wordSeparators, captureMatches, limitResultCount);
+ => runtime.InvokeAsync<List<FindMatch>>("devtoys.MonacoEditor.TextModel.findMatches", Uri, searchString, searchOnlyEditableRange, isRegex, matchCase, wordSeparators, captureMatches, limitResultCount);
/// <summary>
/// Search the model.
@@ -230,7 +230,7 @@ public class TextModel
/// @return The ranges where the matches are. It is empty if no matches have been found.
/// </summary>
public ValueTask<List<FindMatch>> FindMatchesAsync(IJSRuntime runtime, string searchString, Range searchScope, bool isRegex, bool matchCase, string wordSeparators, bool captureMatches, int? limitResultCount)
- => runtime.InvokeAsync<List<FindMatch>>("devtoys.MonacoEditor.model.findMatches", Uri, searchString, searchScope, isRegex, matchCase, wordSeparators, captureMatches, limitResultCount);
+ => runtime.InvokeAsync<List<FindMatch>>("devtoys.MonacoEditor.TextModel.findMatches", Uri, searchString, searchScope, isRegex, matchCase, wordSeparators, captureMatches, limitResultCount);
/// <summary>
/// Search the model for the next match. Loops to the beginning of the model if needed.
@@ -243,7 +243,7 @@ public class TextModel
/// @return The range where the next match is. It is null if no next match has been found.
/// </summary>
public ValueTask<FindMatch> FindNextMatchAsync(IJSRuntime runtime, string searchString, Position searchStart, bool isRegex, bool matchCase, string wordSeparators, bool captureMatches)
- => runtime.InvokeAsync<FindMatch>("devtoys.MonacoEditor.model.findNextMatch", Uri, searchString, searchStart, isRegex, matchCase, wordSeparators, captureMatches);
+ => runtime.InvokeAsync<FindMatch>("devtoys.MonacoEditor.TextModel.findNextMatch", Uri, searchString, searchStart, isRegex, matchCase, wordSeparators, captureMatches);
/// <summary>
/// Search the model for the previous match. Loops to the end of the model if needed.
@@ -256,13 +256,13 @@ public class TextModel
/// @return The range where the previous match is. It is null if no previous match has been found.
/// </summary>
public ValueTask<FindMatch> FindPreviousMatchAsync(IJSRuntime runtime, string searchString, Position searchStart, bool isRegex, bool matchCase, string wordSeparators, bool captureMatches)
- => runtime.InvokeAsync<FindMatch>("devtoys.MonacoEditor.model.findPreviousMatch", Uri, searchString, searchStart, isRegex, matchCase, wordSeparators, captureMatches);
+ => runtime.InvokeAsync<FindMatch>("devtoys.MonacoEditor.TextModel.findPreviousMatch", Uri, searchString, searchStart, isRegex, matchCase, wordSeparators, captureMatches);
/// <summary>
/// Get the language associated with this model.
/// </summary>
public ValueTask<string> GetLanguageIdAsync(IJSRuntime runtime)
- => runtime.InvokeAsync<string>("devtoys.MonacoEditor.model.getLanguageId", Uri);
+ => runtime.InvokeAsync<string>("devtoys.MonacoEditor.TextModel.getLanguageId", Uri);
/// <summary>
/// Get the word under or besides `position`.
@@ -270,7 +270,7 @@ public class TextModel
/// @return The word under or besides `position`. Might be null.
/// </summary>
public ValueTask<WordAtPosition> GetWordAtPositionAsync(IJSRuntime runtime, Position position)
- => runtime.InvokeAsync<WordAtPosition>("devtoys.MonacoEditor.model.getWordAtPosition", Uri, position);
+ => runtime.InvokeAsync<WordAtPosition>("devtoys.MonacoEditor.TextModel.getWordAtPosition", Uri, position);
/// <summary>
/// Get the word under or besides `position` trimmed to `position`.column
@@ -278,7 +278,7 @@ public class TextModel
/// @return The word under or besides `position`. Will never be null.
/// </summary>
public ValueTask<WordAtPosition> GetWordUntilPositionAsync(IJSRuntime runtime, Position position)
- => runtime.InvokeAsync<WordAtPosition>("devtoys.MonacoEditor.model.getWordUntilPosition", Uri, position);
+ => runtime.InvokeAsync<WordAtPosition>("devtoys.MonacoEditor.TextModel.getWordUntilPosition", Uri, position);
/// <summary>
/// Perform a minimum amount of operations, in order to transform the decorations
@@ -291,7 +291,7 @@ public class TextModel
/// @return An array containing the new decorations identifiers.
/// </summary>
public ValueTask<List<string>> DeltaDecorationsAsync(IJSRuntime runtime, List<string> oldDecorations, List<ModelDeltaDecoration> newDecorations, int? ownerId)
- => runtime.InvokeAsync<List<string>>("devtoys.MonacoEditor.model.deltaDecorations", Uri, oldDecorations, newDecorations, ownerId);
+ => runtime.InvokeAsync<List<string>>("devtoys.MonacoEditor.TextModel.deltaDecorations", Uri, oldDecorations, newDecorations, ownerId);
/// <summary>
/// Get the options associated with a decoration.
@@ -299,7 +299,7 @@ public class TextModel
/// @return The decoration options or null if the decoration was not found.
/// </summary>
public ValueTask<ModelDecorationOptions> GetDecorationOptionsAsync(IJSRuntime runtime, string id)
- => runtime.InvokeAsync<ModelDecorationOptions>("devtoys.MonacoEditor.model.getDecorationOptions", Uri, id);
+ => runtime.InvokeAsync<ModelDecorationOptions>("devtoys.MonacoEditor.TextModel.getDecorationOptions", Uri, id);
/// <summary>
/// Get the range associated with a decoration.
@@ -307,7 +307,7 @@ public class TextModel
/// @return The decoration range or null if the decoration was not found.
/// </summary>
public ValueTask<Range> GetDecorationRangeAsync(IJSRuntime runtime, string id)
- => runtime.InvokeAsync<Range>("devtoys.MonacoEditor.model.getDecorationRange", Uri, id);
+ => runtime.InvokeAsync<Range>("devtoys.MonacoEditor.TextModel.getDecorationRange", Uri, id);
/// <summary>
/// Gets all the decorations for the line `lineNumber` as an array.
@@ -317,7 +317,7 @@ public class TextModel
/// @return An array with the decorations
/// </summary>
public ValueTask<ModelDecoration> GetLineDecorationsAsync(IJSRuntime runtime, int lineNumber, int? ownerId, bool? filterOutValidation)
- => runtime.InvokeAsync<ModelDecoration>("devtoys.MonacoEditor.model.getLineDecorations", Uri, lineNumber, ownerId, filterOutValidation);
+ => runtime.InvokeAsync<ModelDecoration>("devtoys.MonacoEditor.TextModel.getLineDecorations", Uri, lineNumber, ownerId, filterOutValidation);
/// <summary>
/// Gets all the decorations for the lines between `startLineNumber` and `endLineNumber` as an array.
@@ -328,7 +328,7 @@ public class TextModel
/// @return An array with the decorations
/// </summary>
public ValueTask<ModelDecoration> GetLinesDecorationsAsync(IJSRuntime runtime, int startLineNumber, int endLineNumber, int? ownerId, bool? filterOutValidation)
- => runtime.InvokeAsync<ModelDecoration>("devtoys.MonacoEditor.model.getLinesDecorations", Uri, startLineNumber, endLineNumber, ownerId, filterOutValidation);
+ => runtime.InvokeAsync<ModelDecoration>("devtoys.MonacoEditor.TextModel.getLinesDecorations", Uri, startLineNumber, endLineNumber, ownerId, filterOutValidation);
/// <summary>
/// Gets all the decorations in a range as an array. Only `startLineNumber` and `endLineNumber` from `range` are used for filtering.
@@ -339,7 +339,7 @@ public class TextModel
/// @return An array with the decorations
/// </summary>
public ValueTask<ModelDecoration[]> GetDecorationsInRangeAsync(IJSRuntime runtime, Range range, int? ownerId, bool? filterOutValidation)
- => runtime.InvokeAsync<ModelDecoration[]>("devtoys.MonacoEditor.model.getDecorationsInRange", Uri, range, ownerId, filterOutValidation);
+ => runtime.InvokeAsync<ModelDecoration[]>("devtoys.MonacoEditor.TextModel.getDecorationsInRange", Uri, range, ownerId, filterOutValidation);
/// <summary>
/// Gets all the decorations as an array.
@@ -347,7 +347,7 @@ public class TextModel
/// @param filterOutValidation If set, it will ignore decorations specific to validation (i.e. warnings, errors).
/// </summary>
public ValueTask<List<ModelDecoration>> GetAllDecorationsAsync(IJSRuntime runtime, int? ownerId, bool? filterOutValidation)
- => runtime.InvokeAsync<List<ModelDecoration>>("devtoys.MonacoEditor.model.getAllDecorations", Uri, ownerId, filterOutValidation);
+ => runtime.InvokeAsync<List<ModelDecoration>>("devtoys.MonacoEditor.TextModel.getAllDecorations", Uri, ownerId, filterOutValidation);
/// <summary>
/// Gets all the decorations that should be rendered in the overview ruler as an array.
@@ -355,53 +355,53 @@ public class TextModel
/// @param filterOutValidation If set, it will ignore decorations specific to validation (i.e. warnings, errors).
/// </summary>
public ValueTask<ModelDecoration> GetOverviewRulerDecorationsAsync(IJSRuntime runtime, int? ownerId, bool? filterOutValidation)
- => runtime.InvokeAsync<ModelDecoration>("devtoys.MonacoEditor.model.getOverviewRulerDecorations", Uri, ownerId, filterOutValidation);
+ => runtime.InvokeAsync<ModelDecoration>("devtoys.MonacoEditor.TextModel.getOverviewRulerDecorations", Uri, ownerId, filterOutValidation);
/// <summary>
/// Gets all the decorations that contain injected text.
/// @param ownerId If set, it will ignore decorations belonging to other owners.
/// </summary>
public ValueTask<ModelDecoration[]> GetInjectedTextDecorationsAsync(IJSRuntime runtime, int? ownerId)
- => runtime.InvokeAsync<ModelDecoration[]>("devtoys.MonacoEditor.model.getInjectedTextDecorations", Uri, ownerId);
+ => runtime.InvokeAsync<ModelDecoration[]>("devtoys.MonacoEditor.TextModel.getInjectedTextDecorations", Uri, ownerId);
/// <summary>
/// Normalize a string containing whitespace according to indentation rules (converts to spaces or to tabs).
/// </summary>
public ValueTask<string> NormalizeIndentationAsync(IJSRuntime runtime, string str)
- => runtime.InvokeAsync<string>("devtoys.MonacoEditor.model.normalizeIndentation", Uri, str);
+ => runtime.InvokeAsync<string>("devtoys.MonacoEditor.TextModel.normalizeIndentation", Uri, str);
/// <summary>
/// Change the options of this model.
/// </summary>
public ValueTask<bool> UpdateOptionsAsync(IJSRuntime runtime, TextModelUpdateOptions newOptions)
- => runtime.InvokeVoidWithErrorHandlingAsync("devtoys.MonacoEditor.model.updateOptions", Uri, newOptions);
+ => runtime.InvokeVoidWithErrorHandlingAsync("devtoys.MonacoEditor.TextModel.updateOptions", Uri, newOptions);
/// <summary>
/// Detect the indentation options for this model from its content.
/// </summary>
public ValueTask<bool> DetectIndentationAsync(IJSRuntime runtime, bool defaultInsertSpaces, int defaultTabSize)
- => runtime.InvokeVoidWithErrorHandlingAsync("devtoys.MonacoEditor.model.detectIndentation", Uri, defaultInsertSpaces, defaultTabSize);
+ => runtime.InvokeVoidWithErrorHandlingAsync("devtoys.MonacoEditor.TextModel.detectIndentation", Uri, defaultInsertSpaces, defaultTabSize);
/// <summary>
/// Close the current undo-redo element.
/// This offers a way to create an undo/redo stop point.
/// </summary>
public ValueTask<bool> PushStackElementAsync(IJSRuntime runtime)
- => runtime.InvokeVoidWithErrorHandlingAsync("devtoys.MonacoEditor.model.pushStackElement", Uri);
+ => runtime.InvokeVoidWithErrorHandlingAsync("devtoys.MonacoEditor.TextModel.pushStackElement", Uri);
/// <summary>
/// Open the current undo-redo element.
/// This offers a way to remove the current undo/redo stop point.
/// </summary>
public ValueTask<bool> PopStackElementAsync(IJSRuntime runtime)
- => runtime.InvokeVoidWithErrorHandlingAsync("devtoys.MonacoEditor.model.popStackElement", Uri);
+ => runtime.InvokeVoidWithErrorHandlingAsync("devtoys.MonacoEditor.TextModel.popStackElement", Uri);
/// <summary>
/// Change the end of line sequence. This is the preferred way of
/// changing the eol sequence. This will land on the undo stack.
/// </summary>
public ValueTask<bool> PushEOLAsync(IJSRuntime runtime, EndOfLineSequence eol)
- => runtime.InvokeVoidWithErrorHandlingAsync("devtoys.MonacoEditor.model.pushEOL", Uri, eol);
+ => runtime.InvokeVoidWithErrorHandlingAsync("devtoys.MonacoEditor.TextModel.pushEOL", Uri, eol);
/// <summary>
/// Edit the model without adding the edits to the undo stack.
@@ -410,18 +410,18 @@ public class TextModel
/// @return If desired, the inverse edit operations, that, when applied, will bring the model back to the previous state.
/// </summary>
public ValueTask<List<ValidEditOperation>> ApplyEditsAsync(IJSRuntime runtime, List<IdentifiedSingleEditOperation> operations, bool computeUndoEdits = false)
- => runtime.InvokeAsync<List<ValidEditOperation>>("devtoys.MonacoEditor.model.applyEdits", Uri, operations, computeUndoEdits);
+ => runtime.InvokeAsync<List<ValidEditOperation>>("devtoys.MonacoEditor.TextModel.applyEdits", Uri, operations, computeUndoEdits);
/// <summary>
/// Change the end of line sequence without recording in the undo stack.
/// This can have dire consequences on the undo stack! See @pushEOL for the preferred way.
/// </summary>
public ValueTask<bool> SetEOLAsync(IJSRuntime runtime, EndOfLineSequence eol)
- => runtime.InvokeVoidWithErrorHandlingAsync("devtoys.MonacoEditor.model.setEOL", Uri, eol);
+ => runtime.InvokeVoidWithErrorHandlingAsync("devtoys.MonacoEditor.TextModel.setEOL", Uri, eol);
/// <summary>
/// Destroy this model.
/// </summary>
public ValueTask<bool> DisposeModelAsync(IJSRuntime runtime)
- => runtime.InvokeVoidWithErrorHandlingAsync("devtoys.MonacoEditor.model.dispose", Uri);
+ => runtime.InvokeVoidWithErrorHandlingAsync("devtoys.MonacoEditor.TextModel.dispose", Uri);
}
diff --git a/src/app/dev/DevToys.Blazor/Components/Text/MonacoEditor/MonacoEditor.razor.cs b/src/app/dev/DevToys.Blazor/Components/Text/MonacoEditor/MonacoEditor.razor.cs
index eef9fcdb1..fb1abd42a 100644
--- a/src/app/dev/DevToys.Blazor/Components/Text/MonacoEditor/MonacoEditor.razor.cs
+++ b/src/app/dev/DevToys.Blazor/Components/Text/MonacoEditor/MonacoEditor.razor.cs
@@ -13,17 +13,14 @@ public partial class MonacoEditor : RicherMonacoEditorBase
#pragma warning disable IDE0044 // Add readonly modifier
[Import]
private IThemeListener _themeListener = default!;
-
- [Import]
- private ISettingsProvider _settingsProvider = default!;
#pragma warning restore IDE0044 // Add readonly modifier
public MonacoEditor()
{
}
- public MonacoEditor(IJSRuntime? jsRuntime = null, string? id = null, string? @class = null)
- : base(jsRuntime)
+ public MonacoEditor(IJSRuntime? jsRuntime = null, string? id = null, string? @class = null, ISettingsProvider? settingsProvider = null)
+ : base(jsRuntime, settingsProvider)
{
if (!string.IsNullOrEmpty(id))
{
@@ -49,7 +46,7 @@ public partial class MonacoEditor : RicherMonacoEditorBase
base.OnInitialized();
_themeListener.ThemeChanged += ThemeListener_ThemeChanged;
- _settingsProvider.SettingChanged += SettingsProvider_SettingChanged;
+ SettingsProvider.SettingChanged += SettingsProvider_SettingChanged;
_oldIsActuallyEnabled = IsActuallyEnabled;
}
@@ -57,9 +54,9 @@ public partial class MonacoEditor : RicherMonacoEditorBase
{
base.OnParametersSet();
- if (_settingsProvider is not null)
+ if (SettingsProvider is not null)
{
- SettingsProvider_SettingChanged(_settingsProvider, null!);
+ SettingsProvider_SettingChanged(SettingsProvider, null!);
}
}
@@ -92,10 +89,10 @@ public partial class MonacoEditor : RicherMonacoEditorBase
options.RenderLineHighlightOnlyWhenFocus = true;
// Apply global user settings
- options.FontFamily = _settingsProvider.GetSetting(PredefinedSettings.TextEditorFont);
- options.LineNumbers = _settingsProvider.GetSetting(PredefinedSettings.TextEditorLineNumbers) ? "on" : "off";
- options.RenderLineHighlight = _settingsProvider.GetSetting(PredefinedSettings.TextEditorHighlightCurrentLine) ? "all" : "none";
- options.RenderWhitespace = _settingsProvider.GetSetting(PredefinedSettings.TextEditorRenderWhitespace) ? "all" : "none";
+ options.FontFamily = SettingsProvider.GetSetting(PredefinedSettings.TextEditorFont);
+ options.LineNumbers = SettingsProvider.GetSetting(PredefinedSettings.TextEditorLineNumbers) ? "on" : "off";
+ options.RenderLineHighlight = SettingsProvider.GetSetting(PredefinedSettings.TextEditorHighlightCurrentLine) ? "all" : "none";
+ options.RenderWhitespace = SettingsProvider.GetSetting(PredefinedSettings.TextEditorRenderWhitespace) ? "all" : "none";
ApplyWordWrapOption(options);
// Create the editor
@@ -131,9 +128,9 @@ public partial class MonacoEditor : RicherMonacoEditorBase
_themeListener.ThemeChanged -= ThemeListener_ThemeChanged;
}
- if (_settingsProvider is not null)
+ if (SettingsProvider is not null)
{
- _settingsProvider.SettingChanged -= SettingsProvider_SettingChanged;
+ SettingsProvider.SettingChanged -= SettingsProvider_SettingChanged;
}
return base.DisposeAsync();
@@ -147,9 +144,9 @@ public partial class MonacoEditor : RicherMonacoEditorBase
_isLoaded = true;
ShowLoading = false;
- if (_settingsProvider is not null)
+ if (SettingsProvider is not null)
{
- SettingsProvider_SettingChanged(_settingsProvider, null!);
+ SettingsProvider_SettingChanged(SettingsProvider, null!);
}
StateHasChanged();
@@ -162,15 +159,15 @@ public partial class MonacoEditor : RicherMonacoEditorBase
{
var options = new EditorUpdateOptions()
{
- FontFamily = _settingsProvider.GetSetting(PredefinedSettings.TextEditorFont),
- LineNumbers = _settingsProvider.GetSetting(PredefinedSettings.TextEditorLineNumbers) ? "on" : "off",
- RenderLineHighlight = _settingsProvider.GetSetting(PredefinedSettings.TextEditorHighlightCurrentLine) ? "all" : "none",
- RenderWhitespace = _settingsProvider.GetSetting(PredefinedSettings.TextEditorRenderWhitespace) ? "all" : "none"
+ FontFamily = SettingsProvider.GetSetting(PredefinedSettings.TextEditorFont),
+ LineNumbers = SettingsProvider.GetSetting(PredefinedSettings.TextEditorLineNumbers) ? "on" : "off",
+ RenderLineHighlight = SettingsProvider.GetSetting(PredefinedSettings.TextEditorHighlightCurrentLine) ? "all" : "none",
+ RenderWhitespace = SettingsProvider.GetSetting(PredefinedSettings.TextEditorRenderWhitespace) ? "all" : "none",
};
ApplyWordWrapOption(options);
- UpdateOptionsAsync(options);
+ UpdateOptionsAsync(options).Forget();
}
}
@@ -181,7 +178,7 @@ public partial class MonacoEditor : RicherMonacoEditorBase
Theme = GetTheme()
};
- UpdateOptionsAsync(options);
+ UpdateOptionsAsync(options).Forget();
}
private string GetTheme()
@@ -216,7 +213,7 @@ public partial class MonacoEditor : RicherMonacoEditorBase
switch (WrapMode)
{
case UITextWrapMode.Auto:
- editorOptions.WordWrap = _settingsProvider.GetSetting(PredefinedSettings.TextEditorTextWrapping) ? "on" : "off";
+ editorOptions.WordWrap = SettingsProvider.GetSetting(PredefinedSettings.TextEditorTextWrapping) ? "on" : "off";
break;
case UITextWrapMode.Wrap:
editorOptions.WordWrap = "on";
diff --git a/src/app/dev/DevToys.Blazor/Components/Text/MonacoEditor/MonacoEditorBase.cs b/src/app/dev/DevToys.Blazor/Components/Text/MonacoEditor/MonacoEditorBase.cs
index 2422b59ca..994e8807c 100644
--- a/src/app/dev/DevToys.Blazor/Components/Text/MonacoEditor/MonacoEditorBase.cs
+++ b/src/app/dev/DevToys.Blazor/Components/Text/MonacoEditor/MonacoEditorBase.cs
@@ -14,6 +14,9 @@ public abstract class MonacoEditorBase : MefComponentBase, IFocusable
[Parameter]
public EventCallback OnDidDispose { get; set; }
+ [Parameter]
+ public EventCallback OnTextModelInitializationRequested { get; set; }
+
[Parameter]
public EventCallback OnDidInit { get; set; }
@@ -79,6 +82,7 @@ public abstract class MonacoEditorBase : MefComponentBase, IFocusable
if (firstRender)
{
await SetEventListenersAsync();
+ await OnTextModelInitializationRequested.InvokeAsync(this);
OnEditorLoaded();
await OnDidInit.InvokeAsync(this);
}
diff --git a/src/app/dev/DevToys.Blazor/Components/Text/MonacoEditor/MonacoEditorDiff.razor.cs b/src/app/dev/DevToys.Blazor/Components/Text/MonacoEditor/MonacoEditorDiff.razor.cs
index 0b567083b..4f8849d41 100644
--- a/src/app/dev/DevToys.Blazor/Components/Text/MonacoEditor/MonacoEditorDiff.razor.cs
+++ b/src/app/dev/DevToys.Blazor/Components/Text/MonacoEditor/MonacoEditorDiff.razor.cs
@@ -13,9 +13,6 @@ public partial class MonacoEditorDiff : RicherMonacoEditorDiffBase
#pragma warning disable IDE0044 // Add readonly modifier
[Import]
private IThemeListener _themeListener = default!;
-
- [Import]
- private ISettingsProvider _settingsProvider = default!;
#pragma warning restore IDE0044 // Add readonly modifier
[Parameter]
@@ -31,7 +28,7 @@ public partial class MonacoEditorDiff : RicherMonacoEditorDiffBase
base.OnInitialized();
_themeListener.ThemeChanged += ThemeListener_ThemeChanged;
- _settingsProvider.SettingChanged += SettingsProvider_SettingChanged;
+ SettingsProvider.SettingChanged += SettingsProvider_SettingChanged;
_oldIsActuallyEnabled = IsActuallyEnabled;
}
@@ -68,15 +65,16 @@ public partial class MonacoEditorDiff : RicherMonacoEditorDiffBase
options.RenderOverviewRuler = true;
// Apply global user settings
- options.FontFamily = _settingsProvider.GetSetting(PredefinedSettings.TextEditorFont);
- options.WordWrap = _settingsProvider.GetSetting(PredefinedSettings.TextEditorTextWrapping) ? "on" : "off";
- options.LineNumbers = _settingsProvider.GetSetting(PredefinedSettings.TextEditorLineNumbers) ? "on" : "off";
- options.RenderLineHighlight = _settingsProvider.GetSetting(PredefinedSettings.TextEditorHighlightCurrentLine) ? "all" : "none";
- options.RenderWhitespace = _settingsProvider.GetSetting(PredefinedSettings.TextEditorRenderWhitespace) ? "all" : "none";
+ options.FontFamily = SettingsProvider.GetSetting(PredefinedSettings.TextEditorFont);
+ options.WordWrap = SettingsProvider.GetSetting(PredefinedSettings.TextEditorTextWrapping) ? "on" : "off";
+ options.LineNumbers = SettingsProvider.GetSetting(PredefinedSettings.TextEditorLineNumbers) ? "on" : "off";
+ options.RenderLineHighlight = SettingsProvider.GetSetting(PredefinedSettings.TextEditorHighlightCurrentLine) ? "all" : "none";
+ options.RenderWhitespace = SettingsProvider.GetSetting(PredefinedSettings.TextEditorRenderWhitespace) ? "all" : "none";
// Create the bridges for the inner editors
- _originalEditor = MonacoEditorHelper.CreateVirtualEditor(JSRuntime, Id + "_original");
- _modifiedEditor = MonacoEditorHelper.CreateVirtualEditor(JSRuntime, Id + "_modified");
+ Guard.IsNotNull(SettingsProvider);
+ _originalEditor = MonacoEditorHelper.CreateVirtualEditor(JSRuntime, Id + "_original", settingsProvider: SettingsProvider);
+ _modifiedEditor = MonacoEditorHelper.CreateVirtualEditor(JSRuntime, Id + "_modified", settingsProvider: SettingsProvider);
Guard.IsNotNull(OriginalEditor);
Guard.IsNotNull(ModifiedEditor);
@@ -122,9 +120,9 @@ public partial class MonacoEditorDiff : RicherMonacoEditorDiffBase
_themeListener.ThemeChanged -= ThemeListener_ThemeChanged;
}
- if (_settingsProvider is not null)
+ if (SettingsProvider is not null)
{
- _settingsProvider.SettingChanged -= SettingsProvider_SettingChanged;
+ SettingsProvider.SettingChanged -= SettingsProvider_SettingChanged;
}
return base.DisposeAsync();
@@ -145,14 +143,14 @@ public partial class MonacoEditorDiff : RicherMonacoEditorDiffBase
{
var options = new DiffEditorOptions()
{
- FontFamily = _settingsProvider.GetSetting(PredefinedSettings.TextEditorFont),
- WordWrap = _settingsProvider.GetSetting(PredefinedSettings.TextEditorTextWrapping) ? "on" : "off",
- LineNumbers = _settingsProvider.GetSetting(PredefinedSettings.TextEditorLineNumbers) ? "on" : "off",
- RenderLineHighlight = _settingsProvider.GetSetting(PredefinedSettings.TextEditorHighlightCurrentLine) ? "all" : "none",
- RenderWhitespace = _settingsProvider.GetSetting(PredefinedSettings.TextEditorRenderWhitespace) ? "all" : "none"
+ FontFamily = SettingsProvider.GetSetting(PredefinedSettings.TextEditorFont),
+ WordWrap = SettingsProvider.GetSetting(PredefinedSettings.TextEditorTextWrapping) ? "on" : "off",
+ LineNumbers = SettingsProvider.GetSetting(PredefinedSettings.TextEditorLineNumbers) ? "on" : "off",
+ RenderLineHighlight = SettingsProvider.GetSetting(PredefinedSettings.TextEditorHighlightCurrentLine) ? "all" : "none",
+ RenderWhitespace = SettingsProvider.GetSetting(PredefinedSettings.TextEditorRenderWhitespace) ? "all" : "none"
};
- UpdateOptionsAsync(options);
+ UpdateOptionsAsync(options).Forget();
}
private void ThemeListener_ThemeChanged(object? sender, EventArgs e)
diff --git a/src/app/dev/DevToys.Blazor/Components/Text/MonacoEditor/MonacoEditorHelper.cs b/src/app/dev/DevToys.Blazor/Components/Text/MonacoEditor/MonacoEditorHelper.cs
index 3e8890870..d4fd32da6 100644
--- a/src/app/dev/DevToys.Blazor/Components/Text/MonacoEditor/MonacoEditorHelper.cs
+++ b/src/app/dev/DevToys.Blazor/Components/Text/MonacoEditor/MonacoEditorHelper.cs
@@ -145,8 +145,8 @@ internal static class MonacoEditorHelper
internal static ValueTask<bool> RemeasureFontsAsync(IJSRuntime runtime)
=> runtime.InvokeVoidWithErrorHandlingAsync("devtoys.MonacoEditor.remeasureFonts");
- internal static MonacoEditor CreateVirtualEditor(IJSRuntime jsRuntime, string id, string? cssClass = null)
+ internal static MonacoEditor CreateVirtualEditor(IJSRuntime jsRuntime, string id, string? cssClass = null, ISettingsProvider? settingsProvider = null)
{
- return new MonacoEditor(jsRuntime, id, cssClass);
+ return new MonacoEditor(jsRuntime, id, cssClass, settingsProvider);
}
}
diff --git a/src/app/dev/DevToys.Blazor/Components/Text/MonacoEditor/RicherMonacoEditorBase.cs b/src/app/dev/DevToys.Blazor/Components/Text/MonacoEditor/RicherMonacoEditorBase.cs
index 99940ec55..6f146e050 100644
--- a/src/app/dev/DevToys.Blazor/Components/Text/MonacoEditor/RicherMonacoEditorBase.cs
+++ b/src/app/dev/DevToys.Blazor/Components/Text/MonacoEditor/RicherMonacoEditorBase.cs
@@ -2,6 +2,7 @@
using System.Text.Json.Serialization;
using DevToys.Blazor.Components.Monaco;
using DevToys.Blazor.Components.Monaco.Editor;
+using DevToys.Core.Settings;
namespace DevToys.Blazor.Components;
@@ -10,12 +11,20 @@ public class RicherMonacoEditorBase : MonacoEditorBase
private readonly List<string> _deltaDecorationIds = new();
private CursorStateComputer? _executeEditsLambda;
- public RicherMonacoEditorBase(IJSRuntime? jsRuntime = null)
+ [Import]
+ protected ISettingsProvider SettingsProvider = default!;
+
+ public RicherMonacoEditorBase(IJSRuntime? jsRuntime = null, ISettingsProvider? settingsProvider = null)
{
if (jsRuntime is not null)
{
JSRuntime = jsRuntime;
}
+
+ if (settingsProvider is not null)
+ {
+ SettingsProvider = settingsProvider;
+ }
}
/// <summary>
@@ -716,11 +725,11 @@ public class RicherMonacoEditorBase : MonacoEditorBase
internal async ValueTask<bool> SetLanguageAsync(string languageId)
=> await MonacoEditorHelper.SetModelLanguageAsync(JSRuntime, await GetModelAsync(), languageId);
- internal ValueTask<bool> UpdateOptionsAsync(EditorUpdateOptions newOptions)
+ internal async ValueTask<bool> UpdateOptionsAsync(EditorUpdateOptions newOptions)
{
if (_isDisposed)
{
- return new ValueTask<bool>(false);
+ return false;
}
// Convert the options object into a JsonElement to get rid of the properties with null values
@@ -730,6 +739,28 @@ public class RicherMonacoEditorBase : MonacoEditorBase
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
});
JsonElement optionsDict = JsonSerializer.Deserialize<JsonElement>(optionsJson);
- return JSRuntime.InvokeVoidWithErrorHandlingAsync("devtoys.MonacoEditor.updateOptions", Id, optionsDict);
+ bool success = await JSRuntime.InvokeVoidWithErrorHandlingAsync("devtoys.MonacoEditor.updateOptions", Id, optionsDict);
+
+ if (success)
+ {
+ TextModel textModel = await this.GetModelAsync();
+
+ if (textModel is not null)
+ {
+ UITextEndOfLinePreference eolPreference = SettingsProvider.GetSetting(PredefinedSettings.TextEditorEndOfLinePreference);
+ if (eolPreference != UITextEndOfLinePreference.TextDefault)
+ {
+ EndOfLineSequence eol = EndOfLineSequence.CRLF;
+ if (eolPreference == UITextEndOfLinePreference.LF)
+ {
+ eol = EndOfLineSequence.LF;
+ }
+
+ success = await textModel.SetEOLAsync(JSRuntime, eol);
+ }
+ }
+ }
+
+ return success;
}
}
diff --git a/src/app/dev/DevToys.Blazor/Components/Text/MonacoEditor/RicherMonacoEditorDiffBase.cs b/src/app/dev/DevToys.Blazor/Components/Text/MonacoEditor/RicherMonacoEditorDiffBase.cs
index 08831a927..5db246863 100644
--- a/src/app/dev/DevToys.Blazor/Components/Text/MonacoEditor/RicherMonacoEditorDiffBase.cs
+++ b/src/app/dev/DevToys.Blazor/Components/Text/MonacoEditor/RicherMonacoEditorDiffBase.cs
@@ -2,6 +2,7 @@
using System.Text.Json.Serialization;
using DevToys.Blazor.Components.Monaco;
using DevToys.Blazor.Components.Monaco.Editor;
+using DevToys.Core.Settings;
namespace DevToys.Blazor.Components;
@@ -10,6 +11,9 @@ public abstract class RicherMonacoEditorDiffBase : MonacoEditorBase
protected RicherMonacoEditorBase? _originalEditor;
protected RicherMonacoEditorBase? _modifiedEditor;
+ [Import]
+ protected ISettingsProvider SettingsProvider = default!;
+
/// <summary>
/// Get the `original` editor.
/// </summary>
@@ -339,7 +343,7 @@ public abstract class RicherMonacoEditorDiffBase : MonacoEditorBase
/// <summary>
/// Update the editor's options after the editor has been created.
/// </summary>
- public ValueTask<bool> UpdateOptionsAsync(DiffEditorOptions newOptions)
+ public async ValueTask<bool> UpdateOptionsAsync(DiffEditorOptions newOptions)
{
// Convert the options object into a JsonElement to get rid of the properties with null values
string optionsJson = JsonSerializer.Serialize(newOptions, new JsonSerializerOptions
@@ -348,6 +352,33 @@ public abstract class RicherMonacoEditorDiffBase : MonacoEditorBase
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
});
JsonElement optionsDict = JsonSerializer.Deserialize<JsonElement>(optionsJson);
- return JSRuntime.InvokeVoidWithErrorHandlingAsync("devtoys.MonacoEditor.updateOptions", Id, optionsDict);
+
+ bool success = await JSRuntime.InvokeVoidWithErrorHandlingAsync("devtoys.MonacoEditor.updateOptions", Id, optionsDict);
+
+ if (success)
+ {
+ DiffEditorModel textModel = await this.GetModelAsync();
+
+ if (textModel is not null && textModel.Original is not null && textModel.Modified is not null)
+ {
+ UITextEndOfLinePreference eolPreference = SettingsProvider.GetSetting(PredefinedSettings.TextEditorEndOfLinePreference);
+ if (eolPreference != UITextEndOfLinePreference.TextDefault)
+ {
+ EndOfLineSequence eol = EndOfLineSequence.CRLF;
+ if (eolPreference == UITextEndOfLinePreference.LF)
+ {
+ eol = EndOfLineSequence.LF;
+ }
+
+ success = await textModel.Original.SetEOLAsync(JSRuntime, eol);
+ if (success)
+ {
+ success = await textModel.Modified.SetEOLAsync(JSRuntime, eol);
+ }
+ }
+ }
+ }
+
+ return success;
}
}
diff --git a/src/app/dev/DevToys.Blazor/Components/UIElements/UIDiffTextInputPresenter.razor b/src/app/dev/DevToys.Blazor/Components/UIElements/UIDiffTextInputPresenter.razor
index e6ea7590f..fd8e6f4ee 100644
--- a/src/app/dev/DevToys.Blazor/Components/UIElements/UIDiffTextInputPresenter.razor
+++ b/src/app/dev/DevToys.Blazor/Components/UIElements/UIDiffTextInputPresenter.razor
@@ -50,6 +50,7 @@
<GridCell Row="1" Column="0" ColumnSpan="2">
<MonacoEditorDiff @ref="_monacoEditor"
ConstructionOptions="@OnMonacoConstructionOptions"
+ OnTextModelInitializationRequested="OnMonacoTextModelInitializationRequestedAsync"
OnDidInit="@OnMonacoEditorInitializedAsync" />
</GridCell>
</Grid>
\ No newline at end of file
diff --git a/src/app/dev/DevToys.Blazor/Components/UIElements/UIDiffTextInputPresenter.razor.cs b/src/app/dev/DevToys.Blazor/Components/UIElements/UIDiffTextInputPresenter.razor.cs
index 56d7c92ff..18a154ae1 100644
--- a/src/app/dev/DevToys.Blazor/Components/UIElements/UIDiffTextInputPresenter.razor.cs
+++ b/src/app/dev/DevToys.Blazor/Components/UIElements/UIDiffTextInputPresenter.razor.cs
@@ -41,7 +41,7 @@ public partial class UIDiffTextInputPresenter : JSStyledComponentBase
return base.DisposeAsync();
}
- private async Task OnMonacoEditorInitializedAsync()
+ private async Task OnMonacoTextModelInitializationRequestedAsync()
{
// Get or create the original model
TextModel original_model = await MonacoEditorHelper.GetModelAsync(JSRuntime, uri: _originalModelName);
@@ -67,7 +67,10 @@ public partial class UIDiffTextInputPresenter : JSStyledComponentBase
Original = original_model,
Modified = modified_model
});
+ }
+ private async Task OnMonacoEditorInitializedAsync()
+ {
Guard.IsNotNull(_monacoEditor.ModifiedEditor);
await _monacoEditor.ModifiedEditor.UpdateOptionsAsync(
new EditorUpdateOptions
diff --git a/src/app/dev/DevToys.Blazor/Components/UIElements/UIMultiLineTextInputPresenter.razor b/src/app/dev/DevToys.Blazor/Components/UIElements/UIMultiLineTextInputPresenter.razor
index 089808945..bb6c76b52 100644
--- a/src/app/dev/DevToys.Blazor/Components/UIElements/UIMultiLineTextInputPresenter.razor
+++ b/src/app/dev/DevToys.Blazor/Components/UIElements/UIMultiLineTextInputPresenter.razor
@@ -13,6 +13,7 @@
WrapMode="@UIMultiLineTextInput.WrapMode"
ConstructionOptions="@OnMonacoConstructionOptions"
OnDidChangeModelContent="OnMonacoEditorTextChangedAsync"
+ OnTextModelInitializationRequested="OnMonacoTextModelInitializationRequestedAsync"
OnDidInit="OnMonacoEditorInitializedAsync"
ParentComponent="_textInputWrapper"/>
</UITextInputWrapper>
\ No newline at end of file
diff --git a/src/app/dev/DevToys.Blazor/Components/UIElements/UIMultiLineTextInputPresenter.razor.cs b/src/app/dev/DevToys.Blazor/Components/UIElements/UIMultiLineTextInputPresenter.razor.cs
index 3b097a245..0ddcb2f00 100644
--- a/src/app/dev/DevToys.Blazor/Components/UIElements/UIMultiLineTextInputPresenter.razor.cs
+++ b/src/app/dev/DevToys.Blazor/Components/UIElements/UIMultiLineTextInputPresenter.razor.cs
@@ -71,7 +71,7 @@ public partial class UIMultiLineTextInputPresenter : JSStyledComponentBase
}
}
- private async Task OnMonacoEditorInitializedAsync()
+ private async Task OnMonacoTextModelInitializationRequestedAsync()
{
// Get or create the text model
TextModel textModel = await MonacoEditorHelper.GetModelAsync(JSRuntime, uri: _modelName);
@@ -90,8 +90,12 @@ public partial class UIMultiLineTextInputPresenter : JSStyledComponentBase
// Set the text of model
await textModel.SetValueAsync(JSRuntime, UIMultiLineTextInput.Text);
+ }
+ private Task OnMonacoEditorInitializedAsync()
+ {
_monacoInitializationAwaiter.TrySetResult();
+ return Task.CompletedTask;
}
private StandaloneEditorConstructionOptions OnMonacoConstructionOptions(MonacoEditor monacoEditor)
diff --git a/src/app/dev/DevToys.Core/Settings/PredefinedSettings.cs b/src/app/dev/DevToys.Core/Settings/PredefinedSettings.cs
index b3de402bf..3ed728484 100644
--- a/src/app/dev/DevToys.Core/Settings/PredefinedSettings.cs
+++ b/src/app/dev/DevToys.Core/Settings/PredefinedSettings.cs
@@ -139,6 +139,14 @@ public static class PredefinedSettings
name: nameof(TextEditorRenderWhitespace),
defaultValue: false);
+ /// <summary>
+ /// The kind of EOL (End Of Line sequence) to use in the text editor (LF vs CRLF).
+ /// </summary>
+ public static readonly SettingDefinition<UITextEndOfLinePreference> TextEditorEndOfLinePreference
+ = new(
+ name: nameof(TextEditorEndOfLinePreference),
+ defaultValue: UITextEndOfLinePreference.TextDefault);
+
/// <summary>
/// Whether when using the Paste command, the text in the editor should be replaced or appended.
/// </summary>