angular - 如何使用 typescript 替换 Ace 编辑器中的一行?

标签 angular typescript ace-editor

我的问题是我有一个 WEB 应用程序,我正在使用带有 angularjs 4 的 typescript

我需要制作一个 keyEvent 或其他东西,每次我在任何地方输入小注释('//')时,我需要将它们替换为大注释('/* XXX */')和光标应该放在它们之间(标有字符XXX)

我到目前为止,能够从编辑器中读取行,并编辑注释,但是替换行时出现问题,我不知道为什么,因为没有错误在控制台上。

代码如下:

@HostListener( 'window:keydown', ['$event'] ) keyboardInput( e: any ) {
    if ( e.key === '/' && this.globalService.mode === 'EDIT' ) {
        this.backslashCounter++;
        const column = this.globalService.editor.getEditor().session.selection.selectionAnchor.column;
        const row = this.globalService.editor.getEditor().session.selection.selectionAnchor.row;
        this.currentLine = '' + this.globalService.editor.getEditor().session.selection.doc.getLines( row, row );
        if ( this.backslashCounter === 2 ) {
            if ( this.currentLine.substr( column - 1, 1 ) === '/' ) {
                // e.preventDefault();
                // e.stopPropagation();
                this.backslashCounter = 0;
                currentLine = this.removeSmallCommentsWithBigOnes(currentLine);
                const editor = this.globalService.editor.getEditor();
                const range = this.globalService.editor.getEditor().session.selection.getRange();
                range.start.column = 0;
                range.end.column = currentLine.length + 5;
                editor.replace( range, currentLine );   // THIS LINE HERE DOES NOT WORK!!!!!!!!!!!!!!!!
                this.globalService.editor.getEditor().session.selection.moveTo( row, column + 2 ); // this line sets the selection back to the right position in the middle of the big comments (or it should, did not have a chance to see :))
            } else {
                this.backslashCounter--;
            }
        }
    }

}

因此,代码执行以下操作: 首先 IF,检查按下的键是否是 '/' 键

第二个 IF,检查是否有 2 个,

第三个 IF,检查它们是否彼此相邻,如果不相邻,则计数器减 1

现在我标记了在 JAVASCRIPT 中有效但在 TYPESCRIPT 中无效的行,请帮忙。

也许还有一个更好的方法来获取当前行和行,因为,这仅在我使用鼠标并直接单击我想写评论的地方时有效,但如果我移动光标则无效箭头按钮。

谢谢。

最佳答案

您需要使用 editor.session.replace 而不是 editor.replaceeditor.replace 会做其他事情。

关于angular - 如何使用 typescript 替换 Ace 编辑器中的一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48316631/

相关文章:

angular - 使用 templateRef 查看Children

javascript - 获取 typescript 默认/初始参数类型以扩展它

javascript - 动态更新 Ace 编辑器的语法高亮模式规则

javascript - 如何将一个json数组插入到一个json中

node.js - 默认文档不在 Azure 托管的 Node Web 应用程序上提供

angular - 如何在 angular 7 中创建可选的路由器参数

javascript - Angular 中输入和跨度之间的文本绑定(bind)

html - 在多张图像上添加删除按钮

firefox - Ace 在 Firefox 的焦点上选择编辑器中的所有文本

javascript - 如何使用 Ace 突出显示多行?