Skip to content

Commit 1d6887c

Browse files
committed
feat: 增加代码编辑器插入文本方法
1 parent 7f95f94 commit 1d6887c

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

src/components/BootstrapBlazor.CodeEditor/Components/CodeEditor/CodeEditor.razor.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
1+
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33
// Website: https://www.blazor.zone or https://argozhang.github.io/
44

@@ -117,6 +117,13 @@ protected override async Task InvokeInitAsync()
117117
/// <returns></returns>
118118
public async Task Resize() => await InvokeVoidAsync("resize");
119119

120+
/// <summary>
121+
///
122+
/// </summary>
123+
/// <param name="data"></param>
124+
/// <returns></returns>
125+
public async Task InsertText(string data) => await InvokeVoidAsync("insertText",Id,data);
126+
120127
/// <summary>
121128
///
122129
/// </summary>

src/components/BootstrapBlazor.CodeEditor/Components/CodeEditor/CodeEditor.razor.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { addLink, addScript } from '../../../BootstrapBlazor/modules/utility.js'
1+
import { addLink, addScript } from '../../../BootstrapBlazor/modules/utility.js'
22
import Data from '../../../BootstrapBlazor/modules/data.js'
33
import EventHandler from "../../../BootstrapBlazor/modules/event-handler.js"
44

@@ -76,6 +76,29 @@ export async function init(id, interop, options) {
7676
});
7777
}
7878

79+
export function insertText(id, insertData) {
80+
81+
const wrapper = Data.get(id);
82+
if (!wrapper) return;
83+
84+
const editor = wrapper.editor;
85+
86+
const selection = editor.getSelection();
87+
88+
editor.executeEdits('insert-custom-text', [
89+
{
90+
range: selection,
91+
text: insertData,
92+
forceMoveMarkers: true
93+
}
94+
]);
95+
//bug: 插入文本后,虽然光标显示正常,但是再次插入文本,实际位置是第一行第一列
96+
97+
editor.focus();
98+
}
99+
100+
101+
79102
// Update the editor options
80103
export function monacoSetOptions(id, options) {
81104
var editor = Data.get(id);

0 commit comments

Comments
 (0)